Changeset 32380 in osm for applications/editors/josm/plugins/seachart/src/render/Renderer.java
- Timestamp:
- 2016-06-23T14:17:55+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/src/render/Renderer.java
r32101 r32380 391 391 if ((str == null) || (str.isEmpty())) str = " "; 392 392 FontRenderContext frc = g2.getFontRenderContext(); 393 GlyphVector gv = font.deriveFont((float)(font.getSize())).createGlyphVector(frc, str.equals(" ") ? " !" : str);393 GlyphVector gv = font.deriveFont((float)(font.getSize())).createGlyphVector(frc, str.equals(" ") ? "M" : str); 394 394 Rectangle2D bounds = gv.getVisualBounds(); 395 395 double width = bounds.getWidth(); … … 485 485 } 486 486 487 public static void lineText(String str, Font font, Color colour, double offset, doubledy) {487 public static void lineText(String str, Font font, Color colour, double dy) { 488 488 if (!str.isEmpty()) { 489 489 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 490 g2.setPaint(colour); 491 FontRenderContext frc = g2.getFontRenderContext(); 492 GlyphVector gv = font.deriveFont(font.getSize2D() * (float)sScale).createGlyphVector(frc, (" " + str)); 493 GeneralPath path = new GeneralPath(); 494 Point2D prev = new Point2D.Double(); 495 Point2D next = new Point2D.Double(); 496 Point2D curr = new Point2D.Double(); 497 Point2D succ = new Point2D.Double(); 498 boolean piv = false; 499 double angle = 0; 500 int index = 0; 501 double gwidth = offset * (Rules.feature.geom.length * context.mile(Rules.feature) - gv.getLogicalBounds().getWidth()) + gv.getGlyphMetrics(0).getAdvance(); 502 GeomIterator git = map.new GeomIterator(Rules.feature.geom); 503 while (git.hasComp()) { 504 git.nextComp(); 505 boolean first = true; 506 while (git.hasEdge()) { 507 git.nextEdge(); 508 while (git.hasNode()) { 509 Snode node = git.next(); 510 if (node == null) continue; 511 prev = next; 512 next = context.getPoint(node); 513 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX()); 514 piv = true; 515 if (first) { 516 curr = succ = next; 517 first = false; 518 } else { 519 while (curr.distance(next) >= gwidth) { 520 if (piv) { 521 double rem = gwidth; 522 double s = prev.distance(next); 523 double p = curr.distance(prev); 524 if ((s > 0) && (p > 0)) { 525 double n = curr.distance(next); 526 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p); 527 double phi = Math.asin(p / gwidth * Math.sin(theta)); 528 rem = gwidth * Math.sin(Math.PI - theta - phi) / Math.sin(theta); 529 } 530 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle))); 531 piv = false; 490 g2.setPaint(colour); 491 FontRenderContext frc = g2.getFontRenderContext(); 492 GlyphVector gv = font.deriveFont(font.getSize2D() * (float) sScale).createGlyphVector(frc, str); 493 double width = gv.getVisualBounds().getWidth(); 494 double height = gv.getVisualBounds().getHeight(); 495 double offset = (Rules.feature.geom.length * context.mile(Rules.feature) - width) / 2; 496 if (offset > 0) { 497 Point2D before = null; 498 Point2D after = null; 499 ArrayList<Point2D> between = new ArrayList<>(); 500 Point2D prev = null; 501 Point2D next = null; 502 double length = 0; 503 double lb = 0; 504 double la = 0; 505 GeomIterator git = map.new GeomIterator(Rules.feature.geom); 506 if (git.hasComp()) { 507 git.nextComp(); 508 while (git.hasEdge()) { 509 git.nextEdge(); 510 while (git.hasNode()) { 511 Snode node = git.next(); 512 if (node == null) 513 continue; 514 prev = next; 515 next = context.getPoint(node); 516 if (prev != null) 517 length += Math.sqrt(Math.pow((next.getX() - prev.getX()), 2) + Math.pow((next.getY() - prev.getY()), 2)); 518 if (length < offset) { 519 before = next; 520 lb = la = length; 521 } else if (after == null) { 522 if (length > (offset + width)) { 523 after = next; 524 la = length; 525 break; 532 526 } else { 533 succ = new Point2D.Double(curr.getX() + (gwidth * Math.cos(angle)), curr.getY() + (gwidth * Math.sin(angle))); 534 } 535 Shape shape = gv.getGlyphOutline(index); 536 Point2D point = gv.getGlyphPosition(index); 537 AffineTransform at = AffineTransform.getTranslateInstance(curr.getX(), curr.getY()); 538 at.rotate(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX()))); 539 at.translate(-point.getX(), -point.getY() + (dy * sScale)); 540 path.append(at.createTransformedShape(shape), false); 541 curr = succ; 542 if (++index < gv.getNumGlyphs()) { 543 gwidth = gv.getGlyphMetrics(index).getAdvance(); 544 } else { 545 g2.fill(path); 546 return; 527 between.add(next); 547 528 } 548 529 } 549 530 } 531 if (after != null) 532 break; 550 533 } 534 } 535 if (after != null) { 536 double angle = Math.atan2((after.getY() - before.getY()), (after.getX() - before.getX())); 537 double rotate = Math.abs(angle) < (Math.PI / 2) ? angle : angle + Math.PI; 538 Point2D mid = new Point2D.Double((before.getX() + after.getX()) / 2, (before.getY() + after.getY()) / 2); 539 Point2D centre = context.getPoint(Rules.feature.geom.centre); 540 AffineTransform pos = AffineTransform.getTranslateInstance(-dy * Math.sin(rotate), dy * Math.cos(rotate)); 541 pos.rotate(rotate); 542 pos.translate((mid.getX() - centre.getX()), (mid.getY() - centre.getY())); 543 Symbol label = new Symbol(); 544 label.add(new Instr(Form.BBOX, new Rectangle2D.Double((-width / 2), (-height), width, height))); 545 label.add(new Instr(Form.TEXT, new Caption(str, font, colour, new Delta(Handle.BC)))); 546 Symbols.drawSymbol(g2, label, sScale, centre.getX(), centre.getY(), null, new Delta(Handle.BC, pos)); 551 547 } 552 548 } … … 580 576 } 581 577 if ((str != null) && (!str.isEmpty())) { 582 FontRenderContext frc = g2.getFontRenderContext();583 578 Font font = new Font("Arial", Font.PLAIN, 40); 584 GlyphVector gv = font.deriveFont(font.getSize2D() * (float)sScale).createGlyphVector(frc, str);585 579 double arc = (s2 > s1) ? (s2 - s1) : (s2 - s1 + 360); 586 580 double awidth = (Math.toRadians(arc) * radial); … … 589 583 radial += 30 * sScale; 590 584 AffineTransform at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale); 591 if ( gv.getLogicalBounds().getWidth() < awidth) {585 if ((font.getSize() * sScale * str.length()) < awidth) { 592 586 at.rotate(Math.toRadians(mid + (hand ? 0 : 180))); 593 Renderer.labelText(str, font, Color.black, new Delta(Handle.CC, at));594 } else if ( gv.getLogicalBounds().getHeight() < awidth) {587 labelText(str, font, Color.black, new Delta(Handle.CC, at)); 588 } else if ((font.getSize() * sScale) < awidth) { 595 589 hand = (mid < 180); 596 590 at.rotate(Math.toRadians(mid + (hand ? -90 : 90))); 597 Renderer.labelText(str, font, Color.black, hand ? new Delta(Handle.RC, at) : new Delta(Handle.LC, at));591 labelText(str, font, Color.black, hand ? new Delta(Handle.RC, at) : new Delta(Handle.LC, at)); 598 592 } 599 593 if (dir != null) { … … 605 599 at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale); 606 600 at.rotate(Math.toRadians(dir + (hand ? 90 : -90))); 607 Renderer.labelText(str, font, Color.black, hand ? new Delta(Handle.BR, at) : new Delta(Handle.BL, at));601 labelText(str, font, Color.black, hand ? new Delta(Handle.BR, at) : new Delta(Handle.BL, at)); 608 602 } 609 603 }
Note:
See TracChangeset
for help on using the changeset viewer.
