Skip to content

Commit 940925d

Browse files
committed
Small improvements to the Vf class
Expose the scale attribute, allow overriding the widths, add some convenience methods.
1 parent ba47418 commit 940925d

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

lib/matplotlib/dviread.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ def _put_char_real(self, char):
422422
self.text.append(Text(self.h, self.v, font, char,
423423
font._width_of(char)))
424424
else:
425-
scale = font._scale
425+
scale = font.scale
426426
for x, y, f, g, w in font._vf[char].text:
427-
newf = DviFont(scale=_mul2012(scale, f._scale),
427+
newf = DviFont(scale=_mul2012(scale, f.scale),
428428
tfm=f._tfm, texname=f.texname, vf=f._vf)
429429
self.text.append(Text(self.h + _mul2012(x, scale),
430430
self.v + _mul2012(y, scale),
@@ -580,16 +580,19 @@ class DviFont(object):
580580
----------
581581
582582
scale : float
583-
Factor by which the font is scaled from its natural size.
584-
tfm : Tfm
585-
TeX font metrics for this font
583+
Factor by which the font is scaled from its natural size,
584+
represented as an integer in 20.12 fixed-point format.
585+
tfm : Tfm, may be None if widths given
586+
TeX Font Metrics file for this font
586587
texname : bytes
587588
Name of the font as used internally by TeX and friends, as an
588589
ASCII bytestring. This is usually very different from any external
589590
font names, and :class:`dviread.PsfontsMap` can be used to find
590591
the external name of the font.
591-
vf : Vf
592+
vf : Vf or None
592593
A TeX "virtual font" file, or None if this font is not virtual.
594+
widths : list of integers, optional
595+
Widths for this font. Overrides the widths read from the tfm file.
593596
594597
Attributes
595598
----------
@@ -598,26 +601,37 @@ class DviFont(object):
598601
size : float
599602
Size of the font in Adobe points, converted from the slightly
600603
smaller TeX points.
604+
scale : int
605+
Factor by which the font is scaled from its natural size,
606+
represented as an integer in 20.12 fixed-point format.
601607
widths : list
602608
Widths of glyphs in glyph-space units, typically 1/1000ths of
603609
the point size.
604610
605611
"""
606-
__slots__ = ('texname', 'size', 'widths', '_scale', '_vf', '_tfm')
612+
__slots__ = ('texname', 'size', 'widths', 'scale', '_vf', '_tfm')
607613

608-
def __init__(self, scale, tfm, texname, vf):
614+
def __init__(self, scale, tfm, texname, vf, widths=None):
609615
if not isinstance(texname, bytes):
610616
raise ValueError("texname must be a bytestring, got %s"
611617
% type(texname))
612-
self._scale, self._tfm, self.texname, self._vf = \
613-
scale, tfm, texname, vf
618+
self.scale, self._tfm, self.texname, self._vf, self.widths = \
619+
scale, tfm, texname, vf, widths
614620
self.size = scale * (72.0 / (72.27 * 2**16))
615-
try:
616-
nchars = max(tfm.width) + 1
617-
except ValueError:
618-
nchars = 0
619-
self.widths = [(1000*tfm.width.get(char, 0)) >> 20
620-
for char in range(nchars)]
621+
622+
if self.widths is None:
623+
try:
624+
nchars = max(tfm.width) + 1
625+
except ValueError:
626+
nchars = 0
627+
self.widths = [(1000*tfm.width.get(char, 0)) >> 20
628+
for char in range(nchars)]
629+
630+
def __repr__(self):
631+
return '<DviFont %s *%f>' % (self.texname, self.scale / 2**20)
632+
633+
def __hash__(self):
634+
return 1001 * hash(self.texname) + hash(self.size)
621635

622636
def __eq__(self, other):
623637
return self.__class__ == other.__class__ and \
@@ -633,7 +647,7 @@ def _width_of(self, char):
633647

634648
width = self._tfm.width.get(char, None)
635649
if width is not None:
636-
return _mul2012(width, self._scale)
650+
return _mul2012(width, self.scale)
637651
_log.debug('No width for char %d in font %s.', char, self.texname)
638652
return 0
639653

@@ -651,7 +665,7 @@ def _height_depth_of(self, char):
651665
name, char, self.texname)
652666
result.append(0)
653667
else:
654-
result.append(_mul2012(value, self._scale))
668+
result.append(_mul2012(value, self.scale))
655669
return result
656670

657671

@@ -1374,7 +1388,7 @@ def _fontfile(cls, suffix, texname):
13741388
fPrev = None
13751389
for x, y, f, c, w in page.text:
13761390
if f != fPrev:
1377-
print('font', f.texname, 'scaled', f._scale/pow(2.0, 20))
1391+
print('font', f.texname, 'scaled', f.scale/pow(2.0, 20))
13781392
fPrev = f
13791393
print(x, y, c, 32 <= c < 128 and chr(c) or '.', w)
13801394
for x, y, w, h in page.boxes:

0 commit comments

Comments
 (0)