1
2 package org.freehep.graphics2d;
3
4 import java.awt.Graphics2D;
5 import java.awt.font.TextAttribute;
6 import java.awt.font.TextLayout;
7 import java.awt.geom.AffineTransform;
8 import java.text.AttributedString;
9 import java.util.Hashtable;
10 import java.util.Stack;
11 import java.util.Vector;
12
13 import org.freehep.graphics2d.font.FontUtilities;
14
15
16
17
18
19
20
21
22 public class GenericTagHandler extends TagHandler {
23
24
25
26
27 public static Integer UNDERLINE_OVERLINE = new Integer(128);
28
29
30
31
32 private Graphics2D graphics;
33
34
35
36
37
38 private StringBuffer clearedText;
39
40
41
42
43 private Vector
44
45
46
47
48
49
50
51 private Hashtable tags;
52
53
54
55
56 private Stack
57
58
59
60
61
62 private double superscriptCorrection;
63
64
65
66
67
68
69 public GenericTagHandler(Graphics2D graphics) {
70 super();
71 this.graphics = graphics;
72 this.clearedText = new StringBuffer();
73 this.tags = new Hashtable();
74 }
75
76
77
78
79
80
81
82
83
84 public void print(TagString s, double x, double y, double superscriptCorrection) {
85
86 fontFamilyStack = new Stack();
87
88 this.clearedText = new StringBuffer();
89 this.attributes = new Vector();
90 this.superscriptCorrection = superscriptCorrection;
91
92 parse(s);
93
94
95
96 while (tags.size() > 0) {
97 closeTag((String) tags.keys().nextElement());
98 }
99
100
101
102 AttributedString attributedString = new AttributedString(
103 clearedText.toString(),
104 FontUtilities.getAttributes(graphics.getFont()));
105
106
107 for (int i = 0; i < attributes.size(); i++) {
108 ((AttributeEntry)attributes.elementAt(i)).apply(attributedString);
109 }
110
111 graphics.drawString(attributedString.getIterator(), (float)x, (float)y);
112 }
113
114
115
116
117
118
119
120 public TextLayout createTextLayout(TagString s, double superscriptCorrection) {
121
122 fontFamilyStack = new Stack();
123
124 this.clearedText = new StringBuffer();
125 this.attributes = new Vector();
126 this.superscriptCorrection = superscriptCorrection;
127
128 parse(s);
129
130
131
132 while (tags.size() > 0) {
133 closeTag((String) tags.keys().nextElement());
134 }
135
136
137
138 AttributedString attributedString = new AttributedString(
139 clearedText.toString(),
140 FontUtilities.getAttributes(graphics.getFont()));
141
142
143 for (int i = 0; i < attributes.size(); i++) {
144 ((AttributeEntry)attributes.elementAt(i)).apply(attributedString);
145 }
146
147
148 return new TextLayout(
149 attributedString.getIterator(),
150 graphics.getFontRenderContext());
151 }
152
153
154
155
156
157
158
159
160
161
162 protected String openTag(String tag) {
163
164
165
166 if (!tags.containsKey(tag)) {
167 tags.put(tag, new Integer(clearedText.length()));
168 }
169 return "";
170 }
171
172
173
174
175
176
177
178
179 protected String closeTag(String tag) {
180
181 int begin;
182
183
184 if (!tags.containsKey(tag)) {
185 return super.closeTag(tag);
186 } else {
187 begin = ((Integer)tags.get(tag)).intValue();
188 tags.remove(tag);
189 }
190
191
192 if (tag.equalsIgnoreCase("b")) {
193 this.attributes.add(new AttributeEntry(
194 begin,
195 clearedText.length(),
196 TextAttribute.WEIGHT,
197 TextAttribute.WEIGHT_BOLD));
198 } else if (tag.equalsIgnoreCase("i")) {
199 this.attributes.add(new AttributeEntry(
200 begin,
201 clearedText.length(),
202 TextAttribute.POSTURE,
203 TextAttribute.POSTURE_OBLIQUE));
204 } else if (tag.equalsIgnoreCase("s") || tag.equalsIgnoreCase("strike")) {
205 this.attributes.add(new AttributeEntry(
206 begin,
207 clearedText.length(),
208 TextAttribute.STRIKETHROUGH,
209 TextAttribute.STRIKETHROUGH_ON));
210 } else if (tag.equalsIgnoreCase("udash")) {
211 this.attributes.add(new AttributeEntry(
212 begin,
213 clearedText.length(),
214 TextAttribute.UNDERLINE,
215 TextAttribute.UNDERLINE_LOW_DASHED));
216 } else if (tag.equalsIgnoreCase("udot")) {
217 this.attributes.add(new AttributeEntry(
218 begin,
219 clearedText.length(),
220 TextAttribute.UNDERLINE,
221 TextAttribute.UNDERLINE_LOW_DOTTED));
222 } else if (tag.equalsIgnoreCase("u")) {
223 this.attributes.add(new AttributeEntry(
224 begin,
225 clearedText.length(),
226 TextAttribute.UNDERLINE,
227 TextAttribute.UNDERLINE_ON));
228 } else if (tag.equalsIgnoreCase("tt")) {
229 this.attributes.add(new AttributeEntry(
230 begin,
231 clearedText.length(),
232 TextAttribute.FAMILY,
233 fontFamilyStack.pop()));
234 } else if (tag.equalsIgnoreCase("v")) {
235
236 } else if (tag.equalsIgnoreCase("over")) {
237 this.attributes.add(new AttributeEntry(
238 begin,
239 clearedText.length(),
240 TextAttribute.UNDERLINE,
241 UNDERLINE_OVERLINE));
242 } else if (tag.equalsIgnoreCase("sup")) {
243
244
245 this.attributes.add(new AttributeEntry(
246 begin,
247 clearedText.length(),
248 TextAttribute.TRANSFORM,
249 AffineTransform.getTranslateInstance(0, superscriptCorrection)));
250
251 this.attributes.add(new AttributeEntry(
252 begin,
253 clearedText.length(),
254 TextAttribute.SUPERSCRIPT,
255 TextAttribute.SUPERSCRIPT_SUPER));
256
257 } else if (tag.equalsIgnoreCase("sub")) {
258
259
260 this.attributes.add(new AttributeEntry(
261 begin,
262 clearedText.length(),
263 TextAttribute.TRANSFORM,
264 AffineTransform.getTranslateInstance(0, -superscriptCorrection)));
265
266 this.attributes.add(new AttributeEntry(
267 begin,
268 clearedText.length(),
269 TextAttribute.SUPERSCRIPT,
270 TextAttribute.SUPERSCRIPT_SUB));
271 } else {
272 return super.closeTag(tag);
273 }
274
275
276 return "";
277 }
278
279
280
281
282
283
284
285
286
287
288 protected String text(String text) {
289
290 clearedText.append(text);
291
292 return text;
293 }
294
295
296
297
298
299
300
301 private class AttributeEntry {
302
303
304
305
306 private int begin;
307
308
309
310
311 private int end;
312
313
314
315
316
317 private TextAttribute textAttribute;
318
319
320
321
322
323 private Object value;
324
325
326
327
328
329
330
331
332
333 protected AttributeEntry(int begin, int end, TextAttribute textAttribute, Object value) {
334 this.begin = begin;
335 this.end = end;
336 this.textAttribute = textAttribute;
337 this.value = value;
338 }
339
340
341
342
343
344
345 protected void apply(AttributedString as) {
346 as.addAttribute(textAttribute, value, begin, end);
347 }
348 }
349 }