)Uc @sdZddlZddlZddljZddlZddlZddl Z ej ddddddd d d d Z d e fdYZ de fdYZde fdYZdZdZde fdYZde fdYZde fdYZedZddZiZiZdZdZdZed krddlZejj d!ej!dZ"ye#ej!dZ$Wne%k reZ$nXe e"e$Z&eed"Z'xe&D]Z(d#GHeZ)xe(j*D]\Z+Z,Z-Z.Z/e-e)krGd$Ge-j0Gd%Ge-j1e2d&d'GHe-Z)ne+Ge,Ge.Gd(e.kojd)knoxe3e.p~d*Ge/GHqWx2e(j4D]'\Z+Z,Z/Z5e+Ge,Gd+Ge/Ge5GHqWqWndS(,sI An experimental module for reading dvi files output by TeX. Several limitations make this not (currently) useful as a general-purpose dvi preprocessor, but it is currently used by the pdf backend for processing usetex text. Interface:: dvi = Dvi(filename, 72) # iterate over pages (but only one page is supported for now): for page in dvi: w, h, d = page.width, page.height, page.descent for x,y,font,glyph,width in page.text: fontname = font.texname pointsize = font.size ... for x,y,height,width in page.boxes: ... iNtpreitouteritinpageit post_postitfinaleitDvicBs eZdZdZdZdZdZdZedZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZRS(s A dvi ("device-independent") file, as produced by TeX. The current implementation only reads the first page and does not even attempt to verify the postamble. cCsKtjjd|dt|d|_||_i|_tj|_ dS(s Initialize the object. This takes the filename as input and opens the file; actually reading the file happens when iterating through the pages of the file. sDvi: tdebugtrbN( t matplotlibtverbosetreporttopentfiletdpitfontst _dvistateRtstate(tselftfilenameR ((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyt__init__&s   ccs2x+tr-|j}|r)|jVqPqWdS(s Iterate through the pages of the file. Returns (text, boxes) pairs, where: text is a list of (x, y, fontnum, glyphnum, width) tuples boxes is a list of (x, y, height, width) tuples The coordinates are transformed into a standard Cartesian coordinate system at the dpi value given when initializing. The coordinates are floating point numbers, but otherwise precision is not lost and coordinate values are not clipped to integers. N(tTruet_readt_output(Rt have_page((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyt__iter__2s   cCs |jjs|jjndS(s: Close the underlying file if it is open. N(R tclosedtclose(R((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRGs c Cs3tjtjtj tj f\}}}}tj }x|j|jD]}t|dkrz|\}}} } d} n*|\}}} } } | j| \} } t||}t||| }t||| }t||| }t||}qGW|jd krHt j d|jd|jd||d||d||S|jdd }g|jD]>\}}}} } |||||||| | |f^qc}g|jD]<\}}} } ||||||| || |f^q}t j d|d|d|||d|||d|||S( sk Output the text and boxes belonging to the most recent page. page = dvi._output() iittexttboxestwidththeighttdescentgzGR@iiNi( tnptinfRRtlent_height_depth_oftmintmaxR tNonet mpl_cbooktBunch(Rtminxtminytmaxxtmaxyt maxy_puretelttxtythtwtetfonttgtdtfRR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRNs2,   KLcCsfx_trat|jjd}|j||dkr>tS|jtjkr|jt SqWdS(st Read one page from the file. Return True if successful, False if there were no more pages. iiN( RtordR treadt _dispatchRRRRtFalse(Rtbyte((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRrs    cCsw|jj|}t|d}|rA|dkrA|d}nx/td|D]}d|t||}qQW|S(s~ Read and return an integer argument *nbytes* long. Signedness is determined by the *signed* keyword. iiii(R R9R8trange(Rtnbytestsignedtstrtvalueti((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyt_args c Cs d|kodknr,|j|n|dkrQ|j|jdn|dkrv|j|jdn|dkr|j|jdne|d kr|j|jd tn=|d kr|j|jd t|jd tn|d kr|j|jdn|d krD|j|jdn|dkri|j|jdn|dkr|j|jd tno|dkr|j|jd t|jd tn8|dkr|jn|dkr"|jgtdD]}|jd t^qn|dkr;|j n|dkrT|j n|dkrm|j n|dkr|j |jdtnk|dkr|j |jdtnC|dkr|j |jdtn|dkr |j |jd tn|dkr)|j dAn|dkrQ|j |jdtn|dkry|j |jdtn|dkr|j |jdtn_|dkr|j |jd tn7|d kr|jdAn|d!kr |j|jdtn|d"kr5|j|jdtn|d#kr]|j|jdtn|d$kr|j|jd tn{|d%kr|j|jdtnS|d&kr|j|jdtn+|d'kr|j|jdtn|d(kr%|j|jd tn|d)krA|jdAn|d*kri|j|jdtn|d+kr|j|jdtno|d,kr|j|jdtnG|d-kr|j|jd tn|d.kr|jdAn|d/kr%|j|jdtn|d0krM|j|jdtn|d1kru|j|jdtn|d2kr|j|jd tncd3|kod4knr|j|d3n3|d5kr|j|jdn|d6kr|j|jdn|d7kr<|j|jdn|d8krd|j|jd tnd9|ko{d:knr|j|d8}|jj|}|j|nKd;|kod<knrV|j|d:|d<k}gdBD]}|j|^q\}}} } } |jj| | } |j|||| | | | n|d=krgdCD]}|j|^qi\}} }}}|jj|}|j|| |||n?|d>kr|jn&|d?kr|jn td@|dAS(Ds Based on the opcode *byte*, read the correct kinds of arguments from the dvi file and call the method implementing that opcode with those arguments. iiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiisunknown command: byte %dN(iiiii(iiiii(t _set_charRCRt _set_rulet _put_chart _put_rulet_nopt_bopR=t_eopt_pusht_popt_rightt_right_wR&t_right_xt_downt_down_yt_down_zt_fnt_numR R9t_xxxt_fnt_deft_pret_postt _post_postt ValueError(RR<RBR"tspecialtkR/tctsR6tatltntnumtdentmag((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyR:s     +     +  5                                   1" 1  cCs|jtjkrtdn|dkr:td|n|dksV|dd krbtdn|dkrztd ntj|_dS( Ns!pre command in middle of dvi fileisUnknown dvi format %dii;isnonstandard units in dvi fileis%nonstandard magnification in dvi filei(RRRRYR(RRBRaRbRctcomment((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRVs     cCsQ|jtjkrtdn|j||j|j|jj|7_dS(Nsmisplaced set_char in dvi file( RRRRYRFR1RR7t _width_of(Rtchar((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRDs  cCsA|jtjkrtdn|j|||j|7_dS(Nsmisplaced set_rule in dvi file(RRRRYRGR1(RR^tb((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyREs c Cs|jtjkrtdn|j|j}|jdkrn|jj |j |j |||j |fn(|j }x|j|jD]\}}}}}tdt||j d|jd|jd|j} |jj |j t|||j t||| || j |fqW|jjg|j|jD]V\}}} } |j t|||j t||t| |t| |f^q9dS(Nsmisplaced put_char in dvi filetscalettfmttexnametvf(RRRRYRR7t_vfR&RtappendR1tvRet_scaletDviFontt_mul2012t_tfmRjRtextend( RRfR4RhR/R0R7R5R2tnewfR^Rg((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRFs  & cCs_|jtjkrtdn|dkr[|dkr[|jj|j|j||fndS(Nsmisplaced put_rule in dvi filei(RRRRYRRmR1Rn(RR^Rg((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRGs cCsdS(N((R((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRHsc Csz|jtjkr%td|jntj|_d\|_|_|_|_|_ |_ g|_ g|_ g|_ dS(Ns$misplaced bop in dvi file (state %d)i(iiiiii(RRRRYRR1RnR2R/R0tztstackRR( Rtc0tc1tc2tc3tc4tc5tc6tc7tc8tc9tp((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRIs *  cCsX|jtjkrtdntj|_|`|`|`|`|` |` |` dS(Nsmisplaced eop in dvi file( RRRRYRR1RnR2R/R0RuRv(R((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRJ&s  cCsV|jtjkrtdn|jj|j|j|j|j |j |j fdS(Nsmisplaced push in dvi file( RRRRYRvRmR1RnR2R/R0Ru(R((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRK,s cCsU|jtjkrtdn|jj\|_|_|_|_ |_ |_ dS(Nsmisplaced pop in dvi file( RRRRYRvtpopR1RnR2R/R0Ru(R((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRL1s cCs1|jtjkrtdn|j|7_dS(Nsmisplaced right in dvi file(RRRRYR1(RRg((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRM6s cCsL|jtjkrtdn|dk r6||_n|j|j7_dS(Nsmisplaced w in dvi file(RRRRYR&R2R1(Rtnew_w((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRN;s    cCsL|jtjkrtdn|dk r6||_n|j|j7_dS(Nsmisplaced x in dvi file(RRRRYR&R/R1(Rtnew_x((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyROBs    cCs1|jtjkrtdn|j|7_dS(Nsmisplaced down in dvi file(RRRRYRn(RR^((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRPIs cCsL|jtjkrtdn|dk r6||_n|j|j7_dS(Nsmisplaced y in dvi file(RRRRYR&R0Rn(Rtnew_y((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRQNs    cCsL|jtjkrtdn|dk r6||_n|j|j7_dS(Nsmisplaced z in dvi file(RRRRYR&RuRn(Rtnew_z((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRRUs    cCs+|jtjkrtdn||_dS(Nsmisplaced fnt_num in dvi file(RRRRYR7(RR[((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRS\s c Csitjjddjg|D]>}dt|ko<dknrG|pTdt|^qddS(Ns!Dvi._xxx: encountered special: %sti is<%02x>R(RR R tjoinR8(RRZtch((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRTas   Ic Cst|| }|dkrK|jdkrK||jkrKtd|nt|| } td|d|d|d| |j|>> map = PsfontsMap(find_tex_file('pdftex.map')) >>> entry = map['ptmbo8r'] >>> entry.texname 'ptmbo8r' >>> entry.psname 'Times-Bold' >>> entry.encoding '/usr/local/texlive/2008/texmf-dist/fonts/enc/dvips/base/8r.enc' >>> entry.effects {'slant': 0.16700000000000001} >>> entry.filename For historical reasons, TeX knows many Type-1 fonts by different names than the outside world. (For one thing, the names have to fit in eight characters.) Also, TeX's native fonts are not Type-1 but Metafont, which is nontrivial to convert to PostScript except as a bitmap. While high-quality conversions to Type-1 format exist and are shipped with modern TeX distributions, we need to know which Type-1 fonts are the counterparts of which native fonts. For these reasons a mapping is needed from internal font names to font file names. A texmf tree typically includes mapping files called e.g. psfonts.map, pdftex.map, dvipdfm.map. psfonts.map is used by dvips, pdftex.map by pdfTeX, and dvipdfm.map by dvipdfm. psfonts.map might avoid embedding the 35 PostScript fonts (i.e., have no filename for them, as in the Times-Bold example above), while the pdf-related files perhaps only avoid the "Base 14" pdf fonts. But the user may have configured these files differently. t_fontcCs;i|_t|d}z|j|Wd|jXdS(Ntrt(RR t_parseR(RRR ((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRs  cCs|j|}|j|j}}|dk rN|jd rNt||_n|dk r|jd rt|j|_n|S(Nt/(RRtencodingR&t startswitht find_tex_file(RRjRtfntenc((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRs cCs@x9|D]1}|j}|dks|jdr:qngd}}x|t|kr*||dkr|d7}|jd|}|j|||!|d}nK|jd|d}|dkrt|}n|j|||!|}x0|t|kr&||dkr&|d7}qWqJW|j|qWdS( sParse each line into words.Rt%it"it iN(tstripRR"tindexRmtfindt _register(RR tlinetwordstpostend((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRs&      %c Cs|d \}}d\}}}x|dD]}|jdsH|}q*|jd}|jdr|dksxt|d}q*|jdr|dkst|}q*|dkst|}q*W|j}i}y%t||jdd|dRegister a font described by "words". The format is, AFAIK: texname fontname [effects and filenames] Effects are PostScript snippets like ".177 SlantFont", filenames begin with one or two less-than signs. A filename ending in enc is an encoding file, other filenames are font files. This can be overridden with a left bracket: <[foobar indicates an encoding file named foobar. There is some difference between `_ The library that :program:`kpsewhich` is part of. t kpsewhichs --format=sfind_tex_file(%s): %sRtstdoutisfind_tex_file result: %sN( R&RR R t subprocesstPopentPIPEt communicatetrstrip(RtformattcmdtpipeR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyR%s    cCsSxLtrNy|j|SWqtk rJ}|jtjkrDqqKqXqWdS(N(RR9tOSErrorterrnotEINTR(RtbufsizeR3((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyt _read_nointrBs cCsYy ||SWntk rnXt||}|rE||}nd}|||<|S(N(tKeyErrorRR&(Rjtclass_tsuffixtcacheRR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyt _fontfileTs   cCst|tdtS(Ns.tfm(RRt _tfmcache(Rj((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRcscCst|tdtS(Ns.vf(RRt_vfcache(Rj((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pyRfst__main__sdebug-annoyings pdftex.maps=== new page ===R4tscaledg@ii it.tBOX(6RRRtmatplotlib.cbooktcbookR'tnumpyR RRR(RtobjectRRpRRRqRRRR&RRRRRRRRtsysR t set_leveltargvtfnameRR t IndexErrortdvitfontmaptpagetfPrevRR/R0R7R\R2RjRotpowtchrRR1(((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/dviread.pytsV     *bOW <y9           $ A