� J�g�� �0�dZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZ dd lmZn!#e$rejd ��dd lmZYnwxYwdd lmZdd lmZmZmZmZmZmZmZdd lm Z m!Z!ddl"m#Z#ej$e%��Z&ddiZ'd�Z(ddd�Z)ej*d��Z+ej*d��Z,e(Gd�de-����Z.ej*d��Z/hd�Z0ddddddd d!�Z1ej2d"z ej2d#z dej2d$�Z3e(dRd&���Z4e(dSd(���Z5e(d)���Z6e(d*���Z7e(d+���Z8e7d,d-d.d/d0d1d2��Z9e(d3���Z:e(d4���Z;e(d5���Z<e(d6���Z=e(d7�fd8���Z>e(d9�fd:���Z?d;�d<�d=�d>�d?�d@�dA�dB�dC�dD�dE�dF� Z@e(dG���ZAe(GdH�dI����ZBe(dJ���ZCGdK�dLe ��ZDe(dM���ZEGdN�dO��ZFGdP�dQe��ZGdS)Tz� Utilities to parse SVG graphics into fpdf.drawing objects. The contents of this module are internal to fpdf2, and not part of the public API. They may change at any time without prior warning or any deprecation period, in non-backward-compatible ways. �N)�Number)� NamedTuple)� parse_path)�BasePen�)� PathPaintRule)� fromstringzYdefusedxml could not be imported - fpdf2 will not be able to sanitize SVG images provided)�html)�color_from_hex_string�color_from_rgb_string�GraphicsContext� GraphicsStyle� PaintedPath� ClippingPath� Transform)� ImageCache�VectorImageInfo)�stream_content_for_raster_image�force_nodocumentFc�$�dt|j<|S)zQA decorator that forces pdoc not to document the decorated item (class or method)F)�__pdoc__� __qualname__)�items �X/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/fpdf/svg.pyrr-s��"'�H�T� �� �K�zhttp://www.w3.org/2000/svgzhttp://www.w3.org/1999/xlink)�svg�xlinkz(?:\s+,\s+|\s+,|,\s+|\s+|,)zx(matrix|rotate|scale|scaleX|scaleY|skew|skewX|skewY|translate|translateX|translateY)\(((?:\s*(?:[-+]?[\d\.]+,?)+\s*)+)\)c��eZdZdZdS)�Percentz$class to represent percentage valuesN)�__name__� __module__r�__doc__�rrrrAs������.�.�.�.rrz2\s*(?P<value>[-+]?[\d\.]+)\s*(?P<unit>%|[a-zA-Z]*)>�%�ch�em�ex�ic�lh�vb�vh�vi�vw�cap�rem�rlh�vmax�vmin�Hg��b�X<@g��j�Z�@� g�?g��j�Z��?)�in�cm�mm�pt�pc�px�Qihi�)�deg�grad�rad�turnr8c�V�t�|��}|�td|�d���d�|���\}}|s|} t |��t |zS#t $r4|tvrt|�d|����d�t|�d|����d�wxYw)z7Convert a length unit to our canonical length unit, pt.NzUnable to parse 'z ' as a lengthz" uses unsupported relative length z contains unrecognized unit )� unit_splitter�match� ValueError�groups�float�absolute_length_units�KeyError�relative_length_units)� length_str� default_unitrB�value�units r�resolve_lengthrMvs��� � � � � +� +�E� �}��F�Z�F�F�F�G�G�T�Q��,�,�.�.�K�E�4� ����V��U�|�|�3�D�9�9�9�� �V�V�V� �(� (� (���G�G��G�G���� ��J�J�J�D�J�J�K�K�QU�U� V���s � A*�*>B(r<c���t�|�����\}}|s|} t|��t|zS#t $rt d|�d|����d�wxYw)z;Convert an angle value to our canonical angle unit, radianszangle z has unknown unit N)rArBrDrE� angle_unitsrGrC)� angle_strrJrKrLs r� resolve_anglerQ�s��� �%�%�i�0�0�7�7�9�9�K�E�4� ����Q��U�|�|�k�$�/�/�/�� �Q�Q�Q��E�)�E�E�t�E�E�F�F�D�P�Q���s �A�!A3c�X� dt|�d�}n#t$rd}YnwxYw|�|��S)zECreate an XML namespace string representation for the given tag name.�{�}�)�_HANDY_NAMESPACESrG)�space�names r�xmlnsrY�sV���1�&�u�-�1�1�1���� ������������� �T� � �s �� #�#c�D�i}|D]}||t||��<|||<�|S)z>Create a lookup for the given name in the given XML namespace.)rY)rW�names�resultrXs r� xmlns_lookupr]�s<���F�����%)��u�U�D�!�!�"���t� � � �Mrc�V�|�d��}|dkr ||dzd�S|S)z8Remove the xmlns namespace from a qualified XML tag namerTrrN)�index)� qualified_tag�is r� without_nsrb�s9�� ���C� � �A��A�v�v��Q��U�W�W�%�%� �rr�rect�circle�ellipse�line�polyline�polygonc��� tj|}n#t$rYnwxYw|�d��rt |��S|�d��rt |��St d|�����)N�#�rgbz unsupported color specification )r � COLOR_DICTrG� startswithr r rC��colorstrs r�svgcolorrp�s��� ��?�8�,���� � � � � �� �������3���/�$�X�.�.�.����5�!�!�/�$�X�.�.�.� �B��B�B� C� C�Cs �� "�"c�f�t|��}|dkrtd|�d����|dkrdS|S)Nrz stroke width z cannot be negative)rMrC��incoming�vals r�convert_stroke_widthru�sE�� �� "� "�C� �Q�w�w��F��F�F�F�G�G�G� �a�x�x��t� �Jrc�V�t|��}|dkrtd|�d����|S)N��?z miter limit z cannot be less than 1)rErCrrs r�convert_miterlimitrx�s5�� ��/�/�C� �S�y�y��H��H�H�H�I�I�I� �Jrc������fd�}|S)Nc�F��t|��}|�kr�S|�kr�S|S�N�rE)rKrt�max_val�min_vals ��r� converterzclamp_float.<locals>.converter�s/����E�l�l�� ��=�=��N� ��=�=��N�� rr#)r~r}rs`` r� clamp_floatr��s*���������� �rc��|Sr{r#)rKs r�<lambda>r��s��u�rc�<�|dkr tjS||��S)N�inherit)r�INHERIT�rKrs r� inheritabler��s&�� � ����$�$� �9�U� � �rc��|Sr{r#)�noops rr�r��s��4�rc�2�|dkrdSt||��S)N�none�r�r�s r�optionalr��s �� �����t� �u�i� (� (�(rc�0�dt|t��fS)N� fill_color�r�rprns rr�r�s��l�H�X�x�,H�,H�I�rc�$�dt|��fS)N�intersection_ruler�)� fillrulestrs rr�r�s��&9�;�{�;S�;S�%T�rc�B�dt|tdd����fS)N� fill_opacity�rw�r�r�)�filopstrs rr�r�s#����H�k�#�s�3�3�4�4�&�rc�0�dt|t��fS)N� stroke_colorr�rns rr�r� s�����8�0L�0L�M�rc�0�dt|t��fS)N� stroke_width)r�ru)�valuestrs rr�r� s����H�2�3�3�&�rc�(�dt|d���fS)N�stroke_dash_patternc�J�d�t�|��D��S)Nc�,�g|]}t|����Sr#r|)�.0rs r� <listcomp>z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>s��"R�"R�"R�4�5��;�;�"R�"R�"Rr)� NUMBER_SPLIT�split)�das rr�z<lambda>.<locals>.<lambda>s#��"R�"R�<�;M�;M�b�;Q�;Q�"R�"R�"R�r)r�)� dasharrays rr�r�s"���� �R�R� � �+�rc�0�dt|t��fS)N�stroke_dash_phase)r�rE)�dashoffs rr�r�s����G�U�#�#�*�rc�$�dt|��fS)N�stroke_cap_styler�)�capstrs rr�r�s��&8�+�f�:M�:M�%N�rc�$�dt|��fS)N�stroke_join_styler�)�joinstrs rr�r�!s��(;�[��=Q�=Q�'R�rc�0�dt|t��fS)N�stroke_miter_limit)r�rx)�limstrs rr�r�#s����F�.�/�/�)�rc�B�dt|tdd����fS)N�stroke_opacityr�rwr�)�stropstrs rr�r�(s#����H�k�#�s�3�3�4�4�(�r) �fillz fill-rulez fill-opacity�strokez stroke-widthzstroke-dasharrayzstroke-dashoffsetzstroke-linecapzstroke-linejoinzstroke-miterlimitzstroke-opacityc�^�tj|j�dd����}d|j_t ���D]P\}}|�||j�|����}|rt|jg||���R��Q|�d|j�d����}|r't|��}||j_ ||j_ |j�d��}|rt|��|_ dSdS)zKApply the known styles from `svg_element` to the pdf path/group `stylable`.�stylerUF�opacity� transformN)r �parse_css_style�attrib�getr�� auto_close� svg_attr_map�items�setattrrEr�r��convert_transformsr�)�stylable� svg_elementr�� attr_namerrKr��tfstrs r� apply_stylesr�/s+�� � ��!3�!7�!7���!D�!D� E� E�E� %�H�N�� ,� 2� 2� 4� 4�7�7�� �9�� � �)�[�%7�%;�%;�I�%F�%F�G�G�� � 7� �H�N� 6�Y�Y�u�%5�%5� 6� 6� 6� 6���i�i� �;�#5�#9�#9�)�#D�#D�E�E�G��0���.�.��&-���#�(/���%� � � "� "�;� /� /�E� �7�/��6�6�����7�7rc���eZdZdZed defd���Zed defd���Zed defd���Z ed defd���Z ed���Z ed ���Z ed defd ���Z d S) � ShapeBuilderzNA namespace within which methods for converting basic shapes can be looked up.F� clipping_pathc�b�t��}|rt��}t||��|S)z.Create a new path with the appropriate styles.)rrr�)�tagr��paths r�new_pathzShapeBuilder.new_pathKs3���}�}�� � "��>�>�D��T�3����� rc�t�t|j�dd����}t|j�dd����}|j�dd��}|�d��rt |dd���}nt|��}|j�dd��}|�d��rt |dd���}nt|��}|j�d d ��}|j�d d ��}|d krd }|d krd }||cxkrd krnnd x}}nN|d krt |��x}}n6|d krt |��x}}nt |��}t |��}|d ks|d ks |d ks|d krt d|�����|d ks|d krt��S||dz kr|dz }||dz kr|dz }|�||��} | � ||||||��| S)z&Convert an SVG <rect> into a PDF path.�x�0�y�widthr$N������height�rx�auto�ryr�rz bad rect �) rMr�r��endswithrrErCrr�� rectangle) �clsr�r�r�r�r�r�r�r�r�s rrczShapeBuilder.rectTsN�� �3�:�>�>�#�s�3�3� 4� 4�� �3�:�>�>�#�s�3�3� 4� 4��� ���w��,�,�� �>�>�#� � � *��E�#�2�#�J�'�'�E�E�"�5�)�)�E������#�.�.�� �?�?�3� � � ,��V�C�R�C�[�)�)�F�F�#�F�+�+�F� �Z�^�^�D�&� )� )�� �Z�^�^�D�&� )� )�� ��<�<��B� ��<�<��B� �� � � � �v� � � � � ��K�B��� �6�\�\��B�i�i� �B��� �6�\�\��B�i�i� �B����r���B��r���B� �A�I�I�6�A�:�:�2��6�6�r�A�v�v��.��.�.�/�/� /� �Q�J�J�F�a�K�K��=�=� � ���� � ����B� ��!�� � ��!��B��|�|�C��/�/�� ���q�!�U�F�B��3�3�3�� rc�4�t|j�dd����}t|j�dd����}t|jd��}|�||��}|�|||��|S)z(Convert an SVG <circle> into a PDF path.�cxr�cy�r)rEr�r�r�rd)r�r�r�r�r�r�r�s rrdzShapeBuilder.circle�s���3�:�>�>�$��*�*� +� +�� �3�:�>�>�$��*�*� +� +�� �#�*�S�/� "� "���|�|�C��/�/�� � � �B��A����� rc�D�t|j�dd����}t|j�dd����}|j�dd��}|j�dd��}|�||��}||cxkrdksn|dks|dkr|S|dkrt|��x}}n6|dkrt|��x}}nt|��}t|��}|�||||��|S)z)Convert an SVG <ellipse> into a PDF path.r�rr�r�r�r�)rEr�r�r�re)r�r�r�r�r�r�r�r�s rrezShapeBuilder.ellipse�s���3�:�>�>�$��*�*� +� +�� �3�:�>�>�$��*�*� +� +�� �Z�^�^�D�&� )� )�� �Z�^�^�D�&� )� )���|�|�C��/�/�� �"� � � � �� � � � �B�!�G�G��q����K� ��<�<��B�i�i� �B��� �6�\�\��B�i�i� �B����r���B��r���B� � � �R��R��$�$�$�� rc�X�t|jd��}t|jd��}t|jd��}t|jd��}|�|��}|�||��|�||��|S)z&Convert an SVG <line> into a PDF path.�x1�y1�x2�y2)rEr�r��move_to�line_to)r�r�r�r�r�r�r�s rrfzShapeBuilder.line�s����3�:�d�#� $� $�� �3�:�d�#� $� $�� �3�:�d�#� $� $�� �3�:�d�#� $� $���|�|�C� � �� � � �R����� � � �R������ rc�p�|�|��}d|jdz}t||��|S)z*Convert an SVG <polyline> into a PDF path.�M�points�r�r��svg_path_converter)r�r�r�r�s rrgzShapeBuilder.polyline�s;���|�|�C� � ���s�z�(�+�+���4��(�(�(�� rc�x�|�||��}d|jdzdz}t||��|S)z)Convert an SVG <polygon> into a PDF path.r�r��Zr�)r�r�r�r�r�s rrhzShapeBuilder.polygon�sB���|�|�C��/�/���s�z�(�+�+�c�1���4��(�(�(�� rN)F)r r!rr"� staticmethod�boolr�� classmethodrcrdrerfrgrhr#rrr�r�Gs+������X�X����T�����\���.�.�d�.�.�.��[�.�`��������[����������[��0� � ��[� �����[����������[���rr�c � �t�|��}tj��}|D�]�\}}|dkrPt d�t �|��D����\}}}}} } t||||| | ��|z}�\|dkr�t �|��^} } t| ��} tj| ���} | rft| ��dkr=| � t| d��t| d����} ntd|�d |�d ����| |z}��|d kr�t �|��}t|��dkr+t|d��}t|d��}n=t|��dkrt|d��x}}ntd |�����tj ||� ��|z}���|dkr(tj t|��d� ��|z}���|dkr(tj dt|��� ��|z}��#|dkr�t �|��}t|��dkr+t|d��}t|d��}n=t|��dkrt|d��}d}ntd|�����tjtj|��tj|��� ��|z}���|dkr:tjtjt|����d� ��|z}��=|dkr:tjdtjt|����� ��|z}��}|dkr�t �|��}t|��dkr+t#|d��}t#|d��}n=t|��dkrt#|d��}d}ntd|�����tj||� ��|z}��3|dkr(tjt#|��d� ��|z}��a|dkr&tjdt#|��� ��|z}���|S)z8Convert SVG/CSS transform functions into PDF transforms.�matrixc3�4K�|]}t|��V��dSr{r|)r��ns r� <genexpr>z%convert_transforms.<locals>.<genexpr>�s(����$P�$P�!�U�1�X�X�$P�$P�$P�$P�$P�$Pr�rotate)�thetar�rrzrotation transform �(z) is malformed�scalezbad scale transform �r�r��scaleX�scaleY�skewzbad skew transform �skewX�skewY� translatezbad translation transform � translateX� translateY)�TRANSFORM_GETTER�findallr�identity�tupler�r�rQ�rotation�len�aboutrErC�scaling�shearing�math�tanrM� translation)r��parsedr��tf_type�args�a�b�c�d�e�fr�r r �sx�syr�r�s rr�r��s���� %� %�e� ,� ,�F��"�$�$�I��RW�RW� ��� �h� � �$�$P�$P�|�7I�7I�$�7O�7O�$P�$P�$P�P�P� �A�q�!�Q��1�!�!�Q��1�a��3�3�i�?�I�I� �� � �(�.�.�t�4�4�M�E�E�!�%�(�(�E� �)��6�6�6�H�� ��u�:�:��?�?�'�~�~�e�E�!�H�o�o�u�U�1�X���O�O�H�H�$�L�g�L�L��L�L�L����!�9�,�I�I� �� � ��%�%�d�+�+�D��4�y�y�A�~�~��4��7�^�^���4��7�^�^����T���a�����Q��.�.�(��R�R� �!?��!?�!?�@�@�@�!�)�B�"�5�5�5� �A�I�I� �� � �!�)�E�$�K�K�1�=�=�=� �I�I�I� �� � �!�)�A��t���=�=�=� �I�I�I� �� � ��%�%�d�+�+�D��4�y�y�A�~�~�"�4��7�+�+��"�4��7�+�+����T���a���"�4��7�+�+����� �!>�u�!>�!>�?�?�?�!�*�T�X�b�\�\�T�X�b�\�\�J�J�J�Y�V�I�I� �� � ��"�T�X�m�D�.A�.A�%B�%B�a�H�H�H�9�T� �I��� � ��"�Q�$�(�=��3F�3F�*G�*G�H�H�H�9�T� �I�� � #� #��%�%�d�+�+�D��4�y�y�A�~�~�"�4��7�+�+��"�4��7�+�+����T���a���"�4��7�+�+����� �!E�e�!E�!E�F�F�F�!�-��Q�7�7�7�)�C�I�I� � � $� $�!�-��t�0D�0D��J�J�J�Y�V�I�I� � � $� $�!�-��^�D�5I�5I�J�J�J�Y�V�I�� �rc�B��eZdZ�fd�Zd�Zd�Zd�Zd�Zd�Zd�Z �xZ S)�PathPenc�d��||_d|_d|_t��j|i|��dS)NF)�pdf_path�last_was_line_to� first_is_move�super�__init__)�selfr!r�kwargs� __class__s �rr%zPathPen.__init__+s<��� �� � %���!��������$�)�&�)�)�)�)�)rc�R�|jj|�d|_|j� d|_dSdS)NFT)r!r�r"r#�r&r8s r�_moveTozPathPen._moveTo1s;���� ��r�"�"� %��� � � %�!%�D� � � � &� %rc�R�|jj|�d|_|j� d|_dSdS)NTF)r!r�r"r#r*s r�_lineTozPathPen._lineTo7s;���� ��r�"�"� $��� � � %�!&�D� � � � &� %rc ��|j�|d|d|d|d|d|d���d|_|j� d|_dSdS)Nrr)r�r�r�r��x3�y3F)r!�curve_tor"r#)r&�pt1�pt2�pt3s r� _curveToOnezPathPen._curveToOne=so�� � ����1�v�#�a�&�S��V��A��3�q�6�c�!�f� � � � �!&��� � � %�!&�D� � � � &� %rc��|j�|d|d|d|d���d|_|j� d|_dSdS)Nrr)r�r�r�r�F)r!�quadratic_curve_tor"r#)r&r2r3s r� _qCurveToOnezPathPen._qCurveToOneEsY�� � �(�(�C��F�s�1�v�#�a�&�S�QR�V�(�T�T�T� %��� � � %�!&�D� � � � &� %rc ��|j�||||||d|d���d|_|j� d|_dSdS)Nrr)r�r�r � large_arc�positive_sweepr�r�F)r!�arc_tor"r#)r&r�r�r �arc�sweep�ends r�arcToz PathPen.arcToKsg�� � ������� ��!�f��!�f� � � � �!&��� � � %�!&�D� � � � &� %rc�x�|jr|j���|j���dSr{)r"r!�remove_last_path_element�close)r&s r� _closePathzPathPen._closePathYs=�� � � 5� �M� 2� 2� 4� 4� 4� � �������r) r r!rr%r+r-r5r8r@rD� __classcell__)r(s@rrr*s��������*�*�*�*�*� &�&�&� '�'�'� '�'�'�'�'�'� '� '� '�������rrc�p�t|��}t||��|jstd���dS)Nz"Path does not start with move item)rrr#rC)r!�svg_path�pens rr�r�asC�� �(� � �C��x����� � �?��=�>�>�>�?�?rc�J�eZdZdZedd�d���Zddefd�Zed���Z ed ���Z ed ���Z dd �Z dd�Z dd�Zed���Zed���Zedd���Zed���Zed���Zed���Zed���Zed���ZdS)� SVGObjectzU A representation of an SVG that has been converted to a PDF representation. zutf-8��encodingc��t|d|���5}||���g|�Ri|��cddd��S#1swxYwYdS)a� Create an `SVGObject` from the contents of the file at `filename`. Args: filename (path-like): the path to a file containing SVG data. *args: forwarded directly to the SVGObject initializer. For subclass use. encoding (str): optional charset encoding to use when reading the file. **kwargs: forwarded directly to the SVGObject initializer. For subclass use. Returns: A converted `SVGObject`. r�rKN)�open�read)r��filenamerLrr'�svgfiles r� from_filezSVGObject.from_filens����(�C�(� 3� 3� 3� 8�w��3�w�|�|�~�~�7��7�7�7��7�7� 8� 8� 8� 8� 8� 8� 8� 8� 8� 8� 8� 8���� 8� 8� 8� 8� 8� 8s�>�A�AN� image_cachec���||_i|_t|��}|jt dd��vrt d|j�����|�|��|�|��dS)Nrzroot tag must be svg, not )rS�cross_references� parse_xml_strr�r]rC�extract_shape_info�convert_graphics)r&�svg_textrS�svg_trees rr%zSVGObject.__init__s��&��� "���!��*�*�� �<�|�E�5�9�9� 9� 9��H�(�,�H�H�I�I� I� ����)�)�)� ���h�'�'�'�'�'rc�Z�|r(|�d��sd|zn|}||j|<dSdS)Nrj)rmrU)r&�key� referenceds r� update_xrefzSVGObject.update_xref�sE�� � 4�#&�>�>�#�#6�#6�?�#��)�)�C�C�)3�D� !�#� &� &� &� 4� 4rc�H�|�d��}|�d��}|�d��}|�dd��}|dkrd|_nd|_d|_|�Z|���|�d��rt |dd ���|_nt |��|_d|_|�Z|���|�d��rt |dd ���|_nt |��|_|� d|_dS|���d �t� |��D��\}}}} |d ks| d krtd |�����|||| g|_dS) z&Collect shape info from the given SVG.r�r��viewBox�preserveAspectRatioTr�Nr$r�c�,�g|]}t|����Sr#r|)r��nums rr�z0SVGObject.extract_shape_info.<locals>.<listcomp>�s��P�P�P�S�e�C�j�j�P�P�Prrz)invalid negative width/height in viewbox ) r�� preserve_arr��stripr�rrMr��viewboxr�r�rC) r&�root_tagr�r�rfrd�vx�vyr-r+s rrWzSVGObject.extract_shape_info�s���� � �W�%�%�����h�'�'���,�,�y�)�)���l�l�#8�$�?�?� � �&� � �#�D� � �#�D� ��� � � � �K�K�M�M�M��~�~�c�"�"� 3�$�U�3�B�3�Z�0�0�� � �+�E�2�2�� ��� � � � �L�L�N�N�N����s�#�#� 5�%�f�S�b�S�k�2�2�� � �,�V�4�4�� � �?��D�L�L�L� �M�M�O�O�O�P�P�L�4F�4F�w�4O�4O�P�P�P�N�B��B���Q���B��F�F� �!V�W�!V�!V�W�W�W���B��+�D�L�L�Lrc��t��}d|j_d|j_d|j_|�||��||_dS)zFConvert the graphics contained in the SVG into the PDF representation.NF�butt)r r�r�r�r�� build_group� base_group)r&rgrms rrXzSVGObject.convert_graphics�sQ��%�&�&� �(,� ��%�&+� ��#�,2� ��)� ����:�.�.�.�$����rTc�P�|�|j|j|j|��S)a Size the converted SVG paths to the page viewport. The SVG document size can be specified relative to the rendering viewport (e.g. width=50%). If the converted SVG sizes are relative units, then this computes the appropriate scale transform to size the SVG to the correct dimensions for a page in the current PDF document. If the SVG document size is specified in absolute units, then it is not scaled. Args: pdf (fpdf.FPDF): the pdf to use the page size of. align_viewbox (bool): if True, mimic some of the SVG alignment rules if the viewbox aspect ratio does not match that of the viewport. Returns: The same thing as `SVGObject.transform_to_rect_viewport`. )�transform_to_rect_viewport�k�epw�eph)r&�pdf� align_viewboxs r�transform_to_page_viewportz$SVGObject.transform_to_page_viewport�s$��(�.�.�s�u�c�g�s�w� �V�V�VrFc�8�|r|}nBt|jt��r|std���|j|zdz }n |jp|}|r|}nBt|jt��r|std���|j|zdz }n |jp|}|dkrt j��}nt jd|z ��}|jr�|j\} } } } | dks| dkrddt��fS|| z } || z }|s|j r| |krt| |��x} }|t j | | ���zt j| |���z}|r1|t j |dz | dz | zz |dz | dz |zz ���z}||j _||z ||z |j fS)a� Size the converted SVG paths to an arbitrarily sized viewport. The SVG document size can be specified relative to the rendering viewport (e.g. width=50%). If the converted SVG sizes are relative units, then this computes the appropriate scale transform to size the SVG to the correct dimensions for a page in the current PDF document. Args: scale (Number): the scale factor from document units to PDF points. width (Number): the width of the viewport to scale to in document units. height (Number): the height of the viewport to scale to in document units. align_viewbox (bool): if True, mimic some of the SVG alignment rules if the viewbox aspect ratio does not match that of the viewport. ignore_svg_top_attrs (bool): ignore <svg> top attributes like "width", "height" or "preserveAspectRatio" when figuring the image dimensions. Require width & height to be provided as parameters. Returns: A tuple of (width, height, `fpdf.drawing.GraphicsContext`), where width and height are the resolved width and height (they may be 0. If 0, the returned `fpdf.drawing.GraphicsContext` will be empty). The `fpdf.drawing.GraphicsContext` contains all of the paths that were converted from the SVG, scaled to the given viewport size. z?SVG "width" is a percentage, hence a viewport width is required�dzASVG "height" is a percentage, hence a viewport height is requiredrrr�r�)� isinstancer�rrCr�rr rrfr rd�minrrmr�)r&r�r�r�rt�ignore_svg_top_attrs�vp_width� vp_heightr�rhrir-r+�w_ratio�h_ratios rroz$SVGObject.transform_to_rect_viewport�s'��: � +��H�H� �� �G� ,� ,� +�� � �U�����z�E�)�C�/�H�H��z�*�U�H� � .��I�I� �� �W� -� -� .�� � �W����� �f�,�s�2�I�I�� �-�v�I� �A�:�:�!�*�,�,�I�I�!�)�!�e�)�4�4�I� �<� �!�\�N�B��B���a���R�1�W�W��!�_�.�.�.�.���m�G��"�n�G�'� :�D�,<� :�'�W�BT�BT�$'���$9�$9�9��'���'�2�#�"��5�5�5�6��#�g��9�9�9�:� � � �%� �(=���l�b�1�f��%7�7��!�m�r�A�v��&8�8�)�)�)�� � %.���!��%���U�!2�D�O�C�Crc�t�|j|_|�|��\}}}|j|j}} |�:|�8|�dd��|jt j||��z|_|�||��|�||��dS#|�||��wxYw)a� Directly draw the converted SVG to the given PDF's current page. The page viewport is used for sizing the SVG. Args: pdf (fpdf.FPDF): the document to which the converted SVG is rendered. x (Number): abscissa of the converted SVG's top-left corner. y (Number): ordinate of the converted SVG's top-left corner. debug_stream (io.TextIO): the stream to which rendering debug info will be written. Nr) rSrur�r��set_xyr�rr� draw_path) r&rsr�r�� debug_stream�_r��old_x�old_ys r� draw_to_pagezSVGObject.draw_to_page0s����?����4�4�S�9�9� ��1�d��u�c�e�u�� %��}���� � �1�a� � � �!%��)�2G��1�2M�2M�!M��� �M�M�$� � -� -� -� �J�J�u�e� $� $� $� $� $��C�J�J�u�e� $� $� $� $���s �AB�B7c�n�|D�]0}|jtdd��vr|�|���0|jtdd��vr|�|���]|jtdd��vr|�|����|jt vr|�|����|jtdd��vr> |jd}n#t$rd}YnwxYw|D]}|� ||�����t� dt|j������2dS) z:Produce lookups for groups and paths inside the <defs> tagr�gr��image�clipPath�idN�TIgnoring unsupported SVG tag: <%s> (contributions are welcome to add support for it)) r�r]rl� build_path� build_image� shape_tags� build_shaper�rG�build_clipping_path�LOGGER�warningrb)r&�defs�child�clip_id�child_s r� handle_defszSVGObject.handle_defsMsq��� � �E��y�L���4�4�4�4�� � ��'�'�'�'���l�5�&�9�9�9�9�����&�&�&�&���l�5�'�:�:�:�:�� � ��'�'�'�'���j�(�(�� � ��'�'�'�'���l�5�*�=�=�=�=�#�#�l�4�0�G�G���#�#�#�"�G�G�G�#����#�>�>�F��,�,�V�W�=�=�=�=�>����j��u�y�)�)�����# � s� C� C&�%C&c�T�t��}t||��tddd��D]!} |j|}n##t$rY�wxYwt d|�d���� |�|j|��n$#t$rt d|�d|����d�wxYwd|jvs d |jvrkt|j� dd ����t|j� d d ����}}tj ||� ��|_ |S) z?Resolve a cross-reference to an already-seen SVG element by ID.r�hrefr�zuse z% doesn't contain known xref attributez references nonexistent ref id Nr�r�rr�) r r�r]r�rGrC�add_itemrUrEr�rrr�)r&�xref� pdf_group� candidate�refr�r�s r� build_xrefzSVGObject.build_xrefhsk��$�%�%� ��Y��%�%�%�%�g�v�t�<�<� Q� Q�I� ��k�)�,������ � � ��� �����O�D�O�O�O�P�P� P� � � � �t�4�S�9� :� :� :� :��� � � ��A�t�A�A�C�A�A���� � ���� �$�+� � ��� �!3�!3�������a�0�0�1�1�5������a�9P�9P�3Q�3Q�q�A�"+�"7�!�q�"A�"A�"A�I� ��s� A� A�A�& B�!B(c��|�t��}t||��d�|D��D]}|�|���|D�]�}|jt dd��vr|�|���0|jt dd��vr)|�|�|�����p|jt dd��vr)|�|�|������|jtvr)|�|� |������|jt dd��vr*|�|� |������(|jt dd��vr*|�|� |������it� d t|j�������|�|j�d ��|��|S) z+Handle nested items within a group <g> tag.Nc�@�g|]}|jtdd��v�|��S)rr�)r�r])r�r�s rr�z)SVGObject.build_group.<locals>.<listcomp>�s5�� � � ��� �\�%��5P�5P�(P�(P�E�(P�(P�(Prrr�r�r��user�r�r�)r r�r�r�r]r�rlr�r�r�r�r�r�r�rbr^r�r�)r&�groupr�r�s rrlzSVGObject.build_group�s�� � �'�)�)�I� ��E� *� *� *� � �$� � � � $� $�E� � � �U� #� #� #� #�� � �E��y�L���7�7�7�7�� � ��'�'�'�'���l�5�#�6�6�6�6��"�"�4�#3�#3�E�#:�#:�;�;�;�;���l�5�&�9�9�9�9��"�"�4�?�?�5�#9�#9�:�:�:�:���j�(�(��"�"�4�#3�#3�E�#:�#:�;�;�;�;���l�5�%�8�8�8�8��"�"�4�?�?�5�#9�#9�:�:�:�:���l�5�'�:�:�:�:��"�"�4�#3�#3�E�#:�#:�;�;�;�;����j��u�y�)�)����� �����)�)�$�/�/��;�;�;��rc�"�t��}t||��|�||��|j�d��}|�t ||��|�|j�d��|��|S)z1Convert an SVG <path> tag into a PDF path object.rNr�)rr��apply_clipping_pathr�r�r�r^)r&r�r!rGs rr�zSVGObject.build_path�s����=�=���X�t�$�$�$� � � ��4�0�0�0��;�?�?�3�'�'�� � � �x�� 2� 2� 2� ��������.�.��9�9�9��rc���ttt|j��}||��}|�||��|�|j�d��|��|S)z�Convert an SVG shape tag into a PDF path object. Necessary to make xref (because ShapeBuilder doesn't have access to this object.)r�)�getattrr�r�r�r�r^r�r�)r&�shape� shape_builder� shape_paths rr�zSVGObject.build_shape�sk�� � �j���.C�D�D� �"�]�5�)�)� � � � ��U�3�3�3� �����)�)�$�/�/��<�<�<��rc���|jtvr2ttt|j��}||d��}n�|jt dd��vr\t ��}t ||��tj|_ |j � d��}|�t||��n/t�dt|j����dS|�||��dS)NTrr�rzaIgnoring unsupported <clipPath> child tag: <%s> (contributions are welcome to add support for it))r�r�r�r�r]rr�r� DONT_PAINT� paint_ruler�r�r�r�r�rbr^)r&r�r�r��clipping_path_shaperGs rr�zSVGObject.build_clipping_path�s��� �9� � "� "�#�L�*�U�Y�2G�H�H�M�"/�-��t�"<�"<� � � �Y�,�u�f�5�5� 5� 5�"-�-�-� � �,�e� 4� 4� 4�-:�-E� � *��|�'�'��,�,�H��#�"�#6��A�A�A�� �N�N�s��5�9�%�%� � � � �F� ����"5�6�6�6�6�6rc��|j�d��}|r/tjd|��}|j|d|_dSdS)Nz clip-pathzurl\((\#\w+)\)r)r�r��re�searchrUr�)r&r�r�r��clipping_path_ids rr�zSVGObject.apply_clipping_path�s\��#�*�.�.�{�;�;� � � P�!�y�):�M�J�J� �%)�%:�;K�A�;N�%O�H� "� "� "� P� Prc ��d}tdd��D]}||jvr|j|}n�|std���t|j�dd����}t|j�dd����}d|jvrt �d��d |jvrt �d ��d |jvrt �d ��t|t|j�d d����t|j�dd����|||���}|�|j�d��|��|S)Nrr�z#<image> is missing a href attributer�rr�razu"preserveAspectRatio" defined on <image> is currently not supported (contributions are welcome to add support for it)r�zg"style" defined on <image> is currently not supported (contributions are welcome to add support for it)r�zk"transform" defined on <image> is currently not supported (contributions are welcome to add support for it)r�r�r��r�r�r�r�r��svg_objr�) r]r�rCrEr�r�r��SVGImager^)r&r�r�r\r�r�� svg_images rr�zSVGObject.build_image�s��������0�0� � �C��e�l�"�"��|�C�(����#�� D��B�C�C� C��e�l�&�&�w��2�2�3�3���u�|�'�'��!�4�4�5�5�� �E�L� 0� 0� �N�N�H� � � � �e�l� "� "� �N�N�y� � � � �%�,� &� &� �N�N�}� � � ����E�L�$�$�S�#�.�.�/�/��E�L�$�$�S�#�.�.�/�/����  � � � � �����)�)�$�/�/��;�;�;��rr{)T)TF)NNN)r r!rr"r�rRrr%rr^rWrXruror�r�r�rlr�r�r�r�r�r#rrrJrJis����������18�8�8�8�8��[�8� (� (�j� (� (� (� (��4�4���4� �%,�%,���%,�N� %� %��� %�W�W�W�W�.NS�RD�RD�RD�RD�h%�%�%�%�:������4������<�!�!�!���!�F� � ��� ��������7�7���7�&�P�P���P� � � ��� � � rrJc�~�eZdZUeed<eed<eed<eed<eed<eed<d�Zed���Z ed ���Z d S) r�r�r�r�r�r�r�c�h�t|j|j|j|j|j|j���S)Nr�)r�r�r�r�r�r�r�)r&�_memos r� __deepcopy__zSVGImage.__deepcopy__s7������f��f��*��;��L�  � � � rc�v�|jo |jj}|std���ddlm}|||j��\}}}t |t��rt� d��d||fS|� |j |j ��\} } t||j|j| | d���} | ||fS)NzHfpdf2 bug - Cannot render a raster image without a SVGObject.image_cacher)� preload_imagez{Inserting .svg vector graphics in <image> tags is currently not supported (contributions are welcome to add support for it)rUT)�infor�r��w�h�keep_aspect_ratio)r�rS�AssertionError� image_parsingr�r�rxrr�r��size_in_document_unitsr�r�rr�r�) r&� _gsd_registry�_style� last_item� initial_pointrSr�r�r�r�r��stream_contents r�renderzSVGImage.renders����l�?�t�|�'?� �� � �Z��� � 1�0�0�0�0�0�"�]�;�� �:�:� ��1�d� �d�O� ,� ,� 0� �N�N�N� � � ��y�-�/� /��*�*�4�:�t�{�C�C���1�8���f��f���"�  � � ���y�-�7�7rc��|�||||��\}}}|�|j�d|�d���|||fS)Nz rendered as: � )r��writer�)r&� gsd_registryr�r�r�r��_pfxr�s r� render_debugzSVGImage.render_debug/s]��48�;�;� �%��M�4 �4 �0�� �=� ���d�i�I�I�~�I�I�I�J�J�J��y�-�7�7rN) r r!r�str�__annotations__rrJr�rr�r�r#rrr�r��s�������� �I�I�I� �I�I�I� �I�I�I� �M�M�M� �N�N�N� ����  �  �  ��8�8���8�8�8�8���8�8�8rr�)r8)r<)Hr"�loggingrr��warnings�numbersr�typingr�fontTools.svgLib.pathr�fontTools.pens.basePenr�enumsr�defusedxml.ElementTreer rV� ImportError�warn�xml.etree.ElementTreerUr �drawingr r r rrrr�image_datastructuresrr�outputr� getLoggerr r�rrrV�compiler�rrErrArHrF�taurOrMrQrYr]rbr�rprurxr�r�r�r�r�r�r�rr�rJr�r#rr�<module>r�s�����#�"�"�"�"�"�"�"�"�"�"�"�"�"�"�"�������������,�,�,�,�,�,�*�*�*�*�*�*� � � � � � �B�B�B�B�B�B�B�B���B�B�B��H�M�c����B�A�A�A�A�A�A�A� B����������������������������>�=�=�=�=�=�=�=�3�3�3�3�3�3� �� �8� $� $�� �� &����� (� +���� �r�z�8�9�9� ��2�:�,���� �/�/�/�/�/�e�/�/���/��� �P�Q�Q� �����* � � � � � � � � �� �8�c�>� �H�s�N� � �H� �� ��V�V�V���V�(� Q� Q� Q��� Q��������������������\� �6�8�Y�� �I��� � � D� D��� D�������������� � ��� ��!4�!4��������/�/�)�)�)���)� J� I�T�T��� N�M��� ���� O�N�R�R��� ��Q,�,� �^�7�7���7�.�|�|�|�|�|�|�|���|�~�_�_���_�D4�4�4�4�4�g�4�4�4�n�?�?���?�S�S�S�S�S�S�S�S�l 88�88�88�88�88�z�88�88�88�88�88s�9�A�A
Memory