
     hu1                     *   d Z ddlZddlmZmZmZmZmZmZ ddl	m
Z
 ddlmZ dddd	d
ddZ ej                  d      ZdZ ej                  d      Z G d de      Zd Zd Zd"dZd Zd Zd Zd#dZd Zd Zd Zd#dZd Zd Z d Z!d Z"d  Z#d$d!Z$y)%z
Surface helpers.

    N)atan2coshypotradianssintan   )cairo)	parse_urlg
BP(?gL&d2?gqq?gUUUUUU?)mmcminptpcpxz(url\(.+\)) *(.*)achlmqstvzACHLMQSTVZzrect\( ?(.+?) ?\)c                       e Zd ZdZy)
PointErrorz,Exception raised when parsing a point fails.N)__name__
__module____qualname____doc__     l/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/cairosvg/helpers.pyr   r      s    6r   r   c                 &    t        || z
  ||z
        S )z$Get the distance between two points.)r   )x1y1x2y2s       r   distancer!          b"r'""r   c                     | sy| j                         } t        j                  |       }|r=t        |j	                  d            j
                  }|j	                  d      xs d}||fS d}| xs d}||fS )zlExtract from value an uri and a color.

    See http://www.w3.org/TR/SVG/painting.html#SpecifyingPaint

    NNr	      N)strip	PAINT_URLsearchr   groupfragment)valuematchsourcecolors       r   paintr/   #   sx     KKMEU#E5;;q>*33A&$
 E? E?r   c                 P   |rdnd}t        | |j                  dd      |d         }t        | |j                  dd      |d         }|j                  d      }|rIt        j                  d	d
|      }t	        d |j                         D              }|xs |d   }|xs |d   }|||fS )zReturn ``(width, height, viewbox)`` of ``node``.

    If ``reference`` is ``True``, we can rely on surface size to resolve
    percentages.

    xyr   r   widthz100%r   heightr	   viewBox[ 
	,]+ c              3   2   K   | ]  }t        |        y w)N)float).0positions     r   	<genexpr>znode_format.<locals>.<genexpr>E   s     HHhHs   r%      )sizegetresubtuplesplit)surfacenode	referencereference_sizer3   r4   viewboxs          r   node_formatrI   8   s     'TFN$((7F3^A5FGE'488Hf5~a7HIFhhy!G&&W5HHH#%71:&'!!r   c                     | j                  dd      } t        j                  dd|       } t        j                  dd|       } t        j                  dd|       } | j                         S )	z?Normalize a string corresponding to an array of various values.Eez(?<!e)-z -r6   r7   z(\.[0-9-]+)(?=\.)z\1 )replacer@   rA   r&   )strings    r   	normalizerO   K   sU    ^^C%FVVItV,FVVM3/FVV(&&9F<<>r   c                     t        j                  d|      }|rC|j                  dd      \  }}||j                         d }t	        | |d      t	        | |d      |fS t
        )z1Return ``(x, y, trailing_text)`` from ``string``.z(.*?) (.*?)(?: |$)r	   r%   Nxy)r@   r,   r)   endr>   r   )rD   rN   r,   rQ   rR   s        r   pointrT   T   sa    HH)62E{{1a 1		%Wa%tGQ'<fEEr   c                 &    t        ||z
  || z
        S )z;Return angle between x axis and point knowing given center.)r   )cxcyr   pys       r   point_anglerY   _   r"   r   c                    |j                   dk(  r[|xs t        | |j                  dd      d      }|xs t        | |j                  dd      d      }t        | |      \  }}}|dd \  }}n\|j                   d	v r5t        | |      \  }}	}|xs |}|xs |	}|j                  |j
                  }}nt        d
|j                    d      d}
d}|dkD  r||z  nd}|dkD  r||z  nd}|j                  dd      j                         }|d   }|dk(  rd}d}n]t        |      dkD  r|d   nd}|dk(  rt        ||      }nt        ||      }|x}}|dd j                         }|dd j                         }|j                   dk(  r=t        | |j                  dd      d       }
t        | |j                  dd      d       }nFd}
|dk(  r||z  |z
  dz  }
n|dk(  r||z  |z
  }
d}|dk(  r|||z  |z
  dz  z  }n|dk(  r|||z  |z
  z  }|||
|fS )zManage the ratio preservation.markermarkerWidth3rQ   markerHeightrR   r%   N)svgimagegzRoot node is z,. Should be one of marker, svg, image, or g.r   r	   preserveAspectRatioxMidYMidnoneminslice      refX0refYmidmax)tagr>   r?   rI   image_widthimage_height	TypeErrorrC   lenrm   re   lower)rD   rE   r3   r4   _rH   viewbox_widthviewbox_height
node_widthnode_heighttranslate_xtranslate_yscale_xscale_yaspect_ratioalign
x_position
y_positionmeet_or_slicescale_values                       r   preserve_ratior   d   so   xx8Igtxxs'CSIL4.#)FL#GT21g(/%~	*	*%0$%?"
K#&;(,(8(8$:K:K~DHH: &( (
 	

 KK'4q'8em#aG)7!);f~%G881:>DDFLOE

+.|+<q+@QdG#gw/Kgw/K'''1QZ%%'
12Y__&
xx8GTXXfc%:C@@GTXXfc%:C@@ 7?]:a?K5 '/M9KFW,~=BBK5 6G+n<<KG[+55r   c                    t        | |j                  dd      d      }t        | |j                  dd      d      }t        | |      \  }}}|dd \  }}	|j                  dd	      j                  d
      d   }
|
dk(  rdn|
dd j	                         }|
dk(  rdn|
dd j	                         }|d   }|dk(  r||||z  z
  dz  z  }n|dk(  r||||z  z
  z  }|d   }|dk(  r||	||z  z
  dz  z  }n|dk(  r||	||z  z
  z  }||||z  ||z  fS )z9Get the clip ``(x, y, width, height)`` of the marker box.r\   r]   rQ   r^   rR   r%   Nrb   rc   r7   r   rd   re   r	   rg   rh   rl   g       @rm   )r>   r?   rI   rC   rs   )rD   rE   r{   r|   r3   r4   rt   rH   ru   rv   r~   r   r   clip_xclip_ys                  r   clip_marker_boxr      sO   $((=#6<E'488NC8#>F.MAq'$+ABK!M>HH*J7==cB1EE6/uQqz/?/?/AJ6/uQRy/@JQZFU=57?2b88	u	-%'/11QZFU>FW$44::	u	.6G#333657?FW,<<<r   c                     |dz  dz  | dz  z   }|dz  dz  |dz  z   }|dz  dz  |dz  z   }|dz  dz  |dz  z   }	||||	||fS )z7Return the quadratic points to create quadratic curves.r%   r=   r   )
r   r   r   r    x3y3xq1yq1xq2yq2s
             r   quadratic_pointsr      sm    
q&1*rAv
C
q&1*rAv
C
q&1*rAv
C
q&1*rAv
CS#r2%%r   c                 v    | t        |      z  |t        |      z  z
  |t        |      z  | t        |      z  z   fS )z3Rotate a point of an angle around the origin point.)r   r   )rQ   rR   angles      r   rotater      s7    s5z>AE
N*AE
NQU^,KKKr   c           	      8   |syt        j                  dt        |            }t        j                         }|r|j                  d      }|d   }t        |      dk(  r&|dv r|}| j                  dz  }n1| j                  dz  }n!t        |      dkD  r|dv r|}|d   }n|d   }ny|dk(  r| j                  dz  }n'|d	k(  rd}n|d
k(  r| j                  }nt        | |d      }|dk(  r| j                  dz  }n'|dk(  rd}n|dk(  r| j                  }nt        | |d      }|j                  t        |      t        |             |D ]  \  }	}
|
j                  d      D cg c]  }t        | |       }}|	dk(  r"t        j                  | j                  |      }V|	dk(  rdt        t        |j                  d                  }|xs d\  }}|j                  ||       |j                  |       |j                  | |        |	dk(  rKt!        t        t        |d                     }t        j                  dd|ddd      j                  |      }|	dk(  rKt!        t        t        |d                     }t        j                  d|dddd      j                  |      }_|	dk(  r't        |      dk(  r|dz  } |j                  |dd   |	dk(  st        |      dk(  rd|z  } |j"                  |dd    |r&|j                  t               t                      	 |j%                          |r1|j'                         }|j)                  |j                  |             y|j%                          | j*                  j-                  |       yc c}w # t        j.                  $ rl | j*                  j1                         }| j*                  j3                          | j*                  j5                          | j*                  j7                  |       Y yw xY w)zTransform ``surface`` or ``gradient`` if supplied using ``string``.

    See http://www.w3.org/TR/SVG/coords.html#TransformAttribute

    Nz(\w+) ?\( ?(.*?) ?\)r7   r   r	   )topbottomr%   centerleftrightrQ   r   r   rR   matrixr   r2   skewXskewY	translate)r   scale)r@   findallrO   r
   MatrixrC   rr   r3   r4   r>   r   r9   multiplyr   popr   r   r   invert
get_matrix
set_matrixcontext	transformError	copy_pathnew_pathclipappend_path)rD   transform_stringgradienttransform_origintransformationsr   originorigin_xorigin_ytransformation_typetransformationr+   valuesr   rQ   rR   tangent
matrix_nowactive_paths                      r   r   r      s    jj+;!<>O\\^F!'',!9v;!,,#"==1,">>A-[1_,,#!!9!!9x}}q(HH }}HGXs3Hx~~)HH!~~HGXs3Hx%/:/> &+^4B4H4H4MN5$w&NN(*\\6*33F;F H,E&**Q-01E#VDAqQ"MM% aR!$ G+'%q	"234G\\!QAq9BB6JF G+'%q	"234G\\!WaAq9BB6JF K/6{a$FfRaj) G+6{aVFLL&!*%/&2 %/)E(O+;<.  ",,.J
 3 3F ;<MMOOO%%f-W O: ;; 1oo//1  "##K01s   N&N A<PPc                     t         j                  t        | xs d            }|r |j                  d      j	                  d      S g S )zParse the rect value of a clip. r	   r7   )RECTr(   rO   r)   rC   )rN   r,   s     r   	clip_rectr   '  s9    KK	&,B/0E(-5;;q>$525r   c                     d| v rCt        | d         j                         j                  d      D cg c]  }t        |       }}|S g S c c}w )z=Retrieves the original rotations of a `text` or `tspan` node.r   r7   )rO   r&   rC   r9   )rE   ioriginal_rotates      r   	rotationsr   -  sY    4'X7==?EEcJLE!HL LILs   A
c                 |    dj                  fdt        t        | j                              D              | d<   y)z6Removes the rotations of a node that are already used.r7   c              3   `   K   | ]%  }t        rj                  d       nd          ' yw)r   N)strr   )r:   r   r   r   s     r   r<   zpop_rotation.<locals>.<genexpr>8  s/      ( 	VFJJqM)<=(s   +.r   N)joinrangerr   text)rE   r   r   s    ``r   pop_rotationr   6  s/    XX (s499~&( (DNr   c                 *      fd|D        S )aI  Returns a list with the current letter's positions (x, y and rotation).

    E.g.: for letter 'L' with positions x = 10, y = 20 and rotation = 30:
    >>> [[10, 20, 30], 'L']

    Store the last value of each position and pop the first one in order to
    avoid setting an x,y or rotation value that have already been used.

    c              3   |   K   | ].  }fD cg c]  }|r|j                  d       nd c}|f 0 yc c}w w)r   N)r   )r:   charpldxldylrlxlyls      r   r<   zzip_letters.<locals>.<genexpr>G  sF       022sC.D	Er"&&)t
#	EtL	Es   <7<r   )r   r   r   r   r   words   ````` r   zip_lettersr   =  s     r   c                     | j                   xs dg}t        |       D ]L  }|j                  t        |             |j                  |j                  xs d       | j                  |       N dj                  |      S )z,Flatten the text of a node and its children.r   )r   listappendflattentailremover   )rE   flattened_textchilds      r   r   r   L  sk    iio2&Nd gen-ejj.B/E 77>""r   c                 J   |sy	 t        |      S # t        $ r Y nw xY w| yt        |      j                  dd      d   }|j	                  d      rh|dk(  r| j
                  xs d}n>|dk(  r| j                  xs d}n(|dk(  r#t        | j
                  | j                        d	z  }t        |dd
       |z  dz  S |j	                  d      r| j                  t        |dd       z  S |j	                  d      r| j                  t        |dd       z  dz  S |j	                  d      r| j                  t        |dd       z  dz  S t        j                         D ]J  \  }}|j	                  |      st        |dt        |              }||r| j                  |z  z  c S dz  c S  y)a`  Replace a ``string`` with units by a float value.

    If ``reference`` is a float, it is used as reference for percentages. If it
    is ``'x'``, we use the viewport width as reference. If it is ``'y'``, we
    use the viewport height as reference. If it is ``'xy'``, we use
    ``hypot(viewport_width, viewport_height) / 2 ** .5`` as reference.

    r   Nr7   r	   %rQ   rR   r1   g;f?r   d   emexr%   ch)r9   
ValueErrorrO   rC   endswithcontext_widthcontext_heightr   	font_sizeUNITSitemsrr   dpi)rD   rN   rF   unitcoefficientnumbers         r   r>   r>   V  s    V} 
 v$$S!,Q/Fs--2I#..3!I$g++W-C-CDwN  VCR[!I-33		  5#555		  5#5599		  5#5599"[[] Nk??4 6+CI:./F+W[[;6MM1MMN s   
 	)Tr$   )r1   )%r   r@   mathr   r   r   r   r   r   rD   r
   urlr   r   compiler'   PATH_LETTERSr   	Exceptionr   r!   r/   rI   rO   rT   rY   r   r   r   r   r   r   r   r   r   r   r>   r   r   r   <module>r      s   
 
 5 5   




	 BJJ+,	%rzz&'7 7#
*"&#
66r=4&L
_.D6(#0r   