
    3vjA                         d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZ dd	lmZmZ dd
lmZ ddlmZ ddlmZmZ d Zd ZddZ G d de      Z G d d      Zy)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                 j   g | _         |j                  d   }|dk(  rd}nJ|dk(  rd}nB|dk(  r|j                  d   dk(  }n*|d	k(  r|j                  d   d
k(  }n|j                  d   dk(  }d}d|j                         d   d}dgddgidhgg f}ddg g d}| j                   j                  |||||f       y)zoInitialize ``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           M/root/.hermes/venv/lib/python3.12/site-packages/weasyprint/layout/__init__.pyinitialize_page_makerr/      s     G /J W
	v	
	w	^^K0E9
	w	^^K0E9
^^K0E9
I)=)=)?)BCI 
	1#

J !	L 9j*lD E    c              #     K   |D ]  }|j                   D ]r  }t        |t        j                        r|j	                         }g }t        | |||t         d      \  }}| |sOg }|D ]  }t        | |||t         d        |}|r$t  yw)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>   N   s      4## 	4C #u001hhj  N1o~!Td4OL!  %'") <C#o7I&)Td<< "4 !	44s   A B##BBc           
   #   (  K   t        ||       g }g }d}t        |      D ]  }|dkD  r+t        j                  d|       |j	                         |_        |}|dk(  r|j
                  j	                         }t        t        ||| |            }t        |      }d}	d}
|j                  D ]'  }|\  }}}}}|d   }|g|d<   |d   rd}	|d   s#||k7  }
) |	r|
r n g }g }g }t        |      D ]  \  }}|j                  |dz      \  }}}}}|d   }|j                         D ]P  }|j                  ro|j                  j                  d	      r|}n |j                  j                  d
      r|}n|}|j                  |v rd|_        n|j!                  |j                         |j"                  r|j$                  j&                  j)                         D ]X  \  \  }}}|j"                  |k(  s|dk7  s|dk(  r|j                  s1|j+                  |       |dk(  sH|j                  |_        Z |j,                  }|s|dk7  s!|D ]+  }|\  }}|j,                  |   |dz      j!                  |       - S  t        |      D ]  \  }}g }|j.                  \  }} |j1                  t3        ||d| |             |j1                  |j.                         |j1                  t3        |||dz   d |             ||_        |dz   |_        |j                  |j4                     d   }!|f|_        | j.                  r|xj.                  | fz  c_        |xj.                  t7        t9        |||!            z  c_        t;        ||j<                         |  yw)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   )r/   ranger	   infor7   	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	checklistr9   	css_tokenitemstring_setsrT   string_nametextroot_childrenrootfootnote_areastates"                                     r.   layout_documentrs   g   s     '8,Ei  !8  =tE 2 7 7 9G019!(!2!2!7!7!9^GXtUCD Z  ++ 	II09-Aq!Z",Q-,>+?(-.!%N+26HH	I l7F NU# %F4!(!3!3AE!:1aQ(m%%'  	FE##$$--j9 5I&&//	: 4I .I==I-+-E($$U]]3!!00EEKKMF*$S)d))S0Y)5K%)99$)$8$8$(()<= %(88363E3EE0F  **K{f4"- FJ(2%K&&{3AaC8??EF= 	F%F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N		NNDN&N,(N!N8N?ENc                       e Zd ZdZd Zy)FakeListu)   List in which you can’t append objects.c                      y N )selfrk   s     r.   r%   zFakeList.append   s    r0   N)__name__
__module____qualname____doc__r%   rx   r0   r.   ru   ru      s
    3r0   ru   c                       e Zd Zd Zd Zed        Zed        Zej                  d        ZddZ
ddZd	 Zd
 Zd ZddZddZd Zd Zd Zd Zd Zy)LayoutContextc                 h   || _         t        ||       | _        || _        || _        || _        g | _        i | _        g | _        i | _	        g | _
        g | _        d | _        d | _        t        d       | _        t        d       | _        d | _        d| _        i | _        d| _        i | _        i | _        y )N)r&   c                       t        t              S rw   r   rG   rx   r0   r.   <lambda>z(LayoutContext.__init__.<locals>.<lambda>   s    k$.? r0   c                       t        t              S rw   r   rx   r0   r.   r   z(LayoutContext.__init__.<locals>.<lambda>   s    K4E r0   F)	style_forr   rY   font_configcounter_stylerP   _excluded_shapes_root_boxes_excluded_shapesrF   page_footnotescurrent_page_footnotesreported_footnotescurrent_footnote_areapage_bottomr   rT   running_elementsrW   forced_breakbroken_out_of_flow	in_columntablesdictionaries)ry   r   rY   r   r   rP   s         r.   __init__zLayoutContext.__init__   s    "")*<d"K&* 0+-( " &(#"$%)"%&?@ +,E F !"$ r0   c                 @    | j                  | j                  |z
  |      S rw   )	overflowsr   )ry   r2   
position_ys      r.   overflows_pagezLayoutContext.overflows_page   s    ~~d..=zJJr0   c                     || dz  kD  S )Ng0D   ?rx   )bottomr   s     r.   r   zLayoutContext.overflows   s     Fh///r0   c                 :    | j                   | j                  d      S Nr   r   )ry   s    r.   excluded_shapeszLayoutContext.excluded_shapes  s    $$T%E%Eb%IJJr0   c                 <    || j                   | j                  d   <   y r   r   )ry   r   s     r.   r   zLayoutContext.excluded_shapes	  s    FUd>>rBCr0   Nc                     || j                   vsJ | j                   j                  |       || j                  vr|g n|| j                  |<   y y rw   )r   r%   r   )ry   r'   new_lists      r.   create_block_formatting_contextz-LayoutContext.create_block_formatting_context  sT    t?????((//940004<4Db(D!!(+ 1r0   c                    |r|j                   d   dk(  r| j                  rw|j                         |j                  z   }t	        | j                  D cg c]  }|j
                  |j                         z   ! c}|gz         }|xj                  ||z
  z  c_        | j                  j                  | j                  j                                y c c}w )Nheightauto)
r#   r   content_box_yr   maxr   margin_heightr   popr   )ry   r'   
box_bottomshapemax_shape_bottoms        r.   finish_block_formatting_contextz-LayoutContext.finish_block_formatting_context  s    x0F:t?S?S!//1HOOCJ"!11$3   5#6#6#88$36@\$B  C OO/*<<O!!$"B"B"F"F"HI	$3s   $Cc                 8    | j                  |t                      y rw   )r   ru   ry   r'   s     r.   create_flex_formatting_contextz,LayoutContext.create_flex_formatting_context  s    ,,XxzBr0   c                 &    | j                  |       y rw   )r   r   s     r.   finish_flex_formatting_contextz,LayoutContext.finish_flex_formatting_context   s    ,,X6r0   c                 D    ||| j                   d   |f| j                  |<   y r   )r   r   )ry   new_boxr9   containing_blockr*   s        r.   add_broken_out_of_flowz$LayoutContext.add_broken_out_of_flow#  s*    !4#C#CB#G,T(r0   c                 >    | j                  | j                  |||      S )z!Resolve value of string function.)get_string_or_element_forrT   ry   r   namekeywords       r.   get_string_set_forz LayoutContext.get_string_set_for'  s"    --OOT42 	2r0   c                 >    | j                  | j                  |||      S )z"Resolve value of element function.)r   r   r   s       r.   get_running_element_forz%LayoutContext.get_running_element_for,  s$    --!!4w8 	8r0   c                    | j                   ||   v r||   | j                      d   }||   | j                      d   }|dk(  r|S |dk(  rS|}|r\|j                  d   dk7  r |j                  d   D ]  \  }}	||k(  s|c S  |j                  r|j                  d   }Pn|dk(  r|S |dk(  ry	t        | j                   d
z
  dd      D ]  }
|
||   v s||   |
   d   c S  y	)aZ  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startrT   rB   lastzfirst-exceptNr
   )rW   r#   rU   rD   )ry   storer   r   r   first_stringlast_stringrN   rm   r<   previous_pages              r.   r   z'LayoutContext.get_string_or_element_for1  s   4 d+ ;t'8'89!<L+d&7&78<K'!##G#}}\2f<07l0K 4,[!*d2'3 34 ''")"2"21"5 F"""N*"4#4#4q#8!R@ 	6Md+T{=1"55	6r0   c                     | j                   j                  |       | j                  j                  |       | j	                         S )z+Add a footnote to the layout for this page.)rF   remover   r%   _update_footnote_areary   footnotes     r.   layout_footnotezLayoutContext.layout_footnotee  s7    h'##**84))++r0   c                    || j                   vr| j                   j                  |       || j                  v r| j                  j                  |       n)|| j                  v r| j                  j                  |       | j                          yy)z@Remove a footnote from the layout and return it to the waitlist.N)rF   r%   r   r   r   r   r   s     r.   unlayout_footnotezLayoutContext.unlayout_footnotek  sw     4>>)NN!!(+4666++228<T444''..x8&&( *r0   c                     | j                   j                  |       | j                  j                  |       | j	                          y)z0Mark a footnote as being moved to the next page.N)r   r   r   r%   r   r   s     r.   report_footnotezLayoutContext.report_footnotew  s6    ##**84&&x0""$r0   c                    | j                   j                  dk7  r9| j                  s-| xj                  | j                   j	                         z  c_        | j
                  | j                   _        | j                   j                  rt        j                  | j                   j                               }t        | |t         d| j                   j                        d   }|j                  | j                   _        | j                  s#| xj                  |j	                         z  c_        |j                  d   }|j                  |j	                         z   |j                  z
  }|j                  |j	                         z   |j                  z
  }||kD  }|S d| j                   _        | j                  s-| xj                  | j                   j	                         z  c_        y)z9Update the page bottom size and our footnote area height.r   Nr   r   F)r   r   r   r   r   r   rU   r   create_anonymous_boxesdeepcopyr   r   r   r   margin_bottom)ry   rq   
last_childlast_child_bottomfootnote_area_bottomoverflows         r.   r   z#LayoutContext._update_footnote_area}  s   %%,,6t~~ : : H H JJ.2.I.I""+%%..!88**3357M.mcT4**//1124M 1>0D0DD&&->>  M$?$?$AA &//3J%%
(@(@(BB(()  ((=+F+F+HH++, ! )+??HO01D&&->>  D$>$>$L$L$NN r0   )NNrw   )r   )rz   r{   r|   r   r   staticmethodr   propertyr   setterr   r   r   r   r   r   r   r   r   r   r   r   rx   r0   r.   r   r      s    4K 0 0
 K K V VSJC7T2
8
26h,
)%r0   r   N)   )r}   collectionsr   	functoolsr   mathr   formatting_structurer   r   loggerr	   absoluter   r   
backgroundr   blockr   r   r   r   r/   r>   rs   rG   ru   r   rx   r0   r.   <module>r      sR   
 $   / $ : * % 32Ej42qht w wr0   