)Uc@s+dZddlZddlZddlZddlmZdZeZe Z dZ dZ dZ dZd Zd Zd Zd Zd ZdZdddYZedkr'dZxVejeD]BZeejjeeZeeZejd\Z Z!qWndS(s This is a python interface to Adobe Font Metrics Files. Although a number of other python implementations exist (and may be more complete than mine) I decided not to go with them because either they were either 1) copyrighted or used a non-BSD compatible license 2) had too many dependencies and I wanted a free standing lib 3) Did more than I needed and it was easier to write my own than figure out how to just get what I needed from theirs It is pretty easy to use, and requires only built-in python libs:: >>> from afm import AFM >>> fh = file('ptmr8a.afm') >>> afm = AFM(fh) >>> afm.string_width_height('What the heck?') (6220.0, 683) >>> 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] >>> afm.get_bbox_font() [-168, -218, 1000, 898] AUTHOR: John D. Hunter iN(t uni2type1cCstt|S(N(tinttfloat(tx((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_to_int0scCs5|jdd}g|jD]}t|^qS(Nt,t (treplacetsplitR(tstval((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_to_list_of_ints6scCs#g|jD]}t|^q S(N(Rt _to_float(R R ((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_to_list_of_floats:scCs$|jjdkrtStSdS(Ntfalset0tno(RRR(tlowertstriptFalsetTrue(R ((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_to_bool<scCsR|j}z|j}Wd|j|dX|jdsNtdndS(s[ Check if the file at least looks like AFM. If not, raise :exc:`RuntimeError`. NitStartFontMetricssNot an AFM file(ttelltreadlinetseekt startswitht RuntimeError(tfhtpostline((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt _sanity_checkAs  cCsitd6td6td6td6td6td6td6td6td 6td 6td 6td 6td 6td6td6td6td6td6td6td6td6td6td6}i}x|j}|sPn|j}|jdrqn|jdd}|d}t |dkr"|d}nd}y|||||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 RtFontNametFullNamet FamilyNametWeightt ItalicAnglet IsFixedPitchtFontBBoxtUnderlinePositiontUnderlineThicknesstVersiontNoticetEncodingSchemet CapHeightt CapheighttXHeighttAscendert DescendertStdHWtStdVWtStartCharMetricst CharacterSett CharacterstCommentRiiits"Value error parsing header in AFM:s/Found an unknown keyword in AFM header (was %s)s Bad parseN(R t_to_strRR RRtrstripRRtlent ValueErrortsyststderrtKeyErrorR(RtheaderConverterstdRtlsttkeyR ((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt _parse_headerVs`        c CsGi}i}x(|j}|s%Pn|j}|jdrJ||fS|jdd }t|dkrtd|nt|djd}t|djd}|djd}t|dd}t t |}|d krd }n|d kr$|||f||(RoR ttotalwtnamelasttminytmaxyRtRLRMRNRbtbtwthtkptthismaxtthismin((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytstring_width_height=s.       cCst|sdSd}d}d}d}d}t|tsL|j}nx|D]}|dkrkqSntjt|d}y|j|\} } Wn*t k rd}|j|\} } nX| \} } } }| |kr| }ny|j ||f}Wnt k rd}nX|| |7}| |}||krL|}n| }||krS|}qSqSW|||||| fS(s0 Return the string bounding box igeAs tquestion(iiiiN( R:Rwt isinstancetunicodetdecodeRtgetRsRmR>Rk(RoR RxRyRzR{tleftRtRMRLRNRbR|R}R~RRR((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_str_bbox_and_descent[sB         cCs|j|d S(s0 Return the string bounding box i(R(RoR ((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_str_bboxscCs/|st|}n|j|\}}}|S(sG Get the name of the character, ie, ';' is 'semicolon' (RsRl(RoRtRuRLRMRN((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_name_charscCs/|st|}n|j|\}}}|S(s[ Get the width of the character from the character metric WX field (RsRl(RoRtRuRLRMRN((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_width_charscCs|j|\}}|S(sL Get the width of the character from a type1 character name (Rm(RoRMRLRN((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_width_from_char_namescCs3|st|}n|j|\}}}|dS(su Get the height of character *c* from the bounding box. This is the ink height (space is 0) i(RsRl(RoRtRuRLRMRN((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_height_charscCs/|j||j|}}|j||S(s_ Return the kerning pair distance (possibly 0) for chars *c1* and *c2* (Rtget_kern_dist_from_name(RoRSRTtname1tname2((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_kern_distscCs$y|j||fSWndSXdS(se Return the kerning pair distance (possibly 0) for chars *name1* and *name2* iN(Rk(RoRR((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyRscCs |jdS(s'Return the font name, eg, 'Times-Roman'R (Rj(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_fontnamescCs2|jjd}|dkr.|jd}n|S(s,Return the font full name, eg, 'Times-Roman'R!R N(RjRRw(RoRM((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_fullnames cCsG|jjd}|dk r"|S|j}d}tj|d|S(s(Return the font family name, eg, 'Times'R"sY(?i)([ -](regular|plain|italic|oblique|bold|semibold|light|ultralight|extra|condensed))+$R7N(RjRRwRtretsub(RoRMtextras((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_familynames   cCs |jdS(s-Return the font weight, eg, 'Bold' or 'Roman'R#(Rj(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_weightscCs |jdS(sReturn the fontangle as floatR$(Rj(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_anglescCs |jdS(sReturn the cap height as floatR,(Rj(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_capheightscCs |jdS(sReturn the xheight as floatR.(Rj(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt get_xheightscCs |jdS(s'Return the underline thickness as floatR((Rj(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_underline_thicknessscCs|jjddS(su Return the standard horizontal stem width as float, or *None* if not specified in AFM file. R1N(RjRRw(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_horizontal_stem_widthscCs|jjddS(ss Return the standard vertical stem width as float, or *None* if not specified in AFM file. R2N(RjRRw(Ro((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pytget_vertical_stem_widths(t__name__t __module__RrRRvRRRRRRRRRRRRRRRRRRR(((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyRi)s*   &             t__main__s!/usr/local/share/fonts/afms/adobesJohn Hunter is the Man!(("t__doc__R<tosRt_mathtext_dataRRRR tstrR8R R RRRCRORUR^RcRhRiRtpathnametlistdirtfnametfiletpathtjoinRtafmRR}R~(((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt#s,$      D " "