
,Sc           @   s  d  Z  d d l m Z m Z m Z m Z d d l m Z d d l m	 Z	 d d l
 m Z d Z d Z d Z d	 Z d
 Z d Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d  e f d!     YZ d" e f d#     YZ d$ e	 f d%     YZ  e! d& k rd d' l" m# Z# d d( l$ Td d( l% Td d( l& Td d) l' Z' d d) l( Z( d d) l) Z) e*   Z+ e e e e f Z, d* j-   Z. d+   Z/ d,   Z0 d- e d d. d.  d/  Z1 d0 d1 d2  Z2 d3   Z3 d4   Z4 e3   e' j5 d5  n  d) S(6   s  
A paragraph class to be used with ReportLab Platypus.

TODO
====

- Bullets
- Weblinks and internal links
- Borders and margins (Box)
- Underline, Background, Strike
- Images
- Hyphenation
+ Alignment
+ Breakline, empty lines
+ TextIndent
- Sub and super

i(   t	   TA_CENTERt
   TA_JUSTIFYt   TA_LEFTt   TA_RIGHT(   t   stringWidth(   t   Flowable(   t   Colori   i   i   i   i   i   t   Stylec           B   sv   e  Z d  Z i e d 6d d 6d d 6d d 6d d 6d d	 6e d
 d
 d
  d 6d d 6d d 6d
 d 6d d 6Z d   Z RS(   s   
    Style.

    Single place for style definitions: Paragraphs and Fragments. The
    naming follows the convention of CSS written in camelCase letters.
    t	   textAligng        t
   textIndentt   widtht   heights   Times-Romant   fontNameg      $@t   fontSizei    t   colorg      ?t
   lineHeightt   lineHeightAbsolutet   pdfLineSpacingt   linkc         K   s<   |  j  |  j  |  j  |  d |  _ d |  _ t |  _ d  S(   Ni    (   t   updatet   DEFAULTt   spaceBeforet
   spaceAftert   Falset   keepWithNext(   t   selft   kw(    (    s   /xhtml2pdf/paragraph2.pyt   __init__I   s
    		N(   t   __name__t
   __module__t   __doc__R   t   NoneR   R   R   (    (    (    s   /xhtml2pdf/paragraph2.pyR   3   s   
t   Boxc           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s  
    Box.

    Handles the following styles:

        # underline, underLineColor (?)
        # strike, strikeColor (?)

        backgroundColor, backgroundImage
        paddingLeft, paddingRight, paddingTop, paddingBottom
        marginLeft, marginRight, marginTop, marginBottom
        borderLeftColor, borderLeftWidth, borderLeftStyle
        borderRightColor, borderRightWidth, borderRightStyle
        borderTopColor, borderTopWidth, borderTopStyle
        borderBottomColor, borderBottomWidth, borderBottomStyle

    Not used in inline Elements:

        paddingTop, paddingBottom
        marginTop, marginBottom

    Needs also:

        fontName, fontSize, color

    t   boxc   
   	      s!    j    |  j d t d d d    |  j d d   } | d  k	 rx   j |    j | | | | d d d d n     f d   } | |  j d d   |  j d	 d   |  j d
 d   | | | | |  | |  j d d   |  j d d   |  j d d   | | | | | | |  | |  j d d   |  j d d   |  j d d   | | | | | | |  | |  j d d   |  j d d   |  j d d   | | | | |  |  j d d   rd |  d } | d | }	   j | d    j     j | |	 | | |	  n    j	   d  S(   NR   i    t   backgroundColort   filli   t   strokec            sg   | rc |  rc | d  k r!  } n  | d  k	 rc   j |    j |    j | | | |  qc n  d  S(   N(   R   t   setStrokeColort   setLineWidtht   line(   t   bstyleR
   R   t   x1t   y1t   x2t   y2(   t   canvast	   textColor(    s   /xhtml2pdf/paragraph2.pyt   _drawBorderLine|   s    	t   borderLeftStylet   borderLeftWidtht   borderLeftColort   borderRightStylet   borderRightWidtht   borderRightColort   borderTopStylet   borderTopWidtht   borderTopColort   borderBottomStylet   borderBottomWidtht   borderBottomColort	   underlineg      ?R   g      ?g      ?(
   t	   saveStatet   getR   R   t   setFillColort   rectR&   R%   R'   t   restoreState(
   R   R-   t   xt   yt   wt   ht   bgR/   t   fft
   yUnderline(    (   R-   R.   s   /xhtml2pdf/paragraph2.pyt   _drawBeforeo   s<    
%c   	      C   s   |  j  d d   r d |  d } | d | } |  j  d t d d d   } | j   | j | d  | j |  | j | | | | |  | j   n  d  S(   Nt   strikeg      ?R   g       @R   i    g      ?(   R>   R   R   R=   R&   R%   R'   RA   (	   R   R-   RB   RC   RD   RE   RG   t   yStrikeR.   (    (    s   /xhtml2pdf/paragraph2.pyt
   _drawAfter   s    
(   R   R   R   t   nameRI   RL   (    (    (    s   /xhtml2pdf/paragraph2.pyR    Q   s   	3t   Fragmentc           B   s>   e  Z d  Z d Z d Z e Z e Z e Z	 d   Z
 d   Z RS(   s   
    Fragment.

    text:       String containing text
    fontName:
    fontSize:
    width:      Width of string
    height:     Height of string
    t   fragmentc         C   s   d |  d <d  S(   Ni    R
   (    (   R   (    (    s   /xhtml2pdf/paragraph2.pyt   calc   s    c         C   s&   |  j  r d |  d Sd |  j j   S(   Ns   '%s't   texts   <%s>(   t   isTextRM   t   upper(   R   (    (    s   /xhtml2pdf/paragraph2.pyt   __str__   s    	N(   R   R   R   RM   R   t   typeR   t   isSoftRR   t   isLFRP   RT   (    (    (    s   /xhtml2pdf/paragraph2.pyRN      s   		t   BoxBeginc           B   s/   e  Z d  Z e Z d   Z d   Z d   Z RS(   t   beginc         C   s*   |  j  d d  |  j  d d  |  d <d  S(   Nt
   marginLefti    t   paddingLeftR
   (   R>   (   R   (    (    s   /xhtml2pdf/paragraph2.pyRP      s    c         C   sn   |  j  d d  |  d } |  d |  j  d d  } |  d } | | | f |  d <|  j | | | | |  d  S(   NRZ   i    RB   t   lengtht   paddingRightR   R!   (   R>   RI   (   R   R-   RC   RB   RD   RE   (    (    s   /xhtml2pdf/paragraph2.pyt
   drawBefore   s
    
c         C   s0   |  d \ } } } |  j  | | | | |  d  S(   NR!   (   RL   (   R   R-   RC   RB   RD   RE   (    (    s   /xhtml2pdf/paragraph2.pyt	   drawAfter   s    (   R   R   RM   t   BEGINRU   RP   R^   R_   (    (    (    s   /xhtml2pdf/paragraph2.pyRX      s
   		t   BoxEndc           B   s   e  Z d  Z e Z d   Z RS(   t   endc         C   s*   |  j  d d  |  j  d d  |  d <d  S(   Nt   marginRighti    R]   R
   (   R>   (   R   (    (    s   /xhtml2pdf/paragraph2.pyRP      s    (   R   R   RM   t   ENDRU   RP   (    (    (    s   /xhtml2pdf/paragraph2.pyRa      s   t   Wordc           B   s)   e  Z d  Z d Z e Z e Z d   Z RS(   s   
    A single word.
    t   wordc         C   s&   t  |  d |  d |  d  |  d <d S(   s;   
        XXX Cache stringWith if not accelerated?!
        RQ   R   R   R
   N(   R   (   R   (    (    s   /xhtml2pdf/paragraph2.pyRP      s    (	   R   R   R   RM   t   WORDRU   t   TrueRR   RP   (    (    (    s   /xhtml2pdf/paragraph2.pyRe      s
   t   Spacec           B   s)   e  Z d  Z d Z e Z e Z d   Z RS(   sN   
    A space between fragments that is the usual place for line breaking.
    t   spacec         C   s"   t  d |  d |  d  |  d <d  S(   Nt    R   R   R
   (   R   (   R   (    (    s   /xhtml2pdf/paragraph2.pyRP     s    (	   R   R   R   RM   t   SPACERU   Rh   RV   RP   (    (    (    s   /xhtml2pdf/paragraph2.pyRi      s
   t	   LineBreakc           B   s&   e  Z d  Z d Z e Z e Z e Z RS(   s    Line break. t   br(	   R   R   R   RM   t   BRRU   Rh   RV   RW   (    (    (    s   /xhtml2pdf/paragraph2.pyRm     s   t   Imagec           B   s   e  Z d  Z e Z e Z RS(   t   image(   R   R   RM   t   IMAGERU   Rh   RR   (    (    (    s   /xhtml2pdf/paragraph2.pyRp     s   t   Linec           B   s8   e  Z d  Z d Z d   Z d   Z d   Z d   Z RS(   s'   
    Container for line fragments.
    g      ?c         C   sG   d |  _  d |  _ t |  _ t |  _ | |  _ g  |  _ t j |   d  S(   Ni    (	   R
   R   R   t   isLastRn   t   stylet   boxStackt   listR   (   R   Ru   (    (    s   /xhtml2pdf/paragraph2.pyR   $  s    						c   	      C   s  | t  k r |  d d |  d d } | | } | t k r` x |  D] } | d c | 7<qC Wq | t k r x |  D] } | d c | d 7<qs Wq | t k r |  j r | t |   d } x1 t |   D]  \ } } | d c | | 7<q Wq n  x |  D]y } | d | d } t | t  r6|  j	 j
 |  q t | t  r |  j	 ru|  j	 j   } | | d | d <quq q Wx# |  j	 D] } | | d | d <qWd  S(   NiRB   R
   g       @i   R\   (   R   R   R    R   Rn   t   lent	   enumeratet
   isinstanceRX   Rv   t   appendRa   t   pop(	   R   R
   t	   alignmentt	   lineWidtht
   emptySpacet   fragt   deltat   iRB   (    (    s   /xhtml2pdf/paragraph2.pyt   doAlignment-  s.    
	c            sz   |   _  t   f d     D    _   _ t d     D    _   j   j } x   D] } | | d <q_ W  j S(   s   Align words in previous line.c         3   s(   |  ] } | j  d  d    j Vq d S(   R   i    N(   R>   t
   LINEHEIGHT(   t   .0R   (   R   (    s   /xhtml2pdf/paragraph2.pys	   <genexpr>P  s    c         s   s!   |  ] } | j  d  d  Vq d S(   R   i    N(   R>   (   R   R   (    (    s   /xhtml2pdf/paragraph2.pys	   <genexpr>S  s    RC   (   R
   t   maxR   R   R   (   R   R
   RC   R   (    (   R   s   /xhtml2pdf/paragraph2.pyt   doLayoutK  s    	&c         C   s@   d Gd d GHx+ |  D]# } d | j  d | j j    Gq WHd  S(   NRs   i(   t   -s   %sRQ   (   R>   RM   RS   (   R   R   (    (    s   /xhtml2pdf/paragraph2.pyt   dumpFragmentsZ  s    !(   R   R   R   R   R   R   R   R   (    (    (    s   /xhtml2pdf/paragraph2.pyRs     s   				t   Groupc           B   s   e  Z RS(    (   R   R   (    (    (    s   /xhtml2pdf/paragraph2.pyR   a  s   t   Textc           B   sD   e  Z d  Z d d d  Z d   Z d   Z e d  Z d   Z	 RS(   s   
    Container for text fragments.

    Helper functions for splitting text into lines and calculating sizes
    and positions.
    c         C   sz   | d  k r g  } n  g  |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  |  _ d  |  _ | |  _	 t
 j |  |  d  S(   Ni    (   R   t   linesR
   R   t   maxWidtht	   maxHeightt   post   oldSpacet   newSpaceRu   Rw   R   (   R   t   dataRu   (    (    s   /xhtml2pdf/paragraph2.pyR   m  s    										c         C   s   x |  D] } | j    q Wd S(   s/   
        Calculate sizes of fragments.
        N(   RP   (   R   Rf   (    (    s   /xhtml2pdf/paragraph2.pyRP   |  s    c         C   s   |  j  |  _ g  } d } t } t |   } x |  j | k  r |  |  j } | j } |  j d 7_ | t k r | |  _  | r- Pq- q- n  | j |  | | j d d  7} | t	 k r- t
 } Pq- q- W| | | f S(   Ni    i   R
   (   R   R   R   Rx   R   RU   Rl   R{   R>   Ro   Rh   (   R   t   groupR
   Rn   R\   R   t   type_(    (    s   /xhtml2pdf/paragraph2.pyt   getGroup  s(    		c         C   sV  g  |  _  d |  _ d |  _ | |  _ |  _ | |  _ g  } |  j } d } | s\ | d } n  xt r|  j } t |  } x4 t	 j
 |  D]# }	 d |	 d <| j t |	   q Wx t r|  j |  _ |  j   \ }
 } } | s Pn  |
 | | k r|  j |  _ Pn  |  j r$| j d |  j  n  xn | D]f } | | d <| | d 7} | j |  t | t  ru| j |  q+t | t  r+| j   q+q+W| r t | _ Pq q Wd |  _ | j   | r|  j | j |  j  7_ |  j  j |  n  |  j | k r| S| sPn  d } q_ Wt |  j  d _ x% |  j  D] } | j | | d  q4Wd S(   s   
        Split text into lines and calculate X positions. If we need more
        space in height than available we return the rest of the text
        i    R	   RB   R
   iR   N(   R   R   R   R   R
   R   Ru   Rh   Rs   t   copyt   deepcopyR{   RX   t   groupPosR   R   t   insertRz   Ra   R|   Rn   R   R   R   R   R   (   R   R   R   t   splittedRv   Ru   RB   t   posBeginR'   R!   t
   groupWidthRn   R   R   (    (    s   /xhtml2pdf/paragraph2.pyt   splitIntoLines  sf    							
		
		

c         C   s6   x/ t  |  j  D] \ } } d | G| j   q Wd S(   s?   
        For debugging dump all line and their content
        s   Line %d:N(   Ry   R   R   (   R   R   R'   (    (    s   /xhtml2pdf/paragraph2.pyt	   dumpLines  s    N(
   R   R   R   R   R   RP   R   R   R   R   (    (    (    s   /xhtml2pdf/paragraph2.pyR   e  s   		bt	   Paragraphc           B   s8   e  Z d  Z e e d  Z d   Z d   Z d   Z RS(   s5  A simple Paragraph class respecting alignment.

    Does text without tags.

    Respects only the following global style attributes:
    fontName, fontSize, leading, firstLineIndent, leftIndent,
    rightIndent, textColor, alignment.
    (spaceBefore, spaceAfter are handled by the Platypus framework.)

    c         K   s   t  j |   | |  _ |  j j   | |  _ | |  j _ | |  _ | |  _ x* | j   D] \ } } t |  | |  qW Wd  |  _
 d  S(   N(   R   R   RQ   RP   Ru   t   debugR   t	   iteritemst   setattrR   t
   splitIndex(   R   RQ   Ru   R   R   t   kwDictt   kt   v(    (    s   /xhtml2pdf/paragraph2.pyR     s    				c         C   s   | |  _  | |  _ |  j r- d | | f GHn  |  j sO |  j rK d d GHn  d S| } |  j j | |  |  _ | |  j j |  _ |  _ |  j r d |  j |  j |  j f GHn  |  j |  j f S(   sF   
        Determine the rectangle this paragraph really needs.
        s   *** wrap (%f, %f)s   *** wrap (%f, %f) neededi    s'   *** wrap (%f, %f) needed, splitIndex %r(   i    i    (   i    i    (   t   avWidtht   avHeightR   RQ   R   R   R   R
   (   R   t
   availWidtht   availHeightR
   (    (    s   /xhtml2pdf/paragraph2.pyt   wrap%  s    						c         C   s   |  j  r d | | f GHn  g  } |  j r |  j |  j  } |  j |  j } t t |  |  j d |  j  } t t |  |  j d |  j  d t } | | g } |  j  r d t |  t |  f GHq n  |  j  r d |  j GHn  | S(   s2   
        Split ourself in two paragraphs.
        s   *** split (%f, %f)R   R   s   *** text1 %s / text %ss   *** return %s(	   R   R   RQ   R   R   Ru   Rh   Rx   R   (   R   R   R   R   t   text1t   text2t   p1t   p2(    (    s   /xhtml2pdf/paragraph2.pyt   splitA  s    		!'	!	c      	   C   sY  |  j  r d GHn  |  j s d S|  j } |  j } | j   |  j  r d } t d d d  } t d d d  } | j |  | j |  | j |  | j	 | j
 d |  j |  j d d d d n  d } |  j } xw|  j j D]i} | | j 7} xS| D]K}	 |	 j }
 |
 t k r*|	 j | | |  n  |
 t k r| j |	 d	 |	 d
  | j |	 j d | d   | j |	 d | | |	 d |	 d  n  |
 t k r|	 j | | |  n  |	 j d d  } | r t j d  } |	 d | | |	 d |	 d
 f \ } } } } | | | | f } t | t  r@| j d  } n  | j d d  } t |  d k rt| d j   pwd } | j |  r| d k r| j   d k rd pd } | d k r| d } n  t  j! j" | | d d d | qC| d d k r| d } d } n  | j# d | d k r,| p3| d | d d q q Wq W| j$   d S(   s6   
        Render the content of the paragraph.
        s   *** drawNg      ?i   i    g?R#   R$   R   R   R   RB   RC   RQ   R   s   ^[a-zA-Z][-+a-zA-Z0-9]+$R
   t   utf8t   :i   t    t   documentt   pdft   GoToRt   URIt   relativet   kindt   #(%   R   RQ   t   canvRu   R=   R   R%   R&   R?   R@   t
   leftIndentR
   R   R   RU   R`   R^   Rg   t   setFontR>   t
   drawStringR_   R   t   ret   compileRz   t   unicodet   encodeR   Rx   t   lowert   matcht   txt   _canvast   linkURLt   linkRectRA   (   R   R-   Ru   t   bwt   bcRF   RC   t   dyR'   R   R   R   t
   _scheme_reRB   RD   RE   R@   t   partst   schemeR   (    (    s   /xhtml2pdf/paragraph2.pyt   drawY  sl    				
		
		*.( "
	:(   R   R   R   R   R   R   R   R   (    (    (    s   /xhtml2pdf/paragraph2.pyR     s
   
		t   __main__(   t   SimpleDocTemplate(   t   *Ns=  
    Lörem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
    ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
    in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
    officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet,
    consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
    ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
    dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
    nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
    in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum
    dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    incididunt ut labore et dolore magna aliqua.
    c         c   sr   d } xe t  j d |   D]Q } | r t d d | | f d | d |  Vt d | d |  V| d 7} q q Wd  S(   Ni   s   \s+RQ   s   [%d|%s]R   R   (   R   R   Re   Ri   (   R   t   fnt   fsR   Rf   (    (    s   /xhtml2pdf/paragraph2.pyt   textGenerator  s    c         C   s"   t  t t |  | |    } | S(   N(   R   Rw   R   (   R   R   R   RQ   (    (    s   /xhtml2pdf/paragraph2.pyt
   createText  s    t   solidi    c         C   sO   t  d | d |  d | d | d |  d | d | d |  d	 | d
 | d |  d |  S(   NR2   R1   R0   R5   R4   R3   R8   R7   R6   R;   R:   R9   (   t   dict(   R
   Ru   R   (    (    s   /xhtml2pdf/paragraph2.pyt
   makeBorder  s    s   Times-Romani
   c      -   C   s  t  d |  d |  t d d d d g d |  d |  t  d |  d |  t d d d	 t d
 d d  d |  d |  t  d |  d |  t d |  d | d t d t  t d d d |  d | d  t  d |  d |  t d d d d d |  t   t  d |  d |  t d d d d d |  t  d |  d |  t d |  d | t d d t d d
 d    t d d d d d |  t d d d |  d |  t d d d |  d |  t d d d |  d |  t d d d |  d |  t d d d |  d |  t d d d |  d |  t d d d |  d |  t d d d |  d |  t d d d d d |  t  d |  d |  t d d d |  d |  t  d |  d |  t d d d |  d |  t  d |  d |  t d d d |  d |  t  d |  d |  t d |  d | d t d
 d
 d  t d
 d t d
 d d    t d d d |  d |  t   t  d |  d |  t d d d |  d |  t  d |  d |  t   t d |  d |  t d |  d |  g( S(   NR   R   RQ   t   TrennbarTrennbart   pairss   Trenn-t   barTrennbart   NormalR   i   i    R<   RJ   s	   gGrößerg      ?t   Bolds
   Times-Boldt   jItalics   Times-Italicg      ?R   t   LoremR"   (   s   Trenn-R   (   Ri   Re   R   RX   Rh   Ra   R   Rm   (   R   R   (    (    s   /xhtml2pdf/paragraph2.pyt   makeSpecial  s   	
c          C   s   t  d  }  g  } t d d d d  } | d } | d } t t d  | |  } t t d | |  } t | t | |  |  } | j t t j |  | d d	  |  j	 |  d  S(
   Ns   test.pdfR   t	   HelveticaR	   g      8@R   id   R   i    (
   R   R   R   t   TEXTR   R   R{   R   R   t   build(   t   doct   storyRu   R   R   t   sampleText1t   sampleText2RQ   (    (    s   /xhtml2pdf/paragraph2.pyt   test  s    

	
c          C   s   t  t    }  |  j   |  d j GHxa |  j   \ } } } | sV d Gt |  GHPn  | G| Gd j g  | D] } t |  ^ qk  GHq( Wd  S(   Ni   t   ENDERk   (   R   R   RP   RU   R   t   reprt   joint   str(   RQ   R
   Rn   R   RB   (    (    s   /xhtml2pdf/paragraph2.pyt   test2  s    
s   start test.pdf(6   R   t   reportlab.lib.enumsR    R   R   R   t   reportlab.pdfbase.pdfmetricsR   t   reportlab.platypus.flowablesR   t   reportlab.lib.colorsR   Rg   Rl   Ro   R`   Rd   Rr   R   R   R    RN   RX   Ra   Re   Ri   Rm   Rp   Rw   Rs   R   R   R   R   t   reportlab.platypusR   t   reportlab.lib.stylest   reportlab.rl_configt   reportlab.lib.unitst   osR   R   t   getSampleStyleSheett   stylest
   ALIGNMENTSt   stripR   R   R   R   R   R   R   t   system(    (    (    s   /xhtml2pdf/paragraph2.pyt   <module>#   sT   "`	D


					