
    e@d@                        d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ  ej        d          Z G d d	e          Z G d
 de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d  d!e          ZdS )"a  
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

    N)Color)	TA_CENTER
TA_JUSTIFYTA_LEFTTA_RIGHTstringWidth)Flowable	xhtml2pdfc                   J    e Zd ZdZeddddd eddd          dddddZd	 ZdS )
Stylez
    Style.

    Single place for style definitions: Paragraphs and Fragments. The
    naming follows the convention of CSS written in camelCase letters.
    g        NzTimes-Romang      $@r   g      ?)	textAlign
textIndentwidthheightfontNamefontSizecolor
lineHeightlineHeightAbsolutepdfLineSpacinglinkc                     |                      | j                   |                      |           d| _        d| _        d| _        d S Nr   F)updateDEFAULTspaceBefore
spaceAfterkeepWithNext)selfkws     /./xhtml2pdf/paragraph.py__init__zStyle.__init__E   sB    DL!!!B!    )__name__
__module____qualname____doc__r   r   r   r#    r$   r"   r   r   /   sh          !q!Q" G" " " " "r$   r   c                       e Zd ZdZdZd ZdS )Boxa%  
    Box.

    Handles the following styles:

        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

    boxc           
                                                                 dd           }|0                    |                               ||||dd            fd} |                     dd                                dd                                dd           |||||z               |                     d	d                                d
d                                dd           ||z   |||z   ||z               |                     dd                                dd                                dd           |||z   ||z   ||z               |                     dd                                dd                                dd           ||||z   |                                            d S )NbackgroundColor   r   fillstrokec                     |ro| ro|%                     dt          ddd                    }|H                    |                               |                               ||||           d S d S d S d S )N	textColorr   )getr   setStrokeColorsetLineWidthline)	bstyler   r   x1y1x2y2canvasr    s	          r"   _drawBorderLinez$Box.drawBox.<locals>._drawBorderLineo   s     0 0= HH[%1a..AAE$))%000''...KKBB/////0 0 0 0 %$r$   borderLeftStyleborderLeftWidthborderLeftColorborderRightStyleborderRightWidthborderRightColorborderTopStyleborderTopWidthborderTopColorborderBottomStyleborderBottomWidthborderBottomColor)	saveStater5   setFillColorrectrestoreState)r    r>   xywhbgr?   s   ``      r"   drawBoxzBox.drawBoxd   s    XX'..>###KK1a1K555		0 		0 		0 		0 		0 		0 	!2D99!2D99!2D991aQ	( 	( 	( 	!3T::!3T::!3T::Aq!a%Q	0 	0 	0 	!1488!1488!14881q5!a%Q	0 	0 	0 	!4d;;!4d;;!4d;;1a!eQ	( 	( 	(
 	r$   N)r%   r&   r'   r(   namerU   r)   r$   r"   r+   r+   M   s4         ( D' ' ' ' 'r$   r+   c                   (    e Zd ZdZdZdZdZdZd ZdS )Fragmentz
    Fragment.

    text:       String containing text
    fontName:
    fontSize:
    width:      Width of string
    height:     Height of string
    fragmentFc                     d| d<   d S )Nr   r   r)   r    s    r"   calczFragment.calc   s    Wr$   N)	r%   r&   r'   r(   rV   isSoftisTextisLFr\   r)   r$   r"   rX   rX      sC          DFFD    r$   rX   c                        e Zd ZdZdZdZd ZdS )Wordz
    A single word.
    wordTc                 R    t          | d         | d         | d                   | d<   dS )z;
        XXX Cache stringWith if not accelerated?!
        textr   r   r   Nr   r[   s    r"   r\   z	Word.calc   s+     $DL$z2BDDTUUWr$   N)r%   r&   r'   r(   rV   r^   r\   r)   r$   r"   ra   ra      s>          DFV V V V Vr$   ra   c                        e Zd ZdZdZdZd ZdS )SpacezN
    A space between fragments that is the usual place for line breaking.
    spaceTc                 F    t          d| d         | d                   | d<   d S )N r   r   r   r   r[   s    r"   r\   z
Space.calc   s%    #Cj)94
;KLLWr$   N)r%   r&   r'   r(   rV   r]   r\   r)   r$   r"   rf   rf      s>          DFM M M M Mr$   rf   c                       e Zd ZdZdZdZdZdS )	LineBreakz
    Line break.
    brTN)r%   r&   r'   r(   rV   r]   r_   r)   r$   r"   rk   rk      s*          DFDDr$   rk   c                       e Zd ZdZd Zd ZdS )BoxBeginbeginc                 f    |                      dd          |                      dd          z   | d<   d S )N
marginLeftr   paddingLeftr   r5   r[   s    r"   r\   zBoxBegin.calc   s0    q11DHH]A4N4NNWr$   c                     |                      dd          | d         z   }| d         |                      dd          z   }| d         }|                     |||||           d S )Nrq   r   rP   lengthpaddingRightr   )r5   rU   )r    r>   rQ   rP   rR   rS   s         r"   drawzBoxBegin.draw   sd    HH\1%%S	1NTXXna888VQ1a(((((r$   N)r%   r&   r'   rV   r\   rw   r)   r$   r"   rn   rn      s:        DO O O) ) ) ) )r$   rn   c                       e Zd ZdZd ZdS )BoxEndendc                 f    |                      dd          |                      dd          z   | d<   d S )NmarginRightr   rv   r   rs   r[   s    r"   r\   zBoxEnd.calc   s0    22TXXna5P5PPWr$   N)r%   r&   r'   rV   r\   r)   r$   r"   ry   ry      s-        DQ Q Q Q Qr$   ry   c                       e Zd ZdZdS )ImageimageN)r%   r&   r'   rV   r)   r$   r"   r~   r~      s        DDr$   r~   c                   .    e Zd ZdZdZd Zd Zd Zd ZdS )Linez'
    Container for line fragments.
    g      ?c                     d| _         d| _        d| _        || _        g | _        t
                              |            d S r   )r   r   isLaststyleboxStacklistr#   )r    r   s     r"   r#   zLine.__init__   s=    

dr$   c                    |t           k    r| d         d         | d         d         z   }||z
  }|t          k    r| D ]}|dxx         |z  cc<   ns|t          k    r| D ]}|dxx         |dz  z  cc<   nO|t          k    rD| j        s=|t          |           dz
  z  }t          |           D ]\  }}|dxx         ||z  z  cc<   | D ]}|d         |d         z   }t          |t                    r| j	        
                    |           Ct          |t                    r.| j	        r'| j	                                        }||d         z
  |d<   | j	        D ]}||d         z
  |d<   d S )NrP   r   g       @r/   ru   )r   r   r   r   r   len	enumerate
isinstancern   r   appendry   pop)	r    r   	alignment	lineWidth
emptySpacefragdeltairP   s	            r"   doAlignmentzLine.doAlignment   s   S	#c7);;I*JH$$  , ,DIII+IIII,i''  2 2DIIIc!11IIII2j((("c$ii!m4( + +GAtIIIU*IIII  	3 	3DS	DM)A$)) 3$$T****D&)) 3= 3=,,..D%&c]DN M 	+ 	+Dc]DNN	+ 	+r$   c                      | _         dgd  D             z   }t          |           _        t           fd|D                       x _         _         j         j        z
  } D ]}||d<    j        S )z/
        Align words in previous line.
        r   c                 :    g | ]}|                     d d          S )r   r   rs   ).0r   s     r"   
<listcomp>z!Line.doLayout.<locals>.<listcomp>  s&    EEEDHHZ33EEEr$   c              3   ,   K   | ]}|j         z  V  d S N)
LINEHEIGHT)r   r   r    s     r"   	<genexpr>z Line.doLayout.<locals>.<genexpr>  s*      +Z+ZtD4?,B+Z+Z+Z+Z+Z+Zr$   rQ   )r   maxr   r   r   )r    r   
font_sizesrQ   r   s   `    r"   doLayoutzLine.doLayout  s     
SEEEEEE
J(++Z+Z+Z+Zz+Z+Z+Z(Z(ZZdo _t}, 	 	DDII{r$   c           	         t                               d           t                               d           | D ]H}t                               d|                    d|j                                                             Id S )Nr   z(----------------------------------------z%srd   )loggerdebugr5   rV   upper)r    r   s     r"   dumpFragmentszLine.dumpFragments%  st    VX 	D 	DDLLtxx	0A0ABBCCCC	D 	Dr$   N)	r%   r&   r'   r(   r   r#   r   r   r   r)   r$   r"   r   r      sf          J  + + +<  &D D D D Dr$   r   c                   .    e Zd ZdZddZd Zd	dZd ZdS )
Textz
    Container for text fragments.

    Helper functions for splitting text into lines and calculating sizes
    and positions.
    Nc                     |g }g | _         d| _        d| _        d| _        d| _        || _        t                              | |           d S )Nr   )linesr   r   maxWidth	maxHeightr   r   r#   )r    datar   s      r"   r#   zText.__init__4  sP    <D


dD!!!!!r$   c                 8    | D ]}|                                  dS )z/
        Calculate sizes of fragments.
        N)r\   )r    rb   s     r"   r\   z	Text.calcA  s,      	 	DIIKKKK	 	r$   Fc                    g | _         d| _        |x| _        | _        || _        g }| j        }d}|s|d         }t          |           }d}||k     r|}	t          |          }
t          j        |          D ])}d|d<   |
	                    t          |                     *||k     r| |         }|d         }||d<   |dz  }t          |t                    r|	                    |           n)t          |t                    r|                                 |j        r%|j        r|
	                    |           n2|dk    rn
||z   |k    rn ||z  }|
	                    |           ||k     |
r4|
d         j        dv r%|
                                 |
r|
d         j        dv %|
                                 | xj        |
                    | j                  z  c_        | j         	                    |
           | j        |k    r|	S d}||k     d| j         d         _        | j         D ]}
|
                    ||d	                    d
S )z
        Split text into lines and calculate X positions. If we need more
        space in height than available we return the rest of the text
        r   r   rP   r   r/   r   )rg   rl   Tr   N)r   r   r   r   r   r   r   r   copyr   rn   r   ry   r   r]   r_   rV   r   r   r   r   )r    r   r   splittedr   r   rP   lenTextposposBeginr8   r,   r   	fragWidths                 r"   splitIntoLineszText.splitIntoLinesH  s   
 
%--
"
  	$l#Ad))Gmm H;;D y** + +CHSMM****-- Cy M	S	q dH-- #OOD))))f-- #LLNNN ; y D)))Avv  
 ]X-- YD!!!= --B  48=O;;


  48=O;;    KK4==444KKJd### {Y&& Au Gmmz "&
3J 	; 	;DXu['9::::tr$   c                     t          | j                  D ]L\  }}t                              d|           t                              |                                           MdS )z?
        For debugging dump all line and their content
        zLine %d:N)r   r   r   r   r   )r    r   r8   s      r"   	dumpLineszText.dumpLines  s`     !,, 	/ 	/GAtLLQ'''LL++--....	/ 	/r$   )NN)F)r%   r&   r'   r(   r#   r\   r   r   r)   r$   r"   r   r   ,  si         " " " "  U U U Un/ / / / /r$   r   c                   ,    e Zd ZdZddZd Zd Zd ZdS )		Paragrapha:  
    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.)

    Fc                    t          j        |            || _        | j                                         || _        || j        _        || _        || _        |                                D ]\  }}t          | ||           d | _	        d S r   )
r
   r#   rd   r\   r   r   r   itemssetattr
splitIndex)r    rd   r   r   r   kwDictkvs           r"   r#   zParagraph.__init__  s    $		
	
  LLNN 	  	 DAqD!Q r$   c                    || _         || _        t                              d||           | j        st                              ddd           dS |}| j                            ||          | _        || j        j        c| _        | _        t                              d| j        | j        | j                   | j        | j        fS )zF
        Determine the rectangle this paragraph really needs.
        z*** wrap (%f, %f)z*** wrap (%f, %f) neededr   )r   r   z'*** wrap (%f, %f) needed, splitIndex %r)	avWidthavHeightr   r   rd   r   r   r   r   )r    
availWidthavailHeightr   s       r"   wrapzParagraph.wrap  s     "#(*kBBBy 	LL3Q:::4 )225+FF",di.>
DK>
DKY]Yhiiiz4;&&r$   c                    t                               d||           g }| j        r| j        d| j                 }| j        | j        d         }t	          t          |          | j        | j                  }t	          t          |          | j        | j        d          }||g}t                               dt          |          t          |                     t                               d| j                   |S )z4
        Split ourselves in two paragraphs.
        z*** split (%f, %f)N)r   T)r   r   z*** text1 %s / text %sz*** return %s)	r   r   r   rd   r   r   r   r   r   )r    r   r   r   text1text2p1p2s           r"   splitzParagraph.split  s    
 	):{CCC? 	KI.t./EIdo../E4;;
$*EEEB4;;
$*tTTTBBxHLL13u::s5zzJJJ_dm444r$   c           	      v   t                               d           | j        sdS | j        }| j        }|                                 | j        rd}t          ddd          }t          ddd          }|                    |           |                    |           |	                    |           |
                    |j        d| j        | j        dd           d}| j        }| j        j        D ]4}||j        z  }|D ]#}	t          |	d          r|	                    |||z
             |	                    d	d
          r|                    |	d         |	d                    |	                    |	                    d|d                              |                    |	d         ||z
  |	d         z   |	d	                    |	                    dd          }
|
rHt)          j        d          }|	d         ||z
  |	d         |	d         f\  }}}}||||f}t-          |
t.                    r|
                    d          }
|
                    dd          }t5          |          dk    r|d                                         pd
}|                    |          rK|dk    rE|                                dk    rdpd}|dk    r|d         }
|                    |
|d|           |
d         dk    r|
dd         }
d
}|                    d
|dk    r|
p|d         |d           %6|                                 dS )z6
        Render the content of the paragraph.
        z*** drawNg      ?r/   r   g?r0   rw   rd    r   r   r   rP   rQ   r   z^[a-zA-Z][-+a-zA-Z0-9]+$r   utf8:   documentpdfGoToRURI)relativekind#)r   ) r   r   rd   canvr   rL   r   r6   r7   rM   rN   
leftIndentr   r   r   hasattrrw   r5   setFont
drawStringrecompiler   strencoder   r   lowermatchlinkURLlinkRectrO   )r    r>   r   bwbcrT   rQ   dyr8   r   r   
_scheme_rerP   rR   rS   rN   partsschemer   s                      r"   rw   zParagraph.draw  sy   
 	Z   y 	F
 : 	Bq!QBsC%%B!!"%%%######KK 
     [IO "	i "	iDA  i  i 4(( .IIfb1f--- 88FB'' SNN4
#3T*5EFFF''%.(I(IJJJ%%d3ia$s)1CT&\RRR xx-- i!#,F!G!GJ!%cBFDM4
CS!SJAq!Qq!Q<D!$,, 3#{{622 JJsA..E ZZ1_Aq1A1AGRF!''// 
iFj4H4H%||~~6B7Ke7??#(8DtTADIIII7c>>#'8D%'FFj,@,IT,UUSTXW[fghhhA iD 	r$   N)FF)r%   r&   r'   r(   r#   r   r   rw   r)   r$   r"   r   r     sd        
 
   (' ' '2  *E E E E Er$   r   c                       e Zd Zd Zd ZdS )PageNumberFlowablec                 J    t          j        |            d | _        d | _        d S r   )r
   r#   page	pagecountr[   s    r"   r#   zPageNumberFlowable.__init__?  s%    $	r$   c                     t          | j        j        j                  | _        t          | j        j        j                  | _        d S r   )r   r   _doctemplater   _page_countr   r[   s    r"   rw   zPageNumberFlowable.drawD  s4    	.344	TY3?@@r$   N)r%   r&   r'   r#   rw   r)   r$   r"   r   r   >  s7          
A A A A Ar$   r   ) r(   r   loggingr   reportlab.lib.colorsr   reportlab.lib.enumsr   r   r   r   reportlab.pdfbase.pdfmetricsr	   reportlab.platypus.flowablesr
   	getLoggerr   dictr   r+   rX   ra   rf   rk   rn   ry   r~   r   r   r   r   r   r)   r$   r"   <module>r      s
  $ $   				 & & & & & & H H H H H H H H H H H H 4 4 4 4 4 4 1 1 1 1 1 1		;	'	'" " " " "D " " "<> > > > >$ > > >B    s   *V V V V V8 V V V	M 	M 	M 	M 	MH 	M 	M 	M		 		 		 		 		 		 		 		) ) ) ) )x ) ) )Q Q Q Q QX Q Q Q	 	 	 	 	H 	 	 	DD DD DD DD DD4 DD DD DDNy/ y/ y/ y/ y/4 y/ y/ y/xS S S S S S S SlA A A A A A A A A Ar$   