ó
    “’¤j°=  ã                  ó¤  • S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSK	r	SSK
Jr  SSKrSSKJrJr  SS	KJr  SS
KJr  \	R*                  (       a"  SSKJr  SSKJr  SSKJr  SSKJrJrJr  SSKJr  SSS.r\R@                  " S5      S 5       r! " S S\	RD                  5      r#      S S jr$S r% " S S5      r& " S S5      r' " S S\5      r(g)!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                óf   • [        U S5       n[        U5      sS S S 5        $ ! , (       d  f       g = f)NÚrb)Úopenr
   )ÚfnameÚfhs     Ú±C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\matplotlib/backends/_backend_pdf_ps.pyÚ_cached_get_afm_from_fnamer   $   s!   € ä	ˆe�TÔ	˜bÜ�2‹w÷ 
×	×	ús   �"¢
0c                  óB   • \ rS rSr% SrS\S'   S\S'   \S
S j5       rSrg	)ÚSubsetResultsr   zæ
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              #  ó`   #   • U R                      U v •  S S S 5        g ! , (       d  f       g = f7f©N)r!   ©Úselfs    r   Ú_as_cmÚSubsetResults._as_cm9   s   é € à�Y‹YØŠJ÷ �YŽYüs   ‚.�”	.�
+§.© N)Úreturnz$Generator[SubsetResults, None, None])	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   r'   Ú__static_attributes__r)   ó    r   r    r    *   s&   ‡ ñ	ð ƒLØ9Ó9àóó ór2   r    c                óV  • [         R                  " SSS9nU=R                  / SQ-  sl        U R                  Ul        [         R
                  " X5      n[         R                  " US9nUR                  US9  UR                  U5        [        X4R                  5      R                  5       $ )a.  
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ÚglyphsrJ   r!   Ú	subsetters        r   Úget_glyphs_subsetrV   ?   s•   € ô( �nŠn¨À$ÑG€Gð ×Òò ñ Õð. #×-Ñ-€GÔä×Ò˜HÓ.€DÜ× Ò ¨Ñ1€IØ×Ñ˜FÐÑ#Ø×Ñ�TÔÜ˜×8Ñ8Ó9×@Ñ@ÓBÐBr2   c                ó:   • [        5       nU R                  USS9  U$ )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_filerZ   v   s    € ô 
‹€BØ‡I�Iˆb €IÑ&Ø€Ir2   c                  óX   • \ rS rSrSrS	S jr    S
S jr    SS jr    SS jrSr	g)ÚGlyphMapé‰   a  
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                ó    • 0 U l         0 U l        g r$   ©Ú_forwardÚ_inverser%   s    r   Ú__init__ÚGlyphMap.__init__”   s   € à=?ð 	Œð IKð 	�r2   c                ó:   • U R                   R                  X45      $ )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   re   ÚGlyphMap.getš   s   € ð �}‰}× Ñ  )Ð!9Ó:Ð:r2   c                ó"   • U R                   X4   $ )z>Get the inverse mapping from a (subset, subset charcode)-pair.)ra   )r&   r   Úsubset_charcodes      r   ÚigetÚGlyphMap.iget£   s   € ð �}‰}˜fÐ6Ñ7Ð7r2   c                óH   • X44U R                   X4'   X4U R                  X44'   g)ad  
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&   Úcharcoderg   r   rj   s        r   ÚaddÚGlyphMap.add¨   s,   € ð  39Ð1Jˆ�‰�xÐ-Ñ.Ø4<Ð3Jˆ�‰�vÐ/Ò0r2   r_   N)r*   ÚNone)rf   Ústrrg   r   r*   z$tuple[int, CharacterCodeType] | None)r   Úintrj   r   r*   ztuple[str, GlyphIndexType])
rn   rr   rg   r   r   rs   rj   r   r*   rq   )
r+   r,   r-   r.   r/   rb   re   rk   ro   r1   r)   r2   r   r\   r\   ‰   sO   † ñôKð;Ø'ð;Ø,Pô;ð8Ø/ð8Ø4Nô8ð
KØ.ðKØ37÷Kr2   r\   c                  óf   • \ rS rSrSrS	S
S jjr  S     SS jjr    SS jr    SS jrSr	g)ÚCharacterTrackeré¼   a1  
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                ó,   • 0 U l         0 U l        Xl        g)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&   rz   s     r   rb   ÚCharacterTracker.__init__×   s   € ð OQˆŒ	Ø/1ˆŒØ&Õr2   Nc                óÞ   • UR                  U[        R                  R                  X4S9 Vs/ s H4  nU R	                  UR
                  UR                  UR                  5      PM6     sn$ s  snf )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Úcharrg   )r&   r!   Úsr}   r~   Ú	raqm_items         r   ÚtrackÚCharacterTracker.trackã   sq   € ðB "Ÿ\™\¨!¬W×->Ñ->×-IÑ-IØ3;ð *ñ Pó
òP�	ð ×Ñ˜Y×0Ñ0°)·.±.À)×BWÑBWÖXñPñ
ð 	
ùò 
s   ¬;A*c                óþ  • [        U[        5      (       a  [        US   5      nOUn[        U5      n[        R
                  " UR                  UR                  5      nU R                  R                  U[        5       5      nUR                  X#5      =n(       a  U$ U R                  R                  U0 /5      n[        U5      S:„  =(       d2    U R                  S:g  =(       a    X@R                  :¬  =(       d    XHS   ;   n	U	(       aY  [        U5      S:X  d  [        US   5      U R                  :X  a  UR                  0 5        [        U5      S-
  n
[        US   5      nOSn
UnX8U
   U'   UR!                  X#X«5        X«4$ )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   éÿÿÿÿ)Ú
isinstancerr   ÚordÚchrr   r   r   rN   ry   Ú
setdefaultr\   re   rx   Úlenrz   Úappendro   )r&   r!   ÚcharsÚglyphrn   Ú	font_pathÚ	glyph_mapÚresultÚsubset_mapsÚuse_next_charmapr   rj   s               r   r‚   ÚCharacterTracker.track_glyph  sY  € ô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÷ à×Ñ Ñ!÷ 	ð
 ×,Ñ,Ñ,÷ .ð ¨1™~Ñ-ð 	ö Ü�;Ó 1Ó$¬¨K¸©OÓ(<À×@PÑ@PÓ(PØ×"Ñ" 2Ô&Ü˜Ó%¨Ñ)ˆFÜ! +¨b¡/Ó2‰OàˆFØ&ˆOØ/4�FÑ˜OÑ,Ø�‰�e FÔ<ØÐ(Ð(r2   c                óD   • U R                   U   R                  X#5      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   )ry   rk   )r&   Úfontnamer   rj   s       r   Úsubset_to_unicodeÚ"CharacterTracker.subset_to_unicodeH  s#   € ð& �‰˜xÑ(×-Ñ-¨fÓFÀqÑIÐIr2   )ry   rz   rx   )r   )rz   rs   )NN)
r!   r   r…   rr   r}   ztuple[str, ...] | Noner~   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š   rr   r   rs   rj   r   r*   rr   )
r+   r,   r-   r.   r/   rb   r‡   r‚   r›   r1   r)   r2   r   ru   ru   ¼   sb   † ñö4
'ð 26ØHLð#
Ø.ð#
àEð#
ð 7õ#
ðJ>)Ø)ð>)Ø.Kô>)ð@JØ+<ðJØAD÷Jr2   ru   c                  ó^   ^ • \ rS rSrU 4S jrS rS rS rS rS r	U 4S jr
S	 rS
 rSrU =r$ )ÚRendererPDFPSBasei^  c                ó:   >• [         TU ]  5         Xl        X l        g r$   )Úsuperrb   ÚwidthÚheight)r&   r¡   r¢   Ú	__class__s      €r   rb   ÚRendererPDFPSBase.__init__c  s   ø€ Ü‰ÑÔØŒ
Ø�r2   c                ó   • g)NFr)   r%   s    r   ÚflipyÚRendererPDFPSBase.flipyh  s   € àr2   c                ó   • g)NTr)   r%   s    r   Úoption_scale_imageÚ$RendererPDFPSBase.option_scale_imagel  s   € àr2   c                ó2   • [         R                  S   (       + $ )Nzimage.composite_image)ÚmplÚrcParamsr%   s    r   Úoption_image_nocompositeÚ*RendererPDFPSBase.option_image_nocompositep  s   € ô —<‘<Ð 7Ñ8Ô8Ð8r2   c                ó>   • U R                   S-  U R                  S-  4$ )Ng      R@)r¡   r¢   r%   s    r   Úget_canvas_width_heightÚ)RendererPDFPSBase.get_canvas_width_heightu  s   € à�z‰z˜DÑ  $§+¡+°Ñ"4Ð4Ð4r2   c                óô   • [         R                  U R                     (       d  gU R                  U5      nUR	                  5       S-  nUR                  5       U-  nUR                  5       * U-  nXE-   S-  nXEU4$ )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_metricsÚ*RendererPDFPSBase._get_font_height_metricsy  sy   € ô" �|‰|˜D×1Ñ1×2Ø#Ø×!Ñ! $Ó'ˆØ×'Ñ'Ó)¨DÑ0ˆØ×ÑÓ %Ñ'ˆØ×ÑÓ!Ð! EÑ)ˆØ‰U�c‰MˆØ�Qˆwˆr2   c                ó„  >• US:X  a  [         TU ]  XU5      $ U(       aJ  U R                  R                  R	                  USU5      nUR
                  UR                  UR                  4$ [        R                  U R                     (       aI  U R                  U5      nUR                  U5      u  pgp‰n
UR                  5       S-  nX‹-  nX›-  n	X«-  n
X‰U
4$ U R                  U5      nUR                  US[         R"                  R$                  S9  UR'                  5       u  p‰UR)                  5       n
SnX‹-  nX›-  n	X«-  n
X‰U
4$ )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	   r€   r�   Úget_width_heightÚget_descent)r&   r…   rº   ÚismathrÈ   r!   ÚlÚbÚwÚhr½   r»   r£   s               €r   rÅ   Ú/RendererPDFPSBase.get_text_width_height_descent“  s.  ø€ à�U‹?Ü‘7Ñ8¸À&ÓIÐIÞØ—O‘O×3Ñ3×9Ñ9¸!¸RÀÓFˆEØ—;‘; §¡¨e¯k©kÐ9Ð9Ü�\‰\˜$×/Ñ/×0Ø×%Ñ% dÓ+ˆDØ ×9Ñ9¸!Ó<‰MˆA�!˜Ø×+Ñ+Ó-°Ñ4ˆEØ‰JˆAØ‰JˆAØ‰JˆAØ˜�7ˆNà×%Ñ% dÓ+ˆDØ�M‰M˜!˜S¬×(9Ñ(9×(DÑ(DˆMÑEØ×(Ñ(Ó*‰DˆAØ× Ñ Ó"ˆAØˆEØ‰JˆAØ‰JˆAØ‰JˆAØ˜�7ˆNr2   c                óX   • [         R                  " USU R                  S9n[        U5      $ )NÚafm)ÚfontextÚ	directory)r   ÚfindfontÚ_afm_font_dirr   )r&   rº   r   s      r   r¶   ÚRendererPDFPSBase._get_font_afm­  s*   € Ü×%Ò%Ø˜%¨4×+=Ñ+=ñ?ˆä)¨%Ó0Ð0r2   c                óD  • [         R                  R                  U5      n [         R                  " U5      nUR	                  5         UR                  UR                  5       S5        U$ ! [         a,    [        R                  " [        5      R                  SU5        e f = f)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Ë   Ú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) Á)6B)r¢   r¡   )r+   r,   r-   r.   rb   r¦   r©   r®   r±   r¿   rÅ   r¶   rË   r1   Ú__classcell__)r£   s   @r   rž   rž   ^  s5   ø† õ
ò
òò9ò
5òõ4ò41÷
ð r2   rž   )rS   r   rT   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    rV   rZ   r\   ru   rž   r)   r2   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_˜õ _r2   