/** * Returns the next GraphicsNode that matches the specified string or null if any. * * @param text the text to match */ protected GraphicsNode getNext(String text) { if (walker == null && gvtRoot != null) { walker = new GVTTreeWalker(gvtRoot); } GraphicsNode gn = walker.getCurrentGraphicsNode(); int index = match(gn, text, currentIndex + text.length()); if (index >= 0) { currentIndex = index; } else { currentIndex = 0; gn = walker.nextGraphicsNode(); while (gn != null && ((currentIndex = match(gn, text, currentIndex)) < 0)) { currentIndex = 0; gn = walker.nextGraphicsNode(); } } return gn; }
public static void main(String[] args) { try { FileInputStream f = new FileInputStream(args[0]); SVGDocument doc = (SVGDocument) svgDocFactory.createDocument(null, f); BridgeContext bc = new BridgeContext(new UserAgentAdapter()); GVTBuilder gvtb = new GVTBuilder(); GraphicsNode gn = gvtb.build(bc, doc); GVTTreeWalker tw = new GVTTreeWalker(gn); gn = tw.nextGraphicsNode(); System.out.print("new Color[] { "); Graphics2D g2 = GraphicsEnvironment.getLocalGraphicsEnvironment() .createGraphics(new BufferedImage(32, 32, BufferedImage.TYPE_3BYTE_BGR)); while (true) { // System.out.println("node " + i); gn = tw.nextGraphicsNode(); if (gn == null) { break; } gn.paint(g2); Color c = g2.getColor(); System.out.print( "new Color(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + "), "); } System.out.println("}"); } catch (IOException e) { e.printStackTrace(); } }
/** Shows the current selected <tt>TextNode</tt>. */ protected void showSelectedGraphicsNode() { GraphicsNode gn = walker.getCurrentGraphicsNode(); if (!(gn instanceof TextNode)) { return; } TextNode textNode = (TextNode) gn; // mark the selection of the substring found String text = textNode.getText(); String pattern = search.getText(); if (!caseSensitive.isSelected()) { text = text.toLowerCase(); pattern = pattern.toLowerCase(); } int end = text.indexOf(pattern, currentIndex); AttributedCharacterIterator aci = textNode.getAttributedCharacterIterator(); aci.first(); for (int i = 0; i < end; ++i) { aci.next(); } Mark startMark = textNode.getMarkerForChar(aci.getIndex(), true); for (int i = 0; i < pattern.length() - 1; ++i) { aci.next(); } Mark endMark = textNode.getMarkerForChar(aci.getIndex(), false); svgCanvas.select(startMark, endMark); // zoom on the TextNode if needed if (highlightButton.isSelected()) { return; } // get the highlight shape in GVT root (global) coordinate sytem Shape s = textNode.getHighlightShape(); AffineTransform at; if (highlightCenterZoomButton.isSelected()) { at = svgCanvas.getInitialTransform(); } else { at = svgCanvas.getRenderingTransform(); } // get the bounds of the highlight shape in the canvas coordinate system Rectangle2D gnb = at.createTransformedShape(s).getBounds(); Dimension canvasSize = svgCanvas.getSize(); // translate the highlight region to (0, 0) in the canvas coordinate // system AffineTransform Tx = AffineTransform.getTranslateInstance( -gnb.getX() - gnb.getWidth() / 2, -gnb.getY() - gnb.getHeight() / 2); if (highlightCenterZoomButton.isSelected()) { // zoom on the highlight shape such as the shape takes x% of the // canvas size double sx = canvasSize.width / gnb.getWidth(); double sy = canvasSize.height / gnb.getHeight(); double scale = Math.min(sx, sy) / 8; if (scale > 1) { Tx.preConcatenate(AffineTransform.getScaleInstance(scale, scale)); } } Tx.preConcatenate( AffineTransform.getTranslateInstance(canvasSize.width / 2, canvasSize.height / 2)); // take into account the initial transform AffineTransform newRT = new AffineTransform(at); newRT.preConcatenate(Tx); // change the rendering transform svgCanvas.setRenderingTransform(newRT); }