
    
3j@              	          S r SSKrSSKrSSKrSSKJrJrJrJr  SSK	J
r
  SSKJrJr  \R                  " \5      rS\\-  S\4S	 jrS\\-  S\4S
 jrS\S\4S jrS\S\\   4S jrS\\-  S\\   4S jrS\S\4S jr " S S\SS9rS\S\4S jr " S S\5      rS\R@                  l         S\RB                  l         S\RD                  l         S\S\#\$\\4   \$\\4   4   4S jr%S\S\$\#\\4   \4   4S jr& " S S\5      r'S\'RB                  l         S \'RP                  l         S!\'RR                  l         S\S\$\\\'   4   4S" jr*S\S\#\$\#\\4   \4   \$\\\'   4   4   4S# jr+ " S$ S%5      r,g)&a  
A Python interface to Adobe Font Metrics Files.

Although a number of other Python implementations exist, and may be more
complete than this, it was decided not to go with them because they were
either:

1) copyrighted or used a non-BSD compatible license
2) had too many dependencies and a free standing lib was needed
3) did more than needed and it was easier to write afresh rather than
   figure out how to get just what was needed.

It is pretty easy to use, and has no external dependencies:

>>> import matplotlib as mpl
>>> from pathlib import Path
>>> afm_path = Path(mpl.get_data_path(), 'fonts', 'afm', 'ptmr8a.afm')
>>>
>>> from matplotlib._afm import AFM
>>> with afm_path.open('rb') as fh:
...     afm = AFM(fh)
>>> afm.get_fontname()
'Times-Roman'

As in the Adobe Font Metrics File Format Specification, all dimensions
are given in units of 1/1000 of the scale factor (point size) of the font
being used.
    N)BinaryIO
NamedTuple	TypedDictcast   )	uni2type1)CharacterCodeTypeGlyphIndexTypexreturnc                 *    [        [        U 5      5      $ N)intfloatr   s    I/home/wildlama/miniconda3/lib/python3.13/site-packages/matplotlib/_afm.py_to_intr   *   s     uQx=    c                     [        U [        5      (       a  U R                  S5      n [        U R	                  SS5      5      $ )Nzlatin-1,.)
isinstancebytesdecoder   replacer   s    r   	_to_floatr   3   s7     !U HHY3$%%r   c                 $    U R                  S5      $ )Nutf8)r   r   s    r   _to_strr   >   s    88Fr   sc                     U R                  SS5      n U R                  5        Vs/ s H  n[        U5      PM     sn$ s  snf )N   ,    )r   splitr   r    vals     r   _to_list_of_intsr'   B   s4    			$A$%GGI.ISGCLI...s   =c                 `    U R                  5        Vs/ s H  n[        U5      PM     sn$ s  snf r   )r$   r   r%   s     r   _to_list_of_floatsr)   G   s$    &'ggi0isIcNi000s   +c                 J    U R                  5       R                  5       S;   a  gg)N)s   false   0s   noFT)lowerstrip)r    s    r   _to_boolr.   K   s    wwy33r   c                      \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\	   \S	'   \\S
'   \\S'   \\S'   \
\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \	\S'   \\S'   \	\S'   Srg)FontMetricsHeaderR   StartFontMetricsFontNameFullName
FamilyNameWeightItalicAngleIsFixedPitchFontBBoxUnderlinePositionUnderlineThicknessVersionNoticeEncodingScheme	CapHeight	CapheightXHeightAscender	DescenderStdHWStdVWStartCharMetricsCharacterSet
Characters N)__name__
__module____qualname____firstlineno__r   __annotations__strboollistr   r   __static_attributes__rI   r   r   r0   r0   R   s    MMOK3iL MNOLLOr   r0   F)totalfhc                    [         [        [        S [        [        [
        [        [        [
           [        [        [        0n[        R                  " [        5      n0 nSnU  H  nUR                  5       nUR                  S5      (       a  M+  UR!                  SS5      nUS   nU(       a  US:w  a  [#        S5      eS	n[%        U5      S
:X  a  US   nOSn [        U5      n	X)   n
 X   nU" U5      X9'   US:X  d  M    U$    [#        S5      e! [&        [(        4 a    [*        R-                  SU5         M  f = f! [.         a    [*        R-                  SXx5         M  f = f)aH  
Read the font metrics header (up to the char metrics).

Returns
-------
dict
    A dictionary mapping *key* to *val*. Dictionary keys are:

        StartFontMetrics, FontName, FullName, FamilyName, Weight, ItalicAngle,
        IsFixedPitch, FontBBox, UnderlinePosition, UnderlineThickness, Version,
        Notice, EncodingScheme, CapHeight, XHeight, Ascender, Descender,
        StartCharMetrics

    *val* will be converted to the appropriate Python type as necessary, e.g.,:

        * 'False' -> False
        * '0' -> 0
        * '-168 -218 1000 898' -> [-168, -218, 1000, 898]
c                     U $ r   rI   r   s    r   <lambda>_parse_header.<locals>.<lambda>   s    r   Ts   Commentr#   r   r   s   StartFontMetricszNot an AFM fileF   r   z/Found an unknown keyword in AFM header (was %r)z)Value error parsing header in AFM: %r, %rs   StartCharMetrics	Bad parse)rP   r.   r   r   r   r   r   rQ   r'   rO   r   inspectget_annotationsr0   rstrip
startswithr$   RuntimeErrorlenKeyErrorUnicodeDecodeError_logerror
ValueError)rT   header_convertersheader_value_typesd
first_linelinelstkeyr&   key_str
value_type	converters               r   _parse_headerrp   o   sb   * 	h{yWS	#W !001BCAJ{{}??:&&jjq!!f ))"#455Js8q=a&CC	clG+4J	)5I"3AJ %% HG D ;'' ,- 	JJH#N	  	JJBCM	s$   D'D:&D76D7: EEc                   F    \ rS rSr% Sr\\S'   \\S'   \\	\	\	\	4   \S'   Sr
g)CharMetrics   z
Represents the character metrics of a single character.

Notes
-----
The fields do currently only describe a subset of character metrics
information defined in the AFM standard.
widthnamebboxrI   N)rJ   rK   rL   rM   __doc__r   rN   rO   tupler   rR   rI   r   r   rr   rr      s'     L
I
S#s"
##r   rr   zThe character width (WX).zThe character name (N).zK
    The bbox of the character (B) as a tuple (*llx*, *lly*, *urx*, *ury*).c           
      r   1 Skn0 n0 nU  GH  n[        UR                  5       5      nUR                  S5      (       a  X#4s  $ [        S UR	                  S5       5       5      nUR                  U5      (       d  [        SU-  5      e[        US   5      n[        US   5      nUS   n	[        [        [        [        US	   5      5      5      n
[        U
5      S
:w  a  [        S[        U
5       S35      e[        XU
5      nU	S:X  a  SnOU	S:X  a  [        S5      nUS:w  a  XU'   XU	'   GM!     [        S5      e)a  
Parse the given filehandle for character metrics information.

It is assumed that the file cursor is on the line behind 'StartCharMetrics'.

Returns
-------
ascii_d : dict
     A mapping "ASCII num of the character" to `.CharMetrics`.
name_d : dict
     A mapping "character name" to `.CharMetrics`.

Notes
-----
This function is incomplete per the standard, but thus far parses
all the sample afm files tried.
>   BCNWXEndCharMetricsc              3   r   #    U  H-  o(       d  M  UR                  5       R                  S S5      v   M/     g7f) r   N)r-   r$   ).0r    s     r   	<genexpr>&_parse_char_metrics.<locals>.<genexpr>   s)     J_+AGGIOOC++_s   
7'7;zBad char metrics line: %sr{   r}   r|   rz      zBad parse: bbox has z elements, should be 4Euro   minusu   −rZ   )r   r]   r^   dictr$   issubsetr_   r   r   rx   mapr   r)   r`   rr   ord)rT   required_keysascii_dname_dblinerj   valsnumwxru   rv   metricss               r   _parse_char_metricsr      s4   & *M46G%'F u||~&??+,,?"JTZZ_JJ%%d++:TABBd3i tDz"CyS0c;<=t9>!5c$i[@VWXXb- 6>CW_&'C"9"CLt7 8 {
##r   c                    [        U 5      nUR                  S5      (       d  [        SU< 35      e0 nU  H  nUR                  5       nU(       d  M  UR                  S5      (       a  [        U 5        Us  $ UR	                  5       n[        U5      S:w  d	  US   S:w  a  [        SU< 35      e[        US   5      [        US	   5      [        US
   5      penXbXE4'   M     [        S5      e)z
Return a kern pairs dictionary.

Returns
-------
dict
    Keys are (*char1*, *char2*) tuples and values are the kern pair value. For
    example, a kern pairs line like ``KPX A y -50`` will be represented as::

        d['A', 'y'] = -50
s   StartKernPairszBad start of kern pairs data: s   EndKernPairsr   r   s   KPXzBad kern pairs line: r   rY      zBad kern pairs parse)nextr^   r_   r]   r$   r`   r   r   )rT   rj   rh   r   c1c2r&   s          r   _parse_kern_pairsr      s     8D??,--;D8DEE&(A{{}???++HHzz|t9>T!W.!6th?@@d1g&Q(8)DG:L2(  -
..r   c                   8    \ rS rSr% Sr\\S'   \\S'   \\S'   Srg)CompositeParti  zFRepresents the information on a composite element of a composite char.ru   dxdyrI   N)	rJ   rK   rL   rM   rw   r   rN   r   rR   rI   r   r   r   r     s    P
KIIr   r   zName of the part, e.g. 'acute'.z+x-displacement of the part from the origin.z+y-displacement of the part from the origin.c           
         0 nU  H  nUR                  5       nU(       d  M  UR                  S5      (       a  Us  $ UR                  S5      nUS   R                  5       nUS   [        US   5      pe[	        U5      US-   :w  a  [        SU S[	        U5      S-
   35      e/ nUSS  HL  nUR                  5       n	[        U	S   [        U	S   5      [        U	S	   5      5      n
UR                  U
5        MN     XqU'   M     [        S
5      e)aE  
Parse the given filehandle for composites information.

It is assumed that the file cursor is on the line behind 'StartComposites'.

Returns
-------
dict
    A dict mapping composite character names to a parts list. The parts
    list is a list of `.CompositePart` entries describing the parts of
    the composite.

Examples
--------
A composite definition line::

  CC Aacute 2 ; PCC A 0 0 ; PCC acute 160 170 ;

will be represented as::

  composites[b'Aacute'] = [CompositePart(name=b'A', dx=0, dy=0),
                           CompositePart(name=b'acute', dx=160, dy=170)]

s   EndComposites   ;r   r   rY   zBad composites parse: expected z parts, but got r   r   zBad composites parse)	r]   r^   r$   r   r`   r_   r   r   append)rT   
compositesrj   r   ccru   
_num_partspccPartsr    pccparts              r   _parse_compositesr   ,  s   2 46J{{}??+,,zz$!W]]_a5'"Q%.jt9
Q&!@ M**-d)a- : ; ;aA'')C Q3q6):Ic!f<MNDOOD!  $4# & -
..r   c                     0 n0 nU  HT  nUR                  5       nU(       d  M  UR                  5       S   =S:X  a    [        U 5      nMB  S:X  d  MI  [        U 5      nMV     X4$ )a"  
Parse the optional fields for kern pair data and composites.

Returns
-------
kern_data : dict
    A dict containing kerning information. May be empty.
    See `._parse_kern_pairs`.
composites : dict
    A dict containing composite information. May be empty.
    See `._parse_composites`.
r   s   StartKernDatas   StartComposites)r]   r$   r   r   )rT   	kern_datar   rj   s       r   _parse_optionalr   \  s[     /1I35J{{}jjl1o!-b1	#.r2
    r   c            	       ^   \ rS rSrS\4S jrS\S\\\\	\\4   4S jr
S\S\4S jrS	\S\4S
 jrS	\S\	4S jrS\S\	4S jrS\S\S\	4S jrS\4S jr\S\4S j5       rS\4S jrS\4S jr\S\4S j5       rS\4S jrS\	4S jrS\	4S jrS\	4S jrS\	4S jrS\	4S jrS\	4S jrSrg)AFMiy  rT   c                     [        U5      U l        [        U5      u  U l        U l        [        U5      u  U l        U l        g)z'Parse the AFM file in file object *fh*.N)rp   _headerr   _metrics_metrics_by_namer   _kern
_composite)selfrT   s     r   __init__AFM.__init__{  s5    $R(/B2/F,t,&5b&9#
DOr   r    r   c                    [        U5      (       d  gSnSnSnSnSnU H  nUS:X  a  M  [        R                  " [        U5      S[        U5      S 35      n U R                  U   u  pnX)U R                  R                  X84S5      -   -  nUu  pp[        Xl5      n[        XM5      n[        X]U-   5      nUnM     XdX%U-
  U* 4$ ! [
         a    S	nU R                  U   u  pn Nzf = f)
z7Return the string bounding box and the maximal descent.)r   r   r   r   r   g         i ʚ;r   
uni04Xquestion)	r`   r   getr   r   ra   r   minmax)r   r    total_widthnamelastminymaxyleftcru   r   _rv   lbwhs                   r   get_str_bbox_and_descentAFM.get_str_bbox_and_descent  s   1vv ADy==Q3s1vcl);<D:"33D9t 

/? CCCKJA!t<Dt<DtU#DH " ;tdU::  :!"33D9t:s   CC$#C$	glyph_indc                 P    U R                   [        [        U5         R                  $ )z9Get the name of the glyph, i.e., ord(';') is 'semicolon'.)r   r   r	   ru   )r   r   s     r   get_glyph_nameAFM.get_glyph_name  s!     }}T"3Y?@EEEr   r   c                 "    [        [        U5      $ )z
Return the glyph index corresponding to a character code point.

Note, for AFM fonts, we treat the glyph index the same as the codepoint.
)r   r
   r   r   s     r   get_char_indexAFM.get_char_index  s     NA&&r   c                 4    U R                   U   R                  $ )zGGet the width of the character code from the character metric WX field.)r   rt   r   s     r   get_width_charAFM.get_width_char  s    }}Q%%%r   ru   c                 4    U R                   U   R                  $ )z;Get the width of the character from a type1 character name.)r   rt   r   ru   s     r   get_width_from_char_nameAFM.get_width_from_char_name  s    $$T*000r   name1name2c                 <    U R                   R                  X4S5      $ )zN
Return the kerning pair distance (possibly 0) for chars *name1* and *name2*.
r   )r   r   )r   r   r   s      r   get_kern_dist_from_nameAFM.get_kern_dist_from_name  s     zz~~una00r   c                      U R                   S   $ )z*Return the font name, e.g., 'Times-Roman'.r3   r   r   s    r   get_fontnameAFM.get_fontname      ||J''r   c                 "    U R                  5       $ r   )r   r   s    r   postscript_nameAFM.postscript_name  s      ""r   c                 `    U R                   R                  S5      nUc  U R                   S   nU$ )z/Return the font full name, e.g., 'Times-Roman'.r4   r3   )r   r   r   s     r   get_fullnameAFM.get_fullname  s.    ||
+<<<
+Dr   c                     U R                   R                  S5      nUb  U$ U R                  5       nSn[        R                  " USU5      $ )z+Return the font family name, e.g., 'Times'.r5   zY(?i)([ -](regular|plain|italic|oblique|bold|semibold|light|ultralight|extra|condensed))+$r   )r   r   r   resub)r   ru   extrass      r   get_familynameAFM.get_familyname  sK    ||-K   ":vvfb$''r   c                 "    U R                  5       $ )z$The font family name, e.g., 'Times'.)r   r   s    r   family_nameAFM.family_name  s     ""$$r   c                      U R                   S   $ )z0Return the font weight, e.g., 'Bold' or 'Roman'.r6   r   r   s    r   
get_weightAFM.get_weight  s    ||H%%r   c                      U R                   S   $ )zReturn the fontangle as float.r7   r   r   s    r   	get_angleAFM.get_angle  s    ||M**r   c                      U R                   S   $ )zReturn the ascent as float.rB   r   r   s    r   get_ascenderAFM.get_ascender  r   r   c                      U R                   S   $ )zReturn the cap height as float.r?   r   r   s    r   get_capheightAFM.get_capheight      ||K((r   c                      U R                   S   $ )zReturn the descent as float.rC   r   r   s    r   get_descenderAFM.get_descender  r  r   c                      U R                   S   $ )zReturn the xheight as float.rA   r   r   s    r   get_xheightAFM.get_xheight  s    ||I&&r   c                      U R                   S   $ )z(Return the underline thickness as float.r;   r   r   s    r   get_underline_thicknessAFM.get_underline_thickness  s    ||011r   )r   r   r   r   r   N) rJ   rK   rL   rM   r   r   rO   rx   r   r   r   r
   r   r	   r   r   r   r   r   propertyr   r   r   r   r   r   r   r  r  r	  r  rR   rI   r   r   r   r   y  sM   :8 :;# ;%S%c8Q2R ;8F"0F58F
'+'0>'& 1 &e &1S 1U 11S 1 1 1(c ( # # #c 
( 
( %S % %&C &+5 +(e ()u ))u )'U '2 2r   r   )-rw   r[   loggingr   typingr   r   r   r   _mathtext_datar   ft2fontr	   r
   	getLoggerrJ   rc   r   rO   r   r   r   r   r   rQ   r'   r)   rP   r.   r0   rp   rr   rt   ru   rv   rx   r   r   r   r   r   r   r   r   r   rI   r   r   <module>r     s'  :   	 8 8 % 6 "us{ s & & &u  / /$s) /
1%#+ 1$u+ 1 $ 	 :Bh B#4 BJ$* $ <   8   N   3$H 3$t4E{4R/S/3C4D/E0F *G 3$l/( /tE#s(OU,B'C /@J  C   L   L   -/( -/tE43F,F'G -/`! !U4c3h0F+G+/tM7J0J+K,L &M !:z2 z2r   