@@ -530,8 +530,10 @@ def __init__(self, scale, tfm, texname, vf):
530
530
if not isinstance (texname , bytes ):
531
531
raise ValueError ("texname must be a bytestring, got %s"
532
532
% type (texname ))
533
- self ._scale , self ._tfm , self .texname , self ._vf = \
534
- scale , tfm , texname , vf
533
+ self ._scale = scale
534
+ self ._tfm = tfm
535
+ self .texname = texname
536
+ self ._vf = vf
535
537
self .size = scale * (72.0 / (72.27 * 2 ** 16 ))
536
538
try :
537
539
nchars = max (tfm .width ) + 1
@@ -541,28 +543,25 @@ def __init__(self, scale, tfm, texname, vf):
541
543
for char in range (nchars )]
542
544
543
545
def __eq__ (self , other ):
544
- return self . __class__ == other . __class__ and \
545
- self .texname == other .texname and self .size == other .size
546
+ return ( type ( self ) == type ( other )
547
+ and self .texname == other .texname and self .size == other .size )
546
548
547
549
def __ne__ (self , other ):
548
550
return not self .__eq__ (other )
549
551
550
- def _width_of (self , char ):
551
- """
552
- Width of char in dvi units. For internal use by dviread.py.
553
- """
552
+ def __repr__ (self ):
553
+ return "<{}: {}>" .format (type (self ).__name__ , self .texname )
554
554
555
+ def _width_of (self , char ):
556
+ """Width of char in dvi units."""
555
557
width = self ._tfm .width .get (char , None )
556
558
if width is not None :
557
559
return _mul2012 (width , self ._scale )
558
560
_log .debug ('No width for char %d in font %s.' , char , self .texname )
559
561
return 0
560
562
561
563
def _height_depth_of (self , char ):
562
- """
563
- Height and depth of char in dvi units. For internal use by dviread.py.
564
- """
565
-
564
+ """Height and depth of char in dvi units."""
566
565
result = []
567
566
for metric , name in ((self ._tfm .height , "height" ),
568
567
(self ._tfm .depth , "depth" )):
@@ -577,8 +576,8 @@ def _height_depth_of(self, char):
577
576
578
577
579
578
class Vf (Dvi ):
580
- """
581
- A virtual font (\\ *.vf file) containing subroutines for dvi files.
579
+ r """
580
+ A virtual font (\*.vf file) containing subroutines for dvi files.
582
581
583
582
Usage::
584
583
@@ -588,12 +587,10 @@ class Vf(Dvi):
588
587
589
588
Parameters
590
589
----------
591
-
592
590
filename : string or bytestring
593
591
594
592
Notes
595
593
-----
596
-
597
594
The virtual font format is a derivative of dvi:
598
595
http://mirrors.ctan.org/info/knuth/virtual-fonts
599
596
This class reuses some of the machinery of `Dvi`
@@ -689,9 +686,7 @@ def _pre(self, i, x, cs, ds):
689
686
690
687
691
688
def _fix2comp (num ):
692
- """
693
- Convert from two's complement to negative.
694
- """
689
+ """Convert from two's complement to negative."""
695
690
assert 0 <= num < 2 ** 32
696
691
if num & 2 ** 31 :
697
692
return num - 2 ** 32
@@ -700,9 +695,7 @@ def _fix2comp(num):
700
695
701
696
702
697
def _mul2012 (num1 , num2 ):
703
- """
704
- Multiply two numbers in 20.12 fixed point format.
705
- """
698
+ """Multiply two numbers in 20.12 fixed point format."""
706
699
# Separated into a function because >> has surprising precedence
707
700
return (num1 * num2 ) >> 20
708
701
@@ -931,8 +924,8 @@ def _parse(self, file):
931
924
932
925
933
926
class Encoding (object ):
934
- """
935
- Parses a \\ *.enc file referenced from a psfonts.map style file.
927
+ r """
928
+ Parses a \*.enc file referenced from a psfonts.map style file.
936
929
The format this class understands is a very limited subset of
937
930
PostScript.
938
931
@@ -1045,21 +1038,25 @@ def _fontfile(cls, suffix, texname):
1045
1038
1046
1039
1047
1040
if __name__ == '__main__' :
1041
+ from argparse import ArgumentParser
1042
+ import itertools
1048
1043
import sys
1049
- fname = sys . argv [ 1 ]
1050
- try :
1051
- dpi = float ( sys . argv [ 2 ] )
1052
- except IndexError :
1053
- dpi = None
1054
- with Dvi (fname , dpi ) as dvi :
1044
+
1045
+ parser = ArgumentParser ()
1046
+ parser . add_argument ( "filename" )
1047
+ parser . add_argument ( "dpi" , nargs = "?" , type = float , default = None )
1048
+ args = parser . parse_args ()
1049
+ with Dvi (args . filename , args . dpi ) as dvi :
1055
1050
fontmap = PsfontsMap (find_tex_file ('pdftex.map' ))
1056
1051
for page in dvi :
1057
1052
print ('=== new page ===' )
1058
- fPrev = None
1059
- for x , y , f , c , w in page .text :
1060
- if f != fPrev :
1061
- print ('font' , f .texname , 'scaled' , f ._scale / pow (2.0 , 20 ))
1062
- fPrev = f
1063
- print (x , y , c , 32 <= c < 128 and chr (c ) or '.' , w )
1053
+ for font , group in itertools .groupby (
1054
+ page .text , lambda text : text .font ):
1055
+ print ('font' , font .texname , 'scaled' , font ._scale / 2 ** 20 )
1056
+ for text in group :
1057
+ print (text .x , text .y , text .glyph ,
1058
+ chr (text .glyph ) if chr (text .glyph ).isprintable ()
1059
+ else "." ,
1060
+ text .width )
1064
1061
for x , y , w , h in page .boxes :
1065
1062
print (x , y , 'BOX' , w , h )
0 commit comments