From c565988c3f2f2ee166f7134fac83bfb29c2608d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rg=20Lehmann?= Date: Sun, 21 Oct 2007 13:21:22 +0000 Subject: [PATCH] add bbox to texfont git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@2925 069f4177-920e-0410-937b-c2a4a81bcd90 --- pyx/dvi/mapfile.py | 62 +++++++++++++++++++++++++++--------------------------- pyx/dvi/texfont.py | 20 +++++++++++++----- pyx/font/font.py | 29 ++++++++++++++++++------- 3 files changed, 67 insertions(+), 44 deletions(-) diff --git a/pyx/dvi/mapfile.py b/pyx/dvi/mapfile.py index 42d48f86..73224d72 100644 --- a/pyx/dvi/mapfile.py +++ b/pyx/dvi/mapfile.py @@ -23,12 +23,12 @@ class MAPline: self.encodingfilename = None # supported postscript fragments occuring in psfonts.map - # XXX extendfont not yet implemented + # XXX extendfont not yet implemented self.reencodefont = self.extendfont = self.slant = None - # cache for openend font and encoding - self._font = None - self._encoding = _marker + # cache for openend font and encoding + self._font = None + self._encoding = _marker tokens = [] while len(s): @@ -83,37 +83,37 @@ class MAPline: self.basepsname = self.texname def getfontname(self): - return self.basepsname + return self.basepsname def getfont(self): - if self._font is None: - if self.fontfilename is not None: - fontpath = pykpathsea.find_file(self.fontfilename, pykpathsea.kpse_type1_format) - if not fontpath: - raise RuntimeError("cannot find type 1 font %s" % self.fontfilename) - if fontpath.endswith(".pfb"): - t1font = t1file.PFBfile(fontpath) - else: - t1font = t1file.PFAfile(fontpath) - assert self.basepsname == t1font.name, "corrupt MAP file" - self._font = font.T1font(t1font) - else: - self._font = font.T1builtinfont(self.basepsname) - return self._font + if self._font is None: + if self.fontfilename is not None: + fontpath = pykpathsea.find_file(self.fontfilename, pykpathsea.kpse_type1_format) + if not fontpath: + raise RuntimeError("cannot find type 1 font %s" % self.fontfilename) + if fontpath.endswith(".pfb"): + t1font = t1file.PFBfile(fontpath) + else: + t1font = t1file.PFAfile(fontpath) + assert self.basepsname == t1font.name, "corrupt MAP file" + self._font = font.T1font(t1font, None) + else: + self._font = font.T1builtinfont(self.basepsname, None) + return self._font def getencoding(self): - if self._encoding is _marker: - if self.encodingfilename is not None: - encodingpath = pykpathsea.find_file(self.encodingfilename, pykpathsea.kpse_tex_ps_header_format) - if not encodingpath: - raise RuntimeError("cannot find font encoding file %s" % self.encodingfilename) - ef = encfile.ENCfile(encodingpath) - assert ef.name == "/%s" % self.reencodefont - self._encoding = ef.vector - - else: - self._encoding = None - return self._encoding + if self._encoding is _marker: + if self.encodingfilename is not None: + encodingpath = pykpathsea.find_file(self.encodingfilename, pykpathsea.kpse_tex_ps_header_format) + if not encodingpath: + raise RuntimeError("cannot find font encoding file %s" % self.encodingfilename) + ef = encfile.ENCfile(encodingpath) + assert ef.name == "/%s" % self.reencodefont + self._encoding = ef.vector + + else: + self._encoding = None + return self._encoding def __str__(self): return ("'%s' is '%s' read from '%s' encoded as '%s'" % diff --git a/pyx/dvi/texfont.py b/pyx/dvi/texfont.py index 3dba5376..43dee6f8 100644 --- a/pyx/dvi/texfont.py +++ b/pyx/dvi/texfont.py @@ -1,4 +1,4 @@ -from pyx import canvasitem, font, pykpathsea +from pyx import bbox, canvasitem, font, pykpathsea import tfmfile, vffile from pyx.font import t1file @@ -188,13 +188,23 @@ class TeXtext_pt(canvasitem.canvasitem): self.x_pt = x_pt self.y_pt = y_pt self.charcodes = charcodes - self.size_pt = size_pt + self.size_pt = size_pt + + self.width_pt = sum([self.font.getwidth_pt(charcode) for charcode in charcodes]) + self.height_pt = max([self.font.getheight_pt(charcode) for charcode in charcodes]) + self.depth_pt = max([self.font.getdepth_pt(charcode) for charcode in charcodes]) + + 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) + + def bbox(self): + return self._bbox def processPS(self, file, writer, context, registry, bbox): + bbox += self.bbox() mapline = self.font.getMAPline(writer.getfontmap()) - font = mapline.getfont() - text = font.text_pt(self.x_pt, self.y_pt, self.charcodes, self.size_pt, decoding=mapline.getencoding(), slant=mapline.slant) - text.processPS(file, writer, context, registry, bbox) + font = mapline.getfont() + text = font.text_pt(self.x_pt, self.y_pt, self.charcodes, self.size_pt, decoding=mapline.getencoding(), slant=mapline.slant, ignorebbox=True) + text.processPS(file, writer, context, registry, bbox) def processPDF(self, file, writer, context, registry, bbox): pass diff --git a/pyx/font/font.py b/pyx/font/font.py index 4dca6b7e..4de61f9b 100644 --- a/pyx/font/font.py +++ b/pyx/font/font.py @@ -20,7 +20,7 @@ # along with PyX; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from pyx import canvasitem, pswriter, trafo, unit +from pyx import bbox, canvasitem, pswriter, trafo, unit import t1file try: @@ -164,9 +164,10 @@ class font: class T1font(font): - def __init__(self, t1file): + def __init__(self, t1file, metric): self.t1file = t1file - self.name = t1file.name + self.name = t1file.name + self.metric = metric def text_pt(self, x, y, charcodes, size_pt, **kwargs): return T1text_pt(self, x, y, charcodes, size_pt, **kwargs) @@ -174,9 +175,10 @@ class T1font(font): class T1builtinfont(T1font): - def __init__(self, name): + def __init__(self, name, metric): self.name = name - self.t1file = None + self.t1file = None + self.metric = metric class selectedfont: @@ -193,12 +195,13 @@ class selectedfont: class text_pt(canvasitem.canvasitem): + pass class T1text_pt(text_pt): - def __init__(self, font, x_pt, y_pt, charcodes, size_pt, decoding=None, slant=None): #, **features): + def __init__(self, font, x_pt, y_pt, charcodes, size_pt, decoding=None, slant=None, ignorebbox=False): #, **features): # features: kerning, ligatures if decoding is not None: self.glyphnames = [decoding[character] for character in charcodes] @@ -211,6 +214,15 @@ class T1text_pt(text_pt): self.y_pt = y_pt self.size_pt = size_pt self.slant = slant + self.ignorebbox = ignorebbox + + def bbox(self): + if self.font.metric is None: + raise NotImplementedError("we don't yet have access to the metric") + return bbox.bbox_pt(self.x_pt, + self.y_pt-self.font.metric.depth_pt(self.glyphnames, self.size_pt), + self.x_pt+self.font.metric.width_pt(self.glyphnames, self.size_pt), + self.y_pt+self.font.metric.height_pt(self.glyphnames, self.size_pt)) def getencodingname(self, encodings): """returns the name of the encoding (in encodings) mapping self.glyphnames to codepoints @@ -224,7 +236,7 @@ class T1text_pt(text_pt): for glyphname in glyphnames: if glyphname not in encoding.keys(): glyphsmissing.append(glyphname) - + if len(glyphsmissing) + len(encoding) < 256: # new glyphs fit in existing encoding which will thus be extended for glyphname in glyphsmissing: @@ -236,7 +248,8 @@ class T1text_pt(text_pt): return encodingname def processPS(self, file, writer, context, registry, bbox): - # bbox += self.bbox() + if not self.ignorebbox: + bbox += self.bbox() # register resources if self.font.t1file is not None: -- 2.11.4.GIT