/**
   * Builds using the specified BridgeContext and element, the specified graphics node.
   *
   * @param ctx the bridge context to use
   * @param e the element that describes the graphics node to build
   * @param node the graphics node to build
   */
  public void buildGraphicsNode(BridgeContext ctx, Element e, GraphicsNode node) {
    CompositeGraphicsNode cgn = (CompositeGraphicsNode) node;

    // build flowRegion shapes
    boolean isStatic = !ctx.isDynamic();
    if (isStatic) {
      flowRegionNodes = new HashMap();
    } else {
      regionChangeListener = new RegionChangeListener();
    }
    CompositeGraphicsNode cgn2 = (CompositeGraphicsNode) cgn.get(0);
    GVTBuilder builder = ctx.getGVTBuilder();
    for (Node n = getFirstChild(e); n != null; n = getNextSibling(n)) {
      if (n instanceof SVGOMFlowRegionElement) {
        for (Node m = getFirstChild(n); m != null; m = getNextSibling(m)) {
          if (m.getNodeType() != Node.ELEMENT_NODE) {
            continue;
          }
          GraphicsNode gn = builder.build(ctx, (Element) m);
          if (gn != null) {
            cgn2.add(gn);
            if (isStatic) {
              flowRegionNodes.put(m, gn);
            }
          }
        }

        if (!isStatic) {
          AbstractNode an = (AbstractNode) n;
          XBLEventSupport es = (XBLEventSupport) an.initializeEventSupport();
          es.addImplementationEventListenerNS(
              SVG_NAMESPACE_URI, "shapechange", regionChangeListener, false);
        }
      }
    }

    // build text node
    GraphicsNode tn = (GraphicsNode) cgn.get(1);
    super.buildGraphicsNode(ctx, e, tn);

    // Drop references once static build is completed.
    flowRegionNodes = null;
  }
Ejemplo n.º 2
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;
  }