1
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
12
13
14
15
16
17
18
19
20
21
22 public abstract class PDFFontEmbedder extends FontEmbedder {
23
24
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
42 protected abstract String getSubtype();
43
44
45 protected abstract void addAdditionalEntries(PDFDictionary fontDict)
46 throws IOException;
47
48
49
50
51
52 protected abstract void addAdditionalInitDicts() throws IOException;
53
54
55
56
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
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
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 }