Changeset 11752 in josm


Ignore:
Timestamp:
2017-03-20T23:26:37+01:00 (8 years ago)
Author:
michael2402
Message:

Document OnLineStrategy better.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/placement/OnLineStrategy.java

    r11749 r11752  
    4949    }
    5050
    51     private double upsideTheta(HalfSegment best) {
     51    private static double upsideTheta(HalfSegment best) {
    5252        double theta = theta(best.start, best.end);
    5353        if (theta < -Math.PI / 2) {
     
    6868    public List<GlyphVector> generateGlyphVectors(MapViewPath path, Rectangle2D nb, List<GlyphVector> gvs,
    6969            boolean isDoubleTranslationBug) {
     70        // Find the position on the way the font should be placed.
     71        // If none is found, use the middle of the way.
    7072        double middleOffset = findOptimalWayPosition(nb, path).map(segment -> segment.offset)
    7173                .orElse(path.getLength() / 2);
    7274
     75        // Check that segment of the way. Compute in which direction the text should be rendered.
     76        // It is rendered in a way that ensures that at least 50% of the text are rotated with the right side up.
    7377        UpsideComputingVisitor upside = new UpsideComputingVisitor(middleOffset - nb.getWidth() / 2,
    7478                middleOffset + nb.getWidth() / 2);
    7579        path.visitLine(upside);
    76 
    77         boolean doRotateText = upside.shouldMirrorText();
     80        boolean doRotateText = upside.shouldRotateText();
     81
     82        // Compute the list of glyphs to draw, along with their offset on the current line.
    7883        List<OffsetGlyph> offsetGlyphs = computeOffsetGlyphs(gvs,
    7984                middleOffset + (doRotateText ? 1 : -1) * nb.getWidth() / 2, doRotateText);
    8085
     86        // Order the glyphs along the line to ensure that they are drawn corretly.
     87        Collections.sort(offsetGlyphs, Comparator.comparing(e -> e.offset));
     88
     89        // Now translate all glyphs. This will modify the glyphs stored in gvs.
    8190        path.visitLine(new GlyphRotatingVisitor(offsetGlyphs, isDoubleTranslationBug));
    8291        return gvs;
     
    100109            offset += (rotateText ? -1 : 1) + gv.getLogicalBounds().getBounds2D().getWidth();
    101110        }
    102         Collections.sort(offsetGlyphs, Comparator.comparing(e -> e.offset));
    103111        return offsetGlyphs;
    104112    }
    105113
    106     private Optional<HalfSegment> findOptimalWayPosition(Rectangle2D rect, MapViewPath path) {
     114    private static Optional<HalfSegment> findOptimalWayPosition(Rectangle2D rect, MapViewPath path) {
    107115        // find half segments that are long enough to draw text on (don't draw text over the cross hair in the center of each segment)
    108116        List<HalfSegment> longHalfSegment = new ArrayList<>();
     
    192200        private final double endOffset;
    193201
    194         private double upsideUpLines = 0;
    195         private double upsideDownLines = 0;
     202        private double upsideUpLines;
     203        private double upsideDownLines;
    196204
    197205        UpsideComputingVisitor(double startOffset, double endOffset) {
     
    223231        }
    224232
    225         public boolean shouldMirrorText() {
     233        /**
     234         * Check if the text should be rotated by 180°
     235         * @return if the text should be rotated.
     236         */
     237        boolean shouldRotateText() {
    226238            return upsideUpLines < upsideDownLines;
    227239        }
     
    236248        private OffsetGlyph next;
    237249
     250        /**
     251         * Create a new {@link GlyphRotatingVisitor}
     252         * @param gvs The glyphs to draw. Sorted along the line
     253         * @param isDoubleTranslationBug true to fix a double translation bug.
     254         */
    238255        GlyphRotatingVisitor(List<OffsetGlyph> gvs, boolean isDoubleTranslationBug) {
    239256            this.isDoubleTranslationBug = isDoubleTranslationBug;
Note: See TracChangeset for help on using the changeset viewer.