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);
}
}
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);
}
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;
}
}
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);
}
}
}
}
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);
}
}
Aggregations