void updateSize() { BufferedImage img = paint.getImage(); int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); double anchorWidth = paint.getAnchorRect().getWidth(); double anchorHeight = paint.getAnchorRect().getHeight(); if ((imageWidth != anchorWidth) || (imageHeight != anchorHeight)) { paint = createTexture(img, imageWidth, imageHeight); } }
/** * DOCUMENT ME! * * @param graphics DOCUMENT ME! * @param shape DOCUMENT ME! * @param style DOCUMENT ME! */ public static void drawShape(Graphics2D graphics, Shape shape, Style2D style) { if (style instanceof MarkStyle2D) { // get the point onto the shape has to be painted float[] coords = new float[2]; PathIterator iter = shape.getPathIterator(IDENTITY_TRANSFORM); iter.currentSegment(coords); MarkStyle2D ms2d = (MarkStyle2D) style; Shape transformedShape = ms2d.getTransformedShape(coords[0], coords[1]); if (transformedShape != null) { if (ms2d.getFill() != null) { graphics.setPaint(ms2d.getFill()); graphics.setComposite(ms2d.getFillComposite()); graphics.fill(transformedShape); } if (ms2d.getContour() != null) { graphics.setPaint(ms2d.getContour()); graphics.setStroke(ms2d.getStroke()); graphics.setComposite(ms2d.getContourComposite()); graphics.draw(transformedShape); } } } else if (style instanceof GraphicStyle2D) { // get the point onto the shape has to be painted float[] coords = new float[2]; PathIterator iter = shape.getPathIterator(IDENTITY_TRANSFORM); iter.currentSegment(coords); GraphicStyle2D gs2d = (GraphicStyle2D) style; renderImage( graphics, coords[0], coords[1], (Image) gs2d.getImage(), gs2d.getRotation(), gs2d.getOpacity()); } else if (style instanceof TextStyle2D) { // get the point onto the shape has to be painted float[] coords = new float[2]; PathIterator iter = shape.getPathIterator(IDENTITY_TRANSFORM); iter.currentSegment(coords); AffineTransform old = graphics.getTransform(); AffineTransform temp = new AffineTransform(old); TextStyle2D ts2d = (TextStyle2D) style; GlyphVector textGv = ts2d.getTextGlyphVector(graphics); Rectangle2D bounds = textGv.getVisualBounds(); temp.translate(coords[0], coords[1]); double x = 0; double y = 0; x = (ts2d.getAnchorX() * (-bounds.getWidth())) + ts2d.getDisplacementX(); y = (ts2d.getAnchorY() * (bounds.getHeight())) + ts2d.getDisplacementY(); temp.rotate(ts2d.getRotation()); temp.translate(x, y); graphics.setTransform(temp); if (ts2d.getHaloFill() != null) { float radious = ts2d.getHaloRadius(); // graphics.translate(radious, -radious); graphics.setPaint(ts2d.getHaloFill()); graphics.setComposite(ts2d.getHaloComposite()); graphics.fill(ts2d.getHaloShape(graphics)); // graphics.translate(radious, radious); } if (ts2d.getFill() != null) { graphics.setPaint(ts2d.getFill()); graphics.setComposite(ts2d.getComposite()); graphics.drawGlyphVector(textGv, 0, 0); } graphics.setTransform(old); } else { // if the style is a polygon one, process it even if the polyline is // not // closed (by SLD specification) if (style instanceof PolygonStyle2D) { PolygonStyle2D ps2d = (PolygonStyle2D) style; if (ps2d.getFill() != null) { Paint paint = ps2d.getFill(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } graphics.setPaint(paint); graphics.setComposite(ps2d.getFillComposite()); graphics.fill(shape); } } if (style instanceof LineStyle2D) { LineStyle2D ls2d = (LineStyle2D) style; if (ls2d.getStroke() != null) { // see if a graphic stroke is to be used, the drawing method // is completely // different in this case Paint paint = ls2d.getContour(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } graphics.setPaint(paint); graphics.setStroke(ls2d.getStroke()); graphics.setComposite(ls2d.getContourComposite()); graphics.draw(shape); } } } }
/** * Invoked automatically when a polyline is about to be draw. This implementation paints the * polyline according to the rendered style * * @param graphics The graphics in which to draw. * @param shape The polygon to draw. * @param style The style to apply, or <code>null</code> if none. * @param scale The scale denominator for the current zoom level * @throws FactoryException * @throws TransformException */ public void paint( final Graphics2D graphics, final LiteShape2 shape, final Style2D style, final double scale, boolean isLabelObstacle) { if (style == null) { // TODO: what's going on? Should not be reached... LOGGER.severe("ShapePainter has been asked to paint a null style!!"); return; } // Is the current scale within the style scale range? if (!style.isScaleInRange(scale)) { LOGGER.fine("Out of scale"); return; } if (style instanceof IconStyle2D) { AffineTransform temp = graphics.getTransform(); try { IconStyle2D icoStyle = (IconStyle2D) style; Icon icon = icoStyle.getIcon(); graphics.setComposite(icoStyle.getComposite()); // the displacement to be applied to all points, centers the icon and applies the // Graphic displacement as well float dx = icoStyle.getDisplacementX(); float dy = icoStyle.getDisplacementY(); // iterate over all points float[] coords = new float[2]; PathIterator citer = getPathIterator(shape); AffineTransform at = new AffineTransform(temp); while (!(citer.isDone())) { if (citer.currentSegment(coords) != PathIterator.SEG_MOVETO) { at.setTransform(temp); double x = coords[0] + dx; double y = coords[1] + dy; at.translate(x, y); at.rotate(icoStyle.getRotation()); at.translate( -(icon.getIconWidth() * icoStyle.getAnchorPointX()), (icon.getIconHeight() * (icoStyle.getAnchorPointY() - 1))); graphics.setTransform(at); icon.paintIcon(null, graphics, 0, 0); if (isLabelObstacle) { // TODO: rotation? labelCache.put( new Rectangle2D.Double(x, y, icon.getIconWidth(), icon.getIconHeight())); } } citer.next(); } } finally { graphics.setTransform(temp); } } else if (style instanceof MarkStyle2D) { PathIterator citer = getPathIterator(shape); // get the point onto the shape has to be painted float[] coords = new float[2]; MarkStyle2D ms2d = (MarkStyle2D) style; Shape transformedShape; while (!(citer.isDone())) { if (citer.currentSegment(coords) != PathIterator.SEG_MOVETO) { transformedShape = ms2d.getTransformedShape(coords[0], coords[1]); if (transformedShape != null) { if (ms2d.getFill() != null) { graphics.setPaint(ms2d.getFill()); graphics.setComposite(ms2d.getFillComposite()); graphics.fill(transformedShape); } if (ms2d.getContour() != null) { graphics.setPaint(ms2d.getContour()); graphics.setStroke(ms2d.getStroke()); graphics.setComposite(ms2d.getContourComposite()); graphics.draw(transformedShape); } if (isLabelObstacle) { labelCache.put(transformedShape.getBounds2D()); } } } citer.next(); } } else if (style instanceof GraphicStyle2D) { float[] coords = new float[2]; PathIterator iter = getPathIterator(shape); iter.currentSegment(coords); GraphicStyle2D gs2d = (GraphicStyle2D) style; BufferedImage image = gs2d.getImage(); double dx = gs2d.getDisplacementX() - gs2d.getAnchorPointX() * image.getWidth(); double dy = gs2d.getDisplacementY() - ((1 - gs2d.getAnchorPointY()) * image.getHeight()); while (!(iter.isDone())) { if (iter.currentSegment(coords) != PathIterator.SEG_MOVETO) { renderImage( graphics, coords[0], coords[1], dx, dy, image, gs2d.getRotation(), gs2d.getComposite(), isLabelObstacle); } iter.next(); } } else { if (isLabelObstacle) { labelCache.put(shape.getBounds2D()); } // if the style is a polygon one, process it even if the polyline is // not closed (by SLD specification) if (style instanceof PolygonStyle2D) { PolygonStyle2D ps2d = (PolygonStyle2D) style; if (ps2d.getFill() != null) { Paint paint = ps2d.getFill(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D cornerRect = tp.getAnchorRect(); Point2D anchorPoint = (Point2D) graphics.getRenderingHint(TEXTURE_ANCHOR_HINT_KEY); Rectangle2D alignedRect = null; if (anchorPoint != null) { alignedRect = new Rectangle2D.Double( Math.round(anchorPoint.getX()), Math.round(anchorPoint.getY()), cornerRect.getWidth(), cornerRect.getHeight()); } else { alignedRect = new Rectangle2D.Double(0.0, 0.0, cornerRect.getWidth(), cornerRect.getHeight()); } paint = new TexturePaint(image, alignedRect); } graphics.setPaint(paint); graphics.setComposite(ps2d.getFillComposite()); fillLiteShape(graphics, shape); } if (ps2d.getGraphicFill() != null) { Shape oldClip = graphics.getClip(); try { paintGraphicFill(graphics, shape, ps2d.getGraphicFill(), scale); } finally { graphics.setClip(oldClip); } } } if (style instanceof LineStyle2D) { LineStyle2D ls2d = (LineStyle2D) style; if (ls2d.getStroke() != null) { // see if a graphic stroke is to be used, the drawing method // is completely // different in this case if (ls2d.getGraphicStroke() != null) { drawWithGraphicsStroke( graphics, dashShape(shape, ls2d.getStroke()), ls2d.getGraphicStroke(), isLabelObstacle); } else { Paint paint = ls2d.getContour(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } // debugShape(shape); Stroke stroke = ls2d.getStroke(); if (graphics.getRenderingHint(RenderingHints.KEY_ANTIALIASING) == RenderingHints.VALUE_ANTIALIAS_ON) { if (stroke instanceof BasicStroke) { BasicStroke bs = (BasicStroke) stroke; stroke = new BasicStroke( bs.getLineWidth() + 0.5f, bs.getEndCap(), bs.getLineJoin(), bs.getMiterLimit(), bs.getDashArray(), bs.getDashPhase()); } } graphics.setPaint(paint); graphics.setStroke(stroke); graphics.setComposite(ls2d.getContourComposite()); graphics.draw(shape); } } } } }
private void setPaint(boolean invert, double xoffset, double yoffset, boolean fill) { if (paint instanceof Color) { Color color = (Color) paint; int alpha = color.getAlpha(); if (fill) { if (alpha != currentFillGState) { currentFillGState = alpha; PdfGState gs = fillGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(alpha / 255f); fillGState[alpha] = gs; } cb.setGState(gs); } cb.setColorFill(color); } else { if (alpha != currentStrokeGState) { currentStrokeGState = alpha; PdfGState gs = strokeGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setStrokeOpacity(alpha / 255f); strokeGState[alpha] = gs; } cb.setGState(gs); } cb.setColorStroke(color); } } else if (paint instanceof GradientPaint) { GradientPaint gp = (GradientPaint) paint; Point2D p1 = gp.getPoint1(); transform.transform(p1, p1); Point2D p2 = gp.getPoint2(); transform.transform(p2, p2); Color c1 = gp.getColor1(); Color c2 = gp.getColor2(); PdfShading shading = PdfShading.simpleAxial( cb.getPdfWriter(), (float) p1.getX(), normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2); PdfShadingPattern pat = new PdfShadingPattern(shading); if (fill) cb.setShadingFill(pat); else cb.setShadingStroke(pat); } else if (paint instanceof TexturePaint) { try { TexturePaint tp = (TexturePaint) paint; BufferedImage img = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight()); AffineTransform inverse = this.normalizeMatrix(); inverse.translate(rect.getX(), rect.getY()); inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight()); double[] mx = new double[6]; inverse.getMatrix(mx); pattern.setPatternMatrix( (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); image.setAbsolutePosition(0, 0); pattern.addImage(image); if (fill) cb.setPatternFill(pattern); else cb.setPatternStroke(pattern); } catch (Exception ex) { if (fill) cb.setColorFill(Color.gray); else cb.setColorStroke(Color.gray); } } else { try { BufferedImage img = null; int type = BufferedImage.TYPE_4BYTE_ABGR; if (paint.getTransparency() == Transparency.OPAQUE) { type = BufferedImage.TYPE_3BYTE_BGR; } img = new BufferedImage((int) width, (int) height, type); Graphics2D g = (Graphics2D) img.getGraphics(); g.transform(transform); AffineTransform inv = transform.createInverse(); Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()); fillRect = inv.createTransformedShape(fillRect); g.setPaint(paint); g.fill(fillRect); if (invert) { AffineTransform tx = new AffineTransform(); tx.scale(1, -1); tx.translate(-xoffset, -yoffset); g.drawImage(img, tx, null); } g.dispose(); g = null; com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); PdfPatternPainter pattern = cb.createPattern(width, height); image.setAbsolutePosition(0, 0); pattern.addImage(image); if (fill) cb.setPatternFill(pattern); else cb.setPatternStroke(pattern); } catch (Exception ex) { if (fill) cb.setColorFill(Color.gray); else cb.setColorStroke(Color.gray); } } }