)EjUdZddlmZmZmZmZmZmZmZm Z m Z m Z ddl m Z ddlmZmZmZddlmZmZmZmZmZddlmZddlmZmZdd lmZdd lm Z m!Z!eZ"d Z#d Z$d Z%gdZ&d'e&Z(dZ)dZ*dZ+GddZ,Gdde,Z-GddeZ.GddeZ/GddeZ0GddZ1Gdd Z2d!S)"ao Routines for organizing lines and larger blocks of text, with manual and automatic line wrapping. The contents of this module are internal to fpdf2, and not part of the public API. They may change at any time without prior warning or any deprecation period, in non-backward-compatible ways. Usage documentation at: ) TYPE_CHECKINGAnyCallableDictList NamedTupleOptionalSequenceTupleUnion)uuid4) DeviceCMYK DeviceGray DeviceRGB)AlignCharVPos TextDirectionTextModeWrapMode) FPDFException)CoreFontTTFFont) GraphicsState)FloatTolerance escape_parens­- )ru​u u u u u u u u u u u u     cleZdZdZ d?deeeefdedede e ezddf dZ defd Z e deezfd Zejd eezddfd Ze defd Ze defdZe defdZe defdZe defdZe defdZe defdZe defdZe defdZe defdZe de eeze zfdZ!e de eeze zfdZ"e de eeze zfdZ#e defdZ$e de%fdZ&e defdZ'e defdZ(e defdZ)e de e*ee+ffdZ,e de-fd Z.e de-fd!Z/d"e ddfd#Z0d$e+defd%Z1de fd&Z2 d@d)e d*e e d+e ed,edef d-Z3d$ddefd.Z4 dAd0ed1ed,edefd2Z5d3ed4ed5ed6ed7ed8edefd9Z6d3ed5edefd:Z7d;edZ9dS)BFragmentzS A fragment of text with font/size/style and other associated information. N charactersgraphics_stateklinkreturnct|trt||_n||_||_||_||_dSN) isinstancestrlistr'r(r)r*)selfr'r(r)r*s AC:\PYTHON\MyICR_Suite\python\Lib\site-packages\fpdf/line_break.py__init__zFragment.__init__CsH j# & & )":..DOO(DO, c Hd|jd|jd|jd|jd S)NzFragment(characters=z, graphics_state=z, k=z, link=)r'r(r)r*r1s r2__repr__zFragment.__repr__RsM .4? . .#2 . .& . .!% . . . r4cDtr|jjJ|jjSr-)rr( current_fontr8s r2fontz Fragment.fontYs)  @&3???"//r4vc||j_dSr-)r(r;)r1r=s r2r<z Fragment.font_s+,(((r4c4|jduo|jjdkS)NTTF)r<typer8s r2 is_ttf_fontzFragment.is_ttf_fontcsy$@5)@@r4c|jjSr-)r( font_styler8s r2rDzFragment.font_styleg"--r4c|jjSr-)r( font_familyr8s r2rGzFragment.font_familyks"..r4c4|jj}|jj}|tjkr||jjz}n_|tjkr||jjz}n?|tjkr||jj z}n|tj kr||jj z}|Sr-) r( font_size_pt char_vposrSUB sub_scaleSUP sup_scaleNOM nom_scaleDENOM denom_scale)r1sizevposs r2rIzFragment.font_size_ptos"/", 8<   D'1 1DD X\ ! ! D'1 1DD X\ ! ! D'1 1DD X^ # # D'3 3D r4c*|jj|jz Sr-)r(rIr)r8s r2 font_sizezFragment.font_size}s"/$&88r4c|jjSr-)r(font_stretchingr8s r2rXzFragment.font_stretchings"22r4c|jjSr-)r( char_spacingr8s r2rZzFragment.char_spacing"//r4c|jjSr-)r( text_moder8s r2r]zFragment.text_mode",,r4c|jjSr-)r( underliner8s r2r`zFragment.underliner^r4c|jjSr-)r( strikethroughr8s r2rbzFragment.strikethroughs"00r4c|jjSr-)r( draw_colorr8s r2rdzFragment.draw_colorrEr4c|jjSr-)r( fill_colorr8s r2rfzFragment.fill_colorrEr4c|jjSr-)r( text_colorr8s r2rhzFragment.text_colorrEr4c|jjSr-)r( line_widthr8s r2rjzFragment.line_widthrEr4c|jjSr-)r(rJr8s r2rJzFragment.char_vposr^r4c$|jj}|tjkr |jj}nY|tjkr |jj}n<|tjkr |jj}n|tj kr |jj }nd}||jj zS)Ng) r(rJrrKsub_liftrMsup_liftrOnom_liftrQ denom_liftrI)r1rTlifts r2rqz Fragment.lifts", 8<  -6DD X\ ! !&/DD X\ ! !&/DD X^ # #&1DDDd)666r4c6d|jS)Nr!)joinr'r8s r2stringzFragment.stringswwt'''r4c*|Sr-) get_widthr8s r2widthzFragment.widths~~r4c|jjSr-)r( text_shapingr8s r2text_shaping_parametersz Fragment.text_shaping_parametersr[r4ctr+|jJt|jdtsJ|jr |jdn tjS)Nparagraph_directionrrzr.rLTRr8s r2r|zFragment.paragraph_directionsi  /;;;,-BC]   + #D ()> ? ?" r4ctr+|jJt|jdtsJ|jr |jdn tjS)Nfragment_directionr}r8s r2rzFragment.fragment_directionsi  /;;;,-ABM   + #D ()= > >" r4indexc.|jd||_dSr-)r')r1rs r2trimz Fragment.trims/&5&1r4otherct|tsdS|j|jko|j|jko|j|jkS)NF)r.r&r'r(r)r1rs r2__eq__zFragment.__eq__sO%** 5 Ou/ / "#u';; "%'! r4cDt|j|j|jfSr-)hashr'r(r)r8s r2__hash__zFragment.__hash__sT_d&946BCCCr4rTstartendchars initial_csc|G|#d|j||}n"d|j|d}|j||j|j\}}|j}|jdkr||jdzz}||jdzz}|jdkr|r |||zz }n |||dz zz }||jz S)a Return the width of the string with the given font/size/style/etc. Args: start (int): Index of the start character. Default start of fragment. end (int): Index of the end character. Default end of fragment. chars (str): Specific text to get the width for (not necessarily the same as the contents of the fragment). If given, this takes precedence over the start/end arguments. Nr!dg{Gz?rr) rsr'r<get_text_widthrIrzrZrXr))r1rrrrchar_lenwrZs r2rvzFragment.get_widths$ =c :;; 788i.. 4$d&B  !(  3 & & %, ,A D047 7L   ! ! 3\H,,\X\2246zr4cb|j|jko|j|jko|j|jkS)zFReturns if 2 fragments are equivalent other than the characters/string)r(r) __class__rs r2has_same_stylezFragment.has_same_styles9  5#7 7 2%'! 2%/1 r4F characterprint_shcX|tkr |st}|||S)zP Return the width of a single character out of the stored text. )rr) SOFT_HYPHENHYPHENrv)r1rrrs r2get_character_widthzFragment.get_character_widths/  # #H #I~~I*~EEEr4frag_ws current_ws word_spacingadjust_xadjust_yhc|jr5|jr|||||S|||S|||Sr-)rBrzrender_with_text_shapingrender_pdf_text_ttfrender_pdf_text_core)r1rrrrrrs r2render_pdf_textzFragment.render_pdf_text'sj   C+ 44h<++G\BB B((*===r4cFt|jtsJd}d}|jD]B}|jjt |}|r|t|z }C|r|jd}|jjt d}|J| t|} g} t| D]s\} } |j| } | dkr| d| d?||j z dz|j z } | | dd|| dtd| }|d|d z }n#|j|}|d|d z }|S) Nr!rr(r6.3f[z] TJ) Tj)r.r<rrtsubsetpickordchr escape_textsplit enumerateappendr)rIrs)r1rrret mapped_textchar mapped_charu_space space_char_idwords words_strlword_iwordadj escaped_texts r2rzFragment.render_pdf_text_ttf8s$)W----- K 0 0D)*//D ::K 0s;///  *i++C00G!I,11#c((;;M ,,,%%c-&8&899E$&J )% 0 0 E E y,,T22Q;;%%k$kkk2222#df,-4t7HHC%%&C&C&C7&CD&C&C&CDDDD88J//L )|))) )CC900==L )|))) )C r4pos_xpos_yctjtsJd}d}jjt d}dt dt ffd }jjdz zj z } j j j j D].} | d jt| d} | dd ks | d d krV|r |d |d z }d}||| dz} ||| d z } |d | j zdd|| z j zddz }|| z }||| d| zz }||| dz }|r| d|kr||z }| ds|r:| d|kr.|r |d |d z }d}|d |j zdd||z j zddz }0|r |d |dz }|S)Nr!rposr+ctrtjtsJ|jjzjzjdz zdz jz S)Nrr)rr.r<rscalerIrXr))rr1s r2 adjust_posz5Fragment.render_with_text_shaping..adjust_poshsj 6!$)W55555)/"#$'#-/  &  r4rrx_offsetry_offsetrz) Tj z1 0 0 1 z.2fz Tm x_advance y_advanceforce_positioningr)r.r<rrrrfloatrZrXr) shape_textrtrIrzrr)r1rrrrrtextspace_mapped_coderrZtiroffsetxoffsetys` r2rz!Fragment.render_with_text_shaping`s$)W----- I,11#c((;; E e      (D,@3,FG$&P )&& K*D,H   X XB- (9((R ->)?)?@@D*~""bn&9&9?t???*CD**R ^"<"<<**R ^"<"<<X461XXXW8NXXXX DLD ZZ;00<? ?E ZZ;00 0E &= 15F F F%%& X X!#M!26G!G!G?t???*CDW5DF"2WWW!e)tv9MWWWWW  " >t>>> !C r4crd}||kr|||jzddz }t|j}|d|dz }|S)Nr!rz Tw rr)r)rrt)r1rrrrs r2rzFragment.render_pdf_text_coresY j g&0000 0C$T[11  %<%%%% r4r-)rNNT)FT):__name__ __module__ __qualname____doc__r r0r/StateStackTyperr intr3r9propertyrrr<setterboolrBrDrGrIrVrXrZrr]r`rbrrrrdrfrhrjrrJrqrtrwrrrzrr|rrrrrvrrrrrrr4r2r&r&>s%)  $s)S.) '    sSy!       #    0h(000X0  [-h(-T---[-ATAAAXA.C...X./S///X/ e   X 95999X93333X30e000X0-8---X--4---X-1t111X1.HY%;j%HI...X..HY%;j%HI...X..HY%;j%HI...X..E...X.-8---X- 7e 7 7 7X 7((((X( u   X 0$sCx.)A000X0  ]    X    M    X  2#2$2222 C D    D#DDDD !# %%%c]%} %  %  %%%%N J 4    JN F F F(, FBF F  F F F F>>> >  >  > > >>>>"&5&&#&&&&P55#(5-25BG5 5555nEur4r&cdeZdZdZdededdffd ZdefdZdededefdZd edeffd Z xZ S) TotalPagesSubstitutionFragmenta` A special type of text fragment that represents a placeholder for the total number of pages in a PDF document. A placeholder will be generated during the initial content rendering phase of a PDF document. This placeholder is later replaced by the total number of pages in the document when the final output is being produced. argskwargsr+Nc`tj|i|t|_dSr-)superr3r uuid)r1rrrs r2r3z'TotalPagesSubstitutionFragment.__init__s.$)&)))GG r4cd|jdS)z This method returns a placeholder string containing a universally unique identifier (UUID4), ensuring that the placeholder is distinct and does not conflict with other placeholders within the document. z::placeholder:z::)rr8s r2get_placeholder_stringz5TotalPagesSubstitutionFragment.get_placeholder_strings . ----r4cF||_||_|S)ac This method is invoked during the page content rendering phase, which is common to all `Fragment` instances. It stores the provided arguments and keyword arguments to preserve the necessary information and graphic state for the final substitution rendering. The method then returns the unique placeholder string. ) _render_args_render_kwargsr)r1rrs r2rz.TotalPagesSubstitutionFragment.render_pdf_texts&!$**,,,r4replacement_textcrt||_tj|ji|jS)a This method is invoked at the output phase. It calls `render_pdf_text()` from the superclass to render the fragment with the preserved rendering state (stored in `_render_args` and `_render_kwargs`) and insert the final text in place of the placeholder. )r0r'rrrr)r1rrs r2render_text_substitutionz7TotalPagesSubstitutionFragment.render_text_substitutions7 /00&uww&(9QT=PQQQr4) rrrrrr3r/rrr __classcell__)rs@r2rrscST..... -S -C -C - - - -RRRRRRRRRRRRr4rceZdZUeeed<eed<eed<eed<eed<e eed<dZ e ed<dZ e ed <d Z eed <d eefd ZdS)TextLine fragments text_widthnumber_of_spacesalignheight max_widthF trailing_nltrailing_form_feedrindentr+c|jsgSg}d}|jD]H}|'|j|kr|d|+||g|j}I|jdjtjks2|jdjs+|jdjtjkr |ddd}g}|D]-}||djtjkr |dddn|z }.|S)Nr)rrrr|rRTL)r1directional_runs directionfragmentordered_fragmentsruns r2get_ordered_fragmentszTextLine.get_ordered_fragmentss!~ I13  8 8H$)D )Q)Q $++H5555 '' 333$7 >!  0M4E E Eq!5 Fq!4 8III/"5 ,.#  C  V6-:KKKDDbD QT   ! r4N)rrrr r&__annotations__rrrr rrrrrrrr4r2rrs!!!! LLL MMMK$$$$FE!tH~!!!!!!r4rcLeZdZUeed<eed<eed<eed<eed<eed<dS) SpaceHintoriginal_fragment_indexoriginal_character_indexcurrent_line_fragment_indexcurrent_line_character_indexrjrN)rrrrrrrr4r2rrsZ    !!!!!$$$$"%%%%r4rcteZdZUeed<eed<eed<eed<eed<eed<eed<eed<eed <eed <d S) HyphenHintrrr r rjrcurchar curchar_widthr(r)N)rrrrrrr/rrr4r2r r s    !!!!!$$$$"%%%% LLL"""" HHHHHr4r ceZdZ ddedededdfdZedefd Z dd ed ed e e zd e de dede ee zddfdZ ddZdee zddfdZ ddedededefdZdefdZdedee e effdZdS) CurrentLineFrrrrr+Ncv||_||_||_g|_d|_d|_d|_d|_dS)z Per-line text fragment management for use by MultiLineBreak. Args: print_sh (bool): If true, a soft-hyphen will be rendered normally, instead of triggering a line break. Default: False rN)rrrrrrspace_break_hinthyphen_break_hint)r1rrrs r2r3zCurrentLine.__init__sG#   )+ %&6:7;r4cxd}t|jD]"\}}|||dkz }#|S)Nrr)rrrv)r1rwirs r2rwzCurrentLine.width$sJ$T^44 : :KAx X''1q5'99 9EE r4rcharacter_widthoriginal_fragmentrrrurlc X|tksJ||_|jsSt|tsJ|j|d|j|j|nt|tr{t|jdtr[| |jds;|j|d|j|j||jd}|tvrXt||t|jt|j |j|j|_|xjdz c_n|t"krt$}|xjdz c_nl|t&kra|jsZt+||t|jt|j |j|jt,||j|j |_|t&ks|jr|j |dSdS)Nr!r7rr)NEWLINErrr.r&rrr(r)rBREAKING_SPACE_SYMBOLS_STRrlenr'rwrrNBSPSPACErrr rr) r1rrrrrrractive_fragments r2 add_characterzCurrentLine.add_character+sOG#### ~ /:: : :: N ! !!++!#4#C') ,    )8 4 4 r"H '66t~b7IJJ %%%//#%'8'G+- 0., 2 2 2$-'(DN##O.// % %%D !  ! !Q & ! ! ! $  I  ! !Q & ! ! ! + % %dm %%/'(DN##O.// %!0!# & &D "  # #t} #  & - -i 8 8 8 8 8 $ #r4c|jsdS|jd}|jd}|dkrO|d|js|jd=|jsdS|jd}|jd}|dkMdSdS)Nrr)rr'r)r1 last_frag last_chars r2trim_trailing_spacesz CurrentLine.trim_trailing_spacesrs~  FN2& (, 3 NN2   ' 'N2&> r*I!,R0I3r4 break_hintc|jd|j|_|jr%|jd|j|j|_dS)z This function mutates the current_line, applying one of the states observed in the past and stored in `hyphen_break_hint` or `space_break_hint` attributes. Nr)rr rr r)r1r&s r2_apply_automatic_hintz!CurrentLine._apply_automatic_hintsU (P**P(PQ > M N2  # #J$K L L L * ;r4rrrc ~t|j|j|j||j|j|jz |||j S)N) rrrrrrrrr)rrrwrrrr)r1rrrs r2 manual_breakzCurrentLine.manual_breaksJnz!2;nt{2#1;    r4c&|jdup|jduSr-)rrr8s r2automatic_break_possiblez$CurrentLine.automatic_break_possibles%T1VT5JRV5VVr4c@|sJ|j|j|jj|jjkr||j||jj|jj|j|jj|jj |j |jj|jj | |fS|jJ||j|jj|jj | |fSr-) r,rrrjr(r!r rrrrr*)r1rs r2automatic_breakzCurrentLine.automatic_breaks%,,.....  ! -  ! )%043H3SSS  & &t'= > > >   &.&4&&>&?    &>&?!!%((  $000 ""4#8999  ! 9  ! :   e $ $  r4)Frr-)r+N)FF)rrrrrr3rrwr/r&r rr r!r%rr(rrr*r,r r.rr4r2rrsHI<<<*.<@E< <<<<<uX$(E9E9E9E9$j0 E9 "% E9 #& E9E9cCi E9 E9E9E9E9N 1 1 1 1 < J0F <4 < < < <SX     )-  KO       W$WWWW U uS#x5G/H      r4rceZdZejdejdddfdeede e e e ge ffdee dede d ed e d e d e fd Z deefdZdS)MultiLineBreakFg?rrrmarginsrrwrapmode line_heightskip_leading_spacesfirst_line_indentc |||_tr|_n fd|_||_||_||_||_||_||_d|_ d|_ d|_ | |_ d|_ dS)a6Accept text as Fragments, to be split into individual lines depending on line width and text height. Args: fragments: A sequence of Fragment()s containing text. max_width: Either a fixed width as float or a callback function get_width(height). If a function, it gets called with the largest height encountered on the current line, and must return the applicable width for the line with the given height at the current vertical position. The height is relevant in those cases where the lateral boundaries of the enclosing TextRegion() are not vertical. margins (sequence of floats): The extra clearance that may apply at the beginning and/or end of a line (usually either FPDF.c_margin or 0.0 for each side). align (Align): The horizontal alignment of the current text block. print_sh (bool): If True, a soft-hyphen will be rendered normally, instead of triggering a line break. Default: False wrapmode (WrapMode): Selects word or character based wrapping. line_height (float, optional): A multiplier relative to the font size changing the vertical space occupied by a line of text. Default 1.0. skip_leading_spaces (bool, optional): On each line, any space characters at the beginning will be skipped. Default value: False. first_line_indent (float, optional): left spacing before first line of text in paragraph. cSr-r)rrs r2z)MultiLineBreak.__init__..sIr4rNT)rcallablervr1rrr2r3r4fragment_indexcharacter_indexidx_last_forced_breakr5_is_first_line) r1rrr1rrr2r3r4r5s ` r2r3zMultiLineBreak.__init__sD 0" I   6&DNN5555DN      &#6 #$$%48"!2"r4r+c  d}|j}d|_|jt|jkrdSd}||}t ||j|jr|jnd}|j D]}|t|z}|jr ||jz}|j r|jt|jkr|j t|j|jj krd|_ |xjdz c_b|j|jj |j }|tkr|xj dz c_ nn|jt|jk|jt|jkr|j|j}tj|j|rP|j}||}||_|j D]}|t|z}|jr ||jz}|j t|j krd|_ |xjdz c_|j |j }|||j| } d}|t(t*fvr}|xj dz c_ |js||jz|_d|_||jt4jkr t4jn|j|t(k|t*kStj|j| z|r,d|_|t<vr*|xj dz c_ ||jS|jt@j!kr.|"||jS|#r:|$|j\|_|_ } |xj dz c_ | S||j krtKd|j |_||jt4jkr t4jn|jS|&|| ||j|j ||jz|j'|xj dz c_ |jt|jk|jrBd|_||jt4jkr t4jn|jSdS) NTr)rrrrrF)rrz8Not enough horizontal space to render a single character)(r<r:rrrvrrr=r5r1rr4r;r'rr greater_thanrVrrr FORM_FEEDr3rr*rrJLrwrr2rCHARr%r,r.rr!r*) r1 first_charr<current_font_heightr current_linemarginrcurrent_fragmentrlines r2get_linezMultiLineBreak.get_linesH $ :%)"  #dn"5"5 5 54%&NN#677 "]-1-@G4))a   l ' 'F v &II   0 / /I  # %DN(;(;;;'3N4#67B,,,-D(''1,'' N4+>?J( %%((A-(((%DN(;(;;;!C$7$777#~d.AB * *,? 8'7&@# NN+>?? )2 &"l//Fv.II&8!77I#s+;+F'G'GGG'($##q(##(3D4HII.BB4=^COJWi000$$)$$#-Q*=@P*PL'&+##00#zUW44EGG$* )W 4'0I'=1 *"_4i ',#!;;;((A-(('44TZ@@@=HM11!55777'44TZ@@@88:: %44TZ@@ +,((A-((K(D,@@@'R.2-A*#00#zUW44EGG$*  & & #$#d&66 %     A % Y!C$7$777\   "'D ,,:00dj tr4N)rrrrrBrWORDr r&r rrrr3r rrJrr4r2r0r0s w%] $)#$2#2#H%2#%% 8892#% 2#  2#  2#2#2#"2#!2#2#2#2#j|(8,||||||r4r0N)3rtypingrrrrrrr r r r rr fpdf.drawing_primitivesrrrenumsrrrrrerrorsrfontsrrr(rutilrrrrrrBREAKING_SPACE_SYMBOLSrsrrrr@r&rrrr rr0rr4r2rSs                          EEEEEEEEEEEEEEEEEEEEEEEE!!!!!!$$$$$$$$))))))////////  " WW%;<<   ________D )R)R)R)R)RX)R)R)RX ! ! ! ! !z ! ! !F           r r r r r r r r jrrrrrrrrrrr4