Search in sources :

Example 1 with PointDoubleXYOrientation

use of com.revolsys.geometry.model.impl.PointDoubleXYOrientation in project com.revolsys.open by revolsys.

the class PdfViewport method drawText.

@Override
public void drawText(final Record record, final Geometry geometry, final TextStyle style) {
    try {
        final String label = TextStyleRenderer.getLabel(record, style);
        if (Property.hasValue(label) && geometry != null) {
            final String textPlacementType = style.getTextPlacementType();
            final PointDoubleXYOrientation point = AbstractRecordLayerRenderer.getPointWithOrientation(this, geometry, textPlacementType);
            if (point != null) {
                final double orientation = point.getOrientation();
                this.contentStream.saveGraphicsState();
                try {
                    // style.setTextStyle(viewport, graphics);
                    final double x = point.getX();
                    final double y = point.getY();
                    final double[] location = toViewCoordinates(x, y);
                    // style.setTextStyle(viewport, graphics);
                    final Quantity<Length> textDx = style.getTextDx();
                    double dx = Viewport2D.toDisplayValue(this, textDx);
                    final Quantity<Length> textDy = style.getTextDy();
                    double dy = -Viewport2D.toDisplayValue(this, textDy);
                    final Font font = style.getFont(this);
                    final FontMetrics fontMetrics = this.canvas.getFontMetrics(font);
                    double maxWidth = 0;
                    final String[] lines = label.split("[\\r\\n]");
                    for (final String line : lines) {
                        final Rectangle2D bounds = fontMetrics.getStringBounds(line, this.canvas.getGraphics());
                        final double width = bounds.getWidth();
                        maxWidth = Math.max(width, maxWidth);
                    }
                    final int descent = fontMetrics.getDescent();
                    final int ascent = fontMetrics.getAscent();
                    final int leading = fontMetrics.getLeading();
                    final double maxHeight = lines.length * (ascent + descent) + (lines.length - 1) * leading;
                    final String verticalAlignment = style.getTextVerticalAlignment();
                    if ("top".equals(verticalAlignment)) {
                    } else if ("middle".equals(verticalAlignment)) {
                        dy -= maxHeight / 2;
                    } else {
                        dy -= maxHeight;
                    }
                    String horizontalAlignment = style.getTextHorizontalAlignment();
                    double screenX = location[0];
                    double screenY = getViewHeightPixels() - location[1];
                    final String textPlacement = style.getTextPlacementType();
                    if ("auto".equals(textPlacement)) {
                        if (screenX < 0) {
                            screenX = 1;
                            dx = 0;
                            horizontalAlignment = "left";
                        }
                        final int viewWidth = getViewWidthPixels();
                        if (screenX + maxWidth > viewWidth) {
                            screenX = (int) (viewWidth - maxWidth - 1);
                            dx = 0;
                            horizontalAlignment = "left";
                        }
                        if (screenY < maxHeight) {
                            screenY = 1;
                            dy = 0;
                        }
                        final int viewHeight = getViewHeightPixels();
                        if (screenY > viewHeight) {
                            screenY = viewHeight - 1 - maxHeight;
                            dy = 0;
                        }
                    }
                    AffineTransform transform = new AffineTransform();
                    transform.translate(screenX, screenY);
                    if (orientation != 0) {
                        transform.rotate(-Math.toRadians(orientation), 0, 0);
                    }
                    transform.translate(dx, dy);
                    for (final String line : lines) {
                        transform.translate(0, ascent);
                        final AffineTransform lineTransform = new AffineTransform(transform);
                        final Rectangle2D bounds = fontMetrics.getStringBounds(line, this.canvas.getGraphics());
                        final double width = bounds.getWidth();
                        final double height = bounds.getHeight();
                        if ("right".equals(horizontalAlignment)) {
                            transform.translate(-width, 0);
                        } else if ("center".equals(horizontalAlignment)) {
                            transform.translate(-width / 2, 0);
                        }
                        transform.translate(dx, 0);
                        transform.scale(1, 1);
                        if (Math.abs(orientation) > 90) {
                            transform.rotate(Math.PI, maxWidth / 2, -height / 4);
                        }
                        /*
               * final double textHaloRadius = Viewport2D.toDisplayValue(this,
               * style.getTextHaloRadius()); if (textHaloRadius > 0) {
               * graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
               * RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); final Stroke savedStroke =
               * graphics.getStroke(); final Stroke outlineStroke = new BasicStroke(
               * (float)textHaloRadius, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
               * graphics.setColor(style.getTextHaloFill()); graphics.setStroke(outlineStroke);
               * final Font font = graphics.getFont(); final FontRenderContext fontRenderContext =
               * graphics.getFontRenderContext(); final TextLayout textLayout = new TextLayout(line,
               * font, fontRenderContext); final Shape outlineShape =
               * textLayout.getOutline(TextStyleRenderer.NOOP_TRANSFORM);
               * graphics.draw(outlineShape); graphics.setStroke(savedStroke); }
               */
                        final Color textBoxColor = style.getTextBoxColor();
                        if (textBoxColor != null) {
                            this.contentStream.setNonStrokingColor(textBoxColor);
                            final double cornerSize = Math.max(height / 2, 5);
                            // final RoundRectangle2D.Double box = new
                            // RoundRectangle2D.Double(
                            // bounds.getX() - 3, bounds.getY() - 1, width + 6, height + 2,
                            // cornerSize, cornerSize);
                            this.contentStream.fillRect((float) bounds.getX() - 3, (float) bounds.getY() - 1, (float) width + 6, (float) height + 2);
                        }
                        this.contentStream.setNonStrokingColor(style.getTextFill());
                        this.contentStream.beginText();
                        final PDFont pdfFont = getFont("/org/apache/pdfbox/resources/ttf/ArialMT.ttf");
                        this.contentStream.setFont(pdfFont, font.getSize2D());
                        this.contentStream.setTextMatrix(transform);
                        this.contentStream.drawString(line);
                        this.contentStream.endText();
                        transform = lineTransform;
                        transform.translate(0, leading + descent);
                    }
                } finally {
                    this.contentStream.restoreGraphicsState();
                }
            }
        }
    } catch (final IOException e) {
        throw new RuntimeException("Unable to write PDF", e);
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) LineString(com.revolsys.geometry.model.LineString) IOException(java.io.IOException) Font(java.awt.Font) PDTrueTypeFont(org.apache.pdfbox.pdmodel.font.PDTrueTypeFont) PDFont(org.apache.pdfbox.pdmodel.font.PDFont) Point(com.revolsys.geometry.model.Point) PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) Length(javax.measure.quantity.Length) FontMetrics(java.awt.FontMetrics) AffineTransform(java.awt.geom.AffineTransform)

Example 2 with PointDoubleXYOrientation

use of com.revolsys.geometry.model.impl.PointDoubleXYOrientation in project com.revolsys.open by revolsys.

the class AbstractRecordLayerRenderer method getPointWithOrientationCentre.

private static PointDoubleXYOrientation getPointWithOrientationCentre(final GeometryFactory geometryFactory2dFloating, final Geometry geometry) {
    double orientation = 0;
    Point point = null;
    if (geometry instanceof LineString) {
        final LineString line = geometry.convertGeometry(geometryFactory2dFloating, 2);
        final double totalLength = line.getLength();
        final double centreLength = totalLength / 2;
        double currentLength = 0;
        for (final Segment segment : line.segments()) {
            final double segmentLength = segment.getLength();
            currentLength += segmentLength;
            if (currentLength >= centreLength) {
                final double segmentFraction = 1 - (currentLength - centreLength) / segmentLength;
                point = segment.pointAlong(segmentFraction);
                orientation = segment.getOrientaton();
                break;
            }
        }
    } else {
        point = geometry.getPointWithin();
        point = point.convertPoint2d(geometryFactory2dFloating);
    }
    return new PointDoubleXYOrientation(point, orientation);
}
Also used : PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) LineSegment(com.revolsys.geometry.model.segment.LineSegment) Segment(com.revolsys.geometry.model.segment.Segment)

Example 3 with PointDoubleXYOrientation

use of com.revolsys.geometry.model.impl.PointDoubleXYOrientation in project com.revolsys.open by revolsys.

the class AbstractRecordLayerRenderer method getPointWithOrientation.

public static PointDoubleXYOrientation getPointWithOrientation(final Viewport2D viewport, final Geometry geometry, final String placementType) {
    if (viewport == null) {
        return new PointDoubleXYOrientation(0.0, 0.0, 0);
    } else {
        final GeometryFactory viewportGeometryFactory2d = viewport.getGeometryFactory2dFloating();
        if (viewportGeometryFactory2d != null && geometry != null && !geometry.isEmpty()) {
            Point point = null;
            double orientation = 0;
            if (geometry instanceof Point) {
                point = (Point) geometry;
            } else {
                final Matcher vertexIndexMatcher = PATTERN_VERTEX_INDEX.matcher(placementType);
                if (vertexIndexMatcher.matches()) {
                    final int vertexCount = geometry.getVertexCount();
                    final int vertexIndex = getIndex(vertexIndexMatcher);
                    if (vertexIndex >= -vertexCount && vertexIndex < vertexCount) {
                        final Vertex vertex = geometry.getVertex(vertexIndex);
                        orientation = vertex.getOrientaton(viewportGeometryFactory2d);
                        point = vertex.convertGeometry(viewportGeometryFactory2d);
                    }
                } else {
                    final Matcher segmentIndexMatcher = PATTERN_SEGMENT_INDEX.matcher(placementType);
                    if (segmentIndexMatcher.matches()) {
                        final int segmentCount = geometry.getSegmentCount();
                        if (segmentCount > 0) {
                            final int index = getIndex(segmentIndexMatcher);
                            LineSegment segment = geometry.getSegment(index);
                            segment = segment.convertGeometry(viewportGeometryFactory2d);
                            if (segment != null) {
                                point = segment.midPoint();
                                orientation = segment.getOrientaton();
                            }
                        }
                    } else {
                        PointDoubleXYOrientation pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, geometry);
                        if (!viewport.getBoundingBox().covers(pointDoubleXYOrientation)) {
                            try {
                                final Geometry clippedGeometry = viewport.getBoundingBox().toPolygon().intersection(geometry);
                                if (!clippedGeometry.isEmpty()) {
                                    double maxArea = 0;
                                    double maxLength = 0;
                                    for (int i = 0; i < clippedGeometry.getGeometryCount(); i++) {
                                        final Geometry part = clippedGeometry.getGeometry(i);
                                        if (part instanceof Polygon) {
                                            final double area = part.getArea();
                                            if (area > maxArea) {
                                                maxArea = area;
                                                pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, part);
                                            }
                                        } else if (part instanceof LineString) {
                                            if (maxArea == 0 && "auto".equals(placementType)) {
                                                final double length = part.getLength();
                                                if (length > maxLength) {
                                                    maxLength = length;
                                                    pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, part);
                                                }
                                            }
                                        } else if (part instanceof Point) {
                                            if (maxArea == 0 && maxLength == 0 && "auto".equals(placementType)) {
                                                pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, part);
                                            }
                                        }
                                    }
                                }
                            } catch (final Throwable t) {
                            }
                        }
                        return pointDoubleXYOrientation;
                    }
                }
            }
            if (Property.hasValue(point)) {
                if (viewport.getBoundingBox().covers(point)) {
                    point = point.convertPoint2d(viewportGeometryFactory2d);
                    return new PointDoubleXYOrientation(point, orientation);
                }
            }
        }
        return null;
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) Vertex(com.revolsys.geometry.model.vertex.Vertex) PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Matcher(java.util.regex.Matcher) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 4 with PointDoubleXYOrientation

use of com.revolsys.geometry.model.impl.PointDoubleXYOrientation in project com.revolsys.open by revolsys.

the class TextStyleRenderer method renderText.

public static void renderText(final Viewport2D viewport, final Graphics2D graphics, final String label, final Geometry geometry, final TextStyle style) {
    if (Property.hasValue(label) && geometry != null || viewport == null) {
        final String textPlacementType = style.getTextPlacementType();
        final PointDoubleXYOrientation point = getPointWithOrientation(viewport, geometry, textPlacementType);
        if (point != null) {
            double orientation;
            final String orientationType = style.getTextOrientationType();
            if ("none".equals(orientationType)) {
                orientation = 0;
            } else {
                orientation = point.getOrientation();
                if (orientation > 270) {
                    orientation -= 360;
                }
            }
            orientation += style.getTextOrientation();
            final Paint paint = graphics.getPaint();
            final Composite composite = graphics.getComposite();
            try {
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
                final double x = point.getX();
                final double y = point.getY();
                final double[] location;
                if (viewport == null) {
                    location = new double[] { x, y };
                } else {
                    location = viewport.toViewCoordinates(x, y);
                }
                final AffineTransform savedTransform = graphics.getTransform();
                style.setTextStyle(viewport, graphics);
                final Quantity<Length> textDx = style.getTextDx();
                double dx = Viewport2D.toDisplayValue(viewport, textDx);
                final Quantity<Length> textDy = style.getTextDy();
                double dy = -Viewport2D.toDisplayValue(viewport, textDy);
                final FontMetrics fontMetrics = graphics.getFontMetrics();
                double maxWidth = 0;
                final String[] lines = label.split("[\\r\\n]");
                for (final String line : lines) {
                    final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
                    final double width = bounds.getWidth();
                    maxWidth = Math.max(width, maxWidth);
                }
                final int descent = fontMetrics.getDescent();
                final int ascent = fontMetrics.getAscent();
                final int leading = fontMetrics.getLeading();
                final double maxHeight = lines.length * (ascent + descent) + (lines.length - 1) * leading;
                final String verticalAlignment = style.getTextVerticalAlignment();
                if ("top".equals(verticalAlignment)) {
                } else if ("middle".equals(verticalAlignment)) {
                    dy -= maxHeight / 2;
                } else {
                    dy -= maxHeight;
                }
                String horizontalAlignment = style.getTextHorizontalAlignment();
                double screenX = location[0];
                double screenY = location[1];
                final String textPlacement = textPlacementType;
                if ("auto".equals(textPlacement) && viewport != null) {
                    if (screenX < 0) {
                        screenX = 1;
                        dx = 0;
                        horizontalAlignment = "left";
                    }
                    final int viewWidth = viewport.getViewWidthPixels();
                    if (screenX + maxWidth > viewWidth) {
                        screenX = (int) (viewWidth - maxWidth - 1);
                        dx = 0;
                        horizontalAlignment = "left";
                    }
                    if (screenY < maxHeight) {
                        screenY = 1;
                        dy = 0;
                    }
                    final int viewHeight = viewport.getViewHeightPixels();
                    if (screenY > viewHeight) {
                        screenY = viewHeight - 1 - maxHeight;
                        dy = 0;
                    }
                }
                graphics.translate(screenX, screenY);
                if (orientation != 0) {
                    graphics.rotate(-Math.toRadians(orientation), 0, 0);
                }
                graphics.translate(dx, dy);
                for (final String line : lines) {
                    graphics.translate(0, ascent);
                    final AffineTransform lineTransform = graphics.getTransform();
                    final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
                    final double width = bounds.getWidth();
                    final double height = bounds.getHeight();
                    if ("right".equals(horizontalAlignment)) {
                        graphics.translate(-width, 0);
                    } else if ("center".equals(horizontalAlignment) || "auto".equals(horizontalAlignment)) {
                        graphics.translate(-width / 2, 0);
                    }
                    graphics.translate(dx, 0);
                    graphics.scale(1, 1);
                    if (Math.abs(orientation) > 90) {
                        graphics.rotate(Math.PI, maxWidth / 2, -height / 4);
                    }
                    final int textBoxOpacity = style.getTextBoxOpacity();
                    final Color textBoxColor = style.getTextBoxColor();
                    if (textBoxOpacity > 0 && textBoxColor != null) {
                        graphics.setPaint(textBoxColor);
                        final double cornerSize = Math.max(height / 2, 5);
                        final RoundRectangle2D.Double box = new RoundRectangle2D.Double(bounds.getX() - 3, bounds.getY() - 1, width + 6, height + 2, cornerSize, cornerSize);
                        graphics.fill(box);
                    }
                    final double radius = style.getTextHaloRadius();
                    final Unit<Length> unit = style.getTextSizeUnit();
                    final double textHaloRadius = Viewport2D.toDisplayValue(viewport, Quantities.getQuantity(radius, unit));
                    if (textHaloRadius > 0) {
                        final Stroke savedStroke = graphics.getStroke();
                        final Stroke outlineStroke = new BasicStroke((float) (textHaloRadius + 1), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
                        graphics.setColor(style.getTextHaloFill());
                        graphics.setStroke(outlineStroke);
                        final Font font = graphics.getFont();
                        final FontRenderContext fontRenderContext = graphics.getFontRenderContext();
                        final TextLayout textLayout = new TextLayout(line, font, fontRenderContext);
                        final Shape outlineShape = textLayout.getOutline(NOOP_TRANSFORM);
                        graphics.draw(outlineShape);
                        graphics.setStroke(savedStroke);
                    }
                    graphics.setColor(style.getTextFill());
                    if (textBoxOpacity > 0 && textBoxOpacity < 255) {
                        graphics.setComposite(AlphaComposite.SrcOut);
                        graphics.drawString(line, (float) 0, (float) 0);
                        graphics.setComposite(AlphaComposite.DstOver);
                        graphics.drawString(line, (float) 0, (float) 0);
                    } else {
                        graphics.setComposite(AlphaComposite.SrcOver);
                        graphics.drawString(line, (float) 0, (float) 0);
                    }
                    graphics.setTransform(lineTransform);
                    graphics.translate(0, leading + descent);
                }
                graphics.setTransform(savedTransform);
            } finally {
                graphics.setPaint(paint);
                graphics.setComposite(composite);
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Paint(java.awt.Paint) Paint(java.awt.Paint) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout) PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) Length(javax.measure.quantity.Length) FontMetrics(java.awt.FontMetrics) AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext)

Example 5 with PointDoubleXYOrientation

use of com.revolsys.geometry.model.impl.PointDoubleXYOrientation in project com.revolsys.open by revolsys.

the class MarkerStyleRenderer method renderMarker.

private static void renderMarker(final Viewport2D viewport, final Graphics2D graphics, final LineString line, final MarkerStyle style) {
    final PointDoubleXYOrientation point = getMarkerLocation(viewport, line, style);
    if (point != null) {
        final double orientation = point.getOrientation();
        renderMarker(viewport, graphics, point, style, orientation);
    }
}
Also used : PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation)

Aggregations

PointDoubleXYOrientation (com.revolsys.geometry.model.impl.PointDoubleXYOrientation)5 LineString (com.revolsys.geometry.model.LineString)3 Point (com.revolsys.geometry.model.Point)3 LineSegment (com.revolsys.geometry.model.segment.LineSegment)2 Color (java.awt.Color)2 Font (java.awt.Font)2 FontMetrics (java.awt.FontMetrics)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2 Length (javax.measure.quantity.Length)2 Geometry (com.revolsys.geometry.model.Geometry)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 Polygon (com.revolsys.geometry.model.Polygon)1 Segment (com.revolsys.geometry.model.segment.Segment)1 Vertex (com.revolsys.geometry.model.vertex.Vertex)1 AlphaComposite (java.awt.AlphaComposite)1 BasicStroke (java.awt.BasicStroke)1 Composite (java.awt.Composite)1 Paint (java.awt.Paint)1 Shape (java.awt.Shape)1