Package org.apache.batik.gvt

Examples of org.apache.batik.gvt.ShapeNode


        return node;
    }

    public void buildGraphicsNode(GraphicsNode gn, BridgeContext ctx,
                                  Element element) {
        ShapeNode node = (ShapeNode)gn;

        SVGElement svgElement = (SVGElement) element;
        CSSStyleDeclaration cssDecl
            = ctx.getViewCSS().getComputedStyle(element, null);
        UnitProcessor.Context uctx
            = new DefaultUnitProcessorContext(ctx,
                                              cssDecl);

        // Initialize the style properties
        ShapePainter painter
            = CSSUtilities.convertStrokeAndFill(svgElement, node,
                                                ctx, cssDecl, uctx);
        node.setShapePainter(painter);

        // Set node composite
        CSSPrimitiveValue opacityVal =
            (CSSPrimitiveValue)cssDecl.getPropertyCSSValue(ATTR_OPACITY);
        Composite composite =
            CSSUtilities.convertOpacityToComposite(opacityVal);
        node.setComposite(composite);

        // Set node filter
        Filter filter = CSSUtilities.convertFilter(element, node, ctx);
        node.setFilter(filter);

        // Set the node mask
        Mask mask = CSSUtilities.convertMask(element, node, ctx);
        node.setMask(mask);

        // Set the node clip
        Clip clip = CSSUtilities.convertClipPath(element, node, ctx);
        node.setClip(clip);

        // <!> TODO only when binding is enabled
        BridgeEventSupport.addDOMListener(ctx, element);
        ctx.bind(element, node);
    }
View Full Code Here


            GeneralPath path = new GeneralPath(clipNode.getOutline());
            path.setWindingRule(wr);
            Shape outline = Tx.createTransformedShape(path);

            // apply the clip-path of the current Element
            ShapeNode outlineNode = gvtFactory.createShapeNode();
            outlineNode.setShape(outline);
            Clip clip = CSSUtilities.convertClipPath(child,
                                                     outlineNode,
                                                     ctx);
            if (clip != null) {
                Area area = new Area(outline);
                area.subtract(new Area(clip.getClipPath()));
                outline = area;
            }
            clipPath.add(new Area(outline));
        }
        // restore the viewport
        ctx.setCurrentViewport(oldViewport);
        if (!hasChildren) {
            return null; // no clipPath defined
        }

        // apply the clip-path of this clipPath Element (already in user space)
        ShapeNode clipPathNode = gvtFactory.createShapeNode();
        clipPathNode.setShape(clipPath);
        Clip clipElementClipPath =
            CSSUtilities.convertClipPath(clipElement, clipPathNode, ctx);
        if (clipElementClipPath != null) {
            clipPath.subtract(new Area(clipElementClipPath.getClipPath()));
        }
View Full Code Here

            = ctx.getViewCSS().getComputedStyle(element, null);
        UnitProcessor.Context uctx
            = new DefaultUnitProcessorContext(ctx,
                                              cssDecl);

        ShapeNode node = ctx.getGVTFactory().createShapeNode();

        // Transform
        AffineTransform at =
            SVGUtilities.convertAffineTransform(element,
                                                ATTR_TRANSFORM,
                                                ctx.getParserFactory());
        node.setTransform(at);

        // <!> TODO only when binding is enabled
        //BridgeEventSupport.addDOMListener(ctx, element);
        //ctx.bind(element, node);

        // Location
        String s = element.getAttributeNS(null, ATTR_START_OFFSET);
        // is this really a HORIZONTAL LENGTH?
        float startOffset = UnitProcessor.svgToUserSpace(s,
                                                         (SVGElement)element,
                                                         UnitProcessor.HORIZONTAL_LENGTH,
                                                         uctx);

        //System.out.println("--- startOffset = " + startOffset);

        // TextLength
        s = element.getAttributeNS(null, ATTR_TEXT_LENGTH);
        // is this really a HORIZONTAL LENGTH?
        boolean lengthSpecified = true;
        float textLength = UnitProcessor.svgToUserSpace(s,
                                                        (SVGElement)element,
                                                        UnitProcessor.HORIZONTAL_LENGTH,
                                                        uctx);

        // FIXME : do this a better way
        if (textLength == 0f) {
            lengthSpecified = false;
        }


        //System.out.println("--- textLength = " + textLength);

        s = element.getAttributeNS(null, ATTR_METHOD);
        int method = TextPathLayout.ADJUST_SPACING;
        if (s.equals(VALUE_ALIGN)) {
            method = TextPathLayout.ADJUST_SPACING;
        } else if (s.equals(VALUE_STRETCH)) {
            method = TextPathLayout.ADJUST_GLYPHS;
        }

        //System.out.println("--- method = " + method);


        // !!! TODO better text handling
        element.normalize();
        String text = element.getFirstChild().getNodeValue();
        String sp = XMLSupport.getXMLSpace(element);
        if (sp.equals("preserve")) {
            text = XMLSupport.preserveXMLSpace(text);
        } else {
            text = XMLSupport.defaultXMLSpace(text);
        }
        text = (text.length() == 0) ? " " : text;


        Map attrs = new HashMap();

        // Font size
        float fs = CSSUtilities.convertFontSize((SVGElement)element,
                                                ctx,
                                                cssDecl,
                                                uctx);
        fs *= 0.92; // Font size correction

        attrs.put(TextAttribute.SIZE, new Float(fs));

        // Text-anchor
        CSSPrimitiveValue v = (CSSPrimitiveValue)cssDecl.getPropertyCSSValue
            (CSS_TEXT_ANCHOR_PROPERTY);
        s = v.getStringValue();
        int align;
        switch (s.charAt(0)) {
        case 's':
            align = TextPathLayout.ALIGN_START;
            break;
        case 'm':
            align = TextPathLayout.ALIGN_MIDDLE;
            break;
        default:
            align = TextPathLayout.ALIGN_END;
        }

        //System.out.println("--- align = " + align);

        // Font family
        CSSValueList ff = (CSSValueList)cssDecl.getPropertyCSSValue
            (CSS_FONT_FAMILY_PROPERTY);
        s = null;
        for (int i = 0; s == null && i < ff.getLength(); i++) {
            v = (CSSPrimitiveValue)ff.item(i);
            s = (String)fonts.get(v.getStringValue());
        }
        s = (s == null) ? "SansSerif" : s;
        attrs.put(TextAttribute.FAMILY, s);

        // Font weight
        v = (CSSPrimitiveValue)cssDecl.getPropertyCSSValue
            (CSS_FONT_WEIGHT_PROPERTY);
        if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
            if (v.getStringValue().charAt(0) == 'n') {
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_REGULAR);
            } else {
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_BOLD);
            }
        } else {
            switch ((int)v.getFloatValue(CSSPrimitiveValue.CSS_NUMBER)) {
            case 100:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_EXTRA_LIGHT);
                break;
            case 200:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_LIGHT);
                break;
            case 300:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_DEMILIGHT);
                break;
            case 400:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_REGULAR);
                break;
            case 500:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_SEMIBOLD);
                break;
            case 600:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_DEMIBOLD);
                break;
            case 700:
                attrs.put(TextAttribute.WEIGHT,
                                TextAttribute.WEIGHT_BOLD);
                break;
            case 800:
                attrs.put(TextAttribute.WEIGHT,
                                //TextAttribute.WEIGHT_EXTRABOLD);
                                TextAttribute.WEIGHT_BOLD);
                break;
            case 900:
                attrs.put(TextAttribute.WEIGHT,
                                //TextAttribute.WEIGHT_ULTRABOLD);
                                TextAttribute.WEIGHT_BOLD);
            }
        }

        // Font style
        v = (CSSPrimitiveValue)cssDecl.getPropertyCSSValue
            (CSS_FONT_STYLE_PROPERTY);
        s = v.getStringValue();
        switch (s.charAt(0)) {
        case 'n':
            attrs.put(TextAttribute.POSTURE,
                            TextAttribute.POSTURE_REGULAR);
            break;
        case 'o':
        case 'i':
            attrs.put(TextAttribute.POSTURE,
                            TextAttribute.POSTURE_OBLIQUE);
        }

        // Font stretch
        v = (CSSPrimitiveValue)cssDecl.getPropertyCSSValue
            (CSS_FONT_STRETCH_PROPERTY);
        s = v.getStringValue();
        switch (s.charAt(0)) {
        case 'u':
            if (s.charAt(6) == 'c') {
                attrs.put(TextAttribute.WIDTH,
                                TextAttribute.WIDTH_CONDENSED);
            } else {
                attrs.put(TextAttribute.WIDTH,
                                TextAttribute.WIDTH_EXTENDED);
            }
            break;
        case 'e':
            if (s.charAt(6) == 'c') {
                attrs.put(TextAttribute.WIDTH,
                                TextAttribute.WIDTH_CONDENSED);
            } else {
                if (s.length() == 8) {
                    attrs.put(TextAttribute.WIDTH,
                                    TextAttribute.WIDTH_SEMI_EXTENDED);
                } else {
                    attrs.put(TextAttribute.WIDTH,
                                    TextAttribute.WIDTH_EXTENDED);
                }
            }
            break;
        case 's':
            if (s.charAt(6) == 'c') {
                attrs.put(TextAttribute.WIDTH,
                                TextAttribute.WIDTH_SEMI_CONDENSED);
            } else {
                attrs.put(TextAttribute.WIDTH,
                                TextAttribute.WIDTH_SEMI_EXTENDED);
            }
            break;
        default:
            attrs.put(TextAttribute.WIDTH,
                            TextAttribute.WIDTH_REGULAR);
        }

        // Text decoration
        CSSValue cssVal = cssDecl.getPropertyCSSValue
            (CSS_TEXT_DECORATION_PROPERTY);
        short t = cssVal.getCssValueType();
        if (t == CSSValue.CSS_VALUE_LIST) {
            CSSValueList lst = (CSSValueList)cssVal;
            for (int i = 0; i < lst.getLength(); i++) {
                v = (CSSPrimitiveValue)lst.item(i);
                s = v.getStringValue();
                switch (s.charAt(0)) {
                case 'u':
                    attrs.put(TextAttribute.UNDERLINE,
                                    TextAttribute.UNDERLINE_ON);
                    break;
                case 'o':
                    // overline
                    break;
                case 'l':
                    attrs.put(TextAttribute.STRIKETHROUGH,
                                    TextAttribute.STRIKETHROUGH_ON);
                }
            }
        }


        Font font = new Font(attrs);

        // get the path

        URIResolver ur;
        ur = new URIResolver((SVGDocument)element.getOwnerDocument(),
                             ctx.getDocumentLoader());
        String uriString = XLinkSupport.getXLinkHref(element);
        Shape path = null;

        try {
            Node n = ur.getNode(uriString);
            if (n.getOwnerDocument() == null) {
                throw new Error("Can't use documents");
            }
            Element elt = (Element)n;
            boolean local =
                n.getOwnerDocument() == element.getOwnerDocument();

            Element pathElement = null;
            if (local) {
                pathElement = (Element)elt.cloneNode(true);
            } else {
                pathElement = (Element)element.getOwnerDocument().importNode(elt, true);

            }

            if (pathElement != null) {

                String d = pathElement.getAttributeNS(null, ATTR_D);
                try {

                    // add the transform for the path as well

                    path = AWTPathProducer.createShape(new StringReader(d),
                                                       PathIterator.WIND_NON_ZERO,
                                                       ctx.getParserFactory());
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage());
                }

            }
        } catch (Exception ex) {
            System.out.println("bad url " + uriString);
            ex.printStackTrace();
        }

        GlyphVector vector = font.createGlyphVector(new FontRenderContext(null, true, true),
                                                    new String(text));

        //System.out.println("num glyphs in vector == " + vector.getNumGlyphs());

        Shape shape = null;

        if (!lengthSpecified) {
            textLength = (float) vector.getVisualBounds().getWidth();
        }

        shape = TextPathLayout.layoutGlyphVector(vector, path, align, startOffset, textLength, method);

        node.setShape(shape);

        return node;
    }
View Full Code Here

        return node;
    }

    public void buildGraphicsNode(GraphicsNode gn, BridgeContext ctx,
                                  Element element) {
        ShapeNode node = (ShapeNode)gn;

        SVGElement svgElement = (SVGElement) element;

        CSSStyleDeclaration cssDecl
            = ctx.getViewCSS().getComputedStyle(element, null);

        UnitProcessor.Context uctx
            = new DefaultUnitProcessorContext(ctx,
                                              cssDecl);

        // Initialize the style properties
        ShapePainter painter
            = CSSUtilities.convertStrokeAndFill(svgElement, node,
                                                ctx, cssDecl, uctx);
        node.setShapePainter(painter);

        // Set node composite
        CSSPrimitiveValue opacityVal =
            (CSSPrimitiveValue)cssDecl.getPropertyCSSValue(ATTR_OPACITY);
        Composite composite =
            CSSUtilities.convertOpacityToComposite(opacityVal);
        node.setComposite(composite);

        // Set node filter
        Filter filter = CSSUtilities.convertFilter(element, node, ctx);
        node.setFilter(filter);

        // Set the node mask
        Mask mask = CSSUtilities.convertMask(element, node, ctx);
        node.setMask(mask);

        // Set the node clip
        Clip clip = CSSUtilities.convertClipPath(element, node, ctx);
        node.setClip(clip);

    }
View Full Code Here

     * @param ctx the bridge context to use
     * @param e the element that describes the graphics node to build
     * @return a graphics node that represents the specified element
     */
    public GraphicsNode createGraphicsNode(BridgeContext ctx, Element e) {
        ShapeNode shapeNode = (ShapeNode)super.createGraphicsNode(ctx, e);
        // delegates to subclasses the shape construction
        buildShape(ctx, e, shapeNode);
  if (shapeNode.getShape() == null) {
      return null; // Disable the rendering if something bad happens
  }
        // 'shape-rendering' and 'color-rendering'
        Map shapeHints = CSSUtilities.convertShapeRendering(e);
        Map colorHints = CSSUtilities.convertColorRendering(e);
        if (shapeHints != null || colorHints != null) {
            RenderingHints hints;
            if (shapeHints == null) {
                hints = new RenderingHints(colorHints);
            } else if (colorHints == null) {
                hints = new RenderingHints(shapeHints);
            } else {
                hints = new RenderingHints(shapeHints);
                hints.putAll(colorHints);
            }
            shapeNode.setRenderingHints(hints);
        }
        return shapeNode;
    }
View Full Code Here

    /**
     * Creates a <tt>ShapeNode</tt>.
     */
    protected GraphicsNode instantiateGraphicsNode() {
        return new ShapeNode();
    }
View Full Code Here

     * @param node the graphics node to build
     */
    public void buildGraphicsNode(BridgeContext ctx,
                                  Element e,
                                  GraphicsNode node) {
        ShapeNode shapeNode = (ShapeNode)node;
        shapeNode.setShapePainter(createShapePainter(ctx, e, shapeNode));
        super.buildGraphicsNode(ctx, e, node);
    }
View Full Code Here

            GeneralPath path = new GeneralPath(clipNode.getOutline(rc));
            path.setWindingRule(wr);
            Shape outline = Tx.createTransformedShape(path);

            // apply the 'clip-path' of the current clipPath's child
            ShapeNode outlineNode = new ShapeNode();
            outlineNode.setShape(outline);
            ClipRable clip = CSSUtilities.convertClipPath(child,
                                                          outlineNode,
                                                          ctx);
            if (clip != null) {
                Area area = new Area(outline);
                area.subtract(new Area(clip.getClipPath()));
                outline = area;
            }
            clipPath.add(new Area(outline));
        }
        if (!hasChildren) {
            return null; // empty clipPath
        }

        // construct the shape node that represents the clipPath
        ShapeNode clipPathNode = new ShapeNode();
        clipPathNode.setShape(clipPath);

        // apply the 'clip-path' of the clipPath element (already in user space)
        ClipRable clipElementClipPath =
            CSSUtilities.convertClipPath(clipElement, clipPathNode, ctx);
        if (clipElementClipPath != null) {
View Full Code Here

            GeneralPath path = new GeneralPath(clipNode.getOutline());
            path.setWindingRule(wr);
            Shape outline = Tx.createTransformedShape(path);

            // apply the 'clip-path' of the current clipPath's child
            ShapeNode outlineNode = new ShapeNode();
            outlineNode.setShape(outline);
            ClipRable clip = CSSUtilities.convertClipPath(child,
                                                          outlineNode,
                                                          ctx);
            if (clip != null) {
                Area area = new Area(outline);
                area.subtract(new Area(clip.getClipPath()));
                outline = area;
            }
            clipPath.add(new Area(outline));
        }
        if (!hasChildren) {
            return null; // empty clipPath
        }

        // construct the shape node that represents the clipPath
        ShapeNode clipPathNode = new ShapeNode();
        clipPathNode.setShape(clipPath);

        // apply the 'clip-path' of the clipPath element (already in user space)
        ClipRable clipElementClipPath =
            CSSUtilities.convertClipPath(clipElement, clipPathNode, ctx);
        if (clipElementClipPath != null) {
View Full Code Here

            = AffineTransform.getScaleInstance(scale, -scale);

        // create a shape node that represents the d attribute
        String d = glyphElement.getAttributeNS(null, SVG_D_ATTRIBUTE);
        if (d.length() != 0) {
            ShapeNode shapeNode = new ShapeNode();
            AWTPathProducer app = new AWTPathProducer();
            app.setWindingRule(CSSUtilities.convertFillRule(glyphElement));
            try {
                PathParser pathParser = new PathParser();
                pathParser.setPathHandler(app);
                pathParser.parse(new StringReader(d));
            } catch (ParseException ex) {
                throw new BridgeException(glyphElement,
                                          ERR_ATTRIBUTE_VALUE_MALFORMED,
                                          new Object [] {SVG_D_ATTRIBUTE});
            } finally {
                // transform the shape into the correct coord system
                Shape shape = app.getShape();
                Shape transformedShape
                    = scaleTransform.createTransformedShape(shape);

                shapeNode.setShape(transformedShape);

                // set up the painter for the d part of the glyph
                ShapePainter painter = PaintServer.convertFillAndStroke(
                                      textElement, shapeNode, ctx);
                shapeNode.setShapePainter(painter);

                glyphContentNode.add(shapeNode);
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.batik.gvt.ShapeNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.