Ë
    ¸Ó^j°=  ã                  ó˜  — d 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	Z	ddl
mZ ddlZddlmZmZ dd	lmZ dd
lmZ e	j*                  r"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dœZ ej@                  d«      d„ «       Z! G d„ de	jD                  «      Z#	 	 	 	 	 	 d d„Z$d„ Z% G d„ d«      Z& G d„ d«      Z' G d„ de«      Z(y)!z7
Common functionality between the PDF and PS backends.
é    )Úannotations)Úcontextmanager)ÚBytesION)Úsubseté   )Úfont_managerÚft2font)ÚAFM)ÚRendererBase)Ú	Generator)ÚAbstractContextManageré   )ÚFontPath)ÚCharacterCodeTypeÚFT2FontÚGlyphIndexType)ÚTTFonté   i   )é   é*   é2   c                ó\   — t        | d«      5 }t        |«      cd d d «       S # 1 sw Y   y xY w)NÚrb)Úopenr
   )ÚfnameÚfhs     ún/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/matplotlib/backends/_backend_pdf_ps.pyÚ_cached_get_afm_from_fnamer   $   s*   € ä	ˆe�TÓ	ð ˜bÜ�2‹w÷÷ ò ús   �"¢+c                  ó8   — e Zd ZU dZded<   ded<   edd„«       Zy)	ÚSubsetResultsa  
    The results of a subsetting operation on a font.

    Attributes
    ----------
    font : TTFont
        An open font object representing the subset.
    glyph_index_map : dict
        Mapping of requested glyph indices to actual subset glyph indices.
    r   Úfontz$dict[GlyphIndexType, GlyphIndexType]Úglyph_index_mapc              #  óV   K  — | j                   5  | –— d d d «       y # 1 sw Y   y xY w­w©N)r!   ©Úselfs    r   Ú_as_cmzSubsetResults._as_cm9   s&   è ø€ à�Y‰Yñ 	ØŠJ÷	÷ 	ñ 	üs   ‚)�”	)�&¢)N)Úreturnz$Generator[SubsetResults, None, None])Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r'   © ó    r   r    r    *   s&   … ñ	ð ƒLØ9Ó9àòó ñr/   r    c                óf  — t        j                  dd¬«      }|xj                  g d¢z  c_        | j                  |_        t        j
                  | |«      }t        j                  |¬«      }|j                  |¬«       |j                  |«       t        ||j                  «      j                  «       S )ab  
    Subset a TTF font.

    Reads the named fontfile and restricts the font to the glyphs.

    Parameters
    ----------
    fontfile : FontPath
        Path to the font file
    glyphs : set[GlyphIndexType]
        Set of glyph indices to include in subset.

    Returns
    -------
    SubsetResults
        The font and new glyph index mapping.
    T)Úglyph_namesÚrecommended_glyphs)ÚFFTMÚPfEdÚBDFÚmetaÚMERGÚTSIVÚZapfÚbdatÚblocÚcidgÚfdscÚfeatÚfmtxÚfondÚjustÚkerxÚltagÚmorxÚtrakÚxref)Úoptions)Úgids)r   ÚOptionsÚdrop_tablesÚ
face_indexÚfont_numberÚ	load_fontÚ	SubsetterÚpopulater    r"   r'   )ÚfontfileÚglyphsrG   r!   Ú	subsetters        r   Úget_glyphs_subsetrS   ?   s™   € ô( �n‰n¨À$ÔG€Gð ×Òò ñ Õð. #×-Ñ-€GÔä×Ñ˜H gÓ.€DÜ× Ñ ¨Ô1€IØ×Ñ˜FÐÔ#Ø×Ñ�TÔÜ˜˜y×8Ñ8Ó9×@Ñ@ÓBÐBr/   c                ó@   — t        «       }| j                  |d¬«       |S )zï
    Convert a TTFont object into a file-like object.

    Parameters
    ----------
    font : fontTools.ttLib.ttFont.TTFont
        A font object

    Returns
    -------
    BytesIO
        A file object with the font saved into it
    F)ÚreorderTables)r   Úsave)r!   r   s     r   Úfont_as_filerW   v   s    € ô 
‹€BØ‡I�Iˆb €IÔ&Ø€Ir/   c                  óH   — e Zd ZdZdd„Z	 	 	 	 dd„Z	 	 	 	 d	d„Z	 	 	 	 d
d„Zy)ÚGlyphMapa  
    A two-way glyph mapping.

    The forward glyph map is from (character string, glyph index)-pairs to
    (subset index, subset character code)-pairs.

    The inverse glyph map is from to (subset index, subset character code)-pairs to
    (character string, glyph index)-pairs.
    c                ó    — i | _         i | _        y r$   ©Ú_forwardÚ_inverser%   s    r   Ú__init__zGlyphMap.__init__”   s   € à=?ð 	Œð IKð 	�r/   c                ó<   — | j                   j                  ||f«      S )z›
        Get the forward mapping from a (character string, glyph index)-pair.

        This may return *None* if the pair is not currently mapped.
        )r\   Úget)r&   Ú	charcodesÚglyph_indexs      r   r`   zGlyphMap.getš   s   € ð �}‰}× Ñ  )¨[Ð!9Ó:Ð:r/   c                ó$   — | j                   ||f   S )z>Get the inverse mapping from a (subset, subset charcode)-pair.)r]   )r&   r   Úsubset_charcodes      r   ÚigetzGlyphMap.iget£   s   € ð �}‰}˜f oÐ6Ñ7Ð7r/   c                óP   — ||f| j                   ||f<   ||f| j                  ||f<   y)aÄ  
        Add a mapping to this instance.

        Parameters
        ----------
        charcode : CharacterCodeType
            The character code to record.
        glyph_index : GlyphIndexType
            The corresponding glyph index to record.
        subset : int
            The subset in which the subset character code resides.
        subset_charcode : CharacterCodeType
            The subset character code within the above subset.
        Nr[   )r&   Úcharcoderb   r   rd   s        r   ÚaddzGlyphMap.add¨   s4   € ð  39¸/Ð1Jˆ�‰�x Ð-Ñ.Ø4<¸kÐ3Jˆ�‰�v˜Ð/Ò0r/   N)r(   ÚNone)ra   Ústrrb   r   r(   z$tuple[int, CharacterCodeType] | None)r   Úintrd   r   r(   ztuple[str, GlyphIndexType])
rg   rj   rb   r   r   rk   rd   r   r(   ri   )r)   r*   r+   r,   r^   r`   re   rh   r.   r/   r   rY   rY   ‰   sO   „ ñóKð;Ø'ð;Ø,Pó;ð8Ø/ð8Ø4Nó8ð
KØ.ðKØ37ôKr/   rY   c                  óR   — e Zd ZdZddd„Z	 	 d		 	 	 	 	 d
d„Z	 	 	 	 dd„Z	 	 	 	 dd„Zy)ÚCharacterTrackera}  
    Helper for font subsetting by the PDF and PS backends.

    Maintains a mapping of font paths to the set of characters and glyphs that are being
    used from that font.

    Attributes
    ----------
    subset_size : int
        The size at which characters are grouped into subsets.
    used : dict
        A dictionary of font files to character maps.

        The key is a font filename.

        The value is a list of dictionaries, each mapping at most *subset_size*
        character codes to glyph indices. Note this mapping is the inverse of FreeType,
        which maps glyph indices to character codes.

        If *subset_size* is not set, then there will only be one subset per font
        filename.
    glyph_maps : dict
        A dictionary of font files to glyph maps. You probably will want to use the
        `.subset_to_unicode` method instead of this attribute.
    c                ó.   — i | _         i | _        || _        y)zë
        Parameters
        ----------
        subset_size : int, optional
            The maximum size that is supported for an embedded font. If provided, then
            characters will be grouped into these sized subsets.
        N)ÚusedÚ
glyph_mapsÚsubset_size)r&   rq   s     r   r^   zCharacterTracker.__init__×   s   € ð OQˆŒ	Ø/1ˆŒØ&ˆÕr/   Nc                óà   — |j                  |t        j                  j                  ||¬«      D �cg c]3  }| j	                  |j
                  |j                  |j                  «      ‘Œ5 c}S c c}w )aá  
        Record that string *s* is being typeset using font *font*.

        Parameters
        ----------
        font : FT2Font
            A font that is being used for the provided string.
        s : str
            The string that should be marked as tracked by the provided font.
        features : tuple[str, ...], optional
            The font feature tags to use for the font.

            Available font feature tags may be found at
            https://learn.microsoft.com/en-us/typography/opentype/spec/featurelist
        language : str, optional
            The language of the text in a format accepted by libraqm, namely `a BCP47
            language code <https://www.w3.org/International/articles/language-tags/>`_.

        Returns
        -------
        list[tuple[int, CharacterCodeType]]
            A list of subset and character code pairs corresponding to the input string.
            If a *subset_size* is specified on this instance, then the character code
            will correspond with the given subset (and not necessarily the string as a
            whole). If *subset_size* is not specified, then the subset will always be 0
            and the character codes will be returned from the string unchanged.
        )ÚfeaturesÚlanguage)Ú_layoutr	   Ú	LoadFlagsÚ
NO_HINTINGÚtrack_glyphÚ	ft_objectÚcharrb   )r&   r!   Úsrs   rt   Ú	raqm_items         r   ÚtrackzCharacterTracker.trackã   sh   € ðB "Ÿ\™\¨!¬W×->Ñ->×-IÑ-IØ3;Àhð *ó Pö
àð ×Ñ˜Y×0Ñ0°)·.±.À)×BWÑBWÕXò
ð 	
ùò 
s   °8A+c                óÈ  — t        |t        «      rt        |d   «      }n|}t        |«      }t	        j
                  |j                  |j                  «      }| j                  j                  |t        «       «      }|j                  ||«      x}r|S | j                  j                  |i g«      }t        |«      dkD  xs) | j                  dk7  xr || j                  k\  xs ||d   v }	|	rWt        |«      dk(  st        |d   «      | j                  k(  r|j                  i «       t        |«      dz
  }
t        |d   «      }nd}
|}|||
   |<   |j!                  |||
|«       |
|fS )aÚ  
        Record character code *charcode* at glyph index *glyph* as using font *font*.

        Parameters
        ----------
        font : FT2Font
            A font that is being used for the provided string.
        chars : str or CharacterCodeType
            The character(s) to record. This may be a single character code, or multiple
            characters in a string, if the glyph maps to several characters. It will be
            normalized to a string internally.
        glyph : GlyphIndexType
            The corresponding glyph index to record.

        Returns
        -------
        subset : int
            The subset in which the returned character code resides. If *subset_size*
            was not specified on this instance, then this is always 0.
        subset_charcode : CharacterCodeType
            The character code within the above subset. If *subset_size* was not
            specified on this instance, then this is just *charcode* unmodified.
        r   r   éÿÿÿÿ)Ú
isinstancerj   ÚordÚchrr   r   r   rK   rp   Ú
setdefaultrY   r`   ro   Úlenrq   Úappendrh   )r&   r!   ÚcharsÚglyphrg   Ú	font_pathÚ	glyph_mapÚresultÚsubset_mapsÚuse_next_charmapr   rd   s               r   rx   zCharacterTracker.track_glyph  sd  € ô2 �eœSÔ!Ü˜5 ™8“}‰HàˆHÜ˜“JˆEä ×)Ñ)¨$¯*©*°d·o±oÓFˆ	Ø—O‘O×.Ñ.¨y¼(»*ÓEˆ	Ø—]‘] 5¨%Ó0Ð0ˆ6Ð0ØˆMà—i‘i×*Ñ*¨9°r°dÓ;ˆô �‹J˜‰Nò à×Ñ Ñ!ò 	ð
 ˜D×,Ñ,Ñ,ò .ð ˜{¨1™~Ð-ð 	ñ Ü�;Ó 1Ò$¬¨K¸©OÓ(<À×@PÑ@PÒ(PØ×"Ñ" 2Ô&Ü˜Ó%¨Ñ)ˆFÜ! +¨b¡/Ó2‰OàˆFØ&ˆOØ/4ˆ�FÑ˜OÑ,Ø�‰�e˜U F¨OÔ<Ø˜Ð(Ð(r/   c                óF   — | j                   |   j                  ||«      d   S )aþ  
        Map a subset index and character code to a Unicode character code.

        Parameters
        ----------
        fontname : str
            The name of the font, from the *used* dictionary key.
        subset : int
            The subset index within a font.
        subset_charcode : CharacterCodeType
            The character code within a subset to map back.

        Returns
        -------
        str
            The Unicode character(s) corresponding to the subsetted character code.
        r   )rp   re   )r&   Úfontnamer   rd   s       r   Úsubset_to_unicodez"CharacterTracker.subset_to_unicodeH  s%   € ð& �‰˜xÑ(×-Ñ-¨f°oÓFÀqÑIÐIr/   )r   )rq   rk   )NN)
r!   r   r{   rj   rs   ztuple[str, ...] | Nonert   z-str | tuple[tuple[str, int, int], ...] | Noner(   z#list[tuple[int, CharacterCodeType]])r!   r   r†   zstr | CharacterCodeTyper‡   r   r(   ztuple[int, CharacterCodeType])rŽ   rj   r   rk   rd   r   r(   rj   )r)   r*   r+   r,   r^   r}   rx   r�   r.   r/   r   rm   rm   ¼   sb   „ ñô4
'ð 26ØHLð#
Ø.ð#
àEð#
ð 7ó#
ðJ>)Ø)ð>)Ø.Kó>)ð@JØ+<ðJØADôJr/   rm   c                  óR   ‡ — e Zd Zˆ fd„Zd„ Zd„ Zd„ Zd„ Zd„ Zˆ fd„Z	d„ Z
d	„ Zˆ xZS )
ÚRendererPDFPSBasec                ó>   •— t         ‰| �  «        || _        || _        y r$   )Úsuperr^   ÚwidthÚheight)r&   r”   r•   Ú	__class__s      €r   r^   zRendererPDFPSBase.__init__c  s   ø€ Ü‰ÑÔØˆŒ
Øˆ�r/   c                 ó   — y)NFr.   r%   s    r   ÚflipyzRendererPDFPSBase.flipyh  s   € àr/   c                 ó   — y)NTr.   r%   s    r   Úoption_scale_imagez$RendererPDFPSBase.option_scale_imagel  s   € àr/   c                ó*   — t         j                  d    S )Nzimage.composite_image)ÚmplÚrcParamsr%   s    r   Úoption_image_nocompositez*RendererPDFPSBase.option_image_nocompositep  s   € ô —<‘<Ð 7Ñ8Ð8Ð8r/   c                ó>   — | j                   dz  | j                  dz  fS )Ng      R@)r”   r•   r%   s    r   Úget_canvas_width_heightz)RendererPDFPSBase.get_canvas_width_heightu  s   € à�z‰z˜DÑ  $§+¡+°Ñ"4Ð4Ð4r/   c                óî   — t         j                  | j                     sy| j                  |«      }|j	                  «       dz  }|j                  «       |z  }|j                  «        |z  }||z   dz  }|||fS )a  
        Return the ascent, descent, and line gap for font described by *prop*.

        TODO: This is a temporary method until we design a proper API for the backends.

        Parameters
        ----------
        prop : `.font_manager.FontProperties`
            The properties describing the font to measure.

        Returns
        -------
        ascent, descent, line_gap : float or None
            The ascent, descent and line gap of the determined font, or None to fall
            back to normal measurements.
        )NNNéè  gš™™™™™É?)rœ   r�   Ú_use_afm_rc_nameÚ_get_font_afmÚget_size_in_pointsÚget_ascenderÚget_descender)r&   Úpropr!   ÚscaleÚaÚdÚgs          r   Ú_get_font_height_metricsz*RendererPDFPSBase._get_font_height_metricsy  s}   € ô" �|‰|˜D×1Ñ1Ò2Ø#Ø×!Ñ! $Ó'ˆØ×'Ñ'Ó)¨DÑ0ˆØ×ÑÓ %Ñ'ˆØ×ÑÓ!Ð! EÑ)ˆØ�‰U�c‰MˆØ�!�Qˆwˆr/   c                óŒ  •— |dk(  rt         ‰| �  |||«      S |rJ| j                  j                  j	                  |d|«      }|j
                  |j                  |j                  fS t        j                  | j                     rO| j                  |«      }|j                  |«      \  }}}}	}
|j                  «       dz  }||z  }|	|z  }	|
|z  }
||	|
fS | j                  |«      }|j                  |dt         j"                  j$                  ¬«       |j'                  «       \  }}	|j)                  «       }
d}||z  }|	|z  }	|
|z  }
||	|
fS )NÚTeXéH   r¢   g        )Úflagsg      �?)r“   Úget_text_width_height_descentÚ
_text2pathÚmathtext_parserÚparser”   r•   Údepthrœ   r�   r£   r¤   Úget_str_bbox_and_descentr¥   Ú_get_font_ttfÚset_textr	   rv   rw   Úget_width_heightÚget_descent)r&   r{   r¨   Úismathrµ   r!   ÚlÚbÚwÚhr«   r©   r–   s               €r   r²   z/RendererPDFPSBase.get_text_width_height_descent“  sF  ø€ à�UŠ?Ü‘7Ñ8¸¸DÀ&ÓIÐIÙØ—O‘O×3Ñ3×9Ñ9¸!¸RÀÓFˆEØ—;‘; §¡¨e¯k©kÐ9Ð9Ü�\‰\˜$×/Ñ/Ò0Ø×%Ñ% dÓ+ˆDØ ×9Ñ9¸!Ó<‰MˆAˆq�!�Q˜Ø×+Ñ+Ó-°Ñ4ˆEØ�‰JˆAØ�‰JˆAØ�‰JˆAØ�a˜�7ˆNà×%Ñ% dÓ+ˆDØ�M‰M˜!˜S¬×(9Ñ(9×(DÑ(DˆMÔEØ×(Ñ(Ó*‰DˆAˆqØ× Ñ Ó"ˆAØˆEØ�‰JˆAØ�‰JˆAØ�‰JˆAØ�a˜�7ˆNr/   c                ó\   — t        j                  |d| j                  ¬«      }t        |«      S )NÚafm)ÚfontextÚ	directory)r   ÚfindfontÚ_afm_font_dirr   )r&   r¨   r   s      r   r¤   zRendererPDFPSBase._get_font_afm­  s*   € Ü×%Ñ%Ø˜%¨4×+=Ñ+=ô?ˆä)¨%Ó0Ð0r/   c                ó>  — t         j                  j                  |«      }	 t        j                  |«      }|j	                  «        |j                  |j                  «       d«       |S # t        $ r+ t        j                  t        «      j                  d|«       ‚ w xY w)Nr°   zMThe PostScript/PDF backend does not currently support the selected font (%s).)r   ÚfontManagerÚ_find_fonts_by_propsÚget_fontÚclearÚset_sizer¥   ÚRuntimeErrorÚloggingÚ	getLoggerr)   Úwarning)r&   r¨   Úfnamesr!   s       r   r¸   zRendererPDFPSBase._get_font_ttf²  s‡   € Ü×)Ñ)×>Ñ>¸tÓDˆð		Ü×(Ñ(¨Ó0ˆDØ�J‰JŒLØ�M‰M˜$×1Ñ1Ó3°RÔ8ØˆKøÜò 	Ü×ÑœhÓ'×/Ñ/ð2Ø39ô;ð ð		ús   ¡AA( Á(4B)r)   r*   r+   r^   r˜   rš   rž   r    r­   r²   r¤   r¸   Ú__classcell__)r–   s   @r   r‘   r‘   ^  s0   ø„ ô
ò
òò9ò
5òô4ò41ö
r/   r‘   )rP   r   rQ   zset[GlyphIndexType]r(   z%AbstractContextManager[SubsetResults]))r,   Ú
__future__r   Ú
contextlibr   Úior   Ú	functoolsrÎ   ÚtypingÚ	fontToolsr   Ú
matplotlibrœ   Ú r   r	   Ú_afmr
   Úbackend_basesr   ÚTYPE_CHECKINGÚcollections.abcr   r   r   r   r   r   ÚfontTools.ttLibr   Ú_FONT_MAX_GLYPHÚ	lru_cacher   Ú
NamedTupler    rS   rW   rY   rm   r‘   r.   r/   r   ú<module>rã      sÝ   ðñõ #å %Ý Û Û Û å ã ß $Ý Ý (ð 
×ÒÝ)Ý1å&ßCÑCÝ&ð Øñ€ð €×Ñ�RÓñó ðô
�F×%Ñ%ô ð*4CØð4CØ 3ð4Cà*ó4Còn÷&0Kñ 0K÷f_Jñ _JôD_˜õ _r/   