Exemplo n.º 1
0
  /**
   * Returns the transformation matrix to apply to initalize a viewport or null if the specified
   * viewBox disables the rendering of the element.
   *
   * @param e the element with a viewbox
   * @param vb the viewBox definition as float
   * @param w the width of the effective viewport
   * @param h The height of the effective viewport
   */
  public static AffineTransform getPreserveAspectRatioTransform(
      Element e, float[] vb, float w, float h) {

    String aspectRatio = e.getAttributeNS(null, SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE);

    // 'preserveAspectRatio' attribute
    PreserveAspectRatioParser p = new PreserveAspectRatioParser();
    ViewHandler ph = new ViewHandler();
    p.setPreserveAspectRatioHandler(ph);
    try {
      p.parse(aspectRatio);
    } catch (ParseException ex) {
      throw new BridgeException(
          e,
          ERR_ATTRIBUTE_VALUE_MALFORMED,
          new Object[] {SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE, aspectRatio, ex});
    }

    return getPreserveAspectRatioTransform(vb, ph.align, ph.meet, w, h);
  }
Exemplo n.º 2
0
  /**
   * Returns the transformation matrix to apply to initalize a viewport or null if the specified
   * viewBox disables the rendering of the element.
   *
   * @param e the element with a viewbox
   * @param viewBox the viewBox definition
   * @param w the width of the effective viewport
   * @param h The height of the effective viewport
   */
  public static AffineTransform getPreserveAspectRatioTransform(
      Element e, String viewBox, String aspectRatio, float w, float h) {

    // no viewBox specified
    if (viewBox.length() == 0) {
      return new AffineTransform();
    }
    float[] vb = parseViewBoxAttribute(e, viewBox);

    // 'preserveAspectRatio' attribute
    PreserveAspectRatioParser p = new PreserveAspectRatioParser();
    ViewHandler ph = new ViewHandler();
    p.setPreserveAspectRatioHandler(ph);
    try {
      p.parse(aspectRatio);
    } catch (ParseException ex) {
      throw new BridgeException(
          e,
          ERR_ATTRIBUTE_VALUE_MALFORMED,
          new Object[] {SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE, aspectRatio, ex});
    }

    return getPreserveAspectRatioTransform(vb, ph.align, ph.meet, w, h);
  }
Exemplo n.º 3
0
  /**
   * Parses the specified reference (from a URI) and returns the appropriate transform.
   *
   * @param ref the reference of the URI that may specify additional attribute values such as the
   *     viewBox, preserveAspectRatio or a transform
   * @param e the element interested in its view transform
   * @param w the width of the effective viewport
   * @param h The height of the effective viewport
   * @exception BridgeException if an error occured while computing the preserveAspectRatio
   *     transform
   */
  public static AffineTransform getViewTransform(String ref, Element e, float w, float h) {

    // no reference has been specified, no extra viewBox is defined
    if (ref == null || ref.length() == 0) {
      return getPreserveAspectRatioTransform(e, w, h);
    }

    ViewHandler vh = new ViewHandler();
    FragmentIdentifierParser p = new FragmentIdentifierParser();
    p.setFragmentIdentifierHandler(vh);
    p.parse(ref);

    Element attrDefElement = e; // the element that defines the attributes
    if (vh.hasId) {
      Document document = e.getOwnerDocument();
      attrDefElement = document.getElementById(vh.id);
    }
    if (attrDefElement == null) {
      throw new BridgeException(e, ERR_URI_MALFORMED, new Object[] {ref});
    }
    // if the referenced element is not a view, the attribute
    // values to use are those defined on the enclosed svg element
    if (!(attrDefElement.getNamespaceURI().equals(SVG_NAMESPACE_URI)
        && attrDefElement.getLocalName().equals(SVG_VIEW_TAG))) {
      attrDefElement = getClosestAncestorSVGElement(e);
    }

    // 'viewBox'
    float[] vb;
    if (vh.hasViewBox) {
      vb = vh.viewBox;
    } else {
      String viewBoxStr = attrDefElement.getAttributeNS(null, SVG_VIEW_BOX_ATTRIBUTE);
      vb = parseViewBoxAttribute(attrDefElement, viewBoxStr);
    }

    // 'preserveAspectRatio'
    short align;
    boolean meet;
    if (vh.hasPreserveAspectRatio) {
      align = vh.align;
      meet = vh.meet;
    } else {
      String aspectRatio = attrDefElement.getAttributeNS(null, SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE);
      PreserveAspectRatioParser pp = new PreserveAspectRatioParser();
      ViewHandler ph = new ViewHandler();
      pp.setPreserveAspectRatioHandler(ph);
      try {
        pp.parse(aspectRatio);
      } catch (ParseException ex) {
        throw new BridgeException(
            attrDefElement,
            ERR_ATTRIBUTE_VALUE_MALFORMED,
            new Object[] {SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE, aspectRatio, ex});
      }
      align = ph.align;
      meet = ph.meet;
    }

    // the additional transform that may appear on the URI
    AffineTransform transform = getPreserveAspectRatioTransform(vb, align, meet, w, h);
    if (vh.hasTransform) {
      transform.concatenate(vh.getAffineTransform());
    }
    return transform;
  }