1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.svg;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.CharacterIterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.batik.bridge.Mark;
import org.apache.batik.bridge.StrokingTextPainter;
import org.apache.batik.bridge.TextNode;
import org.apache.batik.bridge.TextPainter;
import org.apache.batik.gvt.text.GVTAttributedCharacterIterator;
import org.apache.batik.gvt.text.TextPaintInfo;
import org.apache.fop.afp.AFPGraphics2D;
import org.apache.fop.fonts.Font;
/**
* Renders the attributed character iterator of a {@link TextNode}.
* This class draws the text directly into the Graphics2D so that
* the text is not drawn using shapes.
* If the text is simple enough to draw then it sets the font and calls
* drawString. If the text is complex or the cannot be translated
* into a simple drawString the StrokingTextPainter is used instead.
*/
public abstract class AbstractFOPTextPainter implements TextPainter {
/** the logger for this class */
protected Log log = LogFactory.getLog(AbstractFOPTextPainter.class);
private final FOPTextHandler nativeTextHandler;
/**
* Use the stroking text painter to get the bounds and shape.
* Also used as a fallback to draw the string with strokes.
*/
private final TextPainter proxyTextPainter;
/**
* Create a new PS text painter with the given font information.
* @param nativeTextHandler the NativeTextHandler instance used for text painting
*/
public AbstractFOPTextPainter(FOPTextHandler nativeTextHandler, TextPainter proxyTextPainter) {
this.nativeTextHandler = nativeTextHandler;
this.proxyTextPainter = proxyTextPainter;
}
/**
* Paints the specified attributed character iterator using the
* specified Graphics2D and context and font context.
*
* @param node the TextNode to paint
* @param g2d the Graphics2D to use
*/
public void paint(TextNode node, Graphics2D g2d) {
if (isSupportedGraphics2D(g2d)) {
new TextRunPainter().paintTextRuns(node.getTextRuns(), g2d, node.getLocation());
}
proxyTextPainter.paint(node, g2d);
}
/**
* Checks whether the Graphics2D is compatible with this text painter. Batik may
* pass in a Graphics2D instance that paints on a special buffer image, for example
* for filtering operations. In that case, the text painter should be bypassed.
* @param g2d the Graphics2D instance to check
* @return true if the Graphics2D is supported
*/
protected abstract boolean isSupportedGraphics2D(Graphics2D g2d);
private class TextRunPainter {
private Point2D currentLocation;
public void paintTextRuns(Iterable<StrokingTextPainter.TextRun> textRuns, Graphics2D g2d,
Point2D nodeLocation) {
currentLocation = new Point2D.Double(nodeLocation.getX(), nodeLocation.getY());
for (StrokingTextPainter.TextRun run : textRuns) {
paintTextRun(run, g2d);
}
}
private void paintTextRun(StrokingTextPainter.TextRun run, Graphics2D g2d) {
AttributedCharacterIterator aci = run.getACI();
aci.first();
updateLocationFromACI(aci, currentLocation);
// font
Font font = getFont(aci);
if (font != null) {
nativeTextHandler.setOverrideFont(font);
}
// color
TextPaintInfo tpi = (TextPaintInfo) aci.getAttribute(
GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);
if (tpi == null) {
return;
}
Paint foreground = tpi.fillPaint;
if (foreground instanceof Color) {
Color col = (Color) foreground;
g2d.setColor(col);
}
g2d.setPaint(foreground);
// text anchor
TextNode.Anchor anchor = (TextNode.Anchor) aci.getAttribute(
GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE);
// text
String txt = getText(aci);
double advance = font == null ? run.getLayout().getAdvance2D().getX() : getStringWidth(txt, font);
double tx = 0;
if (anchor != null) {
switch (anchor.getType()) {
case TextNode.Anchor.ANCHOR_MIDDLE:
tx = -advance / 2;
break;
case TextNode.Anchor.ANCHOR_END:
tx = -advance;
break;
default: //nop
}
}
// draw string
Point2D outputLocation = g2d.getTransform().transform(currentLocation, null);
double x = outputLocation.getX();
double y = outputLocation.getY();
try {
try {
AFPGraphics2D afpg2d = (AFPGraphics2D)g2d;
int fontSize = 0;
if (font != null) {
fontSize = (int) Math.round(afpg2d.convertToAbsoluteLength(font.getFontSize()));
}
if (fontSize < 6000) {
nativeTextHandler.drawString(g2d, txt, (float) (x + tx), (float) y);
} else {
double scaleX = g2d.getTransform().getScaleX();
for (int i = 0; i < txt.length(); i++) {
double ad = run.getLayout().getGlyphAdvances()[i] * scaleX;
nativeTextHandler.drawString(g2d, txt.charAt(i) + "", (float) (x + tx + ad), (float) y);
}
}
//TODO draw underline and overline if set
//TODO draw strikethrough if set
} catch (IOException ioe) {
if (g2d instanceof AFPGraphics2D) {
((AFPGraphics2D) g2d).handleIOException(ioe);
}
}
} finally {
nativeTextHandler.setOverrideFont(null);
}
currentLocation.setLocation(currentLocation.getX() + advance, currentLocation.getY());
}
private void updateLocationFromACI(AttributedCharacterIterator aci, Point2D loc) {
//Adjust position of span
Float xpos = (Float) aci.getAttribute(
GVTAttributedCharacterIterator.TextAttribute.X);
Float ypos = (Float) aci.getAttribute(
GVTAttributedCharacterIterator.TextAttribute.Y);
Float dxpos = (Float) aci.getAttribute(
GVTAttributedCharacterIterator.TextAttribute.DX);
Float dypos = (Float) aci.getAttribute(
GVTAttributedCharacterIterator.TextAttribute.DY);
if (xpos != null) {
loc.setLocation(xpos.doubleValue(), loc.getY());
}
if (ypos != null) {
loc.setLocation(loc.getX(), ypos.doubleValue());
}
if (dxpos != null) {
loc.setLocation(loc.getX() + dxpos.doubleValue(), loc.getY());
}
if (dypos != null) {
loc.setLocation(loc.getX(), loc.getY() + dypos.doubleValue());
}
}
}
/**
* Extract the raw text from an ACI.
* @param aci ACI to inspect
* @return the extracted text
*/
protected String getText(AttributedCharacterIterator aci) {
StringBuffer sb = new StringBuffer(aci.getEndIndex() - aci.getBeginIndex());
for (char c = aci.first(); c != CharacterIterator.DONE; c = aci.next()) {
sb.append(c);
}
return sb.toString();
}
private Font getFont(AttributedCharacterIterator aci) {
Font[] fonts = ACIUtils.findFontsForBatikACI(aci, nativeTextHandler.getFontInfo());
return fonts == null ? null : fonts[0];
}
private float getStringWidth(String str, Font font) {
float wordWidth = 0;
float whitespaceWidth = font.getWidth(font.mapChar(' '));
for (int i = 0; i < str.length(); i++) {
float charWidth;
char c = str.charAt(i);
if (!((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))) {
charWidth = font.getWidth(font.mapChar(c));
if (charWidth <= 0) {
charWidth = whitespaceWidth;
}
} else {
charWidth = whitespaceWidth;
}
wordWidth += charWidth;
}
return wordWidth / 1000f;
}
/**
* Get the outline shape of the text characters.
* This uses the StrokingTextPainter to get the outline
* shape since in theory it should be the same.
*
* @param node the text node
* @return the outline shape of the text characters
*/
public Shape getOutline(TextNode node) {
return proxyTextPainter.getOutline(node);
}
/**
* Get the bounds.
* This uses the StrokingTextPainter to get the bounds
* since in theory it should be the same.
*
* @param node the text node
* @return the bounds of the text
*/
public Rectangle2D getBounds2D(TextNode node) {
/* (todo) getBounds2D() is too slow
* because it uses the StrokingTextPainter. We should implement this
* method ourselves. */
return proxyTextPainter.getBounds2D(node);
}
/**
* Get the geometry bounds.
* This uses the StrokingTextPainter to get the bounds
* since in theory it should be the same.
*
* @param node the text node
* @return the bounds of the text
*/
public Rectangle2D getGeometryBounds(TextNode node) {
return proxyTextPainter.getGeometryBounds(node);
}
// Methods that have no purpose for PS
/**
* Get the mark.
* This does nothing since the output is AFP and not interactive.
*
* @param node the text node
* @param pos the position
* @param all select all
* @return null
*/
public Mark getMark(TextNode node, int pos, boolean all) {
return null;
}
/**
* Select at.
* This does nothing since the output is AFP and not interactive.
*
* @param x the x position
* @param y the y position
* @param node the text node
* @return null
*/
public Mark selectAt(double x, double y, TextNode node) {
return null;
}
/**
* Select to.
* This does nothing since the output is AFP and not interactive.
*
* @param x the x position
* @param y the y position
* @param beginMark the start mark
* @return null
*/
public Mark selectTo(double x, double y, Mark beginMark) {
return null;
}
/**
* Selec first.
* This does nothing since the output is AFP and not interactive.
*
* @param node the text node
* @return null
*/
public Mark selectFirst(TextNode node) {
return null;
}
/**
* Select last.
* This does nothing since the output is AFP and not interactive.
*
* @param node the text node
* @return null
*/
public Mark selectLast(TextNode node) {
return null;
}
/**
* Get selected.
* This does nothing since the output is AFP and not interactive.
*
* @param start the start mark
* @param finish the finish mark
* @return null
*/
public int[] getSelected(Mark start, Mark finish) {
return null;
}
/**
* Get the highlighted shape.
* This does nothing since the output is AFP and not interactive.
*
* @param beginMark the start mark
* @param endMark the end mark
* @return null
*/
public Shape getHighlightShape(Mark beginMark, Mark endMark) {
return null;
}
}
|