Changeset 8083 in josm for trunk/src/com/kitfox/svg/Text.java
- Timestamp:
- 2015-02-18T15:49:56+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/Text.java
r7676 r8083 36 36 package com.kitfox.svg; 37 37 38 import com.kitfox.svg.util.FontSystem; 39 import com.kitfox.svg.util.TextBuilder; 38 40 import com.kitfox.svg.xml.StyleAttribute; 39 41 import java.awt.Graphics2D; … … 89 91 int fontWeight; 90 92 93 float textLength = -1; 94 String lengthAdjust = "spacing"; 95 91 96 /** 92 97 * Creates a new instance of Stop … … 167 172 { 168 173 fontFamily = sty.getStringValue(); 169 } else 174 } 175 else 170 176 { 171 177 fontFamily = "Sans Serif"; … … 175 181 { 176 182 fontSize = sty.getFloatValueWithUnits(); 177 } else 183 } 184 else 178 185 { 179 186 fontSize = 12f; 187 } 188 189 if (getStyle(sty.setName("textLength"))) 190 { 191 textLength = sty.getFloatValueWithUnits(); 192 } 193 else 194 { 195 textLength = -1; 196 } 197 198 if (getStyle(sty.setName("lengthAdjust"))) 199 { 200 lengthAdjust = sty.getStringValue(); 201 } 202 else 203 { 204 lengthAdjust = "spacing"; 180 205 } 181 206 … … 235 260 //text-rendering 236 261 237 buildFont(); 238 } 239 240 protected void buildFont() throws SVGException 241 { 242 int style; 243 switch (fontStyle) 244 { 245 case TXST_ITALIC: 246 style = java.awt.Font.ITALIC; 247 break; 248 default: 249 style = java.awt.Font.PLAIN; 250 break; 251 } 252 253 int weight; 254 switch (fontWeight) 255 { 256 case TXWE_BOLD: 257 case TXWE_BOLDER: 258 weight = java.awt.Font.BOLD; 259 break; 260 default: 261 weight = java.awt.Font.PLAIN; 262 break; 263 } 262 buildText(); 263 } 264 265 protected void buildText() throws SVGException 266 { 264 267 265 268 //Get font … … 269 272 // System.err.println("Could not load font"); 270 273 271 java.awt.Font sysFont = new java.awt.Font(fontFamily, style | weight, (int) fontSize); 272 buildSysFont(sysFont); 273 return; 274 font = new FontSystem(fontFamily, fontStyle, fontWeight, (int)fontSize); 275 // java.awt.Font sysFont = new java.awt.Font(fontFamily, style | weight, (int)fontSize); 276 // buildSysFont(sysFont); 277 // return; 274 278 } 275 279 … … 288 292 289 293 // AffineTransform oldXform = g.getTransform(); 294 // TextBuilder builder = new TextBuilder(); 295 // 296 // for (Iterator it = content.iterator(); it.hasNext();) 297 // { 298 // Object obj = it.next(); 299 // 300 // if (obj instanceof String) 301 // { 302 // String text = (String) obj; 303 // if (text != null) 304 // { 305 // text = text.trim(); 306 // } 307 // 308 // for (int i = 0; i < text.length(); i++) 309 // { 310 // String unicode = text.substring(i, i + 1); 311 // MissingGlyph glyph = font.getGlyph(unicode); 312 // 313 // builder.appendGlyph(glyph); 314 // } 315 // } 316 // else if (obj instanceof Tspan) 317 // { 318 // Tspan tspan = (Tspan)obj; 319 // tspan.buildGlyphs(builder); 320 // } 321 // } 322 // 323 // builder.formatGlyphs(); 324 325 326 327 328 329 330 331 290 332 AffineTransform xform = new AffineTransform(); 291 333 … … 328 370 329 371 strokeWidthScalar = 1f; 330 } else if (obj instanceof Tspan) 331 { 332 Tspan tspan = (Tspan) obj; 333 334 xform.setToIdentity(); 335 xform.setToTranslation(cursorX, cursorY); 336 xform.scale(fontScale, fontScale); 337 // tspan.setCursorX(cursorX); 338 // tspan.setCursorY(cursorY); 339 340 Shape tspanShape = tspan.getShape(); 341 tspanShape = xform.createTransformedShape(tspanShape); 342 textPath.append(tspanShape, false); 343 // tspan.render(g); 344 // cursorX = tspan.getCursorX(); 345 // cursorY = tspan.getCursorY(); 346 } 347 348 } 349 350 switch (textAnchor) 351 { 352 case TXAN_MIDDLE: 353 { 354 AffineTransform at = new AffineTransform(); 355 at.translate(-textPath.getBounds().getWidth() / 2, 0); 356 textPath.transform(at); 357 break; 358 } 359 case TXAN_END: 360 { 361 AffineTransform at = new AffineTransform(); 362 at.translate(-textPath.getBounds().getWidth(), 0); 363 textPath.transform(at); 364 break; 365 } 366 } 367 } 368 369 private void buildSysFont(java.awt.Font font) throws SVGException 370 { 371 GeneralPath textPath = new GeneralPath(); 372 textShape = textPath; 373 374 float cursorX = x, cursorY = y; 375 376 // FontMetrics fm = g.getFontMetrics(font); 377 FontRenderContext frc = new FontRenderContext(null, true, true); 378 379 // FontFace fontFace = font.getFontFace(); 380 //int unitsPerEm = fontFace.getUnitsPerEm(); 381 // int ascent = fm.getAscent(); 382 // float fontScale = fontSize / (float)ascent; 383 384 // AffineTransform oldXform = g.getTransform(); 385 AffineTransform xform = new AffineTransform(); 386 387 for (Iterator it = content.iterator(); it.hasNext();) 388 { 389 Object obj = it.next(); 390 391 if (obj instanceof String) 392 { 393 String text = (String)obj; 394 text = text.trim(); 395 396 Shape textShape = font.createGlyphVector(frc, text).getOutline(cursorX, cursorY); 397 textPath.append(textShape, false); 398 // renderShape(g, textShape); 399 // g.drawString(text, cursorX, cursorY); 400 401 Rectangle2D rect = font.getStringBounds(text, frc); 402 cursorX += (float) rect.getWidth(); 403 } else if (obj instanceof Tspan) 404 { 405 /* 406 Tspan tspan = (Tspan)obj; 407 408 xform.setToIdentity(); 409 xform.setToTranslation(cursorX, cursorY); 410 411 Shape tspanShape = tspan.getShape(); 412 tspanShape = xform.createTransformedShape(tspanShape); 413 textArea.add(new Area(tspanShape)); 414 415 cursorX += tspanShape.getBounds2D().getWidth(); 416 */ 417 418 372 } 373 else if (obj instanceof Tspan) 374 { 375 // Tspan tspan = (Tspan) obj; 376 // 377 // xform.setToIdentity(); 378 // xform.setToTranslation(cursorX, cursorY); 379 // xform.scale(fontScale, fontScale); 380 //// tspan.setCursorX(cursorX); 381 //// tspan.setCursorY(cursorY); 382 // 383 // Shape tspanShape = tspan.getShape(); 384 // tspanShape = xform.createTransformedShape(tspanShape); 385 // textPath.append(tspanShape, false); 386 //// tspan.render(g); 387 //// cursorX = tspan.getCursorX(); 388 //// cursorY = tspan.getCursorY(); 389 390 419 391 Tspan tspan = (Tspan)obj; 420 392 Point2D cursor = new Point2D.Float(cursorX, cursorY); … … 426 398 cursorX = (float)cursor.getX(); 427 399 cursorY = (float)cursor.getY(); 428 429 } 400 401 } 402 430 403 } 431 404 … … 442 415 { 443 416 AffineTransform at = new AffineTransform(); 444 at.translate(- Math.ceil(textPath.getBounds().getWidth()), 0);417 at.translate(-textPath.getBounds().getWidth(), 0); 445 418 textPath.transform(at); 446 419 break; … … 448 421 } 449 422 } 423 424 // private void buildSysFont(java.awt.Font font) throws SVGException 425 // { 426 // GeneralPath textPath = new GeneralPath(); 427 // textShape = textPath; 428 // 429 // float cursorX = x, cursorY = y; 430 // 431 //// FontMetrics fm = g.getFontMetrics(font); 432 // FontRenderContext frc = new FontRenderContext(null, true, true); 433 // 434 //// FontFace fontFace = font.getFontFace(); 435 // //int unitsPerEm = fontFace.getUnitsPerEm(); 436 //// int ascent = fm.getAscent(); 437 //// float fontScale = fontSize / (float)ascent; 438 // 439 //// AffineTransform oldXform = g.getTransform(); 440 // AffineTransform xform = new AffineTransform(); 441 // 442 // for (Iterator it = content.iterator(); it.hasNext();) 443 // { 444 // Object obj = it.next(); 445 // 446 // if (obj instanceof String) 447 // { 448 // String text = (String)obj; 449 // text = text.trim(); 450 // 451 // Shape textShape = font.createGlyphVector(frc, text).getOutline(cursorX, cursorY); 452 // textPath.append(textShape, false); 453 //// renderShape(g, textShape); 454 //// g.drawString(text, cursorX, cursorY); 455 // 456 // Rectangle2D rect = font.getStringBounds(text, frc); 457 // cursorX += (float) rect.getWidth(); 458 // } else if (obj instanceof Tspan) 459 // { 460 // /* 461 // Tspan tspan = (Tspan)obj; 462 // 463 // xform.setToIdentity(); 464 // xform.setToTranslation(cursorX, cursorY); 465 // 466 // Shape tspanShape = tspan.getShape(); 467 // tspanShape = xform.createTransformedShape(tspanShape); 468 // textArea.add(new Area(tspanShape)); 469 // 470 // cursorX += tspanShape.getBounds2D().getWidth(); 471 // */ 472 // 473 // 474 // Tspan tspan = (Tspan)obj; 475 // Point2D cursor = new Point2D.Float(cursorX, cursorY); 476 //// tspan.setCursorX(cursorX); 477 //// tspan.setCursorY(cursorY); 478 // tspan.appendToShape(textPath, cursor); 479 //// cursorX = tspan.getCursorX(); 480 //// cursorY = tspan.getCursorY(); 481 // cursorX = (float)cursor.getX(); 482 // cursorY = (float)cursor.getY(); 483 // 484 // } 485 // } 486 // 487 // switch (textAnchor) 488 // { 489 // case TXAN_MIDDLE: 490 // { 491 // AffineTransform at = new AffineTransform(); 492 // at.translate(-textPath.getBounds().getWidth() / 2, 0); 493 // textPath.transform(at); 494 // break; 495 // } 496 // case TXAN_END: 497 // { 498 // AffineTransform at = new AffineTransform(); 499 // at.translate(-Math.ceil(textPath.getBounds().getWidth()), 0); 500 // textPath.transform(at); 501 // break; 502 // } 503 // } 504 // } 450 505 451 506 public void render(Graphics2D g) throws SVGException … … 502 557 } 503 558 559 if (getStyle(sty.setName("textLength"))) 560 { 561 textLength = sty.getFloatValueWithUnits(); 562 } 563 else 564 { 565 textLength = -1; 566 } 567 568 if (getStyle(sty.setName("lengthAdjust"))) 569 { 570 lengthAdjust = sty.getStringValue(); 571 } 572 else 573 { 574 lengthAdjust = "spacing"; 575 } 576 504 577 if (getPres(sty.setName("font-family"))) 505 578 {
Note:
See TracChangeset
for help on using the changeset viewer.