1
2 package org.freehep.graphicsio.font;
3
4 import java.awt.Font;
5 import java.awt.font.FontRenderContext;
6 import java.awt.geom.Rectangle2D;
7 import java.io.IOException;
8
9 import org.freehep.graphics2d.font.CharTable;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public abstract class FontIncluder {
28
29 public static final double FONT_SIZE = 1000;
30
31
32
33
34
35
36
37
38 protected abstract void openIncludeFont() throws IOException;
39
40
41 protected abstract void writeEncoding(CharTable charTable)
42 throws IOException;
43
44
45 protected void closeIncludeFont() throws IOException {
46 }
47
48
49
50 private FontRenderContext context;
51
52 private Rectangle2D fontBBox;
53
54 private Font font;
55
56 private String fontName;
57
58 private CharTable charTable;
59
60 private char[] unicode;
61
62 private String[] charName;
63
64 private int noDefinedChars;
65
66 public FontIncluder(FontRenderContext context) {
67 this.context = context;
68 this.noDefinedChars = -1;
69 }
70
71
72
73 protected FontRenderContext getContext() {
74 return context;
75 }
76
77 protected String getFontName() {
78 return fontName;
79 }
80
81 protected Font getFont() {
82 return font;
83 }
84
85 protected CharTable getEncodingTable() {
86 return charTable;
87 }
88
89 protected Rectangle2D getFontBBox() {
90 return fontBBox;
91 }
92
93 protected String getCharName(int i) {
94 return charName[i];
95 }
96
97 protected char getUnicode(int i) {
98 return unicode[i];
99 }
100
101 protected char[] getUnicode() {
102 return unicode;
103 }
104
105 protected int getNODefinedChars() {
106 return noDefinedChars;
107 }
108
109
110
111
112
113
114
115
116
117
118 public void includeFont(Font font, CharTable charTable, String name)
119 throws IOException {
120
121 unicode = null;
122 charName = null;
123
124 this.font = font;
125 this.charTable = charTable;
126 this.fontName = name;
127
128
129 this.fontBBox = font.getMaxCharBounds(context);
130
131
132
133 this.noDefinedChars = 0;
134 this.unicode = new char[256];
135 this.charName = new String[256];
136 for (int i = 0; i < unicode.length; i++) {
137 charName[i] = charTable.toName(i);
138 if (charName[i] != null) {
139 unicode[i] = charTable.toUnicode(charName[i]);
140 noDefinedChars++;
141 } else {
142 unicode[i] = 0;
143 }
144 }
145
146 openIncludeFont();
147 writeEncoding(charTable);
148 closeIncludeFont();
149 }
150
151 protected double getUndefinedWidth() {
152 return FONT_SIZE;
153 }
154 }