Search in sources :

Example 21 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class GeometryStyle method setLineStyle.

public void setLineStyle(final Viewport2D viewport, final Graphics2D graphics) {
    final Color color = getLineColor();
    graphics.setColor(color);
    final Unit<Length> unit = this.lineWidth.getUnit();
    final float width = (float) Viewport2D.toModelValue(viewport, this.lineWidth);
    final float dashOffset = (float) Viewport2D.toModelValue(viewport, Quantities.getQuantity(this.lineDashOffset, unit));
    final float[] dashArray;
    final int dashArraySize = this.lineDashArray.size();
    if (dashArraySize == 0) {
        dashArray = null;
    } else {
        dashArray = new float[dashArraySize];
        for (int i = 0; i < dashArray.length; i++) {
            final Double dashDouble = this.lineDashArray.get(i);
            final float dashFloat = (float) Viewport2D.toModelValue(viewport, Quantities.getQuantity(dashDouble, unit));
            dashArray[i] = dashFloat;
        }
    }
    final int lineCap = this.lineCap.getAwtValue();
    final int lineJoin = this.lineJoin.getAwtValue();
    final BasicStroke basicStroke = new BasicStroke(width, lineCap, lineJoin, this.lineMiterlimit, dashArray, dashOffset);
    graphics.setStroke(basicStroke);
}
Also used : BasicStroke(java.awt.BasicStroke) Length(javax.measure.quantity.Length) Color(java.awt.Color)

Example 22 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class TextMarker method render.

@Override
public void render(final Viewport2D viewport, final Graphics2D graphics, final MarkerStyle style, final double modelX, final double modelY, double orientation) {
    try (BaseCloseable transformCloseable = new CloseableAffineTransform(graphics)) {
        Viewport2D.setUseModelCoordinates(viewport, graphics, false);
        final Quantity<Length> markerHeight = style.getMarkerHeight();
        final double mapHeight = Viewport2D.toDisplayValue(viewport, markerHeight);
        final String orientationType = style.getMarkerOrientationType();
        if ("none".equals(orientationType)) {
            orientation = 0;
        }
        final int fontSize = (int) mapHeight;
        if (this.font == null || this.font.getSize() != fontSize) {
            this.font = Fonts.newFont(this.textFaceName, 0, fontSize);
        }
        final FontRenderContext fontRenderContext = graphics.getFontRenderContext();
        final GlyphVector glyphVector = this.font.createGlyphVector(fontRenderContext, this.text);
        final Shape shape = glyphVector.getOutline();
        final GeneralPath newShape = new GeneralPath(shape);
        final Rectangle2D bounds = newShape.getBounds2D();
        final double shapeWidth = bounds.getWidth();
        final double shapeHeight = bounds.getHeight();
        Viewport2D.translateModelToViewCoordinates(viewport, graphics, modelX, modelY);
        final double markerOrientation = style.getMarkerOrientation();
        orientation = -orientation + markerOrientation;
        if (orientation != 0) {
            graphics.rotate(Math.toRadians(orientation));
        }
        final Quantity<Length> deltaX = style.getMarkerDx();
        final Quantity<Length> deltaY = style.getMarkerDy();
        double dx = Viewport2D.toDisplayValue(viewport, deltaX);
        double dy = Viewport2D.toDisplayValue(viewport, deltaY);
        dy -= bounds.getY();
        final String verticalAlignment = style.getMarkerVerticalAlignment();
        if ("bottom".equals(verticalAlignment)) {
            dy -= shapeHeight;
        } else if ("auto".equals(verticalAlignment) || "middle".equals(verticalAlignment)) {
            dy -= shapeHeight / 2.0;
        }
        final String horizontalAlignment = style.getMarkerHorizontalAlignment();
        if ("right".equals(horizontalAlignment)) {
            dx -= shapeWidth;
        } else if ("auto".equals(horizontalAlignment) || "center".equals(horizontalAlignment)) {
            dx -= shapeWidth / 2;
        }
        graphics.translate(dx, dy);
        if (style.setMarkerFillStyle(viewport, graphics)) {
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            graphics.setFont(this.font);
            graphics.drawString(this.text, 0, 0);
        }
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) GlyphVector(java.awt.font.GlyphVector) Shape(java.awt.Shape) GeneralPath(java.awt.geom.GeneralPath) Length(javax.measure.quantity.Length) CloseableAffineTransform(com.revolsys.awt.CloseableAffineTransform) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext)

Example 23 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class Viewport2D method toModelValue.

public double toModelValue(final Quantity<Length> value) {
    double convertedValue;
    final Unit<Length> unit = value.getUnit();
    if (unit.equals(CustomUnits.PIXEL)) {
        convertedValue = QuantityType.doubleValue(value, CustomUnits.PIXEL);
        final double modelUnitsPerViewUnit = getModelUnitsPerViewUnit();
        convertedValue *= modelUnitsPerViewUnit;
    } else {
        convertedValue = QuantityType.doubleValue(value, Units.METRE);
        final CoordinateSystem coordinateSystem = this.geometryFactory2d.getCoordinateSystem();
        if (coordinateSystem instanceof GeographicCoordinateSystem) {
            final GeographicCoordinateSystem geoCs = (GeographicCoordinateSystem) coordinateSystem;
            final double radius = geoCs.getDatum().getEllipsoid().getSemiMajorAxis();
            convertedValue = Math.toDegrees(convertedValue / radius);
        }
    }
    return convertedValue;
}
Also used : Length(javax.measure.quantity.Length) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem)

Example 24 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class ArcGisRestServerRecordLayer method addTextRenderer.

private void addTextRenderer(final AbstractMultipleRenderer renderers, final MapEx labelProperties) {
    final TextStyle textStyle = new TextStyle();
    final String alignment = labelProperties.getString("labelPlacement");
    if (alignment.endsWith("Left")) {
        textStyle.setTextHorizontalAlignment("right");
    } else if (alignment.endsWith("Right")) {
        textStyle.setTextHorizontalAlignment("left");
    } else if (alignment.endsWith("Before")) {
        textStyle.setTextHorizontalAlignment("right");
        textStyle.setTextPlacementType("vertex(0)");
    } else if (alignment.endsWith("Start")) {
        textStyle.setTextHorizontalAlignment("left");
        textStyle.setTextPlacementType("vertex(0)");
    } else if (alignment.endsWith("After")) {
        textStyle.setTextHorizontalAlignment("left");
        textStyle.setTextPlacementType("vertex(n)");
    } else if (alignment.endsWith("End")) {
        textStyle.setTextHorizontalAlignment("right");
        textStyle.setTextPlacementType("vertex(n)");
    } else if (alignment.endsWith("Along")) {
        textStyle.setTextHorizontalAlignment("center");
        textStyle.setTextPlacementType("auto");
    } else {
        textStyle.setTextHorizontalAlignment("center");
    }
    if (alignment.contains("Above")) {
        textStyle.setTextVerticalAlignment("bottom");
    } else if (alignment.endsWith("Below")) {
        textStyle.setTextVerticalAlignment("top");
    } else {
        textStyle.setTextVerticalAlignment("center");
    }
    String textName = labelProperties.getString("labelExpression");
    textName = textName.replace(" CONCAT ", "");
    textName = textName.replaceAll("\"([^\"]+)\"", "$1");
    textStyle.setTextName(textName);
    final MapEx symbol = labelProperties.getValue("symbol");
    if ("esriTS".equals(symbol.getString("type"))) {
        final Color textFill = getColor(symbol);
        textStyle.setTextFill(textFill);
        final Color backgroundColor = getColor(symbol, "backgroundColor");
        textStyle.setTextBoxColor(backgroundColor);
        // "useCodedValues": false,
        // "borderLineColor": null,
        // "verticalAlignment": "bottom",
        // "horizontalAlignment": "left",
        // "rightToLeft": false,
        // "kerning": true,
        final double angle = symbol.getDouble("angle", 0);
        textStyle.setTextOrientation(angle);
        final Quantity<Length> textDx = Quantities.getQuantity(symbol.getDouble("xoffset", 0), CustomUnits.PIXEL);
        textStyle.setTextDx(textDx);
        final Quantity<Length> textDy = Quantities.getQuantity(symbol.getDouble("yoffset", 0), CustomUnits.PIXEL);
        textStyle.setTextDx(textDy);
        final MapEx font = symbol.getValue("font");
        if (font != null) {
            final String faceName = font.getString("family", "Arial");
            textStyle.setTextFaceName(faceName);
            final Quantity<Length> size = Quantities.getQuantity(font.getDouble("size", 10), CustomUnits.PIXEL);
            textStyle.setTextSize(size);
        }
    // "font": {
    // "style": "normal",
    // "weight": "bold",
    // "decoration": "none"
    }
    final TextStyleRenderer textRenderer = new TextStyleRenderer(this, textStyle);
    textRenderer.setName(textName.replace("[", "").replace("]", ""));
    long minimumScale = labelProperties.getLong("minScale", Long.MAX_VALUE);
    if (minimumScale == 0) {
        minimumScale = Long.MAX_VALUE;
    }
    textRenderer.setMinimumScale(minimumScale);
    final long maximumScale = labelProperties.getLong("maxScale", 0);
    textRenderer.setMaximumScale(maximumScale);
    final String where = labelProperties.getString("where");
    textRenderer.setQueryFilter(where);
    renderers.addRenderer(textRenderer);
}
Also used : TextStyle(com.revolsys.swing.map.layer.record.style.TextStyle) MapEx(com.revolsys.collection.map.MapEx) Length(javax.measure.quantity.Length) Color(java.awt.Color) TextStyleRenderer(com.revolsys.swing.map.layer.record.renderer.TextStyleRenderer)

Example 25 with Length

use of javax.measure.quantity.Length 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)

Aggregations

Length (javax.measure.quantity.Length)44 Angle (javax.measure.quantity.Angle)7 Test (org.junit.Test)7 Color (java.awt.Color)5 Unit (javax.measure.Unit)5 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 AffineTransform (java.awt.geom.AffineTransform)4 ArrayList (java.util.ArrayList)4 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)3 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 LineString (com.revolsys.geometry.model.LineString)3 Point (com.revolsys.geometry.model.Point)3 Shape (java.awt.Shape)3 Rectangle2D (java.awt.geom.Rectangle2D)3 Path (java.nio.file.Path)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Quantity (javax.measure.Quantity)3 CloseableAffineTransform (com.revolsys.awt.CloseableAffineTransform)2 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)2 ChainedCoordinatesOperation (com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation)2