
    
3j<;                    J   S 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JrJr  SSKJr  SSS.r\R6                  " S5      S 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)BytesION)subset   )font_managerft2font)AFM)RendererBase   )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     ]/home/wildlama/miniconda3/lib/python3.13/site-packages/matplotlib/backends/_backend_pdf_ps.py_cached_get_afm_from_fnamer       s!    	eT	b2w 
		s   "
0c                   [         R                  " S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        U$ )aq  
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
-------
fontTools.ttLib.ttFont.TTFont
    An open font object representing the subset, which needs to
    be closed by the caller.
T)glyph_namesrecommended_glyphsretain_gids)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populate)fontfileglyphsr4   font	subsetters        r   get_glyphs_subsetrA   &   s    & nn$)-/G   . #--GH.D  1IF#TK    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_filerF   ]   s      
BIIbI&IrB   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)GlyphMapp   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 N_forward_inverseselfs    r   __init__GlyphMap.__init__{   s    =? 	 IK 	rB   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.
)rM   get)rP   	charcodesglyph_indexs      r   rT   GlyphMap.get   s     }}  )!9::rB   c                "    U R                   X4   $ )z>Get the inverse mapping from a (subset, subset charcode)-pair.)rN   )rP   r   subset_charcodes      r   igetGlyphMap.iget   s     }}f677rB   c                H    X44U R                   X4'   X4U R                  X44'   g)a^  
Add a mapping to this instance.

Parameters
----------
charcode : CharacterCodeType
    The character code to record.
glyph : 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.
NrL   )rP   charcoderV   r   rY   s        r   addGlyphMap.add   s,      391Jx-.4<3Jv/0rB   rL   N)returnNone)rU   strrV   r   r`   z$tuple[int, CharacterCodeType] | None)r   intrY   r   r`   ztuple[str, GlyphIndexType])
r]   rb   rV   r   r   rc   rY   r   r`   ra   )
__name__
__module____qualname____firstlineno____doc__rQ   rT   rZ   r^   __static_attributes__ rB   r   rH   rH   p   sO    K;';,P;8/84N8
K.K37KrB   rH   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)rP   rq   s     r   rQ   CharacterTracker.__init__   s     OQ	/1&rB   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charrV   )rP   r?   srt   ru   	raqm_items         r   trackCharacterTracker.track   sq    B "\\!W->->-I-I3; * P
P	 Y00)..)BWBW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#X5        X4$ )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   )
isinstancerb   ordchrr   r   r   r8   rp   
setdefaultrH   rT   ro   lenrq   appendr^   )rP   r?   charsglyphr]   	font_path	glyph_mapresultsubset_mapsuse_next_charmapr   rY   s               r   ry   CharacterTracker.track_glyph   sY   2 eS!!58}HHJE ))$**dooF	OO..y(*E	]]50060Mii**9rd; JN ! 	
 ,,, . 1~- 	 ;1$KO(<@P@P(P""2&%)F!+b/2OF&O/4FO,eF<((rB   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   )rp   rZ   )rP   fontnamer   rY   s       r   subset_to_unicode"CharacterTracker.subset_to_unicode/  s#    & x(--fFqIIrB   )rp   rq   ro   )r   )rq   rc   )NN)
r?   r   r|   rb   rt   ztuple[str, ...] | Noneru   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   rb   r   rc   rY   r   r`   rb   )
rd   re   rf   rg   rh   rQ   r~   ry   r   ri   rj   rB   r   rl   rl      sb    4
' 26HL#
.#
E#
 7#
J>))>).K>)@J+<JADJrB   rl   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$ )RendererPDFPSBaseiE  c                :   > [         TU ]  5         Xl        X l        g rK   )superrQ   widthheight)rP   r   r   	__class__s      r   rQ   RendererPDFPSBase.__init__J  s    
rB   c                    g)NFrj   rO   s    r   flipyRendererPDFPSBase.flipyO  s    rB   c                    g)NTrj   rO   s    r   option_scale_image$RendererPDFPSBase.option_scale_imageS  s    rB   c                2    [         R                  S   (       + $ )Nzimage.composite_image)mplrcParamsrO   s    r   option_image_nocomposite*RendererPDFPSBase.option_image_nocompositeW  s     << 7888rB   c                >    U R                   S-  U R                  S-  4$ )Ng      R@)r   r   rO   s    r   get_canvas_width_height)RendererPDFPSBase.get_canvas_width_height\  s    zzD $++"444rB   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)rP   propr?   scaleadgs          r   _get_font_height_metrics*RendererPDFPSBase._get_font_height_metrics`  sy    " ||D112#!!$''')D0%'!!E)UcMQwrB   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  pgpn
UR                  5       S-  nX-  nX-  n	X-  n
XU
4$ U R                  U5      nUR                  US[         R"                  R$                  S9  UR'                  5       u  pUR)                  5       n
SnX-  nX-  n	X-  n
XU
4$ )NTeXH   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   rw   rx   get_width_heightget_descent)rP   r|   r   ismathr   r?   lbwhr   r   r   s               r   r   /RendererPDFPSBase.get_text_width_height_descentz  s.   U?78&IIOO3399!RFE;;ekk99\\$//0%%d+D 99!<MA!++-4EJAJAJA7N%%d+DMM!S(9(9(D(DME((*DA  "AEJAJAJA7NrB   c                X    [         R                  " USU R                  S9n[        U5      $ )Nafm)fontext	directory)r   findfont_afm_font_dirr   )rP   r   r   s      r   r   RendererPDFPSBase._get_font_afm  s*    %%%4+=+=?)%00rB   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	getLoggerrd   warning)rP   r   fnamesr?   s       r   r   RendererPDFPSBase._get_font_ttf  s    ))>>tD		((0DJJLMM$113R8K 	h'//239; 		s   AA) )6B)r   r   )rd   re   rf   rg   rQ   r   r   r   r   r   r   r   r   ri   __classcell__)r   s   @r   r   r   E  s5    

9
5441
 rB   r   )r=   r   r>   zset[GlyphIndexType]r`   r   )"rh   
__future__r   ior   	functoolsr   typing	fontToolsr   
matplotlibr    r   r   _afmr	   backend_basesr
   TYPE_CHECKINGr   r   r   r   fontTools.ttLibr   _FONT_MAX_GLYPH	lru_cacher   rA   rF   rH   rl   r   rj   rB   r   <module>r      s    #       $  ( 
&CC&  R 
4n&0K 0Kf_J _JD_ _rB   