rL i4AdZddlmZddlZddlZddlmZejeZ dZ dZ dZ d Z d Zd Zd Zed dZde_dej$_dej&_dej(_dZdZeddZde_dej&_dej0_dej2_dZdZGddZy)aL 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.string_width_height('What the heck?') (6220.0, 694) >>> afm.get_fontname() 'Times-Roman' >>> afm.get_kern_dist('A', 'f') 0 >>> afm.get_kern_dist('A', 'y') -92.0 >>> afm.get_bbox_char('!') [130, -9, 238, 676] 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. ) namedtupleN) uni2type1c*tt|SN)intfloatxs U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/_afm.py_to_intr 0s uQx=czt|tr|jd}t|j ddS)Nzlatin-1,.) isinstancebytesdecoder replacer s r _to_floatr9s4!U HHY  3$ %%rc$|jdS)Nutf8)rr s r _to_strrDs 88F rc~|jdd}|jDcgc] }t|c}Scc}w)N, )rsplitr svals r _to_list_of_intsr!Hs0 $A$%GGI .SGCL .. .s:cZ|jDcgc] }t|c}Scc}wr)rrrs r _to_list_of_floatsr#Ms &'ggi 0sIcN 00 0s(cF|jjdvryy)N)sfalse0snoFT)lowerstrip)rs r _to_boolr(Qswwy33rcidtdtdtdtdtdtdtdtd td td td d dtdtdtdtdtttttttd}i}d}|D]}|j }|j dr%|jdd}|d}|r|dk7r tdd}t|dk(r|d}nd} ||} ||||<|dk(s~|Std #t$rtjd|YwxYw#t$rtjd||YwxYw)!a; Read the font metrics header (up to the char metrics) and returns a dictionary mapping *key* to *val*. *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] Dictionary keys are StartFontMetrics, FontName, FullName, FamilyName, Weight, ItalicAngle, IsFixedPitch, FontBBox, UnderlinePosition, UnderlineThickness, Version, Notice, EncodingScheme, CapHeight, XHeight, Ascender, Descender, StartCharMetrics sStartFontMetricsFontNameFullName FamilyNameWeight ItalicAngles IsFixedPitchsFontBBoxsUnderlinePositionUnderlineThicknesssVersionsNoticec|Srr s r z_parse_header..xsQrsEncodingScheme CapHeights CapheightXHeightsAscender)s DescenderStdHWStdVWStartCharMetricss CharacterSets CharactersTsCommentrrrzNot an AFM fileFrz/Found an unknown keyword in AFM header (was %r)z)Value error parsing header in AFM: %s, %sr7 Bad parse)rrr(r!r rstrip startswithr RuntimeErrorlenKeyError_logerror ValueError) fhheader_convertersd first_linelinelstkeyr converters r _parse_headerrJXs"YW W w  7     % i y G ; 7!" i#$ i%& I'( Y)* $ 58 AJ ({{} ??: & jjq!!f  ))"#455J s8q=a&CC )#.I s^AcF % %  HC (@;''  JJH# N     JJBC M  s$D+ E+E  E  E32E3 CharMetricszwidth, name, bboxz 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. zThe character width (WX).zThe character name (N).zK The bbox of the character (B) as a tuple (*llx*, *lly*, *urx*, *ury*).chd}i}i}|D]}t|j}|jdr||fcStd|j dD}|j |st d|zt|d}t|d}|d}t|d } ttt| } t||| } |d k(rd }n|d k(r td }|dk7r| ||<| ||<t d)a Parse the given filehandle for character metrics information and return the information as dicts. 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. >BCNWXEndCharMetricsc3bK|]'}|s|jjdd)yw) rN)r'r).0rs r z&_parse_char_metrics..s#JAGGIOOC+Js/%/;zBad char metrics line: %srNrPrOrMEurominusu−r9)rr:r;dictrissubsetr<r rr#listmaprrKord) rB required_keysascii_dname_drFvalsnumwxnamebboxmetricss r _parse_char_metricsris(*MG Ft{{}% ??+ ,F? "JTZZ_JJ%%d+:TAB Bd3i  tDz "Cy!$s),CTN#b$- 6>C W_&'C "9"GCLt 56 { ##rct|}|jdstd|zi}|D]}|j}|s|jdrt||cS|j }t |dk7s|ddk7rtd|zt |dt |d t|d }}}||||f<td ) z Return a kern pairs dictionary; 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 sStartKernPairsz Bad start of kern pairs data: %ss EndKernPairsrsKPXzBad kern pairs line: %srr8zBad kern pairs parse)nextr;r<r:rr=rr)rBrFrDrcc1c2r s r _parse_kern_pairsrps 8D ??, -=DEE A {{}  ??? + HHzz| t9>T!W.84?@ @d1g&Q(8)DG:LB2r(   - ..r CompositePartz name, dx, dyzK Represents the information on a composite element of a composite char.zName 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 i}|D]}|j}|s|jdr|cS|jd}|dj}|dt|d}}g}|ddD]K}|j} t | dt | dt | d} |j | M|||<td) a Parse the given filehandle for composites information return them as a dict. 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['Aacute'] = [CompositePart(name='A', dx=0, dy=0), CompositePart(name='acute', dx=160, dy=170)] s EndComposites;rrr8rZrlzBad composites parse)r:r;rr rqrappendr<) rB compositesrFrcccrf _num_partspccPartsrpccparts r _parse_compositesr{s4J${{}  ??+ , zz$ !W]]_a5'"Q%.ja "A'')C Q3q6):Ic!frrr)rrrrrrleftrrfre_rgrrrrs r get_str_bbox_and_descentzAFM.get_str_bbox_and_descents/1v  !S! A ADy==Q3s1vcl);C?c*|j|ddS)zReturn the string bounding box.Nrk)r)rrs r get_str_bboxzAFM.get_str_bboxs,,Q/33rcN|s t|}|j|jS)z8Get the name of the character, i.e., ';' is 'semicolon'.)r_rrfrs r get_name_charzAFM.get_name_chars#AA}}Q$$$rcN|s t|}|j|jS)zT Get the width of the character from the character metric WX field. )r_rwidthrs r get_width_charzAFM.get_width_chars%AA}}Q%%%rc4|j|jS)z;Get the width of the character from a type1 character name.)rrrrfs r get_width_from_char_namezAFM.get_width_from_char_names$$T*000rcT|s t|}|j|jdS)z@Get the bounding box (ink) height of character *c* (space is 0).rZrrs r get_height_charzAFM.get_height_chars(AA}}Q$$R((rcj|j||j|}}|j||S)zX Return the kerning pair distance (possibly 0) for chars *c1* and *c2*. )rget_kern_dist_from_name)rrnroname1name2s r get_kern_distzAFM.get_kern_dists6))"-t/A/A"/Eu++E599rc>|jj||fdS)zf Return the kerning pair distance (possibly 0) for chars *name1* and *name2*. r)rr)rrrs r rzAFM.get_kern_dist_from_names zz~~uena00rc |jdS)z*Return the font name, e.g., 'Times-Roman'.r*rrs r get_fontnamezAFM.get_fontnames||K((rc"|jSr)rrs r postscript_namezAFM.postscript_names  ""rc^|jjd}||jd}|S)z/Return the font full name, e.g., 'Times-Roman'.r+r*rrrs r get_fullnamezAFM.get_fullnames.|| , <<< ,D rc|jjd}||S|j}d}tj|d|S)z+Return the font family name, e.g., 'Times'.r,zY(?i)([ -](regular|plain|italic|oblique|bold|semibold|light|ultralight|extra|condensed))+$)rrrresub)rrfextrass r get_familynamezAFM.get_familynamesK|| .  K  ":vvfb$''rc"|jS)z$The font family name, e.g., 'Times'.)rrs r family_namezAFM.family_names""$$rc |jdS)z0Return the font weight, e.g., 'Bold' or 'Roman'.r-rrs r get_weightzAFM.get_weights||I&&rc |jdS)zReturn the fontangle as float.r.rrs r get_anglez AFM.get_angles||N++rc |jdS)zReturn the cap height as float.r3rrs r get_capheightzAFM.get_capheights||L))rc |jdS)zReturn the xheight as float.r4rrs r get_xheightzAFM.get_xheights||J''rc |jdS)z(Return the underline thickness as float.r/rrs r get_underline_thicknesszAFM.get_underline_thicknesss||122rc:|jjddS)zu Return the standard horizontal stem width as float, or *None* if not specified in AFM file. r5Nrrs r get_horizontal_stem_widthzAFM.get_horizontal_stem_width ||$//rc:|jjddS)zs Return the standard vertical stem width as float, or *None* if not specified in AFM file. r6Nrrs r get_vertical_stem_widthzAFM.get_vertical_stem_widthrrN)F)__name__ __module__ __qualname__rrrrrrrrrrrrpropertyrrrrrrrrrrrr1rr rrfs: % (2;<4% &1) :1)## (%%',*(300rr)__doc__ collectionsrloggingr_mathtext_datar getLoggerrr?r rrr!r#r(rJrKrrfrgrirprqdxdyr{rrr1rr rs#J# %w"&/ 1P f(;<  < 8 N 3$l/@?N; N B L L +/\6@n0n0r