public void paint(Graphics2D g, int width, int height, int x, int y) { // saves the old transform; AffineTransform oldTransform = g.getTransform(); try { if (oldTransform == null) oldTransform = new AffineTransform(); AffineTransform transform = new AffineTransform(oldTransform); // adds scaling to the transform so that we respect the declared size transform.translate(x, y); transform.scale(width / bounds.getWidth(), height / bounds.getHeight()); g.setTransform(transform); node.paint(g); } finally { g.setTransform(oldTransform); } } }
public RenderableSVG(Document doc) { this.node = getGraphicNode(doc); this.bounds = getSvgDocBounds(doc); if (bounds == null) bounds = node.getBounds(); }
private Shape getShape(GraphicsNode node) { if (!node.isVisible()) { return null; gp.append(result.getPathIterator(new AffineTransform()), false); gp.append(subShape.getPathIterator(new AffineTransform()), false); result = gp; AffineTransform transform = node.getTransform(); if (result != null && transform != null && !transform.isIdentity()) { result = new TransformedShape(result, transform);
public static void render( Graphics2D gc, GraphicsNode svgGraphicsNode, Dimension2D svgGraphicsSize, int centerX, int centerY, int width, int height, double angleRadians ) { double scaleX = width / svgGraphicsSize.getWidth(); double scaleY = height / svgGraphicsSize.getHeight(); AffineTransform affineTransform = new AffineTransform(); if ( centerX != 0 || centerY != 0 ) { affineTransform.translate( centerX, centerY ); } affineTransform.scale( scaleX, scaleY ); if ( angleRadians != 0 ) { affineTransform.rotate( angleRadians ); } affineTransform.translate( -svgGraphicsSize.getWidth() / 2, -svgGraphicsSize.getHeight() / 2 ); svgGraphicsNode.setTransform( affineTransform ); svgGraphicsNode.paint( gc ); } }
Rectangle2D svgBounds = svgGN.getSensitiveBounds(); if (svgBounds == null) return ret; if (!rect.intersects(svgBounds)) return ret; AffineTransform ati = svgGN.getGlobalTransform(); try { ati = ati.createInverse(); } catch (NoninvertibleTransformException e) { Node next = base.getFirstChild(); while (next != null) { if (next instanceof Element) || SVG_A_TAG.equals(tag)); GraphicsNode gn = ctx.getGraphicsNode(curr); if (gn == null) { AffineTransform at = gn.getGlobalTransform(); Rectangle2D gnBounds = gn.getSensitiveBounds(); at.preConcatenate(ati); if (gnBounds != null) gnBounds = at.createTransformedShape(gnBounds).getBounds2D(); (!rect.intersects(gnBounds))) {
/** * Returns an AffineTransform to move to the objectBoundingBox * coordinate system. * * @param Tx the original transformation * @param node the graphics node that defines the coordinate * system to move into */ public static AffineTransform toObjectBBox(AffineTransform Tx, GraphicsNode node) { AffineTransform Mx = new AffineTransform(); Rectangle2D bounds = node.getGeometryBounds(); if (bounds != null) { Mx.translate(bounds.getX(), bounds.getY()); Mx.scale(bounds.getWidth(), bounds.getHeight()); } Mx.concatenate(Tx); return Mx; }
public void genRect(WritableRaster wr) { // System.out.println(" Rect: " + wr.getBounds()); BufferedImage offScreen = new BufferedImage(cm, wr.createWritableTranslatedChild(0,0), cm.isAlphaPremultiplied(), null); Graphics2D g = GraphicsUtil.createGraphics(offScreen, hints); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, wr.getWidth(), wr.getHeight()); g.setComposite(AlphaComposite.SrcOver); g.translate(-wr.getMinX(), -wr.getMinY()); // Set transform g.transform(node2dev); // Invoke primitive paint. if (usePrimitivePaint) { node.primitivePaint(g); } else { node.paint (g); } g.dispose(); }
AffineTransform.getTranslateInstance(position.getX(), position.getY()); if (transform != null) { tr.concatenate(transform); Shape tShape = tr.createTransformedShape(dShape); if (tpi.fillPaint != null) { graphics2D.setPaint(tpi.fillPaint); graphics2D.fill(tShape); graphics2D.setStroke(tpi.strokeStroke); graphics2D.setPaint(tpi.strokePaint); graphics2D.draw(tShape); glyphChildrenNode.setTransform(tr); glyphChildrenNode.paint(graphics2D);
Element currentElement = (Element) elements.get(i); GraphicsNode nodeToPaint = canvas.getUpdateManager() .getBridgeContext().getGraphicsNode(currentElement); if (nodeToPaint != null) { AffineTransform elementsAt = nodeToPaint.getGlobalTransform(); Shape selectionHighlight = nodeToPaint.getOutline(); AffineTransform at = canvas.getRenderingTransform(); at.concatenate(elementsAt); Shape s = at.createTransformedShape(selectionHighlight); if (s == null) { break; g2d.setColor(Color.black); g2d.setXORMode(Color.yellow); g2d.fill(s); g2d.draw(s); } else {
GraphicsNode node = ctx.getGVTBuilder().build(ctx, refElement); Filter filter = node.getGraphicsNodeRable(true); AffineTransform at = new AffineTransform(); Element filterDefElement = (Element)(filterElement.getParentNode()); String s = SVGUtilities.getChainableAttributeNS (filterDefElement, null, SVG_PRIMITIVE_UNITS_ATTRIBUTE, ctx); Rectangle2D bounds = filteredNode.getGeometryBounds(); at.preConcatenate(AffineTransform.getTranslateInstance (primitiveRegion.getX() - bounds.getX(), primitiveRegion.getY() - bounds.getY()));
/** * Draws the object. * * @param g the graphics device. * @param area the area inside which the object should be drawn. */ public void draw( final Graphics2D g, final Rectangle2D area ) { if ( width == 0 || height == 0 ) { return; } final Graphics2D g2 = (Graphics2D) g.create(); try { g2.translate( -area.getX(), -area.getY() ); final double sx = area.getWidth() / width; final double sy = area.getHeight() / height; final double sm = Math.min( sx, sy ); g2.scale( sm, sm ); rootNode.paint( g2 ); } finally { g2.dispose(); } } }
SAXSVGDocumentFactory fac = new SAXSVGDocumentFactory( "org.apache.xerces.parsers.SAXParser" ); SVGDocument doc = fac.createSVGDocument( url, in ); GVTBuilder builder = new GVTBuilder(); UserAgent userAgent = new UserAgentAdapter(); DocumentLoader loader = new DocumentLoader( userAgent ); BridgeContext ctx = new BridgeContext( userAgent, loader ); RootGraphicsNode root = builder.build( ctx, doc ).getRoot(); AffineTransform t = new AffineTransform(); Rectangle2D rect = root.getBounds(); double max = max( rect.getWidth(), rect.getHeight() ); t.scale( 1 / max, 1 / max ); t.translate( -rect.getX(), -rect.getY() ); while ( ( node = walker.nextGraphicsNode() ) != null ) { AffineTransform t2 = (AffineTransform) t.clone(); if ( node.getTransform() != null ) { t2.concatenate( node.getTransform() ); node.setTransform( t2 ); shape.append( node.getOutline(), false );
/** * Returns the specified a Rectangle2D move to the objectBoundingBox * coordinate system of the specified graphics node. * * @param r the original Rectangle2D * @param node the graphics node that defines the coordinate * system to move into */ public static Rectangle2D toObjectBBox(Rectangle2D r, GraphicsNode node) { Rectangle2D bounds = node.getGeometryBounds(); if (bounds != null) { return new Rectangle2D.Double (bounds.getX() + r.getX()*bounds.getWidth(), bounds.getY() + r.getY()*bounds.getHeight(), r.getWidth() * bounds.getWidth(), r.getHeight() * bounds.getHeight()); } else { return new Rectangle2D.Double(); } }
public void render(Graphics2D grx, Rectangle2D rectangle) throws JRException { ensureSvg(); AffineTransform transform = ViewBox.getPreserveAspectRatioTransform( new float[]{0, 0, (float) documentSize.getWidth(), (float) documentSize.getHeight()}, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true, (float) rectangle.getWidth(), (float) rectangle.getHeight()); Graphics2D graphics = (Graphics2D) grx.create(); graphics.translate(rectangle.getX(), rectangle.getY()); graphics.transform(transform); // CompositeGraphicsNode not thread safe synchronized (rootNode) { rootNode.paint(graphics); } }
gnBounds = SVGTextElementBridge.getTextBounds (ctx, element, true); Element p = (Element)element.getParentNode(); gn = ctx.getGraphicsNode(p); p = (Element)p.getParentNode(); gnBounds = gn.getSensitiveBounds(); } else if (gn != null) gnBounds = gn.getSensitiveBounds(); GraphicsNode svgGN = ctx.getGraphicsNode(e); if (svgGN == null) return false; // not in tree? (svgRect.getX(), svgRect.getY(), svgRect.getWidth(), svgRect.getHeight()); AffineTransform ati = svgGN.getGlobalTransform(); try { ati = ati.createInverse(); } catch (NoninvertibleTransformException e) { } AffineTransform at = gn.getGlobalTransform(); at.preConcatenate(ati); gnBounds = at.createTransformedShape(gnBounds).getBounds2D(); return rect.contains(gnBounds);
final BridgeContext ctx = new BridgeContext( userAgent, loader ); final GVTBuilder builder = new GVTBuilder(); final GraphicsNode node = builder.build( ctx, document ); final Rectangle2D bounds = node.getBounds(); new BufferedImage( (int) bounds.getWidth(), (int) bounds.getHeight(), BufferedImage.TYPE_INT_ARGB ); final Graphics2D graphics = (Graphics2D) bi.getGraphics(); drawable.draw( graphics, new Rectangle2D.Double( 0, 0, bounds.getWidth(), bounds.getHeight() ) ); graphics.dispose(); return new SimpleResource( data.getKey(), bi, Image.class, version ); } catch ( IOException e ) {
DocumentLoader loader = new DocumentLoader(userAgent); bridgeContext = new BridgeContext(userAgent, loader); bridgeContext.setDynamicState(BridgeContext.STATIC); builder = new GVTBuilder(); try { SVGDocument imageDocument = factory.createSVGDocument(resource.toString()); imageGraphics = builder.build(bridgeContext, imageDocument); } catch (IOException e) { throw new RuntimeException("Couldn't load SVG resource", e); float width = (float) imageGraphics.getBounds().getWidth(); float height = (float) imageGraphics.getBounds().getHeight(); try { Rectangle2D bounds = imageGraphics.getBounds(); graphics.translate(-bounds.getX(), -bounds.getY()); imageGraphics.paint(graphics); throw new RuntimeException("Couldn't generate PDF from SVG", e); } finally { graphics.dispose();
int y = info.currentYPosition; SVGUserAgent ua = new SVGUserAgent(context.getUserAgent(), DefaultFontFamilyResolver.SINGLETON, new AffineTransform()); BridgeContext ctx = new BridgeContext(ua); GVTBuilder builder = new GVTBuilder(); root = builder.build(ctx, clonedDoc); } catch (Exception e) { SVGEventProducer eventProducer = SVGEventProducer.Provider.get( float iw = (float) ctx.getDocumentSize().getWidth() * 1000f; float ih = (float) ctx.getDocumentSize().getHeight() * 1000f; AffineTransform origTransform = info.state.getGraph().getTransform(); info.state.getGraph().translate(x / 1000f, y / 1000f); AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih); if (!at.isIdentity()) { info.state.getGraph().transform(at); root.paint(info.state.getGraph()); } catch (Exception e) { SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName()); UserAgent userAgent = new UserAgentAdapter(); DocumentLoader loader = new DocumentLoader(userAgent); BridgeContext ctx = new BridgeContext(userAgent, loader); ctx.setDynamicState(BridgeContext.DYNAMIC); GVTBuilder builder = new GVTBuilder(); SVGDocument svgDoc = factory.createSVGDocument(rootPath, new StringReader(svg)); PdfTemplate svgTempl = PdfTemplate.createTemplate(writer, Float.parseFloat(svgDoc.getDocumentElement().getAttribute("width")), Float.parseFloat(svgDoc.getDocumentElement().getAttribute("height"))); Graphics2D g2d = new PdfGraphics2D(svgTempl, svgTempl.getWidth(), svgTempl.getHeight()); GraphicsNode chartGfx = builder.build(ctx, svgDoc); chartGfx.paint(g2d); g2d.dispose(); Image svgImg = new ImgTemplate(svgTempl); svgImg.setAlignment(Image.ALIGN_CENTER); //svgImg.setBorder(Image.BOX); //svgImg.setBorderColor(new BaseColor(0xff, 0x00, 0x00)); //svgImg.setBorderWidth(1); document.add(svgImg);
public boolean checkIntersection (Element element, SVGRect svgRect ) { GraphicsNode svgGN = ctx.getGraphicsNode(e); if (svgGN == null) return false; // not in tree? (svgRect.getX(), svgRect.getY(), svgRect.getWidth(), svgRect.getHeight()); AffineTransform ati = svgGN.getGlobalTransform(); ati = ati.createInverse(); } catch (NoninvertibleTransformException e) { } GraphicsNode gn = ctx.getGraphicsNode(element); if (gn != null) gnBounds = gn.getSensitiveBounds(); AffineTransform at = gn.getGlobalTransform(); at.preConcatenate(ati); gnBounds = at.createTransformedShape(gnBounds).getBounds2D(); if (!rect.intersects(gnBounds)) return false; sensitive = at.createTransformedShape(sensitive); if (sensitive.intersects(rect)) return true;