Changeset 6002 in josm for trunk/src/com/kitfox/svg/Tspan.java
- Timestamp:
- 2013-06-11T01:01:28+02:00 (13 years ago)
- File:
-
- 1 edited
-
trunk/src/com/kitfox/svg/Tspan.java (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/Tspan.java
r4256 r6002 1 1 /* 2 * Stop.java 2 * SVG Salamander 3 * Copyright (c) 2004, Mark McKay 4 * All rights reserved. 3 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: 4 9 * 5 * The Salamander Project - 2D and 3D graphics libraries in Java 6 * Copyright (C) 2004 Mark McKay 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. 7 17 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * 22 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 23 * projects can be found at http://www.kitfox.com 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 24 33 * 25 34 * Created on January 26, 2004, 1:56 AM 26 35 */ 27 28 36 package com.kitfox.svg; 29 37 30 38 import com.kitfox.svg.xml.StyleAttribute; 31 import java.awt.*; 32 import java.awt.font.*; 33 import java.awt.geom.*; 34 import java.util.*; 35 36 import com.kitfox.svg.xml.*; 37 import org.xml.sax.*; 38 39 //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath; 39 import java.awt.Graphics2D; 40 import java.awt.Shape; 41 import java.awt.font.FontRenderContext; 42 import java.awt.font.GlyphMetrics; 43 import java.awt.font.GlyphVector; 44 import java.awt.geom.AffineTransform; 45 import java.awt.geom.GeneralPath; 46 import java.awt.geom.Rectangle2D; 40 47 41 48 /** … … 43 50 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 44 51 */ 45 public class Tspan extends ShapeElement { 46 52 public class Tspan extends ShapeElement 53 { 54 55 public static final String TAG_NAME = "tspan"; 47 56 float[] x = null; 48 57 float[] y = null; … … 50 59 float[] dy = null; 51 60 float[] rotate = null; 52 53 61 private String text = ""; 54 55 62 float cursorX; 56 63 float cursorY; 57 64 58 65 // Shape tspanShape; 59 60 /** Creates a new instance of Stop */ 61 public Tspan() { 62 } 63 64 public float getCursorX() { return cursorX; } 65 public float getCursorY() { return cursorY; } 66 /** 67 * Creates a new instance of Stop 68 */ 69 public Tspan() 70 { 71 } 72 73 public String getTagName() 74 { 75 return TAG_NAME; 76 } 77 78 public float getCursorX() 79 { 80 return cursorX; 81 } 82 83 public float getCursorY() 84 { 85 return cursorY; 86 } 66 87 67 88 public void setCursorX(float cursorX) … … 74 95 this.cursorY = cursorY; 75 96 } 76 /* 77 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) 78 { 79 //Load style string80 super.loaderStartElement(helper, attrs, parent);81 82 String x = attrs.getValue("x");83 String y = attrs.getValue("y");84 String dx = attrs.getValue("dx");85 String dy = attrs.getValue("dy");86 String rotate = attrs.getValue("rotate");87 88 if (x != null) this.x = XMLParseUtil.parseFloatList(x);89 if (y != null) this.y = XMLParseUtil.parseFloatList(y);90 if (dx != null) this.dx = XMLParseUtil.parseFloatList(dx);91 if (dy != null) this.dy = XMLParseUtil.parseFloatList(dy);92 if (rotate != null)93 {94 this.rotate = XMLParseUtil.parseFloatList(rotate);95 for (int i = 0; i < this.rotate.length; i++)96 this.rotate[i] = (float)Math.toRadians(this.rotate[i]);97 }98 } 99 */ 97 /* 98 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) 99 { 100 //Load style string 101 super.loaderStartElement(helper, attrs, parent); 102 103 String x = attrs.getValue("x"); 104 String y = attrs.getValue("y"); 105 String dx = attrs.getValue("dx"); 106 String dy = attrs.getValue("dy"); 107 String rotate = attrs.getValue("rotate"); 108 109 if (x != null) this.x = XMLParseUtil.parseFloatList(x); 110 if (y != null) this.y = XMLParseUtil.parseFloatList(y); 111 if (dx != null) this.dx = XMLParseUtil.parseFloatList(dx); 112 if (dy != null) this.dy = XMLParseUtil.parseFloatList(dy); 113 if (rotate != null) 114 { 115 this.rotate = XMLParseUtil.parseFloatList(rotate); 116 for (int i = 0; i < this.rotate.length; i++) 117 this.rotate[i] = (float)Math.toRadians(this.rotate[i]); 118 } 119 } 120 */ 100 121 101 122 /** … … 107 128 } 108 129 109 110 130 protected void build() throws SVGException 111 131 { 112 132 super.build(); 113 133 114 134 StyleAttribute sty = new StyleAttribute(); 115 116 if (getPres(sty.setName("x"))) x = sty.getFloatList(); 117 118 if (getPres(sty.setName("y"))) y = sty.getFloatList(); 119 120 if (getPres(sty.setName("dx"))) dx = sty.getFloatList(); 121 122 if (getPres(sty.setName("dy"))) dy = sty.getFloatList(); 135 136 if (getPres(sty.setName("x"))) 137 { 138 x = sty.getFloatList(); 139 } 140 141 if (getPres(sty.setName("y"))) 142 { 143 y = sty.getFloatList(); 144 } 145 146 if (getPres(sty.setName("dx"))) 147 { 148 dx = sty.getFloatList(); 149 } 150 151 if (getPres(sty.setName("dy"))) 152 { 153 dy = sty.getFloatList(); 154 } 123 155 124 156 if (getPres(sty.setName("rotate"))) … … 127 159 for (int i = 0; i < this.rotate.length; i++) 128 160 { 129 rotate[i] = (float)Math.toRadians(this.rotate[i]); 130 } 131 132 } 133 } 134 161 rotate[i] = (float) Math.toRadians(this.rotate[i]); 162 } 163 164 } 165 } 166 135 167 public void addShape(GeneralPath addShape) throws SVGException 136 168 { … … 138 170 { 139 171 cursorX = x[0]; 172 } else if (dx != null) 173 { 174 cursorX += dx[0]; 175 } 176 177 if (y != null) 178 { 140 179 cursorY = y[0]; 141 } 142 else if (dx != null) 143 { 144 cursorX += dx[0]; 180 } else if (dy != null) 181 { 145 182 cursorY += dy[0]; 146 183 } 147 184 148 185 StyleAttribute sty = new StyleAttribute(); 149 186 150 187 String fontFamily = null; 151 188 if (getStyle(sty.setName("font-family"))) … … 160 197 fontSize = sty.getFloatValueWithUnits(); 161 198 } 199 200 float letterSpacing = 0; 201 if (getStyle(sty.setName("letter-spacing"))) 202 { 203 letterSpacing = sty.getFloatValueWithUnits(); 204 } 205 162 206 163 207 //Get font … … 165 209 if (font == null) 166 210 { 167 addShapeSysFont(addShape, font, fontFamily, fontSize); 211 addShapeSysFont(addShape, font, fontFamily, fontSize, letterSpacing); 168 212 return; 169 213 } … … 171 215 FontFace fontFace = font.getFontFace(); 172 216 int ascent = fontFace.getAscent(); 173 float fontScale = fontSize / (float)ascent; 217 float fontScale = fontSize / (float) ascent; 174 218 175 219 AffineTransform xform = new AffineTransform(); … … 184 228 xform.setToTranslation(cursorX, cursorY); 185 229 xform.scale(fontScale, fontScale); 186 if (rotate != null) xform.rotate(rotate[posPtr]); 230 if (rotate != null) 231 { 232 xform.rotate(rotate[posPtr]); 233 } 187 234 188 235 String unicode = text.substring(i, i + 1); … … 200 247 cursorX = x[posPtr]; 201 248 cursorY = y[posPtr++]; 202 } 203 else if (dx != null && posPtr < dx.length) 249 } else if (dx != null && posPtr < dx.length) 204 250 { 205 251 cursorX += dx[posPtr]; … … 207 253 } 208 254 209 cursorX += fontScale * glyph.getHorizAdvX(); 255 cursorX += fontScale * glyph.getHorizAdvX() + letterSpacing; 210 256 } 211 257 … … 213 259 } 214 260 215 private void addShapeSysFont(GeneralPath addShape, Font font, String fontFamily, float fontSize) 216 { 217 java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int)fontSize); 261 private void addShapeSysFont(GeneralPath addShape, Font font, 262 String fontFamily, float fontSize, float letterSpacing) 263 { 264 java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize); 218 265 219 266 FontRenderContext frc = new FontRenderContext(null, true, true); … … 226 273 { 227 274 xform.setToIdentity(); 228 xform.setToTranslation(cursorX, cursorY); 229 if (rotate != null) xform.rotate(rotate[Math.min(i, rotate.length - 1)]); 275 xform.setToTranslation(cursorX + i * letterSpacing, cursorY); 276 if (rotate != null) 277 { 278 xform.rotate(rotate[Math.min(i, rotate.length - 1)]); 279 } 230 280 231 281 String unicode = text.substring(i, i + 1); … … 240 290 cursorX = x[posPtr]; 241 291 cursorY = y[posPtr++]; 242 } 243 else if (dx != null && posPtr < dx.length) 292 } else if (dx != null && posPtr < dx.length) 244 293 { 245 294 cursorX += dx[posPtr]; … … 255 304 cursorX = x[0]; 256 305 cursorY = y[0]; 257 } 258 else if (dx != null) 306 } else if (dx != null) 259 307 { 260 308 cursorX += dx[0]; … … 263 311 264 312 StyleAttribute sty = new StyleAttribute(); 265 313 266 314 String fontFamily = null; 267 315 if (getPres(sty.setName("font-family"))) … … 282 330 { 283 331 System.err.println("Could not load font"); 284 java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int)fontSize); 332 java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize); 285 333 renderSysFont(g, sysFont); 286 334 return; … … 290 338 FontFace fontFace = font.getFontFace(); 291 339 int ascent = fontFace.getAscent(); 292 float fontScale = fontSize / (float)ascent; 340 float fontScale = fontSize / (float) ascent; 293 341 294 342 AffineTransform oldXform = g.getTransform(); … … 312 360 { 313 361 renderShape(g, path); 314 } 315 else glyph.render(g); 362 } else 363 { 364 glyph.render(g); 365 } 316 366 317 367 if (x != null && posPtr < x.length) … … 319 369 cursorX = x[posPtr]; 320 370 cursorY = y[posPtr++]; 321 } 322 else if (dx != null && posPtr < dx.length) 371 } else if (dx != null && posPtr < dx.length) 323 372 { 324 373 cursorX += dx[posPtr]; … … 342 391 renderShape(g, textShape); 343 392 Rectangle2D rect = font.getStringBounds(text, frc); 344 cursorX += (float)rect.getWidth(); 393 cursorX += (float) rect.getWidth(); 345 394 } 346 395 … … 358 407 359 408 /** 360 * Updates all attributes in this diagram associated with a time event. 361 * Ie, all attributes with track information. 409 * Updates all attributes in this diagram associated with a time event. Ie, 410 * all attributes with track information. 411 * 362 412 * @return - true if this node has changed state as a result of the time 363 413 * update
Note:
See TracChangeset
for help on using the changeset viewer.
