From 091bb29312b821e781e8dad26d0cf392b1de2b23 Mon Sep 17 00:00:00 2001 From: Michael Schindler Date: Thu, 19 Apr 2007 18:53:20 +0000 Subject: [PATCH] implemented font slanting via psfont.map git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@2865 069f4177-920e-0410-937b-c2a4a81bcd90 --- pyx/dvifile.py | 12 ++++++++---- pyx/pswriter.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++----- pyx/type1font.py | 19 +++++++++++++------ 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/pyx/dvifile.py b/pyx/dvifile.py index 75499d53..06248c89 100644 --- a/pyx/dvifile.py +++ b/pyx/dvifile.py @@ -2,7 +2,7 @@ # # # Copyright (C) 2002-2006 Jörg Lehmann -# Copyright (C) 2003-2004,2006 Michael Schindler +# Copyright (C) 2003-2004,2006,2007 Michael Schindler # Copyright (C) 2002-2006 André Wobst # # This file is part of PyX (http://pyx.sourceforge.net/). @@ -762,10 +762,14 @@ class dvifile: raise RuntimeError("cannot find type 1 font %s" % fontmapinfo.fontfile) else: fontfilename = None - + + fontslant = fontmapinfo.slantfont + if fontslant is not None: + fontslant = float(fontslant) + # XXX we currently misuse use self.activefont as metric - font = type1font.font(fontbasefontname, fontfilename, fontencoding, self.activefont) - + font = type1font.font(fontbasefontname, fontfilename, fontencoding, fontslant, self.activefont) + self.activetext = type1font.text_pt(self.pos[_POS_H] * self.pyxconv, -self.pos[_POS_V] * self.pyxconv, font) self.actpage.insert(self.activetext) self.activetext.addchar(char) diff --git a/pyx/pswriter.py b/pyx/pswriter.py index e12abf6a..340bd09a 100644 --- a/pyx/pswriter.py +++ b/pyx/pswriter.py @@ -21,7 +21,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import cStringIO, copy, time, math -import bbox, style, version, type1font, unit +import bbox, style, version, type1font, unit, trafo try: enumerate([]) @@ -118,12 +118,33 @@ class PSfont: font.filename, font.encoding, chars)) + if font.encoding and font.slant: + tmpname = font.name + "tmpunslanted" + # do first the reencoding and then the slanting: + enc_basename, enc_finalname = font.basefontname, tmpname + slt_basename, slt_finalname = tmpname, font.name + elif font.encoding: + enc_basename, enc_finalname = font.basefontname, font.name + elif font.slant: + slt_basename, slt_finalname = font.basefontname, font.name + if font.encoding: registry.add(_ReEncodeFont) registry.add(PSfontencoding(font.encoding)) - registry.add(PSfontreencoding(font.name, - font.basefontname, - font.encoding.name)) + registry.add(PSfontreencoding(enc_finalname, enc_basename, font.encoding.name)) + + if font.slant: + # we need the current fontmatrix in order to manipulate it: + # for this we need to re-read the fontfile as below in + # PSfontfile.ouput: + # XXX Is there a better way to do this? + import font.t1font as t1fontmodule + t1font = t1fontmodule.T1pfbfont(font.filename) + m = t1font.fontmatrixpattern.search(t1font.data1) + m11, m12, m21, m22, v1, v2 = map(float, m.groups()[:6]) + t = trafo.trafo_pt(matrix=((1, font.slant), (0, 1))) + t *= trafo.trafo_pt(matrix=((m11, m12), (m21, m22)), vector=(v1, v2)) + registry.add(PSfontslanting(slt_finalname, slt_basename, t.__str__())) class PSfontfile(PSresource): @@ -200,6 +221,34 @@ class PSfontencoding(PSresource): encodingfile.outputPS(file, writer) +class PSfontslanting(PSresource): + + """ PostScript font slanting directive included in the prolog """ + + def __init__(self, fontname, basefontname, matrixstring): + """ include transformed font directive specified by + + - fontname: PostScript FontName of the new slanted font + - basefontname: PostScript FontName of the original font + - slant: the value of slanting + """ + + self.type = "fontslanting" + self.id = self.fontname = fontname + self.basefontname = basefontname + self.matrixstring = matrixstring + + def output(self, file, writer, registry): + file.write("%%%%BeginProcSet: %s\n" % self.fontname) + file.write("/%s findfont\n" % self.basefontname) + file.write("dup length dict begin\n") + file.write("{ 1 index /FID ne {def} {pop pop} ifelse } forall\n") + file.write("/FontMatrix %s readonly def\n" % self.matrixstring) + file.write("currentdict\n") + file.write("end\n") + file.write("/%s exch definefont pop\n" % self.fontname) + file.write("%%EndProcSet\n") + class PSfontreencoding(PSresource): """ PostScript font re-encoding directive included in the prolog """ @@ -210,7 +259,6 @@ class PSfontreencoding(PSresource): - fontname: PostScript FontName of the new reencoded font - basefontname: PostScript FontName of the original font - encname: name of the encoding - - font: a reference to the font instance (temporarily added for pdf support) Before being able to reencode a font, you have to include the encoding via a fontencoding prolog item with name=encname diff --git a/pyx/type1font.py b/pyx/type1font.py index 1ce67e4c..34f14c10 100644 --- a/pyx/type1font.py +++ b/pyx/type1font.py @@ -3,6 +3,7 @@ # # Copyright (C) 2005-2006 Jörg Lehmann # Copyright (C) 2005-2006 André Wobst +# Copyright (C) 2007 Michael Schindler # # This file is part of PyX (http://pyx.sourceforge.net/). # @@ -98,10 +99,11 @@ class encodingfile: class font: - def __init__(self, basefontname, filename, encoding, metric): + def __init__(self, basefontname, filename, encoding, slant, metric): self.basefontname = basefontname self.filename = filename self.encoding = encoding + self.slant = slant self.metric = metric if encoding is None: @@ -109,6 +111,11 @@ class font: else: self.name = "%s-%s" % (basefontname, encoding.name) + if slant is None: + self.slant = 0.0 + else: + self.name = "%s-slanted%f" % (self.name, self.slant) + class text_pt(canvas.canvasitem): @@ -141,8 +148,8 @@ class text_pt(canvas.canvasitem): pswriter.PSfont(self.font, self.chars, registry) bbox += self.bbox() - if ( context.font is None or - context.font.name != self.font.name or + if ( context.font is None or + context.font.name != self.font.name or context.font.metric.getsize_pt() != self.font.metric.getsize_pt() ): file.write("/%s %f selectfont\n" % (self.font.name, self.font.metric.getsize_pt())) context.font = self.font @@ -159,8 +166,8 @@ class text_pt(canvas.canvasitem): registry.add(pdfwriter.PDFfont(self.font, self.chars, writer, registry)) bbox += self.bbox() - if ( context.font is None or - context.font.name != self.font.name or + if ( context.font is None or + context.font.name != self.font.name or context.font.metric.getsize_pt() != self.font.metric.getsize_pt() ): file.write("/%s %f Tf\n" % (self.font.name, self.font.metric.getsize_pt())) context.font = self.font @@ -171,5 +178,5 @@ class text_pt(canvas.canvasitem): else: ascii = "\\%03o" % char outstring += ascii - file.write("1 0 0 1 %f %f Tm (%s) Tj\n" % (self.x_pt, self.y_pt, outstring)) + file.write("1 0 %f 1 %f %f Tm (%s) Tj\n" % (self.font.slant, self.x_pt, self.y_pt, outstring)) -- 2.11.4.GIT