
    Bh?                         S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	  SSK
Jr  SS	KJrJr  SS
KJr  SSKJr  SSKJrJr  S rS rSS jr " S S\5      r " S S5      rg)aa  Transform a "before layout" box tree into an "after layout" tree.

Break boxes across lines and pages; determine the size and dimension of each
box fragement.

Boxes in the new tree have *used values* in their ``position_x``,
``position_y``, ``width`` and ``height`` attributes, amongst others.

See https://www.w3.org/TR/CSS21/cascade.html#used-value

    defaultdict)partial)inf   )boxesbuild)PROGRESS_LOGGER   )absolute_box_layoutabsolute_layout)layout_backgrounds)block_level_layout)make_all_pagesmake_margin_boxesc                 l   / U l         UR                  S   nUS:X  a  SnOMUS:X  a  SnODUS:X  a  UR                  S   S:H  nO+US	:X  a  UR                  S   S
:H  nOUR                  S   S:H  nSnSUR                  5       S   S.nS/SS/0S1/4nSS/ / S.nU R                   R                  XEX6U45        g)zgInitialize ``context.page_maker``.

Collect the pagination's states required for page based counters.

break_beforerightTleftFrecto	directionltrversortlNanyr   )breakpagepages)content_changedpages_wantedanchorscontent_lookups)
page_makerstylepage_valuesappend)contextroot_box
page_break
right_page	resume_at	next_page
page_stateremake_states           L/var/www/html/env/lib/python3.13/site-packages/weasyprint/layout/__init__.pyinitialize_page_makerr0      s     G /J W
	v	
	w	^^K0E9
	w	^^K0E9
^^K0E9
I)=)=)?)BCI 
	1#
	J !	L jlD E    c              #   4  #    U H  nUR                    H{  n[        U[        R                  5      (       a  UR	                  5       n/ n[        XX%[        * SS9u  pgUv   U(       d  MT  / nU H  n[        XX([        * SS9  M     UnU(       a  M'  M}     M     g7f)zBLay out and yield fixed boxes of ``pages`` on ``containing_page``.N)bottom_space
skip_stack)fixed_boxes
isinstancer   ReplacedBoxcopyr   r   r   )	r'   r   containing_pager   boxabsolute_boxesabsolute_box_new_absolute_boxess	            r/   layout_fixed_boxesr?   M   s     ##C #u0011hhj  N1o!Td4OL  .%'")C#o&)Td< * "4 !. $ s   A"B(%B	Bc           
   #     #    [        X!5        / n/ nSn[        U5       H  nUS:  a,  [        R                  " SU5        UR	                  5       Ul        UnUS:X  a  UR
                  R	                  5       n[        [        X!X5      5      n[        U5      nSn	Sn
UR                   H1  nUu      pnUS   nU/US'   US   (       a  Sn	US   (       d  M-  X:g  n
M3     U	(       a  M  U
(       a  M    O   / n/ n/ n[        U5       GH  u  nnUR                  US-      u      pnUS   nUR                  5        GH  nUR                  (       a{  UR                  R                  S	5      (       a  UnO%UR                  R                  S
5      (       a  UnOUnUR                  U;   a  SUl        OUR!                  UR                  5        UR"                  (       a  UR$                  R&                  R)                  5        Hf  u  u  nnnUR"                  U:X  d  M  US:w  d  M#  US:X  a  UR                  (       d  M<  UR+                  U5        US:X  d  MU  UR                  Ul        Mh     UR,                  nU(       d  GME  US:w  d  GMN  U H,  nUu  nnUR,                  U   US-      R!                  U5        M.     GM     GM     [        U5       GH  u  nn/ nUR.                  u  nn UR1                  [3        X$SU U5      5        UR1                  UR.                  5        UR1                  [3        X$US-   S U5      5        UUl        US-   Ul        UR                  UR4                     S   n!U4Ul        U R.                  (       a  U=R.                  U 4-  sl        U=R.                  [7        [9        UUU!5      5      -  sl        [;        UUR<                  5        Uv   GM     g7f)a  Lay out the whole document.

This includes line breaks, page breaks, absolute size and position for all
boxes. Page based counters might require multiple passes.

:param root_box:
    Root of the box tree (formatting structure of the HTML). The page boxes
    are created from that tree, this structure is not lost during
    pagination.
:returns:
    A list of laid out Page objects.

r   z+Step 5 - Creating layout - Repagination #%dFr   r   r   Tr    z::beforez::after contentzbookmark-labelnoneN   )r0   ranger
   infor8   	footnoteslistr   lenr#   	enumeratedescendantsbookmark_labelelement_tagendswithelementr&   missing_linktarget_collectorcounter_lookup_itemsitemsparse_again
string_setchildrenextendr?   current_pagetupler   r   get_image_from_uri)"htmlr(   r'   	max_loopsr   original_footnotesactual_total_pagesloopinitial_total_pagesreloop_contentreloop_pages	page_datar=   r-   r.   page_counter_valueswatch_elementswatch_elements_beforewatch_elements_afterir   child	checklistr:   	css_tokenitemstring_setsrU   string_nametextroot_childrenrootfootnote_areastates"                                     r/   layout_documentrt   f   s     ',Ei !8  =tE 2 7 7 9G019!(!2!2!7!7!9^GtCD Z  ++I09-Aq!",Q-,>+?(-.!%N++2H , ~ll7 !F NU#4!(!3!3AE!:1aQ(m%%'E##$$--j99 5I&&//	:: 4I .I==I-+-E($$U]]3!!00EEKKM +$S)d))S0Y)5K%)99$)$8$8$(()<= %(88363E3EE0 N  **K{{f4"-J(2%K&&{3AaC8??E #.= ( $P U#4"mmm/rDIJT]]+/q1uvMN% 1u ""7#7#78;!!MMm--M0$FGG4!;!;<
! $s8   CO!O!$O!-D$O!O!.O!&O!:O!EO!c                       \ rS rSrSrS rSrg)FakeList   u)   List in which you can’t append objects.c                     g N )selfrl   s     r/   r&   FakeList.append   s    r1   rz   N)__name__
__module____qualname____firstlineno____doc__r&   __static_attributes__rz   r1   r/   rv   rv      s
    3r1   rv   c                   z    \ rS rSrS rS r\S 5       rS rS r	S r
S rSS	 jrSS
 jrS rS rS rS rS rSrg)LayoutContext   c                 t   Xl         [        X S9U l        X0l        X@l        XPl        / U l        / U l        0 U l        / U l	        / U l
        S U l        S U l        S U l        [        S 5      U l        [        S 5      U l        S U l        SU l        0 U l        SU l        0 U l        0 U l        0 U l        0 U l        g )N)r'   c                      [        S 5      $ )Nc                      / $ ry   rz   rz   r1   r/   <lambda>:LayoutContext.__init__.<locals>.<lambda>.<locals>.<lambda>   s    "r1   r   rz   r1   r/   r   (LayoutContext.__init__.<locals>.<lambda>   s	    k*.Er1   c                      [        S 5      $ )Nc                      / $ ry   rz   rz   r1   r/   r   r      s    r1   r   rz   r1   r/   r   r      s	    K
4Kr1   F)	style_forr   rZ   font_configcounter_stylerQ   _excluded_shapes_listsrG   page_footnotescurrent_page_footnotesreported_footnotescurrent_footnote_areaexcluded_shapespage_bottomr   rU   running_elementsrX   forced_breakbroken_out_of_flow	in_columnstrut_layoutsfont_featurestablesdictionaries)r{   r   rZ   r   r   rQ   s         r/   __init__LayoutContext.__init__   s    "")*<"K&* 0&(# &(#"$%)"#%&EF +,K L !"$  r1   c                 @    U R                  U R                  U-
  U5      $ ry   )	overflowsr   )r{   r3   
position_ys      r/   overflows_pageLayoutContext.overflows_page   s    ~~d..=zJJr1   c                     XS-  :  $ )Ng0D   ?rz   )bottomr   s     r/   r   LayoutContext.overflows   s     h///r1   c                 \    / U l         U R                  R                  U R                   5        g ry   )r   r   r&   r{   s    r/   create_block_formatting_context-LayoutContext.create_block_formatting_context  s$    !##**4+?+?@r1   c                    UR                   S   S:X  a  U R                  (       ay  UR                  5       UR                  -   n[	        U R                   Vs/ s H   nUR
                  UR                  5       -   PM"     snU/-   5      nU=R                  XB-
  -  sl        U R                  R                  5         U R                  (       a  U R                  S   U l        g S U l        g s  snf )Nheightauto)	r$   r   content_box_yr   maxr   margin_heightr   pop)r{   r(   
box_bottomshapemax_shape_bottoms        r/   finish_block_formatting_context-LayoutContext.finish_block_formatting_context
  s    >>(#v-$2F2F!//1HOOCJ"!11$31E   5#6#6#881$36@\$B  C OO/<<O##'')&&#'#>#>r#BD #'D $3s   'C&c                 l    [        5       U l        U R                  R                  U R                  5        g ry   )rv   r   r   r&   r   s    r/   create_flex_formatting_context,LayoutContext.create_flex_formatting_context  s&    'z##**4+?+?@r1   c                     U R                   R                  5         U R                   (       a  U R                   S   U l        g S U l        g )Nr   )r   r   r   )r{   r(   s     r/   finish_flex_formatting_context,LayoutContext.finish_flex_formatting_context  s8    ##'')&&#'#>#>r#BD #'D r1   c                 <    U R                  U R                  XU5      $ )z!Resolve value of string function.)get_string_or_element_forrU   r{   r   namekeywords       r/   get_string_set_for LayoutContext.get_string_set_for#  s     --OOT2 	2r1   c                 <    U R                  U R                  XU5      $ )z"Resolve value of element function.)r   r   r   s       r/   get_running_element_for%LayoutContext.get_running_element_for(  s"    --!!4w8 	8r1   c                    U R                   X   ;   a  X   U R                      S   nX   U R                      S   nUS:X  a  U$ US:X  aa  UnU(       aW  UR                  S   S:w  a!  UR                  S    H  u  pX:X  d  M  Us  $    UR                  (       a  UR                  S   nM]  OOUS:X  a  U$ US:X  a  g	[        U R                   S
-
  SS5       H  n
XU   ;   d  M  X   U
   S   s  $    g	)a  Resolve value of string or element function.

We'll have something like this that represents all assignments on a
given page:

{1: ['First Header'], 3: ['Second Header'],
 4: ['Third Header', '3.5th Header']}

Value depends on current page.
https://drafts.csswg.org/css-gcpm/#funcdef-string

:param dict store:
    Dictionary where the resolved value is stored.
:param page:
    Current page.
:param str name:
    Name of the named string or running element.
:param str keyword:
    Indicates which value of the named string or running element to
    use. Default is the first assignment on the current page else the
    most recent assignment.
:returns:
    Text for string set, box for running element.

r   r   firststartrU   rC   lastzfirst-exceptNr   )rX   r$   rV   rE   )r{   storer   r   r   first_stringlast_stringrO   rn   r=   previous_pages              r/   r   'LayoutContext.get_string_or_element_for-  s   4 + ;t'8'89!<L+d&7&78<K'!##G#}}\2f<07l0K,[*2'3 3 1L ''")"2"21"5   F"""N*"4#4#4q#8!R@Md+{=1"55 Ar1   c                     U R                   R                  U5        U R                  R                  U5        U R	                  5       $ )z+Add a footnote to the layout for this page.)rG   remover   r&   _update_footnote_arear{   footnotes     r/   layout_footnoteLayoutContext.layout_footnotea  s7    h'##**84))++r1   c                 $   XR                   ;  a  U R                   R                  U5        XR                  ;   a  U R                  R                  U5        O*XR                  ;   a  U R                  R                  U5        U R                  5         gg)z@Remove a footnote from the layout and return it to the waitlist.N)rG   r&   r   r   r   r   r   s     r/   unlayout_footnoteLayoutContext.unlayout_footnoteg  sq     >>)NN!!(+666++228<444''..x8&&( *r1   c                     U R                   R                  U5        U R                  R                  U5        U R	                  5         g)z0Mark a footnote as being moved to the next page.N)r   r   r   r&   r   r   s     r/   report_footnoteLayoutContext.report_footnotes  s6    ##**84&&x0""$r1   c                    U R                   R                  S:w  a>  U R                  (       d-  U =R                  U R                   R	                  5       -  sl        U R
                  U R                   l        U R                   R                  (       Ga   [        R                  " U R                   R                  5       5      n[        X[        * SU R                   R                  5      S   nUR                  U R                   l        U R                  (       d#  U =R                  UR	                  5       -  sl        UR                  S   nUR                  UR	                  5       -   UR                  UR	                  5       -   UR                  -
  :  nU$ SU R                   l        U R                  (       d-  U =R                  U R                   R	                  5       -  sl        g)z9Update the page bottom size and our footnote area height.r   Nr   r   F)r   r   r   r   r   r   rV   r	   create_anonymous_boxesdeepcopyr   r   r   r   margin_bottom)r{   rr   
last_childoverflows       r/   r   #LayoutContext._update_footnote_areay  sz   %%,,6t~~ : : H H JJ.2.I.I""+%%...!88**3357M.cT4**//1124M 1>0D0DD&&->>  M$?$?$AA &//3J%%
(@(@(BB((=+F+F+HH++,,  O01D&&->>  D$>$>$L$L$NN r1   )r   r   r   r   rX   r   r   r   r   r   rG   r   rZ   r   r   r   r   r   rU   r   r   r   rQ   N)r   )r}   r~   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r   rz   r1   r/   r   r      s[    8K 0 0
A(A(2
8
26h,
)%r1   r   N)   )r   collectionsr   	functoolsr   mathr   formatting_structurer   r	   loggerr
   absoluter   r   
backgroundr   blockr   r   r   r   r0   r?   rt   rH   rv   r   rz   r1   r/   <module>r      sR   
 $   / $ : * % 31Eh42qht q qr1   