11"""
22An experimental module for reading dvi files output by TeX. Several
33limitations make this not (currently) useful as a general-purpose dvi
4- preprocessor.
4+ preprocessor, but it is currently used by the pdf backend for
5+ processing usetex text.
56
67Interface::
78
89 dvi = Dvi(filename, 72)
9- for page in dvi: # iterate over pages
10+ # iterate over pages (but only one page is supported for now):
11+ for page in dvi:
1012 w, h, d = page.width, page.height, page.descent
1113 for x,y,font,glyph,width in page.text:
1214 fontname = font.texname
@@ -49,7 +51,7 @@ def __iter__(self):
4951 """
5052 Iterate through the pages of the file.
5153
52- Returns (text, pages ) pairs, where:
54+ Returns (text, boxes ) pairs, where:
5355 text is a list of (x, y, fontnum, glyphnum, width) tuples
5456 boxes is a list of (x, y, height, width) tuples
5557
@@ -131,8 +133,8 @@ def _read(self):
131133
132134 def _arg (self , nbytes , signed = False ):
133135 """
134- Read and return an integer argument " nbytes" long.
135- Signedness is determined by the " signed" keyword.
136+ Read and return an integer argument * nbytes* long.
137+ Signedness is determined by the * signed* keyword.
136138 """
137139 str = self .file .read (nbytes )
138140 value = ord (str [0 ])
@@ -144,7 +146,7 @@ def _arg(self, nbytes, signed=False):
144146
145147 def _dispatch (self , byte ):
146148 """
147- Based on the opcode " byte" , read the correct kinds of
149+ Based on the opcode * byte* , read the correct kinds of
148150 arguments from the dvi file and call the method implementing
149151 that opcode with those arguments.
150152 """
@@ -385,9 +387,27 @@ class DviFont(object):
385387 Object that holds a font's texname and size, supports comparison,
386388 and knows the widths of glyphs in the same units as the AFM file.
387389 There are also internal attributes (for use by dviread.py) that
388- are _not_ used for comparison.
390+ are *not* used for comparison.
389391
390392 The size is in Adobe points (converted from TeX points).
393+
394+ .. attribute:: texname
395+
396+ Name of the font as used internally by TeX and friends. This
397+ is usually very different from any external font names, and
398+ :class:`dviread.PsfontsMap` can be used to find the external
399+ name of the font.
400+
401+ .. attribute:: size
402+
403+ Size of the font in Adobe points, converted from the slightly
404+ smaller TeX points.
405+
406+ .. attribute:: widths
407+
408+ Widths of glyphs in glyph-space units, typically 1/1000ths of
409+ the point size.
410+
391411 """
392412 __slots__ = ('texname' , 'size' , 'widths' , '_scale' , '_vf' , '_tfm' )
393413
@@ -532,17 +552,27 @@ class Tfm(object):
532552 A TeX Font Metric file. This implementation covers only the bare
533553 minimum needed by the Dvi class.
534554
535- Attributes:
555+ .. attribute:: checksum
556+
557+ Used for verifying against the dvi file.
558+
559+ .. attribute:: design_size
536560
537- checksum: for verifying against dvi file
561+ Design size of the font (in what units?)
538562
539- design_size: design size of the font (in what units?)
563+ .. attribute:: width
540564
541- width[i]: width of character \#i , needs to be scaled
542- by the factor specified in the dvi file
543- (this is a dict because indexing may not start from 0)
565+ Width of each character , needs to be scaled by the factor
566+ specified in the dvi file. This is a dict because indexing may
567+ not start from 0.
544568
545- height[i], depth[i]: height and depth of character \#i
569+ .. attribute:: height
570+
571+ Height of each character.
572+
573+ .. attribute:: depth
574+
575+ Depth of each character.
546576 """
547577 __slots__ = ('checksum' , 'design_size' , 'width' , 'height' , 'depth' )
548578
@@ -581,7 +611,19 @@ def __init__(self, filename):
581611class PsfontsMap (object ):
582612 """
583613 A psfonts.map formatted file, mapping TeX fonts to PS fonts.
584- Usage: map = PsfontsMap('.../psfonts.map'); map['cmr10']
614+ Usage::
615+
616+ >>> map = PsfontsMap(find_tex_file('pdftex.map'))
617+ >>> entry = map['ptmbo8r']
618+ >>> entry.texname
619+ 'ptmbo8r'
620+ >>> entry.psname
621+ 'Times-Bold'
622+ >>> entry.encoding
623+ '/usr/local/texlive/2008/texmf-dist/fonts/enc/dvips/base/8r.enc'
624+ >>> entry.effects
625+ {'slant': 0.16700000000000001}
626+ >>> entry.filename
585627
586628 For historical reasons, TeX knows many Type-1 fonts by different
587629 names than the outside world. (For one thing, the names have to
@@ -594,11 +636,12 @@ class PsfontsMap(object):
594636 file names.
595637
596638 A texmf tree typically includes mapping files called e.g.
597- psfonts.map, pdftex.map, dvipdfm.map. psfonts.map is used by
639+ psfonts.map, pdftex.map, dvipdfm.map. psfonts.map is used by
598640 dvips, pdftex.map by pdfTeX, and dvipdfm.map by dvipdfm.
599- psfonts.map might avoid embedding the 35 PostScript fonts, while
600- the pdf-related files perhaps only avoid the "Base 14" pdf fonts.
601- But the user may have configured these files differently.
641+ psfonts.map might avoid embedding the 35 PostScript fonts (i.e.,
642+ have no filename for them, as in the Times-Bold example above),
643+ while the pdf-related files perhaps only avoid the "Base 14" pdf
644+ fonts. But the user may have configured these files differently.
602645 """
603646 __slots__ = ('_font' ,)
604647
@@ -655,10 +698,10 @@ def _register(self, words):
655698 subsetting, but I have no example of << in my TeX installation.
656699 """
657700 texname , psname = words [:2 ]
658- effects , encoding , filename = [] , None , None
701+ effects , encoding , filename = '' , None , None
659702 for word in words [2 :]:
660703 if not word .startswith ('<' ):
661- effects . append ( word )
704+ effects = word
662705 else :
663706 word = word .lstrip ('<' )
664707 if word .startswith ('[' ):
@@ -670,6 +713,18 @@ def _register(self, words):
670713 else :
671714 assert filename is None
672715 filename = word
716+
717+ eff = effects .split ()
718+ effects = {}
719+ try :
720+ effects ['slant' ] = float (eff [eff .index ('SlantFont' )- 1 ])
721+ except ValueError :
722+ pass
723+ try :
724+ effects ['extend' ] = float (eff [eff .index ('ExtendFont' )- 1 ])
725+ except ValueError :
726+ pass
727+
673728 self ._font [texname ] = mpl_cbook .Bunch (
674729 texname = texname , psname = psname , effects = effects ,
675730 encoding = encoding , filename = filename )
@@ -733,13 +788,18 @@ def _parse(self, file):
733788
734789def find_tex_file (filename , format = None ):
735790 """
736- Call kpsewhich to find a file in the texmf tree.
737- If format is not None, it is used as the value for the --format option.
738- See the kpathsea documentation for more information .
791+ Call :program:` kpsewhich` to find a file in the texmf tree. If
792+ * format* is not None, it is used as the value for the
793+ :option:`--format` option .
739794
740795 Apparently most existing TeX distributions on Unix-like systems
741796 use kpathsea. I hear MikTeX (a popular distribution on Windows)
742797 doesn't use kpathsea, so what do we do? (TODO)
798+
799+ .. seealso::
800+
801+ `Kpathsea documentation <http://www.tug.org/kpathsea/>`_
802+ The library that :program:`kpsewhich` is part of.
743803 """
744804
745805 cmd = ['kpsewhich' ]
0 commit comments