
    ,g?                         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                 h   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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           r/home/viktor/gitlab-persoonlijk/factuur-applicatie/venv/lib/python3.12/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 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?   M   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   )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     '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 )selfrl   s     r/   r&   zFakeList.append   s    r1   N)__name__
__module____qualname____doc__r&   ry   r1   r/   rv   rv      s
    3r1   rv   c                   n    e Zd Zd Zd Zed        Z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                    || _         t        ||       | _        || _        || _        || _        g | _        g | _        i | _        g | _	        g | _
        d | _        d | _        d | _        t        d       | _        t        d       | _        d | _        d| _        i | _        d| _        i | _        i | _        i | _        i | _        y )N)r'   c                      t        d       S )Nc                      g S rx   ry   ry   r1   r/   <lambda>z:LayoutContext.__init__.<locals>.<lambda>.<locals>.<lambda>   s    " r1   r   ry   r1   r/   r   z(LayoutContext.__init__.<locals>.<lambda>   s    k*.E r1   c                      t        d       S )Nc                      g S rx   ry   ry   r1   r/   r   z:LayoutContext.__init__.<locals>.<lambda>.<locals>.<lambda>   s     r1   r   ry   r1   r/   r   z(LayoutContext.__init__.<locals>.<lambda>   s    K
4K r1   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)rz   r   rZ   r   r   rQ   s         r/   __init__zLayoutContext.__init__   s    "")*<d"K&* 0&(# &(#"$%)"#%&EF +,K L !"$  r1   c                 @    | j                  | j                  |z
  |      S rx   )	overflowsr   )rz   r3   
position_ys      r/   overflows_pagezLayoutContext.overflows_page   s    ~~d..=zJJr1   c                     || dz  kD  S )Ng0D   ?ry   )bottomr   s     r/   r   zLayoutContext.overflows   s     Fh///r1   c                 \    g | _         | j                  j                  | j                          y rx   )r   r   r&   rz   s    r/   create_block_formatting_contextz-LayoutContext.create_block_formatting_context  s$    !##**4+?+?@r1   c                    |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                  r| j                  d   | _        y d | _        y c c}w )Nheightauto)	r$   r   content_box_yr   maxr   margin_heightr   pop)rz   r(   
box_bottomshapemax_shape_bottoms        r/   finish_block_formatting_contextz-LayoutContext.finish_block_formatting_context
  s    >>(#v-$2F2F!//1HOOCJ"!11$3   5#6#6#88$36@\$B  C OO/*<<O##'')&&#'#>#>r#BD #'D $3s   $Cc                 l    t               | _        | j                  j                  | j                         y rx   )rv   r   r   r&   r   s    r/   create_flex_formatting_contextz,LayoutContext.create_flex_formatting_context  s&    'z##**4+?+?@r1   c                     | j                   j                          | j                   r| j                   d   | _        y d | _        y )Nr   )r   r   r   )rz   r(   s     r/   finish_flex_formatting_contextz,LayoutContext.finish_flex_formatting_context  s8    ##'')&&#'#>#>r#BD #'D r1   c                 >    | j                  | j                  |||      S )z!Resolve value of string function.)get_string_or_element_forrU   rz   r   namekeywords       r/   get_string_set_forz LayoutContext.get_string_set_for#  s"    --OOT42 	2r1   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 	8r1   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startrU   rC   lastzfirst-exceptNr   )rX   r$   rV   rE   )rz   storer   r   r   first_stringlast_stringrO   rn   r=   previous_pages              r/   r   z'LayoutContext.get_string_or_element_for-  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	6r1   c                     | j                   j                  |       | j                  j                  |       | j	                         S )z+Add a footnote to the layout for this page.)rG   remover   r&   _update_footnote_arearz   footnotes     r/   layout_footnotezLayoutContext.layout_footnotea  s7    h'##**84))++r1   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)rG   r&   r   r   r   r   r   s     r/   unlayout_footnotezLayoutContext.unlayout_footnoteg  sw     4>>)NN!!(+4666++228<T444''..x8&&( *r1   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_footnotes  s6    ##**84&&x0""$r1   c                    | j                   j                  dk7  r9| j                  s-| xj                  | j                   j	                         z  c_        | j
                  | j                   _        | j                   j                  rt        j                  | j                   j                               }t        | |t         d| j                   j                        d   }|j                  | j                   _        | j                  s#| xj                  |j	                         z  c_        |j                  d   }|j                  |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   rV   r	   create_anonymous_boxesdeepcopyr   r   r   r   margin_bottom)rz   rr   
last_childoverflows       r/   r   z#LayoutContext._update_footnote_areay  sy   %%,,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++,,  O01D&&->>  D$>$>$L$L$NN r1   N)r   )r{   r|   r}   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   ry   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   ry   r1   r/   <module>r      sR   
 $   / $ : * % 31Eh42qht q qr1   