
    3vj                        d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ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 ddlmZmZmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z- d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5ed        Z6d Z7d Z8d Z9d  Z:d! Z;d-d"Z<d# Z=d$ Z>d% Z?d& Z@d' ZAd( ZBd) ZCd* ZDd+ ZEd, ZFy).zLayout for inline-level boxes.    N)inf   )AnonymousStylePending
check_math)	INHERITED)boxesbuild   )AbsolutePlaceholderabsolute_layout)flex_layout)avoid_collisionsfloat_layout)grid_layout)handle_leader)handle_min_max_width)
percentageresolve_one_percentageresolve_percentages)inline_min_content_widthshrink_to_fittrailing_whitespace_size)inline_replaced_box_layout)find_in_flow_baselinetable_wrapper_width)can_break_textcharacter_ratiocreate_layoutsplit_first_linestrutc
              #     K   t        ||       |t        |d|j                         nd|_        	 t	        | |||||||||	
      \  }
}d}	|
r&t        | |
|       |
j                  |
j                  z   }|
y|
|f |y|}d|_        d}Yw)zReturn an iterator of ``(line, resume_at)``.

    ``line`` is a laid-out LineBox with as much content as possible that
    fits in the available width.

    Ntext_indentr   )r   r   widthr#   get_next_lineboxr   
position_yheight)contextboxr&   bottom_space
skip_stackcontaining_blockabsolute_boxesfixed_boxesfirst_letter_stylefirst_line_styleline	resume_ats               K/root/.hermes/venv/lib/python3.12/site-packages/weasyprint/layout/inline.pyiter_line_boxesr4      s      -.sM3C3I3IJ
*S*lJ@PK);=MOi  '4)9:4;;6J<Io
! s   BB
c
                 j   t        ||      }|dk(  ryt        |||      }||_        | j                  r2t	        | ||d      |_        t        |j                        \  |_        }
ndx|_        |_        t        | ||d      \  }}}|j                  }| j                  j                         }	 |x}|_        |x}|_        |j
                  }||z   }||j                  z  }g }g }g }g }g }t        | |||||||||||||	      \  }}}}}}|j
                  |j                  c|_        |_        t        |      r|s	d|_        nt        | |       t        | ||d      \  }}
}t!        | |||du xs |	      } |j                  d
   dk(  r| dz  } | |j
                  z  } t#        |      \  }!}"|"J |!J |" |_        |"|_        |!|"z
  |_        ||"z
  }#d|_        d|_        |j+                  | |#       ||_        |j                  |k  rn|j                  }| j                  }$|| _        t        | ||d      \  }}}|	r|j-                  |j.                        }%|%xj0                  dz  c_        |%j                  j                         |%_        |	j3                         D ]  \  }&}'|'|%j                  |&<    |%g|_        t5        |%       |j                  d
   dk(  r
||f||fk(  }(n||j
                  z   |f||z   |fk(  }(|(r|$| _        nW|j7                  |       |j7                  |       |D ]  })d|)j                  j8                  d   v r |)j+                  d||)j                  z
         >|)j+                  |j                  |)j                  z
  ||j                  z   |)j                  z
          g }*|j                  |j                  z   }+|D ]F  },|+|,_        t;        | |,||||d      \  }-}.|*j=                  |-       |.s3| j?                  |-|,||.       H |*r|xj.                  tA        |*      z  c_        ||fS )zReturn next line from given linebox.

    Return ``(line, resume_at)``, where ``line`` is a new linebox copied from the
    original one, with replaced children.

    This function takes care of excluded floating shapes to avoid collisions.

    continueNNT)r+   
first_liner   F)outerNlast	directionrtlz::first-lineltrinlinedisplayr+   )!skip_first_whitespacefirst_letter_to_boxr&   excluded_shapesr   r$   r!   styler'   r   copy
position_xr#   split_inline_boxis_phantom_lineboxremove_last_whitespace
text_alignline_box_verticalitybaseline
margin_topmargin_bottom	translatecopy_with_childrenchildrenelement_tagitems_adjust_line_heightextend	specifiedr   appendadd_broken_out_of_flowtuple)/r(   lineboxr&   r*   r+   r,   r-   r.   r/   r0   _rH   available_widthcandidate_heightrE   original_position_xoriginal_position_yoriginal_widthmax_xline_placeholdersline_absolutes
line_fixedwaiting_floatsline_childrenr1   r2   preserved_line_breakfirst_letterlast_letterfloat_widthnew_position_xnew_available_widthoffset_xbottomtopoffset_ynew_excluded_shapesfirst_line_boxkeyvalue	conditionplaceholderfloat_childrenwaiting_floats_ywaiting_floatnew_waiting_floatwaiting_float_resume_ats/                                                  r3   r%   r%   9   s    'w
;JZ$Wj:LMJ#G0WF!'--0 *+*.>*%/9+J
O ~~--224O
3==g03==g0 _,g)))

 &6gz5,
~z;L],>@P&R	#y.	k )-

DKK%w~d#,@DKw-1AW.e2=..T.t#;';> !!+.%7NH

"H*40!!!sl#x* %;;**;;%55"12BT+53:/
J !44T]]CN&&.8&#1#7#7#<#<#>N .446 2
U,1$$S)2+,DM/!!+.%7#Z0#%85: :I $djj0*=#n46IBK KI&9G#[ ^ .)z"( C{((229==!!!Z+2H2H%HI !!+"8"88T[[(;+A+AACC N4' 	)#3 5A]$4n$6822 	/0"**!=2B')	) ~..?    c                    |d}d}n|j                         \  \  }}t        | t        j                        r~|J | j                  d   }| j
                  j                         }|t        |      k(  ry|dv r6||d }|r/|j                  d      r|dz  }|dd }|r|j                  d      r|r|diS dS t        | t        j                  t        j                  f      rq|dk(  r| j                  syt        | j                  |   |      }|dk(  r7|dz  }|t        | j                        k\  ryt        | j                  |   d      }|s|r||iS dS |
J d|         y)	zReturn ``skip_stack`` to start just after removable leading spaces.

    See https://www.w3.org/TR/CSS21/text.html#white-space-model

    Nr   white_spacer6   normalnowrappre-line    r   zunexpected skip inside )rU   
isinstancer	   TextBoxrF   textencodelen
startswithLineBox	InlineBoxrS   rC   )r)   r+   indexnext_skip_stackr   r   results          r3   rC   rC      so    $.$4$4$6! #u}}%&&&ii.xx CI::<D4??40
ABx 4??40 !&t}/4/#u78A:cll&s||E':OLZQJECLL))!*3<<+>EF#(Fv==>!8>>r~   c                 f   g }|}t        |t        j                  t        j                  f      rX|j	                  |       |j
                  sy|j
                  d   }t        |t        j                  t        j                  f      rXt        |t        j                        r|j                  d   dv sy|j                  j                  d      }|rnt        |      t        |j                        k(  ry||_        t        | |dd      \  }}}|J |J |j                  |j                  z
  }|j                  |_        n|j                  }d|_        d|_        |j                  j                  dz  r%|j
                  D ]  }	|	j                  | d	
        |D ]  }
|
xj                  |z  c_         y)zRemove in place space characters at the end of a line.

    This also reduces the width and position of the inline parents of the
    modified text.

    Nr>   r   r    r    r   Tdxignore_floats)r   r	   r   r   rY   rS   r   rF   r   rstripr   split_text_boxr$   pango_layoutfirst_line_directionrQ   )r(   r1   	ancestorsr)   new_textnew_boxresumer]   space_widthchildancestors              r3   rK   rK      sy    I
C
S5==%//:
;||ll2	 S5==%//:
;
 sEMM*IIm$(HHxxs#Hx=CM)+GS$B"""~~ii'--/MM	ii	 ,,q0]] 	AEOO|4O@	A  &+%&r~   c                    |rP| j                   rCd|d<   d}| j                   d   }t        |t        j                        r| j                  j                         }|j                         D ]
  \  }}|||<    |j                  j                  d      rJt        j                  | j                   d|| j                  |g      }|g| j                   dd | _         |S |j                  rd}	|r.|j                         \  }
|
r|
\  }|j                  |d |_        d}|j                  rS|j                  d   }t        j                  |      }|d	vr|	rn(d
}	||z  }|j                  dd |_        |j                  rS|j                  d      r|t!        |      }|d   dk(  r|t        j                  | j                   d|| j                  g       }t        j                  | j                   d|| j                  |      }|f|_         |g| j                   | _         nt        j"                  | j                   d|| j                  g       }t        j$                  | j                   d|| j                  g       }|f|_         t        j                  | j                   d|| j                  |      }|f|_         |g| j                   | _         t'        j(                  |       |r$
r"|\  }|
j                         \  \  }}||dz   |ii}|S t        |t        j*                        r.|r|j                         \  }
nd}
t-        ||
|      }
|r|\  }||
i}|S )z-Create a box for the ::first-letter selector.staticpositionr   r   z::first-letterr   NF)PsPePiPfPoT
floatnone)rS   r   r	   r   rF   rG   rU   rT   endswithr   elementr   valuesunicodedatacategorylstripr   BlockBoxr   r
   process_text_transform	ParentBoxrD   )r)   r+   r/   rj   r   letter_styleru   rv   
letter_boxcharacter_foundchild_skip_stackr   next_letterr   children_styletext_boxline_boxchild_indexgrandchild_skip_stacks                      r3   rD   rD   &  sZ   cll *2:&QeU]]+99>>+L0668 *
U$)S!*  ))*:;"__'~6KK%*
 ", ?cll12.> ?x w "'(2(9(9(;%$'!1%*ZZ%7
%)
jj"'**Q-K*33K@H'EE*!*. K/L!&ABEJ jj  &&t, &4L%AN#G,6%*__"/~>(#++r&;
 $)=="/~>KK$7 08k
+(2'BS\\'B%*^^"/~>(#++r&;
 $)=="/~>KK$- 08k
+#(=="/~>KK$7 .6K)(2'BS\\'B00:!&6!+@P@V@V@X=<&;&+kAo?T-U%V
  u/$.$5$5$7! #' 2'); =##%56
r~   c           	      (   t        |t        j                        r3|j                         }t	        ||       |j                         |_        |S t        |t        j                        r|j                  rI|j                  |j                  f}t        | ||       |j                  |j                  |j                  }
}	}t        | ||||||      }|j                  r	
c|_	        |_        |_        |S t        dt!        |      j"                   d      )z7Compute the width and the height of the atomic ``box``.Layout for  not handled yet)r   r	   ReplacedBoxrG   r   margin_heightrN   InlineBlockBoxis_table_wrapperr$   r'   r   	min_width	max_widthinline_block_box_layout	TypeErrortype__name__)r(   r)   rH   r+   r,   r-   r.   containing_sizer$   r   r   s              r3   
atomic_boxr   w  s     #u(()hhj"3(89((* J 
C--	./557G7N7NOOo>*-))S]]CMMi9E%S*j2BK) 6;Y	3CIs}cm J +d3i&8&8%99IJKKr~   c                 p   ddl m} t        ||       |j                  dk(  rd|_        |j                  dk(  rd|_        |j
                  dk(  rd|_        |j                  dk(  rd|_        t        || |       ||_        d|_	         || |t         |d||d d d dd       \  }}}}}}t        |      |_        |S )Nr   )block_container_layoutautor   TF)
r*   r+   page_is_emptyr-   r.   adjoining_marginsr/   r0   discard	max_lines)blockr   r   margin_leftmargin_rightrO   rP   inline_block_widthrH   r&   r   inline_block_baselinerN   )	r(   r)   rH   r+   r,   r-   r.   r   r]   s	            r3   r   r     s    --. & 
6!
~~
F"sG%56CNCN/C4Jd%;RV$QUWCAq!Q )-CLJr~   c                    | j                   r}| j                  D ]m  }t        |t        j                        s|j                  s+|j                  d   j                  sE|j                  d   j                  d   }|j
                  c S  n#| j                  d   dk(  rt        | d      }|r|S | j                  | j                         z   S )zReturn the y position of the baseline for an inline block.

    Position is taken from the top of its margin box.

    https://www.w3.org/TR/CSS21/visudet.html#propdef-vertical-align

    r   overflowvisibleTr:   )
r   rS   r   r	   TableBoxrN   rF   r   r&   r   )r)   r   	first_rowr   s       r3   r   r     s     \\ 	.E%0>>ennQ&7&@&@ %q 1 : :1 =I$---		.
 
:	)	+&s6M>>C--///r~   c                     |j                   | j                  | j                  z   | j                  z   | j                  z   | j
                  z   | j                  z   z
  }| j                   dk(  rt        || |      | _         y y )Nr   )r$   r   r   border_left_widthborder_right_widthpadding_leftpadding_rightr   )r)   r(   r,   available_content_widths       r3   r   r     s    .44#***	 # 6 6	7	,,	-. yyF!'30GH	 r~   c                 2   |rt|j                         }|j                  j                         |_        |j                         D ]  \  }}|t        v s||j                  |<    t	        j
                  |       t        ||       ddd}t        |t        j                        r||_
        |d}n|j                         \  \  }}|xs d}|J t        |      dk(  }t        | |||z
  ||      \  }}}|d}n|di}|j                  rR|j                  d   }||j                  d   }n|j                  j                         d| j                         d   }ndx}}nt        |t        j                         rM|j"                  dk(  rd|_        |j$                  dk(  rd|_        t'        | |||||||||	|
|||      \  }}}}}}nKt        |t        j(                        r"t+        | ||||||      }||_
        d}d}d}d}nt        |t        j,                        r]||_
        d|_        d	D ]%  }t1        |d
|       dk(  st3        |d
| d       ' t5        | |t6         ||d||d	      \  }}}}}d}d}d}nt        |t        j8                        r\||_
        d|_        d	D ]%  }t1        |d
|       dk(  st3        |d
| d       ' t;        | |t6         ||d||      \  }}}}}d}d}d}n"t=        dt?        |      j@                   d      ||||||fS )a  Fit as much content as possible from an inline-level box in a width.

    Return ``(new_box, resume_at, preserved_line_break, first_letter, last_letter)``.
    ``resume_at`` is ``None`` if all of the content fits. Otherwise it can be passed as
    a ``skip_stack`` parameter to resume where we left off.

    ``new_box`` is non-empty (unless the box is empty) and as big as possible
    while respecting ``max_x``, if possible (may overflow is no split is possible.)

    r   leftrightNis_line_startr>   r   Fu   ⺀rq   r   rp   r   margin_r   r   )!rG   rF   rU   r   r
   r   r   r   r	   r   rH   r   r   r   r   decoder   r   r   rI   AtomicInlineLevelBoxr   InlineFlexBoxr&   getattrsetattrr   r   InlineGridBoxr   r   r   r   )r(   r)   rH   rc   r*   r+   r,   r-   r.   rd   rg   rh   r/   r0   ru   rv   float_widthsskipr   r   ri   r2   rj   rk   sider]   s                             r3   split_inline_levelr     s    hhjIINN$	*002 	'JCi!&		#	' 	$$S)-.*L#u}}%#D","2"2"4T:91D%%%M*a/.<S%*,d'/)++ <ItI8888A;L|!hhrl!hhoo/6==?C)--L;	C	)??f$COv% C&6c:ulJ~{<M],>@P'R	$)1<	l 
C33	4S*j2BK) (	$  	C,,	-#6 	2DsgdV,-7wtf-q1	2 '2S3$
,<eK'0#Aq!  %	C,,	-#6 	2DsgdV,-7wtf-q1	2 '2S3$
,<eK')#Aq!  %+d3i&8&8%99IJKK0, r~   c           	         |j                         rm||_        t        |      }|j                  |       |j                  |||f       |j                  d   dk(  r|	j                  |       y |
j                  |       y |j                         r||_        t        | ||j                        }||z   D cg c]  \  }}}|j                         s| }}}|r|t        | |d         z  }|||z
  kD  s|r|j                  |       y t        | |||	|
|d       \  }}|r| j                  ||||       |j                  |||f       |}t        |j                         d      }||j                  d   xx   |z  cc<   |j                  d   dk(  }|j                  d   dk(  }|j                  d	   d
k(  r)|j                  d   dk(  rd}|j                  d   dk(  r+d}n(|j                  d   dk(  rd}|j                  d   dk(  rd}|r t        |t        j                        r||z  }n|r||z  }|D ]Y  \  }}|j!                         s|xr |j                  d	   d
k(  xs |xr |j                  d	   dk(  }|sH|j#                  |       [ y |j%                         r@|j                  d   d   }| j&                  }| j(                  |   |   j                  |       y y c c}}w )Nr   absoluter>   rB   r   r   r   r   r<   r?   zinline-startTz
inline-endr=   r   r   )is_absolutely_positionedrH   r   rY   rF   
is_floatedr   r$   r   r   rZ   maxmargin_widthr   r	   r   is_in_normal_flowrQ   
is_runningcurrent_pagerunning_elements)r(   r)   r,   r   r   rS   rh   waiting_childrenrg   r-   r.   rd   r   rc   rH   r*   rx   rl   r]   child_non_floating_children	new_childfloat_resume_atr   
float_leftfloat_right	old_childfloat_alignrunning_namepages                                 r3   _out_of_flow_layoutr  /  s   
 %%'%)%0  -U ;<;;z"j0!!+.{+				%#GU4D4J4JK
 )13C(C!(#q&!$$& !( !( !3.r24 4K ++~ !!%()5 0.+*/&I ..5"2OE##UIu$=>E U'')1-BW-."4.W-7J++g.'9Kyy%.;;w'>9!%J;;w'<7"&K;;w'>9"&K;;w'<7!%Jc5==1 "$J - /9 224CCIIk$:e$C F DSYY{%;u%D  ''2'./ 
			{{:.q1##  .t4;;EB 
u!(s   5Kc                 @   |r|j                         }|r|j                         \  }}}|j                         rt        |      s2|r
||v r||   }nd }|j                  |j                         z   dz
  }||j                  kD  rAt        | ||j                  |||||||||	||      \  }}}}}}|rn|dz  }||j                  kD  rA|
j                  |       | t        |t        j                        sJ ||iS |
j                  |||f       ||iS |
r|
d   d   dz   d iS y )Nr   r>   r   )rG   popr   can_break_insiderH   r   r   rW   r   r	   r   rY   )r(   r)   rc   r*   initial_skip_stackr-   r.   rd   rg   rh   rS   r  r/   r0   waiting_children_copyr   r   original_childr   r  child_resume_atr]   s                         r3   _break_waiting_childrenr    so     !1 5 5 7#1F1J1J1L.K**,4DU4K!k5G&G#5k#B #'  $$u'9'9';;a?E%***9K^U-=-=u "2C!2N!#57G	:I6	?Aq!Q
 #
 %*** OO12 !%777  11 i ?@11 Q!#T** r~   c                    t        | j                        \  }| _        | j                  d   | _        || j                  z
  dz  }|| j                  z
  | j
                  z
  | _        || j                  z
  | j                  z
  | _	        y)zSet margins to the half leading to respect line height.

    Also compensate for borders and padding, we want margin_height() == line_height.

    	font_sizer   N)
r!   rF   rN   r'   border_top_widthpadding_toprO   border_bottom_widthpadding_bottomrP   )r)   line_heighthalf_leadings      r3   rV   rV     su     !&cii 0K;'CJ#**,1L!C$8$883??JCN$s'>'>>ASASSCr~   c                    |dz  }|du }|}|}t        |t        j                  t        j                  f      sJ |j                  |j
                  z   |j                  z   }|j                  |j                  z   |j                  z   }|}g }g }d}dx}}ddd}d}|j                  d   dk(  rg }|rd}n|j                         \  \  }}t        |j                  |d       D ]  \  }}||z   }|j                  |_        |j                         sGt!        | ||||||||
|||	||||       |j#                         r|dz   }||
vr||j%                         z  }t|t'        |j                        dz
  k(  }|} g }!t)        | ||| ||||||	|!|||      \  }"}#}$}%}&}'|j                  d	   d
k(  r|}(||'d   z  }n
|}(||'d   z  }|r(|(r&|#$| |(z  } t)        | ||| ||||||	|!|||      \  }"}#}$}%}&}'t+        |d   |'d         |d<   t+        |d   |'d         |d<   d}|$rd}d})|du rd}n|du rd}n|j                  d   dv rd})|),d||%fv rd})n#|%dv r|%})nt-        ||%z   |j                  d         })|)r|j/                  |       g }||%}|j0                  rd}n|&}|"t        |t        j2                        sJ t        |t        j                        r|j5                  ||"f       |"j6                  |"j%                         z   }*|*t9        | |"      z
  |kD  rt;        | |||||||	|
|||||      }+|+r|+}# nV|*}|j5                  ||"|f       |
j/                  |!       |#q|j/                  |       ||#i}# n |j/                  |       d}#|j                  d	   d
k(  rt'        |      dkD  rt|D ,-cg c]  \  },}-},|-j                         r|- }.},}-|.d   j6                  }|.ddd   D ]5  }|j=                  ||j6                  z
  d       ||j%                         z  }7 |#du }/|j?                  |D -,cg c]  \  }}-},|-
 c},}-}      }0|0jA                  | |/        t        |t        j                        rvd|0_!        |0j                  }|0j                  d	   dk(  r|ddd   }|D ]C  }|j                         s|j6                  |j%                         z   |0j6                  z
  |0_!         ny nw||0_        |j                  d   dk(  rd}1n|j                  d	   dk(  r|n|/}1|1r#|0j                  D ]  }|j=                  |        ||z
  |0_!        |0j=                  |d   d       tE        |0       |0j                  d   dk(  r|D ]  }2tG        | |2|0||d        |#tI        tK        |#            }||k  r|di}#|jL                  rd}d}|0|#||||fS c c}-},w c c},}-}w )a  Fit as much content as possible from an inline box in a width.

    Return ``(new_box, resume_at, preserved_line_break, first_letter, last_letter)``.
    ``resume_at`` is ``None`` if all of the content fits. Otherwise it can be passed as
    a ``skip_stack`` parameter to resume where we left off.

    ``new_box`` is non-empty (unless the box is empty) and as big as possible while
    respecting ``max_x``, if possible (may overflow is no split is possible.)

    This is the recursive step that loops over the children of the box; base steps of
    recursion are handled by ``split_inline_level()``.

    In this phase, excluded floating shapes are ignored.

    g0D   ?NFr   r   r   relativer   r<   r=   r   r   Tr       r   )prer   )TFlangr>   r   )startendr?   box_decoration_breakcloner   rB   )'r   r	   r   r   r   r   r   r   r   r   rF   rU   	enumeraterS   r&   r   r  r   r   r   r   r   r   rW   trailing_collapsible_spacer   rY   rH   r   r  rQ   rR   remove_decorationr$   rV   r   nextiter	is_leader)3r(   r)   rH   rc   r*   r+   r,   r-   r.   rd   rg   rh   r/   r0   is_startinitial_position_xr  left_spacingright_spacingcontent_box_leftrS   r  ri   rj   rk   r   float_resume_indexr   ir   r   is_last_childr^   child_waiting_floatsr  r2   	preservedfirstr;   new_float_widthsend_spacing	can_breakrm   previous_resume_atr]   	box_childin_flow_childrenis_endr   translation_neededabsolute_boxs3                                                      r3   rI   rI     s3   4 
XET!H##cEMM5??;<<<3??*S-B-BB  	C,,,s/E/EE !H !%%L;*L
yy
*(..0zcll4512 k5D>>&&(.uh/->z<	1
 !%*QY".U//11E#cll"3a"77!
O\,nk!#7"$4	6 	G	9i6F 99[!U*&K%f--E'K%g..E[Y-> {*O"UJ 0.+%';]&(8	: KIy)UD:J  #<#79I&9QRV #L$9;KG;T UW
#' 	$KE!KYY}%)::IU++!	-'!	*%'V)<>	 OO,-! L++KKeU]]333 #u}}-$$eY%78 '11I4J4J4LLN 8) LLuT%<-ulDV"K1BN!8-=?Q$	&&"
 & 2I'J##UIu$=>23 OO,-	*IQkT 	()	 yy&3x=1+<-5.)!Y**, . . &a(33
%dd+ 	/EOO!1!11$  H%,,..J	/
 $F$$.677*ui79GL&jA#u}}%##==%."~H 	E&&($$u'9'9';;&&'  	 099+,7!%  IIk2e;  )) 1<01"%55\&1F }}Z J.* 	!Lw\!	!
 T)_%%%+T2I
}} 	0, o. 	8s    W$Wc                    t        |t        j                        sJ |j                  d   }|j                  j                         |d }|dk(  s|syt        |j                         |j                  | ||j                  |      \  }}}	}
}}|	dk7  sJ |dkD  r|j                  |j                        }|
|_
        ||_        ||_        t        |j                        \  }}||z
  dz  }||_        ||_        ||_        |xj                   |j                  z  c_        nd}|	d}nC|||	 j                         }||	k7  xr |j#                  d      }|rd	}||v sJ d
|d       |	|z  }	||	|fS )av  Keep as much text as possible from a TextBox in a limited width.

    Try not to overflow but always have some text in ``new_box``.

    Return ``(new_box, skip, preserved_line_break)``. ``skip`` is the number of
    UTF-8 bytes to skip form the start of the TextBox for the next line, or
    ``None`` if all of the text fits.

    Also break on preserved line breaks.

    r  Nr   )NNFr   r   Fr   )r   	   u    u    zGot z> between two lines. Expected nothing or a preserved line break)r   r	   r   rF   r   r   r    r   justification_spacingcopy_with_textr$   r   r'   r!   rO   rP   rN   strip)r(   r)   r^   r   r   r  r   layoutlengthresume_indexr$   r'   rN   r  r]   r  ri   betweenline_breakss                      r3   r   r     s    c5==)))		+&I88??TU#DA~T <Lsyy'?!!=@9FFL% 1z  -	!
 
 syy)Q#f,1%(&$vl+224|#;s); 	 KKk) K@GJK) 	222r~   c           
         g }t        | |d      \  }}|D cg c]*  }|j                         rdnt        ||d      \  }}|||f, }}}}|rF|D cg c]  \  }}}|j                         s||z
   }}}}|rt        |      }	t        |||	z         }|D ]c  \  }}}|j                         r||j                  z
  }
n1|j                  d   dk(  r||z
  }
n|j                  d   dk(  sJ ||z
  }
t        ||
       e ||fS c c}}}w c c}}}w )zHandle ``vertical-align`` within a :class:`LineBox`.

    Place all boxes vertically assuming that the baseline of ``box``
    is at `y = 0`.

    Return ``(max_y, min_y)``, the maximum and minimum vertical position
    of margin boxes.

    r   )
baseline_yr7   vertical_alignrq   rp   )aligned_subtree_verticalityr   r   r&   rF   translate_subtree)r)   top_bottom_subtreesmax_ymin_ysubtree	sub_max_y	sub_min_ysubtrees_with_min_maxsub_positionshighest_subdys              r3   rM   rM     sd    . Q0LE5 +> >#..0L,,< !Iy 
)Y'> >  2G) )-I%%' 	!) ) m,Ku{23E)> '%I+++B]]+,5"B==!12h>>>"B'2&' %<5>)s   /D Dc                     t        | t        j                        rE| xj                  |z  c_        | j                  d   dv r| j
                  D ]  }t        ||        y y | j                  |       y )NrP  rq   rp   r\  )r   r	   r   r&   rF   rS   rR  rQ   )r)   r\  r   s      r3   rR  rR    sc    #u'"99%&*;; -!%,- <
 	r~   c                     t        | ||      \  }}|| j                  z
  }|| j                         z   }|||k  r|}|||kD  r|}||fS N)inline_box_verticalityrN   r   )r)   rS  rO  rT  rU  rq   rp   s          r3   rQ  rQ    s`    )#/BJOLE5
s||
#C3$$&&F}e}%<r~   c                    d}d}t        | t        j                  t        j                  f      s||fS | j                  D ]  }|j                         s"|j                         r|j                  |       6|j                  d   }|dk(  r|}nc|dk(  rP| j                  d   t        | j                  d      z  }|||j                         z   dz  z
  }	|	|j                  z   }n|dk(  rF|| j                  z
  | j                  z   | j                  z   | j                  z   }	|	|j                  z   }n|d	k(  rd|| j                  z
  | j                  z   | j                  z   | j                  z   | j                  z   }
|
|j                         z
  |j                  z   }nZ|d
v rd}nSt        |t               st#        |      r3t%        | j                        \  }}|t'        || j                  |      z
  }n||z
  }||j                  z
  }	t        j(                  t        j*                  t        j,                  f}t        ||      r |j/                  |	|j0                  z
         n|	|_        |d
v r|j                  |       9|	|j                         z   }
||	|k  r|	}||
|kD  r|
}t        |t        j                        szt3        |||      \  }}|||k  r|}|||kD  s|} ||fS )zHandle ``vertical-align`` within an :class:`InlineBox`.

    Place all boxes vertically assuming that the baseline of ``box``
    is at `y = baseline_y`.

    Return ``(max_y, min_y)``, the maximum and minimum vertical position
    of margin boxes.

    NrP  rN   middler  exr   ztext-topztext-bottomr^  r   r_  )r   r	   r   r   rS   r   r   rY   rF   r   r   rN   rO   r  r  r'   r   r   r!   r   r   r   r   rQ   r&   rb  )r)   rS  rO  rT  rU  r   rP  child_baseline_yone_exrq   rp   r'   r]   	box_typeschildren_max_ychildren_min_ys                   r3   rb  rb  &  s    EEcEMM5??;<e| ?'&&(!#**51%56Z')x'YY{+ocii.NNF)<)<)> >!CCC"U^^3z),s~~=''(*-//:C"U^^3}, 3<</#..@**+-0__=?BzzJF%(;(;(==N00  !0J~4Ncii(IFA)J		6-3  3
  *N: /))5+>+>@S@ST	eY'OOsU%5%55O6"E ..  &&u-u**,,=C%KE=FUNEeU__--C*,<.>*NN)nu.D&)nu.D&?'@ %<r~   c                 R   |j                   |k\  ry|j                  d   }|r|j                  d   }|dk(  r|n|}|j                  d   dv }|dv r|dk(  |j                  d	   d
k(  z  rd}nd}|dk(  ry||j                   z
  }|dk(  r|rt        | ||       y|dk(  r|dz  S |dk(  sJ |S )z>Return the line horizontal offset according to ``text-align``.r   text_align_alltext_align_lastr   r   r   r   r   r<   r=   r$  r%  justifycenterr   )r$   rF   justify_line)r(   r1   r^   r;   align
align_lastspace_collapseoffsets           r3   rL   rL   x  s     zz_$JJ'(EZZ 12
#v-:ZZ. 3( (N!!VO

; 75 @AEEtzz)F	 $/z~~r~   c                 B    t        |      x}rt        | |||z  d       y y )Nr   )count_expandable_spacesadd_word_spacing)r(   r1   extra_width	nb_spacess       r3   rp  rp    s,     -T22	2$i(?C 	3r~   c                 2   t        | t        j                        r7| j                  j	                  d      | j                  j	                  d      z   S t        | t        j
                  t        j                  f      rt        d | j                  D              S y)z;Count expandable spaces (space and nbsp) for justification.r   r!  c              3   2   K   | ]  }t        |        y wra  )rv  .0r   s     r3   	<genexpr>z*count_expandable_spaces.<locals>.<genexpr>  s     Le*51L   r   )	r   r	   r   r   countr   r   sumrS   )r)   s    r3   rv  rv    sd    #u}}%xx~~c"SXX^^H%===	C%--9	:Ls||LLLr~   c                    t        |t        j                        r||_        |xj                  |z  c_        t        |      }|dkD  rdt        |j                  |j                  | d |j                        }|j                          ||z  }||z  }|xj                  |z  c_
        ||_        |S t        |t        j                  t        j                  f      r}|xj                  |z  c_        |}|j                  }|j                  d   dk(  r|d d d   }|D ]!  }	|	j                         st!        | |	||      }# |xj                  ||z
  z  c_
        |S |j#                  |d       |S )Nr   )r   rF  r<   r=   r>   )r   r	   r   rF  rH   rv  r   r   rF   
deactivater$   r   r   r   rS   r   rw  rQ   )
r(   r)   rF  	x_advancery  rI  extra_spaceprevious_x_advancerS   r   s
             r3   rw  rw    sV   #u}}%$9!)#+C0	q="#))W&)&?&?AF /);K$III$I%C  
C%--9	:)#&<<99[!U*"~H 	FE&&(,U$99F		F 			Y!333	  	i#r~   c                 F   | j                   D ]  }t        |t        j                        rdt	        |      s ydD ]Q  }t        |j                  d|    dd       s0|j                  d| d   s|j                  d|    j                  sP  y |j                         s y y)	NFr   r   rv   border__widthpadding_T)	rS   r   r	   r   rJ   r   rF   rv   r   )r\   r   r   s      r3   rJ   rJ     s    !! 
eU__-%e,: !EKK'$(897DIgdV6$:;htf$56<< 	!
 $$&
 r~   c                 J   | j                   d   dv }t        | t        j                        ry|r=t        | t        j                        r#t        | j                  | j                   d         S |r6t        | t        j                        rt        d | j                  D              S y)Nr   )r   zpre-wrapr   Fr#  c              3   2   K   | ]  }t        |        y wra  )r  r|  s     r3   r~  z#can_break_inside.<locals>.<genexpr>  s     Eu#E*Er  )
rF   r   r	   r   r   r   r   r   anyrS   )r)   	text_wraps     r3   r  r    s{    		-(,NNI#u112	z#u}}5chh		&(9::	z#u7EEEEr~   )T)G__doc__r   mathr   cssr   r   r   css.propertiesr   formatting_structurer	   r
   r   r   r   flexr   r   r   r   gridr   leaderr   min_maxr   percentr   r   r   	preferredr   r   r   replacedr   tabler   r   text.line_breakr   r   r   r    r!   r4   r%   rC   rK   rD   r   r   r   r   r   r  r  rV   rI   r   rM   rR  rQ  rb  rL   rp  rv  rw  rJ   r   r~   r3   <module>r     s    $   5 5 & / :  1  ! ) L L X X 0 =M M"BNb&R'&fNb,:0, I IbJRCj1+h
T]@<3~'T
Od"JD> 	r~   