
     hG?                        d Z ddlZddlZddlmZ ddlmZ ddl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mZ  ed      Z ed      Zd Zd Zd Zd Zd Z G d de      Z  G d de       Z!eeeeeeeeeeeeeedZ"y)z
SVG Parser.

    N)
urlunparse)Element)ElementTree   )css)match_features)flattenpop_rotation	rotations)fetch	parse_urlread_url
safe_fetch)clip	clip-pathdisplayfilterheightidmaskopacityoverflowrotate
stop-colorzstop-opacitystyle	transformztransform-originviewBoxwidthxydxdy"{http://www.w3.org/1999/xlink}hrefhref)fillzflood-colorzlighting-colorr   strokec                     | sy|rt        j                  dd|       S t        j                  dd|       } t        j                  dd|       } t        j                  dd|       S )z_Handle white spaces in text nodes.

    See http://www.w3.org/TR/SVG/text.html#WhiteSpace

     z[
	] z[
]	z +)resub)stringpreserves     k/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/cairosvg/parser.pyhandle_white_spacesr0   8   sW     vvj#v.."f-c6*vvdC((    c                     | j                         j                         } |j                         }| t        v rt        |    |      }| |fS |j                         }| |fS )a  Normalize style declaration consisting of name/value pair.

    Names are always case insensitive, make all lowercase.
    Values are case insensitive in most cases. Adapt for 'specials':
        id - case sensitive identifier
        class - case sensitive identifier(s)
        font-family - case sensitive name(s)
        font - shorthand in which font-family is case sensitive
        any declaration with url in value - url is case sensitive

    )striplowerCASE_SENSITIVE_STYLE_METHODS)namevalues     r/   normalize_style_declarationr8   H   s^     ::<DKKME++,T259 ; ;r1   c                     | S )zNo-operation for normalization where value is case sensitive.

    This is actually the exception to the rule. Normally value will be made
    lowercase (see normalize_style_declaration above).

     r7   s    r/    normalize_noop_style_declarationr<   ^   s	     Lr1   c                 x   t        j                  dt         j                  t         j                  z        }|j	                  |       D ]p  }|j                         dkD  r| d|j                          nd}|j                  d      j                         }| |j                         t        |      z   d }||z   |z   } r | S )zfNormalize style declaration, but keep URL's as-is.

    Lowercase everything except for the URL.

    a  
        (.*?)                               # non-URL part (will be normalized)
        (?:
            url\(\s*                        # url(<whitespace>
                (?:
                      "(?:\\.|[^"])*"       # "<url>"
                    | \'(?:\\.|[^\'])*\'    # '<url>'
                    | (?:\\.|[^\)])*        # <url>
                )
            \s*\)                           # <whitespace>)
            |$
        )
    r   Nr(   r   )	r+   compile
IGNORECASEVERBOSEfinditerstartgroupr4   len)r7   regex_stylematchvalue_startnormalized_value	value_ends         r/   normalize_url_style_declarationrJ   h   s     **  
	#%K %%e, ;/4{{}q/@eNU[[]+b ;;q>//1%++-#.>*??@A	..:	;
 Lr1   c                 R    t        j                  dd | dt         j                        S )a_  Make first part of font style declaration lowercase (case insensitive).

    Lowercase first part of declaration. Only the font name is case sensitive.
    The font name is at the end of the declaration and can be 'recognized'
    by being preceded by a size or line height. There can actually be multiple
    names. So the first part is 'calculated' by selecting everything up to and
    including the last valid token followed by a size or line height (both
    starting with a number). A valid token is either a size/length or an
    identifier.

    See http://www.w3.org/TR/css-fonts-3/#font-prop

    a  
        ^(
            (\d[^\s,]*|\w[^\s,]*)   # <size>, <length> or <identifier>
            (\s+|\s*,\s*)           # <whitespace> and/or comma
        )*                          # Repeat until last
        \d[^\s,]*                   # <size> or <line-height>
    c                 >    | j                         j                         S N)rC   r4   )rF   s    r/   <lambda>z2normalize_font_style_declaration.<locals>.<lambda>   s    u{{}**, r1   r   )r+   r,   r@   r;   s    r/    normalize_font_style_declarationrO      s+     66  
-eQ

D Dr1   c                   <     e Zd ZdZ	 	 d fd	Zd ZddZd Z xZS )Nodez0SVG node with dict-like properties and children.c           
         t         |           d| _        d| _        |j                  }|| _        || _        |j                  dv r|j                  nd|j                   d|j                   | _	        |j                  | _
        || _        || _        t        | d|      | _        |I| j                  |D 	cg c]  }	|	t         vr|	||	   f c}	       |xs |j"                  | _        || _        n$t        | dd      | _        t        | d	d      | _        | j                  | j                  j&                         |j)                  d
      }
|
rt+        j,                  |
      \  }}ng }g }|\  }}|j/                  |      D cg c]  }|d   	 }}|j/                  |      D cg c]  }|d   	 }}||g||gfD ]&  }|D ]  }|D ]  \  }}|j1                         | |<    ! ( t2        D ],  }	| j)                  |	      dk(  s| j)                  dd      | |	<   . | D 	cg c]  }	| |	   dk(  r|	 c}	D ]   }	||	|v r|j)                  |	      | |	<   | |	= " | j                  dv r| j5                  |dd      \  | _        }|rL|j                  D cg c]0  }t7        |j
                  || j                  | | j                        2 c}| _        y| j                  sg | _        |j9                         D ]f  }t;        |j                        s| j                  j=                  t7        ||| j                  | | j                               | j                  dk(  sf y yyc c}	w c c}w c c}w c c}	w c c}w )z@Create the Node from ElementTree ``node``, with ``parent`` Node.r:   F)r(   zhttp://www.w3.org/2000/svg{}xml_treeNurlparentr   currentColorcolorblackinherit)texttextPathaT)trailing_space	text_rootrW   unsafeswitch)super__init__childrenrootetree_elementelementr   namespace_url
local_nametagr]   url_fetcherrc   getattrrU   updateNOT_INHERITED_ATTRIBUTESrV   rW   attribgetr   parse_declarationsrF   r3   COLOR_ATTRIBUTEStext_childrenrQ   iter_childrenr   append)selfrj   r   rn   rW   parent_childrenrV   rc   node	attribute
style_attrnormal_attrimportant_attrnormal_matcherimportant_matcherrulenormal	importantdeclaration_listsdeclarationsr6   r7   _child	__class__s                           r/   rf   zNode.__init__   s    		$$
 $$(JJ &&'r'*<*<)=> 	 II	&  j$7 KK@F>3<$<< F9-.> ? (fjjDH DKtUD1DH!$$7DKDMM(() XXg&
*-*@*@*L'KKN,1))'5';';G'DEt$r(EE*;*A*A'*JK$T"XK	Ky>2B"D 	/ 1 /#/ /KD%!&DJ//	/ * 	=Ixx	"n4"&((7G"<Y	= ,01'	?i/ 1 	$I !i6&9"(**Y"7YO	$ 8800#11  2  >DM1  $__.  U]]E4+;+;D KK).DM DM ..0 !%"5"56MM((UE4+;+;D$(KK12 xx8+ g>& FK1.s   5M M!M M%
5M*c                 0    t        || j                  |      S rM   )r   rn   )ry   rV   resource_types      r/   	fetch_urlzNode.fetch_url   s    T--}==r1   c           	      2   g }d}| j                  |      dk(  }t        |j                  j                  |      | _        |r"|s | j                  j	                  d      | _        t        |       }t        |      }|rt        | ||       | j                  r| j                  j                  d      }|j                         D ]  }	|	j                  }
|
j                  dv r|
j                  d|
j                  d            }t        |      j                         }t        || j                  | | j                        }|j!                          |j#                  |        t%        |	| j&                  | j                  |d| j                  	      }d
|_
        |j(                  }
t+        |
      |
_        t,        j.                  j1                  |
      }	n.t%        |	| j&                  | j                  | | j                        }|j                  |      dk(  }t        |
j                  |      |_        |j3                  |	|      \  |_        }|j                  j                  d      }|rd|vrt        |||       |j7                  |       |
j8                  st;        d      }t%        t,        j.                  j1                  |      | j&                  | j                  | | j                        }t        |
j8                  |      |_        |rt        |||       |r"|s |j                  j	                  d      |_        |j                  r|j                  j                  d      }|j7                  |        |r$|s"|s | j                  j=                  d      | _        ||fS )z Create children and return them.z+{http://www.w3.org/XML/1998/namespace}spacer.   r)   )z {http://www.w3.org/2000/svg}treftrefr#   r$   )rV   rn   rW   rc   T)rW   rz   rc   tspanrb   r   z!{http://www.w3.org/2000/svg}tspan)rs   r0   ri   r]   lstripr   listr
   endswithrw   rm   r   geturlTreern   rc   clearrp   rQ   r   rU   r	   
cssselect2ElementWrapperfrom_xml_rootrv   rg   rx   tailr   rstrip)ry   rj   r`   ra   rg   spacer.   original_rotater   child_elementr   r$   rV   
child_tree
child_nodechild_preserveanonymous_etree	anonymouss                     r/   rv   zNode.text_children   s   =88E?j0'(=(=(B(BHM	(		((-DI#D/o&799!YY//4N$224 .	+M!//EyyHHyy8%))F:KMo,,.!)9)9$;;(
   "!!$'!!4::t/?/?%t;;(
 ")
 #++$U^
 * 9 9 G G N!!4::t/?/?;;(
 (^^E2j@N1%**nMJO2<2J2J~3//J'__55c:N8:#=Z&AOOJ'zz")*M"N --;;OLJJ 0 0;;(	 "5UZZ!J	" OVD!(%.^^%:%:3%?IN>>%.^^%<%<S%AN	*].	+` Xh		((-DI''r1   c                 D    | j                  d| j                  d            S )Nr#   r$   )rs   )ry   s    r/   get_hrefzNode.get_href8  s    xx<dhhv>NOOr1   )NFNF)F)	__name__
__module____qualname____doc__rf   r   rv   r   __classcell__r   s   @r/   rQ   rQ      s%    :;?9>Sj>A(FPr1   rQ   c                   ,     e Zd ZdZ fdZ fdZ xZS )r   z	SVG tree.c                 "   |j                  d      }|r|j                  d      rt        |d         }|j                  }|j                  d      }|j                  d      }t        |d d       rt	        |d d dz         }n|r|j
                  }nd }|rm||f|v rg|||f   }t        |j                  |j                  |j                  ||      }	|j                  |	_
        ||	_        |j                  |	_        d|	_        |	S t        
| 9  |       S )	N
tree_cacherV   rW   rc   rX   r(   )rc   T)rs   r   fragmentanyr   rV   rQ   rj   r   rn   rU   rm   rh   re   __new__)clskwargsr   
parsed_url
element_idrW   rc   rV   cached_treenew_treer   s             r/   r   zTree.__new__>  s   ZZ-
&**U+"6%=1J#,,JZZ)FZZ)F:cr?# CR5!89jjZ(J6(#z):;''):):++VFD %0$8$8!"* $ws##r1   c           	         |j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }d}	|j                  d	t              | _        ||| _        n|9|j	                         }t        |d
d      | _        | j                  dk(  rd| _        n|x|r|j                  nd}
t        ||
      }|j                  r#t        |dd dz         | _        |j                  }	n|j                         | _        d}	| j                  xs d| _        nt        d      t        |t              r|j                         }|xr | j                  |j                  k(  xs$ |xr  |j                  d      xr | j                   }|r4|}|j                  |j                  }|j                  |j                   }ng|s%| j#                  t        | j                        d      }|j                  d      rt%        j&                  |      }t)        j*                  || |       }d	|vr|st,        | _        || _        t.        j0                  j3                  |      }|r|j4                  nt7        j8                  | |      }|	rH|j;                         D ]&  }|j<                  |	k(  s|}|j>                  | _         n t        d|	 d      t@        |   ||| j                  ||| j                  |       d| _"        |.| j                  r!| || j                  | j                  d      f<   yyy)z"Create the Tree from SVG ``text``.
bytestringfile_objrV   rc   rW   rz   r   Nrn   r6   z<stdin>rX   r   z1No input. Use one of bytestring, file_obj or url.#zimage/svg+xmls   )forbid_entitiesforbid_externalzNo tag with id="z" found.Tr   )#rs   r   rn   rV   readro   r   r   r   r   	TypeError
isinstancestrencode
startswithrW   rU   r   gzip
decompressr   
fromstringr   r   r   r   r   r   parse_stylesheetsiter_subtreer   ri   re   rf   rh   )ry   r   r   r   rV   rc   rW   rz   r   r   
parent_urlr   self_is_parentroot_parenttreerh   r   rj   r   s                     r/   rf   zTree.__init__W  s$   ZZ-
::j)jjH%H% **%67ZZ-

!::mU;!DH!!Jx6DHxx9$_'-4J"3
3J""%j"o&=>'00
%,,.!
xx'4DHCE Ej#&#**,J.FJJ. ;9S^^C(9\ 	  K$$0)00 $$0''D!^^dhh':
$$[1!__Z8
))J$*
,D
 &v)D((66t< &C,A,A$,L,,. =::+"D$+$9$9DM	=  &zl(;= =%))6?DHH	 	!dhh59J$((4.12 '/!r1   )r   r   r   r   r   rf   r   r   s   @r/   r   r   <  s    $2J: J:r1   r   )r   classzfont-familyfontr   zcolor-profilecursorr%   r   zmarker-startz
marker-midz
marker-endr   r&   )#r   r   r+   urllib.parser   xml.etree.ElementTreer   r   
defusedxmlr   r(   r   featuresr   helpersr	   r
   r   rV   r   r   r   r   	frozensetrq   ru   r0   r8   r<   rJ   rO   dictrQ   r   r5   r:   r1   r/   <module>r      s   
  	 # )  "  $ 5 5 7 7 % &  4    ) ,6D._P4 _PDe:4 e:R +-3,04-+-311+-  r1   