
    lj\                    J   U d 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Zddl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 d	dl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& ddl'm(Z(m)Z) ddgZ*i Z+de,d<   d!dZ- G d de)e&          Z. G d  de)e&          Z/dS )"z$Mobjects generated from an SVG file.    )annotationsN)Path)Any)ElementTree)configlogger)
ManimColorParsableManimColor   )RIGHT)$get_quadratic_approximation_of_cubic)get_full_vector_image_path)hash_obj   )Circle)Line)Polygon	RectangleRoundedRectangle)ConvertToOpenGL)VGroupVMobject
SVGMobjectVMobjectFromSVGPathzdict[int, SVGMobject]SVG_HASH_TO_MOB_MAPxfloatyreturn
np.ndarrayc                0    t          j        | |dg          S )N        )nparray)r   r   s     X/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/mobject/svg/svg_mobject.py_convert_point_to_3dr&       s    8Q3K       c                  R    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRdS fdZdTdZedUd            ZdVd ZdWd"Z	dXd%Z
dYd'ZdZd+Zd[d/Zed\d4            Zed]d6            Zd^d:Zed_d>            Zed`dB            ZedadF            ZedbdJ            ZdcdMZedddP            ZdVdQZ xZS )er   a\
  A vectorized mobject created from importing an SVG file.

    Parameters
    ----------
    file_name
        The path to the SVG file.
    should_center
        Whether or not the mobject should be centered after
        being imported.
    height
        The target height of the mobject, set to 2 Manim units by default.
        If the height and width are both set to ``None``, the mobject
        is imported without being scaled.
    width
        The target width of the mobject, set to ``None`` by default. If
        the height and the width are both set to ``None``, the mobject
        is imported without being scaled.
    color
        The color (both fill and stroke color) of the mobject. If
        ``None`` (the default), the colors set in the SVG file
        are used.
    opacity
        The opacity (both fill and stroke opacity) of the mobject.
        If ``None`` (the default), the opacity set in the SVG file
        is used.
    fill_color
        The fill color of the mobject. If ``None`` (the default),
        the fill colors set in the SVG file are used.
    fill_opacity
        The fill opacity of the mobject. If ``None`` (the default),
        the fill opacities set in the SVG file are used.
    stroke_color
        The stroke color of the mobject. If ``None`` (the default),
        the stroke colors set in the SVG file are used.
    stroke_opacity
        The stroke opacity of the mobject. If ``None`` (the default),
        the stroke opacities set in the SVG file are used.
    stroke_width
        The stroke width of the mobject. If ``None`` (the default),
        the stroke width values set in the SVG file are used.
    svg_default
        A dictionary in which fallback values for unspecified
        properties of elements in the SVG file are defined. If
        ``None`` (the default), ``color``, ``opacity``, ``fill_color``
        ``fill_opacity``, ``stroke_color``, and ``stroke_opacity``
        are set to ``None``, and ``stroke_width`` is set to 0.
    path_string_config
        A dictionary with keyword arguments passed to
        :class:`.VMobjectFromSVGPath` used for importing path elements.
        If ``None`` (the default), no additional arguments are passed.
    use_svg_cache
        If True (default), the svg inputs (e.g. file_name, settings)
        will be used as a key and a copy of the created mobject will
        be saved using that key to be quickly retrieved if the same
        inputs need be processed later. For large SVGs which are used
        only once, this can be omitted to improve performance.
    kwargs
        Further arguments passed to the parent class.
    NTr   	file_namestr | os.PathLike | Noneshould_centerboolheightfloat | NonewidthcolorParsableManimColor | Noneopacity
fill_colorfill_opacitystroke_colorstroke_opacitystroke_widthsvg_defaultdict | Nonepath_string_configuse_svg_cachekwargsr   c                    t                      j        dd d d d| |t          |          nd | _        || _        || _        || _        t          |          | _        || _	        || _
        || _        |	| _        |
| _        || _        i | _        | j        d| _        |
d d d d dd d d}|| _        |i }|| _        |                     |           |                     |||	|
|           |                                  d S )N)r0   r5   r3   r   )r0   r2   r3   r4   r7   r5   r6   )r;   )r3   r4   r5   r6   r7    )super__init__r   r)   r+   
svg_height	svg_widthr	   r0   r2   r3   r4   r5   r6   r7   id_to_vgroup_dictr8   r:   init_svg_mobject	set_stylemove_into_position)selfr)   r+   r-   r/   r0   r2   r3   r4   r5   r6   r7   r8   r:   r;   r<   	__class__s                   r%   r@   zSVGMobject.__init__a   sO   $ 	Rt$4RR6RRR -6,Ait* &&
$((,(46$ !D" $ ! $"& K '%!#"4M:::!%%)% 	 	
 	
 	
 	!!!!!r'   r   Nonec                   |rTt          | j                  }|t          v r7t          |                                         } | j        |  |j        | _        dS |                                  |r|                                 t          |<   dS dS )zChecks whether the SVG has already been imported and
        generates it if not.

        See also
        --------
        :meth:`.SVGMobject.generate_mobject`
        N)r   	hash_seedr   copyaddrC   generate_mobject)rG   r;   hash_valmobs       r%   rD   zSVGMobject.init_svg_mobject   s      	//H...)(388::#),)>& 	8,0IIKK)))	8 	8r'   tuplec                V    | j         j        | j        | j        | j        t
          j        fS )zA unique hash representing the result of the generated
        mobject points.

        Used as keys in the ``SVG_HASH_TO_MOB_MAP`` caching dictionary.
        )rH   __name__r8   r:   r)   r   rendererrG   s    r%   rK   zSVGMobject.hash_seed   s,     N##NO
 	
r'   c                   |                                  }t          j        |          }|                     |          }|                    |j         d|j                   }|                    |           t          j	                            |          }|
                                 |                     |          \  }} | j        |  || _        |                     t                     dS )z8Parse the SVG and translate its elements to submobjects._N)get_file_pathETparsemodify_xml_tree	with_namestemsuffixwriteseSVGunlinkget_mobjects_fromrM   rC   flipr   )rG   	file_pathelement_treenew_treemodified_file_pathsvgmobjectsmobject_dicts           r%   rN   zSVGMobject.generate_mobject   s    &&((	x	**''55&00IN1W1WYEU1W1WXX)***fll-..!!###!%!7!7!<!<,(!-		%r'   r   c                V    | j         t          d          t          | j                   S )z=Search for an existing file based on the specified file name.Nz Must specify file for SVGMobject)r)   
ValueErrorr   rU   s    r%   rX   zSVGMobject.get_file_path   s)    >!?@@@)$.999r'   rf   ET.ElementTreec                v   |                                  }d|                                }fd|j                                        D             }t	          j        di           }t	          j        |d|          }t	          j        |d|          }|                    |           t	          j        |          S )zModifies the SVG element tree to include default
        style information.

        Parameters
        ----------
        element_tree
            The parsed element tree from the SVG file.
        )fillfill-opacitystrokestroke-opacitystroke-widthstylec                $    i | ]\  }}|v 	||S r>   r>   ).0kv
style_keyss      r%   
<dictcomp>z.SVGMobject.modify_xml_tree.<locals>.<dictcomp>   s$    SSSDAq1
??1a???r'   ri   g)	generate_config_style_dictgetrootattribitemsrY   Element
SubElementextendr   )	rG   rf   config_style_dictrootroot_style_dictnew_rootconfig_style_noderoot_style_noderz   s	           @r%   r[   zSVGMobject.modify_xml_tree   s     !;;==

 ##%%SSSSDK,=,=,?,?SSS:eR((M(C9JKK-(93PPt$$$~h'''r'   dict[str, str]c                    dddddd}| j         }i }|                                D ]+\  }}|D ]#}||         t          ||                   ||<   $,|S )z<Generate a dictionary holding the default style information.)r0   r3   )r2   r4   )r0   r5   )r2   r6   )r7   )rp   rq   rr   rs   rt   )r8   r   str)rG   keys_converting_dictsvg_default_dictresultsvg_keyrz   	style_keys          r%   r}   z%SVGMobject.generate_config_style_dict   s     ,7/;- 
  
  +#7#=#=#?#? 	C 	CGZ' C C	#I.6"%&6y&A"B"BwC r'   ri   se.SVG(tuple[list[VMobject], dict[str, VGroup]]c                b   g }g }|                     |df           d}dg}dg}dt                      i}t          |          dk    r|                                \  }|d         }	 t	          |j        d                   }	n# t          $ r d| }	|dz  }Y nw xY wt                      }
|                     |	           |                     |	           |
||	<   t          |t          j	        t          j
        f          r*|                    fd|ddd         D                        	 t          |t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        f          rT|                     |          }|=|                     |           |dd         D ]}||                             |           n.# t          $ r!}t-          j        d	|            Y d}~nd}~ww xY wt          |          dk    ||fS )
zConvert the elements of the SVG to a list of mobjects.

        Parameters
        ----------
        svg
            The parsed SVG file.
           r   r   idnumbered_group_c              3  &   K   | ]}|d z   fV  dS )r   Nr>   )rw   
subelementdepths     r%   	<genexpr>z/SVGMobject.get_mobjects_from.<locals>.<genexpr>(  s,      UUj%!)4UUUUUUr'   Nz4Exception occurred in 'get_mobjects_from'. Details: )appendr   lenpopr   values	Exception
isinstancer`   GroupUser   r   
SimpleLineRectr   Ellipser   PolylineTextget_mob_from_shape_elementrM   r   error)rG   ri   r   stackgroup_id_numbervgroup_stackvgroup_namesvgroupselement
group_namevgrP   parent_nameer   s                 @r%   rc   zSVGMobject.get_mobjects_from  sd    "$13c1X#)(#)(&,fhh%7%jj1nn"YY[[NGU'5	2L% !566

 % % %@@@
1$% B
+++
+++"$GJ'BHbf#566 VUUUUwttQSt}UUUUUUY	

	  : 99'BBCc***+7+< : :K#K044S9999 Y Y YWTUWWXXXXXXXXYI %jj1nnN ws+   +B BB.B<G+ +
H5HHshapese.SVGElementVMobject | Nonec                   t          |t          j                  r|                     |          }nSt          |t          j                  r|                     |          }n"t          |t          j                  r|                     |          }nt          |t          j        t          j	        f          r| 
                    |          }nt          |t          j                  r|                     |          }nt          |t          j                  r|                     |          }nVt          |t          j                  r|                     |          }n&t#          j        dt'          |                      d }||                                s|S |                     ||           t          |t          j                  r"|j        r|                     ||j                   |S )NUnsupported element type: )r   r`   r   path_to_mobjectr   line_to_mobjectr   rect_to_mobjectr   r   ellipse_to_mobjectr   polygon_to_mobjectr   polyline_to_mobjectr   text_to_mobjectr   warningtype
has_pointsapply_style_to_mobjectTransformableapplyhandle_transform	transform)rG   r   rP   s      r%   r   z%SVGMobject.get_mob_from_shape_elementB  s   eRW%% 	#'#7#7#>#>CCr}-- 	&&u--CCrw'' 	&&u--CC	2:677 
	))%00CCrz** 	))%00CCr{++ 	**511CCrw'' 	&&u--CCNEUEEFFFC;cnn..;J##C///eR-.. 	85; 	8!!#u777
r'   rP   r   matrix	se.Matrixc                    t          j        |j        |j        g|j        |j        gg          }t          j        |j        |j        dg          }|                     |           | 	                    |           | S )zApply SVG transformations to the converted mobject.

        Parameters
        ----------
        mob
            The converted mobject.
        matrix
            The transformation matrix determined from the SVG
            transformation.
        r"   )
r#   r$   acbdr   fapply_matrixshift)rP   r   matvecs       r%   r   zSVGMobject.handle_transform[  sl     h68,vx.BCDDh&(C011		#
r'   se.GraphicObjectc                    |                      |j        |j        j        |j        j        |j        j        |j        j                   | S )zApply SVG style information to the converted mobject.

        Parameters
        ----------
        mob
            The converted mobject.
        shape
            The parsed SVG element.
        )r7   r5   r6   r3   r4   )rE   r7   rr   hexrgbr2   rp   )rP   r   s     r%   r   z!SVGMobject.apply_style_to_mobjectm  sL     	+, </z(+ 	 	
 	
 	
 
r'   pathse.Pathr   c                &    t          |fi | j        S )zConvert a path element to a vectorized mobject.

        Parameters
        ----------
        path
            The parsed SVG path.
        )r   r:   )rG   r   s     r%   r   zSVGMobject.path_to_mobject  s     #4CC4+BCCCr'   linese.Liner   c                    t          t          | j        | j                  t          | j        | j                            S )zConvert a line element to a vectorized mobject.

        Parameters
        ----------
        line
            The parsed SVG line.
        )startend)r   r&   x1y1x2y2)r   s    r%   r   zSVGMobject.line_to_mobject  s=     &tw88$TWdg66
 
 
 	
r'   rectse.Rectr   c                   | j         dk    s| j        dk    rt          | j        | j                  }nKt          | j        | j        | j         z  | j        z  | j                   }|                    | j                   |                    t          | j	        | j        dz  z   | j
        | j        dz  z                        |S )zConvert a rectangle element to a vectorized mobject.

        Parameters
        ----------
        rect
            The parsed SVG rectangle.
        r   )r/   r-   )r/   r-   corner_radiusr   )rxryr   r/   r-   r   stretch_to_fit_heightr   r&   r   r   )r   rP   s     r%   r   zSVGMobject.rect_to_mobject  s     7a<<47a<<j{  CC
 #j{TW,tw6"g  C
 %%dk222		 $*q.!8$&4;QR?:RSS	
 	
 	
 
r'   ellipsese.Ellipse | se.Circler   c                    t          | j                  }| j        | j        k    r|                    d| j        z             |                    t          | j        | j                             |S )zConvert an ellipse or circle element to a vectorized mobject.

        Parameters
        ----------
        ellipse
            The parsed SVG ellipse or circle.
        )radiusr   )r   r   r   r   r   r&   cxcy)r   rP   s     r%   r   zSVGMobject.ellipse_to_mobject  se     GJ''':##%%a'*n555		&wz7:>>???
r'   polygon
se.Polygonr   c                ,    d | D             }t          | S )zConvert a polygon element to a vectorized mobject.

        Parameters
        ----------
        polygon
            The parsed SVG polygon.
        c                     g | ]}t          | S r>   r&   rw   points     r%   
<listcomp>z1SVGMobject.polygon_to_mobject.<locals>.<listcomp>  s    DDD5&.DDDr'   )r   )r   pointss     r%   r   zSVGMobject.polygon_to_mobject  s#     EDGDDDr'   polylinese.Polylinec                |    d |D             }|                                  } |                                |          S )zConvert a polyline element to a vectorized mobject.

        Parameters
        ----------
        polyline
            The parsed SVG polyline.
        c                     g | ]}t          | S r>   r   r   s     r%   r   z2SVGMobject.polyline_to_mobject.<locals>.<listcomp>  s    EEE5&.EEEr'   )get_mobject_type_classset_points_as_corners)rG   r   r   vmobject_classs       r%   r   zSVGMobject.polyline_to_mobject  sE     FEHEEE4466~55f===r'   textse.Textc                N    t          j        dt          |                       dS )zConvert a text element to a vectorized mobject.

        .. warning::

            Not yet implemented.

        Parameters
        ----------
        text
            The parsed SVG text.
        r   N)r   r   r   )r  s    r%   r   zSVGMobject.text_to_mobject  s(     	@DJJ@@AAAr'   c                    | j         r|                                  | j        |                     | j                   | j        |                     | j                   dS dS )z3Scale and move the generated mobject into position.N)r-   )r/   )r+   centerrA   setrB   rU   s    r%   rF   zSVGMobject.move_into_position  sd     	KKMMM?&HHDOH,,,>%HH4>H***** &%r'   )NTr   NNNNNNNNNNT)r)   r*   r+   r,   r-   r.   r/   r.   r0   r1   r2   r.   r3   r1   r4   r.   r5   r1   r6   r.   r7   r.   r8   r9   r:   r9   r;   r,   r<   r   )r;   r,   r   rI   )r   rQ   r   rI   )r   r   )rf   rn   r   rn   )r   r   )ri   r   r   r   )r   r   r   r   )rP   r   r   r   r   r   )rP   r   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r  r  r   r   )rS   
__module____qualname____doc__r@   rD   propertyrK   rN   rX   r[   r}   rc   r   staticmethodr   r   r   r   r   r   r   r   r   rF   __classcell__rH   s   @r%   r   r   $   sG       : :| /3" "+/ $04%)26'+%)#'*.">" >" >" >" >" >" >"@8 8 8 8( 
 
 
 X
   ": : : :( ( ( (6   $8 8 8 8t   2    \"    \&D D D D 
 
 
 \
    \2    \ 	  	  	  \	 
> 
> 
> 
>    \+ + + + + + + +r'   )	metaclassc                  D     e Zd ZdZ	 	 	 dd fdZddZddZddZ xZS )r   a  A vectorized mobject representing an SVG path.

    .. note::

        The ``long_lines``, ``should_subdivide_sharp_curves``,
        and ``should_remove_null_curves`` keyword arguments are
        only respected with the OpenGL renderer.

    Parameters
    ----------
    path_obj
        A parsed SVG path object.
    long_lines
        Whether or not straight lines in the vectorized mobject
        are drawn in one or two segments.
    should_subdivide_sharp_curves
        Whether or not to subdivide subcurves further in case
        two segments meet at an angle that is sharper than a
        given threshold.
    should_remove_null_curves
        Whether or not to remove subcurves of length 0.
    kwargs
        Further keyword arguments are passed to the parent
        class.
    Fpath_objr   
long_linesr,   should_subdivide_sharp_curvesshould_remove_null_curvesr<   r   c                    |                                  || _        || _        || _        || _         t                      j        di | d S )Nr>   )approximate_arcs_with_quadsr  r  r  r  r?   r@   )rG   r  r  r  r  r<   rH   s         r%   r@   zVMobjectFromSVGPath.__init__  sX     	,,... $-J*)B&""6"""""r'   r   rI   c                    |                                   t          j        dk    rK| j        r|                                  | j        r+|                     |                                            d S d S d S )Nopengl)handle_commandsr   rT   r  subdivide_sharp_curvesr  
set_pointsget_points_without_null_curvesrU   s    r%   generate_pointsz#VMobjectFromSVGPath.generate_points!  s     	?h&&1 .++---- G C C E EFFFFF '&G Gr'   c                .    |                                   d S N)r  rU   s    r%   init_pointszVMobjectFromSVGPath.init_points.  s    r'   c           	       	
 g d d d 	ddd	fd	
| j         d
k    rd
fdd 
fdd!
fd}nd
fdd 
fdd!
fd}| j        D ]9}|j        }|t          j        k    r 
t          |j         d           4|t          j        k    r |t          |j                    ]|t          j        k    r& t          |j	         t          |j                    |t          j
        k    r3 t          |j         t          |j         t          |j                    |t          j        k    rCt          t          j                            	z
                      dk    r |	           d )t%          d|           t          j        dd          | _        t+                    dk    r!t          j        | j        d          | _        d S d S )"NF)	true_moveptr    r#  r,   r   rI   c                    | |rd S d S r   r>   )r$  r#  curve_start	last_movelast_true_moves     r%   move_penz5VMobjectFromSVGPath.handle_commands.<locals>.move_pen7  s.    I"' +!*+ +r'      r   cp1cp2r   c                    t                    dz  dk    sJ t                                | |||gz   |           d S )Nr*  r   r   )r   r+  r,  r   
all_pointsr)  s       r%   	add_cubicz6VMobjectFromSVGPath.handle_commands.<locals>.add_cubicA  sS     :*a///Z///uc344
r'   cpc                ^     | | |z   |z   dz  ||z   |z   dz  |            |           d S Nr   r>   )r   r1  r   r0  r)  s      r%   add_quadz5VMobjectFromSVGPath.handle_commands.<locals>.add_quadI  sD    	%%"*r/Q!6b3!8KSQQQr'   c                ^     | | | z   |z   dz  | |z   |z   dz  |            |           d S r3  r>   )r   r   r0  r)  s     r%   add_linez5VMobjectFromSVGPath.handle_commands.<locals>.add_lineM  sO    	EEMC/14us{S7HA6Ms   r'   c                   t                    dz  dk    sJ t                                t          | |||          }|d d                                         z  |dd                                          z   |           d S Nr   r   )r   r   tolist)r   r+  r,  r   	two_quadsr/  r)  s        r%   r0  z6VMobjectFromSVGPath.handle_commands.<locals>.add_cubicU  s     :*a///Z///@	 	 im22444
im22444
r'   c                    t                    dz  dk    sJ t                                | ||gz   |           d S r8  r.  )r   r1  r   r/  r)  s      r%   r4  z5VMobjectFromSVGPath.handle_commands.<locals>.add_quadd  sO    :*a///Z///ub#..
r'   c                D     | | |z   dz  |            |           d S )Nr   r>   )r   r   r4  r)  s     r%   r6  z5VMobjectFromSVGPath.handle_commands.<locals>.add_linej  s1     13777r'   Tg-C6?zNot implemented: r   float64)ndmindtyper   )r   r   )r$  r    r#  r,   r   rI   )
r   r    r+  r    r,  r    r   r    r   rI   )r   r    r1  r    r   r    r   rI   )r   r    r   r    r   rI   )n_points_per_curver  rH   r`   Mover&   r   r   QuadraticBeziercontrolCubicBeziercontrol1control2Closeabsr#   linalgnormAssertionErrorr$   r   r   reshape)rG   r6  segmentsegment_classr0  r4  r/  r&  r'  r(  r)  s       @@@@@@@r%   r  z#VMobjectFromSVGPath.handle_commands1  s   ')
 $	:? 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ "a''                                      } 	J 	JG#-M''-w{;tLLLLL"'))$8'+$FGGGG""444('/:('+6   
 ".00	('*:;('*:;('+6	    "(** ry~~i.&@AABBVKKHY777"$%H%H%HIIIhz)DDD z??a*T[&99DKKK  r'   )FFF)
r  r   r  r,   r  r,   r  r,   r<   r   r  )	rS   r	  r
  r  r@   r  r!  r  r  r  s   @r%   r   r     s         : !.3*/# # # # # # #$G G G G   _: _: _: _: _: _: _: _:r'   )r   r   r   r   r   r    )0r  
__future__r   ospathlibr   typingr   	xml.etreer   rY   numpyr#   svgelementsr`   manimr   r   manim.utils.colorr	   r
   	constantsr   utils.bezierr   utils.imagesr   utils.iterablesr   geometry.arcr   geometry.liner   geometry.polygramr   r   r   opengl.opengl_compatibilityr   types.vectorized_mobjectr   r   __all__r   __annotations__r&   r   r   r>   r'   r%   <module>rc     s   * * * " " " " " " 				             ' ' ' ' ' '                         < < < < < < < <       @ @ @ @ @ @ 6 6 6 6 6 6 ' ' ' ' ' ' ! ! ! ! ! !             D D D D D D D D D D 9 9 9 9 9 9 7 7 7 7 7 7 7 7.
/ .0  / / / /! ! ! !M+ M+ M+ M+ M+_ M+ M+ M+ M+`\: \: \: \: \:(o \: \: \: \: \: \:r'   