source: josm/trunk/src/com/kitfox/svg/Tspan.java@ 10866

Last change on this file since 10866 was 10787, checked in by Don-vip, 8 years ago

fix #13291 - upgrade to svgSalamander v1.1.0 (patched)

now detects two invalid SVG files: presets/sport/volleyball.svg and presets/shop/diy_store.svg

  • Property svn:eol-style set to native
File size: 13.2 KB
Line 
1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
32 * projects can be found at http://www.kitfox.com
33 *
34 * Created on January 26, 2004, 1:56 AM
35 */
36package com.kitfox.svg;
37
38import com.kitfox.svg.util.FontSystem;
39import com.kitfox.svg.xml.StyleAttribute;
40import java.awt.Graphics2D;
41import java.awt.Shape;
42import java.awt.font.FontRenderContext;
43import java.awt.font.GlyphMetrics;
44import java.awt.font.GlyphVector;
45import java.awt.geom.AffineTransform;
46import java.awt.geom.GeneralPath;
47import java.awt.geom.Point2D;
48import java.awt.geom.Rectangle2D;
49
50/**
51 * @author Mark McKay
52 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
53 */
54public class Tspan extends ShapeElement
55{
56
57 public static final String TAG_NAME = "tspan";
58 float[] x = null;
59 float[] y = null;
60 float[] dx = null;
61 float[] dy = null;
62 float[] rotate = null;
63 private String text = "";
64// float cursorX;
65// float cursorY;
66
67// Shape tspanShape;
68 /**
69 * Creates a new instance of Stop
70 */
71 public Tspan()
72 {
73 }
74
75 public String getTagName()
76 {
77 return TAG_NAME;
78 }
79
80// public float getCursorX()
81// {
82// return cursorX;
83// }
84//
85// public float getCursorY()
86// {
87// return cursorY;
88// }
89//
90// public void setCursorX(float cursorX)
91// {
92// this.cursorX = cursorX;
93// }
94//
95// public void setCursorY(float cursorY)
96// {
97// this.cursorY = cursorY;
98// }
99 /*
100 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
101 {
102 //Load style string
103 super.loaderStartElement(helper, attrs, parent);
104
105 String x = attrs.getValue("x");
106 String y = attrs.getValue("y");
107 String dx = attrs.getValue("dx");
108 String dy = attrs.getValue("dy");
109 String rotate = attrs.getValue("rotate");
110
111 if (x != null) this.x = XMLParseUtil.parseFloatList(x);
112 if (y != null) this.y = XMLParseUtil.parseFloatList(y);
113 if (dx != null) this.dx = XMLParseUtil.parseFloatList(dx);
114 if (dy != null) this.dy = XMLParseUtil.parseFloatList(dy);
115 if (rotate != null)
116 {
117 this.rotate = XMLParseUtil.parseFloatList(rotate);
118 for (int i = 0; i < this.rotate.length; i++)
119 this.rotate[i] = (float)Math.toRadians(this.rotate[i]);
120 }
121 }
122 */
123
124 /**
125 * Called during load process to add text scanned within a tag
126 */
127 public void loaderAddText(SVGLoaderHelper helper, String text)
128 {
129 this.text += text;
130 }
131
132 protected void build() throws SVGException
133 {
134 super.build();
135
136 StyleAttribute sty = new StyleAttribute();
137
138 if (getPres(sty.setName("x")))
139 {
140 x = sty.getFloatList();
141 }
142
143 if (getPres(sty.setName("y")))
144 {
145 y = sty.getFloatList();
146 }
147
148 if (getPres(sty.setName("dx")))
149 {
150 dx = sty.getFloatList();
151 }
152
153 if (getPres(sty.setName("dy")))
154 {
155 dy = sty.getFloatList();
156 }
157
158 if (getPres(sty.setName("rotate")))
159 {
160 rotate = sty.getFloatList();
161 for (int i = 0; i < this.rotate.length; i++)
162 {
163 rotate[i] = (float) Math.toRadians(this.rotate[i]);
164 }
165
166 }
167 }
168
169 public void appendToShape(GeneralPath addShape, Point2D cursor) throws SVGException
170 {
171 StyleAttribute sty = new StyleAttribute();
172
173 String fontFamily = null;
174 if (getStyle(sty.setName("font-family")))
175 {
176 fontFamily = sty.getStringValue();
177 }
178
179
180 float fontSize = 12f;
181 if (getStyle(sty.setName("font-size")))
182 {
183 fontSize = sty.getFloatValueWithUnits();
184 }
185
186 float letterSpacing = 0;
187 if (getStyle(sty.setName("letter-spacing")))
188 {
189 letterSpacing = sty.getFloatValueWithUnits();
190 }
191
192 int fontStyle = 0;
193 if (getStyle(sty.setName("font-style")))
194 {
195 String s = sty.getStringValue();
196 if ("normal".equals(s))
197 {
198 fontStyle = Text.TXST_NORMAL;
199 } else if ("italic".equals(s))
200 {
201 fontStyle = Text.TXST_ITALIC;
202 } else if ("oblique".equals(s))
203 {
204 fontStyle = Text.TXST_OBLIQUE;
205 }
206 } else
207 {
208 fontStyle = Text.TXST_NORMAL;
209 }
210
211 int fontWeight = 0;
212 if (getStyle(sty.setName("font-weight")))
213 {
214 String s = sty.getStringValue();
215 if ("normal".equals(s))
216 {
217 fontWeight = Text.TXWE_NORMAL;
218 } else if ("bold".equals(s))
219 {
220 fontWeight = Text.TXWE_BOLD;
221 }
222 } else
223 {
224 fontWeight = Text.TXWE_NORMAL;
225 }
226
227
228 //Get font
229 Font font = diagram.getUniverse().getFont(fontFamily);
230 if (font == null)
231 {
232 font = new FontSystem(fontFamily, fontStyle, fontWeight, (int)fontSize);
233 }
234
235 AffineTransform xform = new AffineTransform();
236
237 float cursorX = (float)cursor.getX();
238 float cursorY = (float)cursor.getY();
239
240 String drawText = this.text;
241 drawText = drawText.trim();
242 for (int i = 0; i < drawText.length(); i++)
243 {
244 if (x != null && i < x.length)
245 {
246 cursorX = x[i];
247 } else if (dx != null && i < dx.length)
248 {
249 cursorX += dx[i];
250 }
251
252 if (y != null && i < y.length)
253 {
254 cursorY = y[i];
255 } else if (dy != null && i < dy.length)
256 {
257 cursorY += dy[i];
258 }
259
260 xform.setToIdentity();
261 xform.setToTranslation(cursorX, cursorY);
262 if (rotate != null)
263 {
264 xform.rotate(rotate[i]);
265 }
266
267 String unicode = drawText.substring(i, i + 1);
268 MissingGlyph glyph = font.getGlyph(unicode);
269
270 Shape path = glyph.getPath();
271 if (path != null)
272 {
273 path = xform.createTransformedShape(path);
274 addShape.append(path, false);
275 }
276
277 cursorX += glyph.getHorizAdvX() + letterSpacing;
278 }
279
280 //Save final draw point so calling method knows where to begin next
281 // text draw
282 cursor.setLocation(cursorX, cursorY);
283 strokeWidthScalar = 1f;
284 }
285
286// private void addShapeSysFont(GeneralPath addShape, Font font,
287// String fontFamily, float fontSize, float letterSpacing, Point2D cursor)
288// {
289//
290// java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize);
291//
292// FontRenderContext frc = new FontRenderContext(null, true, true);
293// String renderText = this.text.trim();
294//
295// AffineTransform xform = new AffineTransform();
296//
297// float cursorX = (float)cursor.getX();
298// float cursorY = (float)cursor.getY();
299//// int i = 0;
300// for (int i = 0; i < renderText.length(); i++)
301// {
302// if (x != null && i < x.length)
303// {
304// cursorX = x[i];
305// } else if (dx != null && i < dx.length)
306// {
307// cursorX += dx[i];
308// }
309//
310// if (y != null && i < y.length)
311// {
312// cursorY = y[i];
313// } else if (dy != null && i < dy.length)
314// {
315// cursorY += dy[i];
316// }
317//// i++;
318//
319// xform.setToIdentity();
320// xform.setToTranslation(cursorX, cursorY);
321// if (rotate != null)
322// {
323// xform.rotate(rotate[Math.min(i, rotate.length - 1)]);
324// }
325//
326//// String unicode = renderText.substring(i, i + 1);
327// GlyphVector textVector = sysFont.createGlyphVector(frc, renderText.substring(i, i + 1));
328// Shape glyphOutline = textVector.getGlyphOutline(0);
329// GlyphMetrics glyphMetrics = textVector.getGlyphMetrics(0);
330//
331// glyphOutline = xform.createTransformedShape(glyphOutline);
332// addShape.append(glyphOutline, false);
333//
334//
335//// cursorX += fontScale * glyph.getHorizAdvX() + letterSpacing;
336// cursorX += glyphMetrics.getAdvance() + letterSpacing;
337// }
338//
339// cursor.setLocation(cursorX, cursorY);
340// }
341
342 public void render(Graphics2D g) throws SVGException
343 {
344 float cursorX = 0;
345 float cursorY = 0;
346
347 if (x != null)
348 {
349 cursorX = x[0];
350 cursorY = y[0];
351 } else if (dx != null)
352 {
353 cursorX += dx[0];
354 cursorY += dy[0];
355 }
356
357 StyleAttribute sty = new StyleAttribute();
358
359 String fontFamily = null;
360 if (getPres(sty.setName("font-family")))
361 {
362 fontFamily = sty.getStringValue();
363 }
364
365
366 float fontSize = 12f;
367 if (getPres(sty.setName("font-size")))
368 {
369 fontSize = sty.getFloatValueWithUnits();
370 }
371
372 //Get font
373 Font font = diagram.getUniverse().getFont(fontFamily);
374 if (font == null)
375 {
376 System.err.println("Could not load font");
377 java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize);
378 renderSysFont(g, sysFont);
379 return;
380 }
381
382
383 FontFace fontFace = font.getFontFace();
384 int ascent = fontFace.getAscent();
385 float fontScale = fontSize / (float) ascent;
386
387 AffineTransform oldXform = g.getTransform();
388 AffineTransform xform = new AffineTransform();
389
390 strokeWidthScalar = 1f / fontScale;
391
392 int posPtr = 1;
393
394 for (int i = 0; i < text.length(); i++)
395 {
396 xform.setToTranslation(cursorX, cursorY);
397 xform.scale(fontScale, fontScale);
398 g.transform(xform);
399
400 String unicode = text.substring(i, i + 1);
401 MissingGlyph glyph = font.getGlyph(unicode);
402
403 Shape path = glyph.getPath();
404 if (path != null)
405 {
406 renderShape(g, path);
407 } else
408 {
409 glyph.render(g);
410 }
411
412 if (x != null && posPtr < x.length)
413 {
414 cursorX = x[posPtr];
415 cursorY = y[posPtr++];
416 } else if (dx != null && posPtr < dx.length)
417 {
418 cursorX += dx[posPtr];
419 cursorY += dy[posPtr++];
420 }
421
422 cursorX += fontScale * glyph.getHorizAdvX();
423
424 g.setTransform(oldXform);
425 }
426
427 strokeWidthScalar = 1f;
428 }
429
430 protected void renderSysFont(Graphics2D g, java.awt.Font font) throws SVGException
431 {
432 float cursorX = 0;
433 float cursorY = 0;
434
435 int posPtr = 1;
436 FontRenderContext frc = g.getFontRenderContext();
437
438 Shape textShape = font.createGlyphVector(frc, text).getOutline(cursorX, cursorY);
439 renderShape(g, textShape);
440 Rectangle2D rect = font.getStringBounds(text, frc);
441 cursorX += (float) rect.getWidth();
442 }
443
444 public Shape getShape()
445 {
446 return null;
447 //return shapeToParent(tspanShape);
448 }
449
450 public Rectangle2D getBoundingBox()
451 {
452 return null;
453 //return boundsToParent(tspanShape.getBounds2D());
454 }
455
456 /**
457 * Updates all attributes in this diagram associated with a time event. Ie,
458 * all attributes with track information.
459 *
460 * @return - true if this node has changed state as a result of the time
461 * update
462 */
463 public boolean updateTime(double curTime) throws SVGException
464 {
465 //Tspan does not change
466 return false;
467 }
468
469 public String getText()
470 {
471 return text;
472 }
473
474 public void setText(String text)
475 {
476 this.text = text;
477 }
478}
Note: See TracBrowser for help on using the repository browser.