Example #1
0
  /**
   * Converts for the specified element, its fill paint properties to a Paint object.
   *
   * @param filledElement the element interested in a Paint
   * @param filledNode the graphics node to fill
   * @param ctx the bridge context
   */
  public static Paint convertFillPaint(
      Element filledElement, GraphicsNode filledNode, BridgeContext ctx) {
    Value v = CSSUtilities.getComputedStyle(filledElement, SVGCSSEngine.FILL_OPACITY_INDEX);
    float opacity = convertOpacity(v);
    v = CSSUtilities.getComputedStyle(filledElement, SVGCSSEngine.FILL_INDEX);

    return convertPaint(filledElement, filledNode, v, opacity, ctx);
  }
Example #2
0
  /**
   * Converts for the specified element, its stroke paint properties to a Paint object.
   *
   * @param strokedElement the element interested in a Paint
   * @param strokedNode the graphics node to stroke
   * @param ctx the bridge context
   */
  public static Paint convertStrokePaint(
      Element strokedElement, GraphicsNode strokedNode, BridgeContext ctx) {
    Value v = CSSUtilities.getComputedStyle(strokedElement, SVGCSSEngine.STROKE_OPACITY_INDEX);
    float opacity = convertOpacity(v);
    v = CSSUtilities.getComputedStyle(strokedElement, SVGCSSEngine.STROKE_INDEX);

    return convertPaint(strokedElement, strokedNode, v, opacity, ctx);
  }
 /**
  * Handles the 'color-interpolation-filters' CSS property.
  *
  * @param filter the filter
  * @param filterElement the filter element
  */
 protected static void handleColorInterpolationFilters(Filter filter, Element filterElement) {
   if (filter instanceof FilterColorInterpolation) {
     boolean isLinear = CSSUtilities.convertColorInterpolationFilters(filterElement);
     // System.out.println("IsLinear: " + isLinear +
     //                    " Filter: " + filter);
     ((FilterColorInterpolation) filter).setColorSpaceLinear(isLinear);
   }
 }
Example #4
0
  /**
   * Returns a <code>ShapePainter</code> defined on the specified element and for the specified
   * shape node.
   *
   * @param e the element with the marker CSS properties
   * @param node the shape node
   * @param ctx the bridge context
   */
  public static ShapePainter convertMarkers(Element e, ShapeNode node, BridgeContext ctx) {
    Value v;
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.MARKER_START_INDEX);
    Marker startMarker = convertMarker(e, v, ctx);
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.MARKER_MID_INDEX);
    Marker midMarker = convertMarker(e, v, ctx);
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.MARKER_END_INDEX);
    Marker endMarker = convertMarker(e, v, ctx);

    if (startMarker != null || midMarker != null || endMarker != null) {
      MarkerShapePainter p = new MarkerShapePainter(node.getShape());
      p.setStartMarker(startMarker);
      p.setMiddleMarker(midMarker);
      p.setEndMarker(endMarker);
      return p;
    } else {
      return null;
    }
  }
Example #5
0
  /**
   * Converts a <code>Stroke</code> object defined on the specified element.
   *
   * @param e the element on which the stroke is specified
   */
  public static Stroke convertStroke(Element e) {
    Value v;
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.STROKE_WIDTH_INDEX);
    float width = v.getFloatValue();
    if (width == 0.0f) return null; // Stop here no stroke should be painted.

    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.STROKE_LINECAP_INDEX);
    int linecap = convertStrokeLinecap(v);
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.STROKE_LINEJOIN_INDEX);
    int linejoin = convertStrokeLinejoin(v);
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.STROKE_MITERLIMIT_INDEX);
    float miterlimit = convertStrokeMiterlimit(v);
    v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.STROKE_DASHARRAY_INDEX);
    float[] dasharray = convertStrokeDasharray(v);

    float dashoffset = 0;
    if (dasharray != null) {
      v = CSSUtilities.getComputedStyle(e, SVGCSSEngine.STROKE_DASHOFFSET_INDEX);
      dashoffset = v.getFloatValue();

      // make the dashoffset positive since BasicStroke cannot handle
      // negative values
      if (dashoffset < 0) {
        float dashpatternlength = 0;
        for (int i = 0; i < dasharray.length; i++) {
          dashpatternlength += dasharray[i];
        }
        // if the dash pattern consists of an odd number of elements,
        // the pattern length must be doubled
        if ((dasharray.length % 2) != 0) dashpatternlength *= 2;

        if (dashpatternlength == 0) {
          dashoffset = 0;
        } else {
          while (dashoffset < 0) dashoffset += dashpatternlength;
        }
      }
    }
    return new BasicStroke(width, linecap, linejoin, miterlimit, dasharray, dashoffset);
  }
Example #6
0
  /** Tries to build a GVTFontFamily from a URL reference */
  protected GVTFontFamily getFontFamily(BridgeContext ctx, ParsedURL purl) {
    String purlStr = purl.toString();

    Element e = getBaseElement(ctx);
    SVGDocument svgDoc = (SVGDocument) e.getOwnerDocument();
    String docURL = svgDoc.getURL();
    ParsedURL pDocURL = null;
    if (docURL != null) pDocURL = new ParsedURL(docURL);

    // try to load an SVG document
    String baseURI = AbstractNode.getBaseURI(e);
    purl = new ParsedURL(baseURI, purlStr);
    UserAgent userAgent = ctx.getUserAgent();

    try {
      userAgent.checkLoadExternalResource(purl, pDocURL);
    } catch (SecurityException ex) {
      // Can't load font - Security violation.
      // We should not throw the error that is for certain, just
      // move down the font list, but do we display the error or not???
      // I'll vote yes just because it is a security exception (other
      // exceptions like font not available etc I would skip).
      userAgent.displayError(ex);
      return null;
    }

    if (purl.getRef() != null) {
      // Reference must be to a SVGFont.
      Element ref = ctx.getReferencedElement(e, purlStr);
      if (!ref.getNamespaceURI().equals(SVG_NAMESPACE_URI)
          || !ref.getLocalName().equals(SVG_FONT_TAG)) {
        return null;
      }

      SVGDocument doc = (SVGDocument) e.getOwnerDocument();
      SVGDocument rdoc = (SVGDocument) ref.getOwnerDocument();

      Element fontElt = ref;
      if (doc != rdoc) {
        fontElt = (Element) doc.importNode(ref, true);
        String base = AbstractNode.getBaseURI(ref);
        Element g = doc.createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG);
        g.appendChild(fontElt);
        g.setAttributeNS(XMLConstants.XML_NAMESPACE_URI, "xml:base", base);
        CSSUtilities.computeStyleAndURIs(ref, fontElt, purlStr);
      }

      // Search for a font-face element
      Element fontFaceElt = null;
      for (Node n = fontElt.getFirstChild(); n != null; n = n.getNextSibling()) {
        if ((n.getNodeType() == Node.ELEMENT_NODE)
            && n.getNamespaceURI().equals(SVG_NAMESPACE_URI)
            && n.getLocalName().equals(SVG_FONT_FACE_TAG)) {
          fontFaceElt = (Element) n;
          break;
        }
      }
      // todo : if the above loop fails to find a fontFaceElt, a null is passed to createFontFace()

      SVGFontFaceElementBridge fontFaceBridge;
      fontFaceBridge =
          (SVGFontFaceElementBridge) ctx.getBridge(SVG_NAMESPACE_URI, SVG_FONT_FACE_TAG);
      GVTFontFace gff = fontFaceBridge.createFontFace(ctx, fontFaceElt);

      return new SVGFontFamily(gff, fontElt, ctx);
    }
    // Must be a reference to a 'Web Font'.
    try {
      return ctx.getFontFamilyResolver().loadFont(purl.openStream(), this);
    } catch (Exception ex) {
    }
    return null;
  }