Debian Codegraph @

freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFFontEmbedder.java

raw
1 // Copyright 2001-2005 freehep
2 package org.freehep.graphicsio.pdf;
3
4 import java.awt.font.FontRenderContext;
5 import java.io.IOException;
6
7 import org.freehep.graphics2d.font.CharTable;
8 import org.freehep.graphicsio.font.FontEmbedder;
9
10 /**
11 * Superclass of all FontEmbedders for PDF documents. Subclasses must implement
12 * all abstract methods which are called in the following order:
13 * <ul>
14 * <li>addAdditionalEntries
15 * <li>addAdditionalInitDicts
16 * <li>writeGlyph once for each glyph in the order of the encoding
17 * </ul>
18 *
19 * @author Simon Fischer
20 * @version $Id: freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFFontEmbedder.java f493ff6e61b2 2005/12/01 18:46:43 duns $
21 */
22 public abstract class PDFFontEmbedder extends FontEmbedder {
23
24 /** sloppily declared protected */
25 protected PDFWriter pdf;
26
27 private PDFDictionary fontDict;
28
29 private String reference;
30
31 private PDFRedundanceTracker redundanceTracker;
32
33 public PDFFontEmbedder(FontRenderContext context, PDFWriter pdf,
34 String reference, PDFRedundanceTracker tracker) {
35 super(context);
36 this.pdf = pdf;
37 this.reference = reference;
38 this.redundanceTracker = tracker;
39 }
40
41 /** Returns the font subtype (currently only Type3). */
42 protected abstract String getSubtype();
43
44 /** Add additional entries to the font Dictionary. */
45 protected abstract void addAdditionalEntries(PDFDictionary fontDict)
46 throws IOException;
47
48 /**
49 * Add additional dicionaries to the PDFWriter which may be referenced by
50 * entries generated by <tt>addAdditionalEntries()</tt>
51 */
52 protected abstract void addAdditionalInitDicts() throws IOException;
53
54 /**
55 * Returns the reference String that identifies the font dictionary. Use
56 * this reference plus underscore plus suffix for other dictionaries.
57 */
58 protected String getReference() {
59 return reference;
60 }
61
62 protected void openIncludeFont() throws IOException {
63
64 fontDict = pdf.openDictionary(reference);
65
66 fontDict.entry("Type", pdf.name("Font"));
67 fontDict.entry("Subtype", pdf.name(getSubtype()));
68 fontDict.entry("Name", pdf.name(getFontName()));
69
70 fontDict.entry("FirstChar", 0);
71 fontDict.entry("LastChar", 255);
72 // fontDict.entry("Encoding", pdf.ref(reference+"Encoding"));
73 fontDict.entry("Encoding", redundanceTracker.getReference(
74 getEncodingTable(), PDFCharTableWriter.getInstance()));
75 fontDict.entry("Widths", pdf.ref(reference + "Widths"));
76
77 addAdditionalEntries(fontDict);
78
79 pdf.close(fontDict);
80
81 addAdditionalInitDicts();
82 }
83
84 protected void closeEmbedFont() {
85 }
86
87 protected void writeWidths(double[] widths) throws IOException {
88 Object[] widthsObj = new Object[256];
89 for (int i = 0; i < widthsObj.length; i++) {
90 widthsObj[i] = new Double(widths[i]);
91 }
92 pdf.object(reference + "Widths", widthsObj);
93 }
94
95 protected void writeEncoding(CharTable charTable) throws IOException {
96 // writeEncoding(pdf, reference+"Encoding", charTable);
97 }
98
99 public static void writeEncoding(PDFWriter pdf, String ref,
100 CharTable charTable) throws IOException {
101 PDFDictionary encoding = pdf.openDictionary(ref);
102 encoding.entry("Type", pdf.name("Encoding"));
103
104 Object[] differences = new Object[257];
105 differences[0] = new Integer(0);
106 for (int i = 0; i < 256; i++) {
107 String charName = charTable.toName(i);
108 differences[i + 1] = (charName != null) ? pdf.name(charName) : pdf
109 .name(NOTDEF);
110 }
111 encoding.entry("Differences", differences);
112
113 pdf.close(encoding);
114 }
115
116 protected String createCharacterReference(String characterName) {
117 return "Glyph_" + reference + ":" + characterName;
118 }
119
120 }