1 # -*- coding: ISO-8859-1 -*-
4 # Copyright (C) 2005-2007 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2005-2007 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 from pyx
import bbox
, canvasitem
, deco
, path
, pswriter
, pdfwriter
, trafo
, unit
30 from sets
import Set
as set
33 ##############################################################################
35 ##############################################################################
37 class PST1file(pswriter
.PSresource
):
39 """ PostScript font definition included in the prolog """
41 def __init__(self
, t1file
, glyphnames
, charcodes
):
42 """ include type 1 font t1file stripped to the given glyphnames"""
46 self
.glyphnames
= set(glyphnames
)
47 self
.charcodes
= set(charcodes
)
49 def merge(self
, other
):
50 self
.glyphnames
.update(other
.glyphnames
)
51 self
.charcodes
.update(other
.charcodes
)
53 def output(self
, file, writer
, registry
):
54 file.write("%%%%BeginFont: %s\n" % self
.t1file
.name
)
57 file.write("%%Included glyphs: %s\n" % " ".join(self
.glyphnames
))
59 file.write("%%Included charcodes: %s\n" % " ".join([str(charcode
) for charcode
in self
.charcodes
]))
60 self
.t1file
.getstrippedfont(self
.glyphnames
, self
.charcodes
).outputPS(file, writer
)
62 self
.t1file
.outputPS(file, writer
)
63 file.write("\n%%EndFont\n")
66 _ReEncodeFont
= pswriter
.PSdefinition("ReEncodeFont", """{
71 /basefontname exch def
72 /basefontdict basefontname findfont def
73 /newfontdict basefontdict maxlength dict def
75 exch dup dup /FID ne exch /Encoding ne and
76 { exch newfontdict 3 1 roll put }
80 newfontdict /FontName newfontname put
81 newfontdict /Encoding newencoding put
82 newfontname newfontdict definefont pop
87 class PSreencodefont(pswriter
.PSresource
):
89 """ reencoded PostScript font"""
91 def __init__(self
, basefontname
, newfontname
, encoding
):
92 """ reencode the font """
94 self
.type = "reencodefont"
95 self
.basefontname
= basefontname
96 self
.id = self
.newfontname
= newfontname
97 self
.encoding
= encoding
99 def output(self
, file, writer
, registry
):
100 file.write("%%%%BeginResource: %s\n" % self
.newfontname
)
101 file.write("/%s /%s\n[" % (self
.basefontname
, self
.newfontname
))
102 vector
= [None] * len(self
.encoding
)
103 for glyphname
, charcode
in self
.encoding
.items():
104 vector
[charcode
] = glyphname
105 for i
, glyphname
in enumerate(vector
):
111 file.write("/%s" % glyphname
)
113 file.write("ReEncodeFont\n")
114 file.write("%%EndResource\n")
117 _ChangeFontMatrix
= pswriter
.PSdefinition("ChangeFontMatrix", """{
120 /newfontmatrix exch def
121 /newfontname exch def
122 /basefontname exch def
123 /basefontdict basefontname findfont def
124 /newfontdict basefontdict maxlength dict def
126 exch dup dup /FID ne exch /FontMatrix ne and
127 { exch newfontdict 3 1 roll put }
131 newfontdict /FontName newfontname put
132 newfontdict /FontMatrix newfontmatrix readonly put
133 newfontname newfontdict definefont pop
138 class PSchangefontmatrix(pswriter
.PSresource
):
140 """ change font matrix of a PostScript font"""
142 def __init__(self
, basefontname
, newfontname
, newfontmatrix
):
143 """ change the font matrix """
145 self
.type = "changefontmatrix"
146 self
.basefontname
= basefontname
147 self
.id = self
.newfontname
= newfontname
148 self
.newfontmatrix
= newfontmatrix
150 def output(self
, file, writer
, registry
):
151 file.write("%%%%BeginResource: %s\n" % self
.newfontname
)
152 file.write("/%s /%s\n" % (self
.basefontname
, self
.newfontname
))
153 file.write(str(self
.newfontmatrix
))
154 file.write("\nChangeFontMatrix\n")
155 file.write("%%EndResource\n")
158 ##############################################################################
160 ##############################################################################
162 class PDFfont(pdfwriter
.PDFobject
):
164 def __init__(self
, fontname
, basefontname
, charcodes
, fontdescriptor
, encoding
, metric
):
165 pdfwriter
.PDFobject
.__init
__(self
, "font", fontname
)
167 self
.fontname
= fontname
168 self
.basefontname
= basefontname
169 self
.charcodes
= set(charcodes
)
170 self
.fontdescriptor
= fontdescriptor
171 self
.encoding
= encoding
174 def merge(self
, other
):
175 self
.charcodes
.update(other
.charcodes
)
177 def write(self
, file, writer
, registry
):
181 file.write("/Name /%s\n" % self
.fontname
)
182 file.write("/BaseFont /%s\n" % self
.basefontname
)
183 firstchar
= min(self
.charcodes
)
184 lastchar
= max(self
.charcodes
)
185 file.write("/FirstChar %d\n" % firstchar
)
186 file.write("/LastChar %d\n" % lastchar
)
187 file.write("/Widths\n"
190 encoding
= self
.encoding
.getvector()
192 encoding
= self
.fontdescriptor
.fontfile
.t1file
.encoding
193 for i
in range(firstchar
, lastchar
+1):
199 if self
.metric
is not None:
200 file.write("%i" % self
.metric
.width_ds(encoding
[i
]))
202 file.write("%i" % self
.fontdescriptor
.fontfile
.t1file
.getglyphinfo(encoding
[i
])[0])
204 file.write("/FontDescriptor %d 0 R\n" % registry
.getrefno(self
.fontdescriptor
))
206 file.write("/Encoding %d 0 R\n" % registry
.getrefno(self
.encoding
))
210 class PDFfontdescriptor(pdfwriter
.PDFobject
):
212 def __init__(self
, fontname
, fontfile
, metric
):
213 pdfwriter
.PDFobject
.__init
__(self
, "fontdescriptor", fontname
)
214 self
.fontname
= fontname
215 self
.fontfile
= fontfile
218 def write(self
, file, writer
, registry
):
220 "/Type /FontDescriptor\n"
221 "/FontName /%s\n" % self
.fontname
)
222 if self
.metric
is not None:
223 self
.metric
.writePDFfontinfo(file)
225 self
.fontfile
.t1file
.writePDFfontinfo(file)
226 if self
.fontfile
is not None:
227 file.write("/FontFile %d 0 R\n" % registry
.getrefno(self
.fontfile
))
231 class PDFfontfile(pdfwriter
.PDFobject
):
233 def __init__(self
, t1file
, glyphnames
, charcodes
):
234 pdfwriter
.PDFobject
.__init
__(self
, "fontfile", t1file
.name
)
236 self
.glyphnames
= set(glyphnames
)
237 self
.charcodes
= set(charcodes
)
239 def merge(self
, other
):
240 self
.glyphnames
.update(other
.glyphnames
)
241 self
.charcodes
.update(other
.charcodes
)
243 def write(self
, file, writer
, registry
):
244 if writer
.stripfonts
:
245 self
.t1file
.getstrippedfont(self
.glyphnames
, self
.charcodes
).outputPDF(file, writer
)
247 self
.t1file
.outputPDF(file, writer
)
250 class PDFencoding(pdfwriter
.PDFobject
):
252 def __init__(self
, encoding
, name
):
253 pdfwriter
.PDFobject
.__init
__(self
, "encoding", name
)
254 self
.encoding
= encoding
257 # As self.encoding might be appended after the constructur has set it,
258 # we need to defer the calculation until the whole content was constructed.
259 vector
= [None] * len(self
.encoding
)
260 for glyphname
, charcode
in self
.encoding
.items():
261 vector
[charcode
] = glyphname
264 def write(self
, file, writer
, registry
):
269 for i
, glyphname
in enumerate(self
.getvector()):
275 file.write("/%s" % glyphname
)
280 ##############################################################################
281 # basic PyX text output
282 ##############################################################################
286 def text(self
, x
, y
, charcodes
, size_pt
, **kwargs
):
287 return self
.text_pt(unit
.topt(x
), unit
.topt(y
), charcodes
, size_pt
, **kwargs
)
292 def __init__(self
, t1file
, metric
):
294 self
.name
= t1file
.name
297 def text_pt(self
, x
, y
, charcodes
, size_pt
, **kwargs
):
298 return T1text_pt(self
, x
, y
, charcodes
, size_pt
, **kwargs
)
301 class T1builtinfont(T1font
):
303 def __init__(self
, name
, metric
):
311 def __init__(self
, name
, size_pt
):
313 self
.size_pt
= size_pt
315 def __ne__(self
, other
):
316 return self
.name
!= other
.name
or self
.size_pt
!= other
.size_pt
318 def outputPS(self
, file, writer
):
319 file.write("/%s %f selectfont\n" % (self
.name
, self
.size_pt
))
321 def outputPDF(self
, file, writer
):
322 file.write("/%s %f Tf\n" % (self
.name
, self
.size_pt
))
325 class text_pt(canvasitem
.canvasitem
):
330 class T1text_pt(text_pt
):
332 def __init__(self
, font
, x_pt
, y_pt
, charcodes
, size_pt
, decoding
=None, slant
=None, ignorebbox
=False, kerning
=False, ligatures
=False):
333 if decoding
is not None:
334 self
.glyphnames
= [decoding
[character
] for character
in charcodes
]
337 self
.charcodes
= charcodes
342 self
.size_pt
= size_pt
344 self
.ignorebbox
= ignorebbox
345 self
.kerning
= kerning
346 self
.ligatures
= ligatures
348 if self
.kerning
and not self
.decode
:
349 raise ValueError("decoding required for font metric access (kerning)")
350 if self
.ligatures
and not self
.decode
:
351 raise ValueError("decoding required for font metric access (ligatures)")
353 self
.glyphnames
= self
.font
.metric
.resolveligatures(self
.glyphnames
)
356 if self
.font
.metric
is None:
357 raise ValueError("metric missing")
359 raise ValueError("decoding required for font metric access (bbox)")
360 return bbox
.bbox_pt(self
.x_pt
,
361 self
.y_pt
+self
.font
.metric
.depth_pt(self
.glyphnames
, self
.size_pt
),
362 self
.x_pt
+self
.font
.metric
.width_pt(self
.glyphnames
, self
.size_pt
),
363 self
.y_pt
+self
.font
.metric
.height_pt(self
.glyphnames
, self
.size_pt
))
365 def getencodingname(self
, encodings
):
366 """returns the name of the encoding (in encodings) mapping self.glyphnames to codepoints
367 If no such encoding can be found or extended, a new encoding is added to encodings
369 glyphnames
= set(self
.glyphnames
)
370 if len(glyphnames
) > 256:
371 raise ValueError("glyphs do not fit into one single encoding")
372 for encodingname
, encoding
in encodings
.items():
374 for glyphname
in glyphnames
:
375 if glyphname
not in encoding
.keys():
376 glyphsmissing
.append(glyphname
)
378 if len(glyphsmissing
) + len(encoding
) < 256:
379 # new glyphs fit in existing encoding which will thus be extended
380 for glyphname
in glyphsmissing
:
381 encoding
[glyphname
] = len(encoding
)
383 # create a new encoding for the glyphnames
384 encodingname
= "encoding%d" % len(encodings
)
385 encodings
[encodingname
] = dict([(glyphname
, i
) for i
, glyphname
in enumerate(glyphnames
)])
388 def processPS(self
, file, writer
, context
, registry
, bbox
):
389 if not self
.ignorebbox
:
392 if writer
.textaspath
:
395 data
= self
.font
.metric
.resolvekernings(self
.glyphnames
, self
.size_pt
)
397 data
= self
.glyphnames
399 data
= self
.charcodes
400 textpath
= path
.path()
401 for i
, value
in enumerate(data
):
402 if self
.kerning
and i
% 2:
403 if value
is not None:
406 glyphpath
, wx_pt
, wy_pt
= self
.font
.t1file
.getglyphpathwxwy_pt(value
, self
.size_pt
, convertcharcode
=not self
.decode
)
407 textpath
+= glyphpath
.transformed(trafo
.translate_pt(self
.x_pt
, self
.y_pt
))
410 deco
.decoratedpath(textpath
, fillstyles
=[]).processPS(file, writer
, context
, registry
, bbox
)
413 if self
.font
.t1file
is not None:
415 registry
.add(PST1file(self
.font
.t1file
, self
.glyphnames
, []))
417 registry
.add(PST1file(self
.font
.t1file
, [], self
.charcodes
))
419 fontname
= self
.font
.name
421 encodingname
= self
.getencodingname(context
.encodings
.setdefault(self
.font
.name
, {}))
422 encoding
= context
.encodings
[self
.font
.name
][encodingname
]
423 newfontname
= "%s-%s" % (fontname
, encodingname
)
424 registry
.add(_ReEncodeFont
)
425 registry
.add(PSreencodefont(fontname
, newfontname
, encoding
))
426 fontname
= newfontname
429 newfontmatrix
= trafo
.trafo_pt(matrix
=((1, self
.slant
), (0, 1))) * self
.font
.t1file
.fontmatrix
430 newfontname
= "%s-slant%f" % (fontname
, self
.slant
)
431 registry
.add(_ChangeFontMatrix
)
432 registry
.add(PSchangefontmatrix(fontname
, newfontname
, newfontmatrix
))
433 fontname
= newfontname
435 # select font if necessary
436 sf
= selectedfont(fontname
, self
.size_pt
)
437 if context
.selectedfont
is None or sf
!= context
.selectedfont
:
438 context
.selectedfont
= sf
439 sf
.outputPS(file, writer
)
441 file.write("%f %f moveto (" % (self
.x_pt
, self
.y_pt
))
444 data
= self
.font
.metric
.resolvekernings(self
.glyphnames
, self
.size_pt
)
446 data
= self
.glyphnames
448 data
= self
.charcodes
449 for i
, value
in enumerate(data
):
450 if self
.kerning
and i
% 2:
451 if value
is not None:
452 file.write(") show\n%f 0 rmoveto (" % value
)
455 value
= encoding
[value
]
456 if 32 < value
< 127 and chr(value
) not in "()[]<>\\":
457 file.write("%s" % chr(value
))
459 file.write("\\%03o" % value
)
460 file.write(") show\n")
462 def processPDF(self
, file, writer
, context
, registry
, bbox
):
463 if not self
.ignorebbox
:
466 if writer
.textaspath
:
469 data
= self
.font
.metric
.resolvekernings(self
.glyphnames
, self
.size_pt
)
471 data
= self
.glyphnames
473 data
= self
.charcodes
474 textpath
= path
.path()
475 for i
, value
in enumerate(data
):
476 if self
.kerning
and i
% 2:
477 if value
is not None:
480 glyphpath
, wx_pt
, wy_pt
= self
.font
.t1file
.getglyphpathwxwy_pt(value
, self
.size_pt
, convertcharcode
=not self
.decode
)
481 textpath
+= glyphpath
.transformed(trafo
.translate_pt(self
.x_pt
, self
.y_pt
))
484 deco
.decoratedpath(textpath
, fillstyles
=[]).processPDF(file, writer
, context
, registry
, bbox
)
487 encodingname
= self
.getencodingname(context
.encodings
.setdefault(self
.font
.name
, {}))
488 encoding
= context
.encodings
[self
.font
.name
][encodingname
]
489 charcodes
= [encoding
[glyphname
] for glyphname
in self
.glyphnames
]
491 charcodes
= self
.charcodes
494 fontname
= self
.font
.name
496 newfontname
= "%s-%s" % (fontname
, encodingname
)
497 _encoding
= PDFencoding(encoding
, newfontname
)
498 fontname
= newfontname
501 if self
.font
.t1file
is not None:
503 fontfile
= PDFfontfile(self
.font
.t1file
, self
.glyphnames
, [])
505 fontfile
= PDFfontfile(self
.font
.t1file
, [], self
.charcodes
)
508 fontdescriptor
= PDFfontdescriptor(self
.font
.name
, fontfile
, self
.font
.metric
)
509 font
= PDFfont(fontname
, self
.font
.name
, charcodes
, fontdescriptor
, _encoding
, self
.font
.metric
)
512 if fontfile
is not None:
513 registry
.add(fontfile
)
514 registry
.add(fontdescriptor
)
515 if _encoding
is not None:
516 registry
.add(_encoding
)
519 registry
.addresource("Font", fontname
, font
, procset
="Text")
521 if self
.slant
is None:
524 slantvalue
= self
.slant
526 # select font if necessary
527 sf
= selectedfont(fontname
, self
.size_pt
)
528 if context
.selectedfont
is None or sf
!= context
.selectedfont
:
529 context
.selectedfont
= sf
530 sf
.outputPDF(file, writer
)
533 file.write("1 0 %f 1 %f %f Tm [(" % (slantvalue
, self
.x_pt
, self
.y_pt
))
535 file.write("1 0 %f 1 %f %f Tm (" % (slantvalue
, self
.x_pt
, self
.y_pt
))
538 data
= self
.font
.metric
.resolvekernings(self
.glyphnames
)
540 data
= self
.glyphnames
542 data
= self
.charcodes
543 for i
, value
in enumerate(data
):
544 if self
.kerning
and i
% 2:
545 if value
is not None:
546 file.write(")%f(" % (-value
))
549 value
= encoding
[value
]
550 if 32 <= value
<= 127 and chr(value
) not in "()[]<>\\":
551 file.write("%s" % chr(value
))
553 file.write("\\%03o" % value
)
555 file.write(")] TJ\n")