• File: afm.pyc
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyc
  • Date Modified: 07/22/2015 11:41 PM
  • File size: 17.02 KB
  • MIME-type: text/x-bytecode.python
  • Charset: 8 bit

)Uc@s+dZddlZddlZddlZddlmZdZeZe	Z
dZdZdZ
dZd	Zd
ZdZd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 <jdh2358@gmail.com>
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
6td6td6td
6td6td6td6td6td6td6td6td6td6td6}i}x|j}|sPn|j}|jdrqn|jdd}|d}t	|dkr"|d}nd}y|||||<WnQt
k
rotjdI|I|IJqn%t
k
rtjd|IJqnX|dkr|Sqtd d!S("s;
    Reads 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; eg:

        * '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

    RtFontNametFullNamet
FamilyNametWeighttItalicAnglet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_strRRRRtrstripRRtlent
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|dkr$|||f||<n||f||<qtdd
S(sz
    Return a character metric dictionary.  Keys are the ASCII num of
    the character, values are a (*wx*, *name*, *bbox*) tuple, where
    *wx* is the character width, *name* is the postscript language
    name, and *bbox* is a (*llx*, *lly*, *urx*, *ury*) tuple.

    This function is incomplete per the standard, but thus far parses
    all the sample afm files tried.
    tEndCharMetricst;isBad char metrics line: %siiiitEuroiis	Bad parseN(RR9RRR:RRRR
tmapR(	Rtascii_dtname_dRtvalstnumtwxtnametbbox((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_parse_char_metricss.
	cCs|j}|jds.td|ni}x|j}|sMPn|j}t|dkrqq7n|jdr|j|S|j}t|dks|ddkrtd|n|d|d	t|d
}}}||||f<q7tddS(
s
    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

    tStartKernPairss Bad start of kern pairs data: %sitEndKernPairsitKPXsBad kern pairs line: %siiisBad kern pairs parseN(RRRR9R:RR(RRR@RJtc1tc2R
((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_parse_kern_pairss(
"&cCsi}x|j}|sPn|j}t|dkrCq	n|jdrV|S|jd}|dj}|dt|d}}g}xc|dd!D]T}|j}	|	dt|	dt|	d}}
}|j||
|fqW|||<q	tdd	S(
sx
    Return a composites dictionary.  Keys are the names of the
    composites.  Values are a num parts list of composite information,
    with each element being a (*name*, *dx*, *dy*) tuple.  Thus a
    composites line reading:

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

    will be represented as::

      d['Aacute'] = [ ('A', 0, 0), ('acute', 160, 170) ]

    it
EndCompositesREiiiisBad composites parseN(	RR9R:RRRRtappendR(RR@RRJtccRMtnumPartstpccPartsR	tpcctdxtdy((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_parse_compositess(,
cCsitd6td6}iid6id6}xp|j}|sAPn|j}t|dkreq+n|jd}||kr+|||||<q+q+|d|df}|S(s

    Parse the optional fields for kern pair data and composites

    return value is a (*kernDict*, *compositeDict*) which are the
    return values from :func:`_parse_kern_pairs`, and
    :func:`_parse_composites` if the data exists, or empty dicts
    otherwise
    t
StartKernDatatStartCompositesi(RUR^RR9R:R(RtoptionalR@RRBtl((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt_parse_optionals 	
cCsOt|t|}t|\}}t|}||||d|dfS(s
    Parse the Adobe Font Metics file in file handle *fh*. Return value
    is a (*dhead*, *dcmetrics*, *dkernpairs*, *dcomposite*) tuple where
    *dhead* is a :func:`_parse_header` dict, *dcmetrics* is a
    :func:`_parse_composites` dict, *dkernpairs* is a
    :func:`_parse_kern_pairs` dict (possibly {}), and *dcomposite* is a
    :func:`_parse_composites` dict (possibly {})
    ii(RRCRORc(Rtdheadtdcmetrics_asciitdcmetrics_namet	doptional((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt	parse_afms
	
tAFMcBseZdZedZdZdZdZedZedZ	dZ
edZd	Zd
Z
dZdZd
ZdZdZdZdZdZdZdZRS(cCsLt|\}}}}}||_||_||_||_||_dS(s8
        Parse the AFM file in file object *fh*
        N(Rht_headert_kernt_metricst_metrics_by_namet
_composite(tselfRRdReRft
dkernpairst
dcomposite((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt__init__+s				cCs/|st|}n|j|\}}}|S(N(tordRl(RotctisordRLRMRN((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt
get_bbox_char8scCst|sdSd}d}d}d}x|D]}|dkrGq/n|jt|\}}}	|	\}
}}}
y|j||f}Wntk
rd}nX|||7}||
}||kr|}n|}||kr/|}q/q/W|||fS(sn
        Return the string width (including kerning) and string height
        as a (*w*, *h*) tuple.
        igeAs
(iiN(R:tNoneRlRsRkR>(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.pytget_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tstrR8RR
RRRCRORUR^RcRhRiRtpathnametlistdirtfnametfiletpathtjoinRtafmRR}R~(((sA/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/afm.pyt<module>#s,$
						D	"	 	"