1 from pyx
import bbox
, canvasitem
, font
, pykpathsea
7 def __init__(self
, name
, c
, q
, d
, tfmconv
, pyxconv
, debug
=0):
9 self
.q
= q
# desired size of font (fix_word) in TeX points
10 self
.d
= d
# design size of font (fix_word) in TeX points
11 self
.tfmconv
= tfmconv
# conversion factor from tfm units to dvi units
12 self
.pyxconv
= pyxconv
# conversion factor from dvi units to PostScript points
13 tfmpath
= pykpathsea
.find_file("%s.tfm" % self
.name
, pykpathsea
.kpse_tfm_format
)
15 raise TFMError("cannot find %s.tfm" % self
.name
)
16 self
.TFMfile
= tfmfile
.TFMfile(tfmpath
, debug
)
18 # We only check for equality of font checksums if none of them
19 # is zero. The case c == 0 happend in some VF files and
20 # according to the VFtoVP documentation, paragraph 40, a check
21 # is only performed if TFMfile.checksum > 0. Anyhow, being
22 # more generous here seems to be reasonable
23 if self
.TFMfile
.checksum
!= c
and self
.TFMfile
.checksum
!= 0 and c
!= 0:
24 raise DVIError("check sums do not agree: %d vs. %d" %
25 (self
.TFMfile
.checksum
, c
))
27 # Check whether the given design size matches the one defined in the tfm file
28 if abs(self
.TFMfile
.designsize
- d
) > 2:
29 raise DVIError("design sizes do not agree: %d vs. %d" % (self
.TFMfile
.designsize
, d
))
30 #if q < 0 or q > 134217728:
31 # raise DVIError("font '%s' not loaded: bad scale" % self.name)
32 if d
< 0 or d
> 134217728:
33 raise DVIError("font '%s' not loaded: bad design size" % self
.name
)
38 return "font %s designed at %g TeX pts used at %g TeX pts" % (self
.name
,
39 16.0*self
.d
/16777216L,
40 16.0*self
.q
/16777216L)
43 """ return size of font in (PS) points """
44 # The factor 16L/16777216L=2**(-20) converts a fix_word (here self.q)
45 # to the corresponding float. Furthermore, we have to convert from TeX
46 # points to points, hence the factor 72/72.27.
47 return 16L*self
.q
/16777216L*72/72.27
49 def _convert_tfm_to_dvi(self
, length
):
50 # doing the integer math with long integers will lead to different roundings
51 # return 16*length*int(round(self.q*self.tfmconv))/16777216
53 # Knuth instead suggests the following algorithm based on 4 byte integer logic only
54 # z = int(round(self.q*self.tfmconv))
55 # b0, b1, b2, b3 = [ord(c) for c in struct.pack(">L", length)]
56 # assert b0 == 0 or b0 == 255
62 # result = ( ( ( ( ( b3 * z ) >> 8 ) + ( b2 * z ) ) >> 8 ) + ( b1 * z ) ) >> shift
64 # result = result - (z << (8-shift))
66 # however, we can simplify this using a single long integer multiplication,
67 # but take into account the transformation of z
68 z
= int(round(self
.q
*self
.tfmconv
))
69 assert -16777216 <= length
< 16777216 # -(1 << 24) <= length < (1 << 24)
70 assert z
< 134217728 # 1 << 27
72 while z
>= 8388608: # 1 << 23
75 # length*z is a long integer, but the result will be a regular integer
76 return int(length
*long(z
) >> shift
)
78 def _convert_tfm_to_pt(self
, length
):
79 return (16*long(round(length
*float(self
.q
)*self
.tfmconv
))/16777216) * self
.pyxconv
81 # routines returning lengths as integers in dvi units
83 def getwidth_dvi(self
, charcode
):
84 return self
._convert
_tfm
_to
_dvi
(self
.TFMfile
.width
[self
.TFMfile
.char_info
[charcode
].width_index
])
86 def getheight_dvi(self
, charcode
):
87 return self
._convert
_tfm
_to
_dvi
(self
.TFMfile
.height
[self
.TFMfile
.char_info
[charcode
].height_index
])
89 def getdepth_dvi(self
, charcode
):
90 return self
._convert
_tfm
_to
_dvi
(self
.TFMfile
.depth
[self
.TFMfile
.char_info
[charcode
].depth_index
])
92 def getitalic_dvi(self
, charcode
):
93 return self
._convert
_tfm
_to
_dvi
(self
.TFMfile
.italic
[self
.TFMfile
.char_info
[charcode
].italic_index
])
95 # routines returning lengths as floats in PostScript points
97 def getwidth_pt(self
, charcode
):
98 return self
._convert
_tfm
_to
_pt
(self
.TFMfile
.width
[self
.TFMfile
.char_info
[charcode
].width_index
])
100 def getheight_pt(self
, charcode
):
101 return self
._convert
_tfm
_to
_pt
(self
.TFMfile
.height
[self
.TFMfile
.char_info
[charcode
].height_index
])
103 def getdepth_pt(self
, charcode
):
104 return self
._convert
_tfm
_to
_pt
(self
.TFMfile
.depth
[self
.TFMfile
.char_info
[charcode
].depth_index
])
106 def getitalic_pt(self
, charcode
):
107 return self
._convert
_tfm
_to
_pt
(self
.TFMfile
.italic
[self
.TFMfile
.char_info
[charcode
].italic_index
])
109 def text_pt(self
, x_pt
, y_pt
, charcodes
, fontmap
=None):
110 return TeXtext_pt(self
, x_pt
, y_pt
, charcodes
, self
.getsize_pt(), fontmap
=fontmap
)
112 def getMAPline(self
, fontmap
):
113 if self
.name
not in fontmap
:
114 raise RuntimeError("missing font information for '%s'; check fontmapping file(s)" % self
.name
)
115 return fontmap
[self
.name
]
118 class virtualfont(TeXfont
):
120 def __init__(self
, name
, path
, c
, q
, d
, tfmconv
, pyxconv
, debug
=0):
121 TeXfont
.__init
__(self
, name
, c
, q
, d
, tfmconv
, pyxconv
, debug
)
122 self
.vffile
= vffile
.vffile(path
, self
.scale
, tfmconv
, pyxconv
, debug
> 1)
125 """ return fonts used in virtual font itself """
126 return self
.vffile
.getfonts()
128 def getchar(self
, cc
):
129 """ return dvi chunk corresponding to char code cc """
130 return self
.vffile
.getchar(cc
)
132 def text_pt(self
, *args
, **kwargs
):
133 raise RuntimeError("you don't know what you're doing")
136 class TeXtext_pt(font
.text_pt
):
138 def __init__(self
, font
, x_pt
, y_pt
, charcodes
, size_pt
, fontmap
=None):
142 self
.charcodes
= charcodes
143 self
.size_pt
= size_pt
144 self
.fontmap
= fontmap
146 self
.width_pt
= sum([self
.font
.getwidth_pt(charcode
) for charcode
in charcodes
])
147 self
.height_pt
= max([self
.font
.getheight_pt(charcode
) for charcode
in charcodes
])
148 self
.depth_pt
= max([self
.font
.getdepth_pt(charcode
) for charcode
in charcodes
])
150 self
._bbox
= bbox
.bbox_pt(self
.x_pt
, self
.y_pt
-self
.depth_pt
, self
.x_pt
+self
.width_pt
, self
.y_pt
+self
.height_pt
)
155 def processPS(self
, file, writer
, context
, registry
, bbox
):
157 if self
.fontmap
is not None:
158 mapline
= self
.font
.getMAPline(self
.fontmap
)
160 mapline
= self
.font
.getMAPline(writer
.getfontmap())
161 font
= mapline
.getfont()
162 text
= font
.text_pt(self
.x_pt
, self
.y_pt
, self
.charcodes
, self
.size_pt
, decoding
=mapline
.getencoding(), slant
=mapline
.slant
, ignorebbox
=True)
163 text
.processPS(file, writer
, context
, registry
, bbox
)
165 def processPDF(self
, file, writer
, context
, registry
, bbox
):
168 mapline
= self
.font
.getMAPline(writer
.getfontmap())
169 font
= mapline
.getfont()
170 text
= font
.text_pt(self
.x_pt
, self
.y_pt
, self
.charcodes
, self
.size_pt
, decoding
=mapline
.getencoding(), slant
=mapline
.slant
, ignorebbox
=True)
171 text
.processPDF(file, writer
, context
, registry
, bbox
)