001/********************************************** 002 * Copyright (C) 2010-2011 Lukas Laag 003 * This file is part of lib-gwt-svg. 004 * 005 * libgwtsvg is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * libgwtsvg is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/ 017 **********************************************/ 018/* 019 * Copyright (c) 2004 World Wide Web Consortium, 020 * 021 * (Massachusetts Institute of Technology, European Research Consortium for 022 * Informatics and Mathematics, Keio University). All Rights Reserved. This 023 * work is distributed under the W3C(r) Software License [1] in the hope that 024 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 025 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 026 * 027 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 028 */ 029 030package org.vectomatic.dom.svg; 031 032import org.vectomatic.dom.svg.impl.SVGElement; 033import org.vectomatic.dom.svg.impl.SVGTextElement; 034import org.vectomatic.dom.svg.itf.ISVGGraphicsElement; 035import org.vectomatic.dom.svg.itf.ISVGTransformable; 036import org.vectomatic.dom.svg.utils.DOMHelper; 037import org.vectomatic.dom.svg.utils.SVGConstants; 038 039import com.google.gwt.core.client.JavaScriptException; 040import com.google.gwt.dom.client.TagName; 041 042/** 043 * The {@link org.vectomatic.dom.svg.OMSVGTextElement} interface corresponds 044 * to the <a href='http://www.w3.org/TR/SVG11/text.html#TextElement' title='text 045 * element specification'>text</a> element. 046 */ 047@TagName("text") 048public class OMSVGTextElement extends OMSVGTextPositioningElement implements ISVGTransformable, ISVGGraphicsElement { 049 public OMSVGTextElement() { 050 this((SVGTextElement)DOMHelper.createElementNS(DOMHelper.getCurrentDocument(), SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_TEXT_TAG).cast()); 051 } 052 053 protected OMSVGTextElement(SVGTextElement ot) { 054 super(ot); 055 } 056 057 // Implementation of the svg::SVGTextElement W3C IDL interface 058 059 // Implementation of the svg::SVGLocatable W3C IDL interface 060 /** 061 * The element which established the current viewport. Often, the nearest 062 * ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' title='svg 063 * element specification'>svg</a> element. Null if the current element is 064 * the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 065 * title='svg element specification'>svg</a> element. 066 */ 067 public final OMSVGElement getNearestViewportElement() { 068 SVGElement elt = ((SVGTextElement)ot).getNearestViewportElement(); 069 return elt != null ? (OMSVGElement)convert(elt) : null; 070 } 071 /** 072 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 073 * title='svg element specification'>svg</a> element. Null if the current 074 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' 075 * title='svg element specification'>svg</a> element. 076 */ 077 public final OMSVGElement getFarthestViewportElement() { 078 SVGElement elt = ((SVGTextElement)ot).getFarthestViewportElement(); 079 return elt != null ? (OMSVGElement)convert(elt) : null; 080 } 081 /** 082 * Returns the tight bounding box in current user space (i.e., after application 083 * of the <code>transform</code> attribute, if any) on the geometry of all 084 * contained graphics elements, exclusive of stroking, clipping, masking and 085 * filter effects). Note that getBBox must return the actual bounding box 086 * at the time the method was called, even in case the element has not yet 087 * been rendered. 088 * @return An {@link org.vectomatic.dom.svg.OMSVGRect} object that defines 089 * the bounding box. 090 */ 091 public final OMSVGRect getBBox() { 092 return ((SVGTextElement)ot).getBBox(); 093 } 094 /** 095 * Returns the transformation matrix from current user units (i.e., after 096 * application of the <code>transform</code> attribute, if any) to the viewport 097 * coordinate system for the {@link org.vectomatic.dom.svg.itf.ISVGLocatable#getNearestViewportElement()}. 098 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 099 * the CTM. 100 */ 101 public final OMSVGMatrix getCTM() { 102 return ((SVGTextElement)ot).getCTM(); 103 } 104 /** 105 * Returns the transformation matrix from current user units (i.e., after 106 * application of the <code>transform</code> attribute, if any) to the parent 107 * user agent's notice of a "pixel". For display devices, ideally this represents 108 * a physical screen pixel. For other devices or environments where physical 109 * pixel sizes are not known, then an algorithm similar to the CSS2 definition 110 * of a "pixel" can be used instead. Note that null is returned if this element 111 * is not hooked into the document tree. This method would have been more 112 * aptly named as <code>getClientCTM</code>, but the name <code>getScreenCTM</code> 113 * is kept for historical reasons. 114 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 115 * the given transformation matrix. 116 */ 117 public final OMSVGMatrix getScreenCTM() { 118 return ((SVGTextElement)ot).getScreenCTM(); 119 } 120 /** 121 * Returns the transformation matrix from the user coordinate system on the 122 * current element (after application of the <code>transform</code> attribute, 123 * if any) to the user coordinate system on parameter <var>element</var> (after 124 * application of its <code>transform</code> attribute, if any). 125 * @param element The target element. 126 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines 127 * the transformation. 128 * @throws SVGException(SVG_MATRIX_NOT_INVERTABLE) Raised if the currently 129 * defined transformation matrices make it impossible to compute the given 130 * matrix (e.g., because one of the transformations is singular). 131 */ 132 public final OMSVGMatrix getTransformToElement(OMSVGElement element) throws JavaScriptException { 133 return ((SVGTextElement)ot).getTransformToElement(((SVGElement)element.ot)); 134 } 135 136 // Implementation of the svg::SVGTransformable W3C IDL interface 137 /** 138 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTransformable#getTransform()} 139 * on the given element. 140 */ 141 public final OMSVGAnimatedTransformList getTransform() { 142 return ((SVGTextElement)ot).getTransform(); 143 } 144 145 // Helper methods 146 /** 147 * Constructor. 148 * @param x the X coordinate of the text in specified units 149 * @param y the Y coordinate of the text in specified units 150 * @param unitType the unit type used to specify the text coordinates 151 * @param data the string represented by the text object. 152 */ 153 public OMSVGTextElement(float x, float y, short unitType, String data) { 154 this(); 155 OMSVGSVGElement svg = new OMSVGSVGElement(); 156 OMSVGLength xCoord = svg.createSVGLength(unitType, x); 157 getX().getBaseVal().appendItem(xCoord); 158 OMSVGLength yCoord = svg.createSVGLength(unitType, y); 159 getY().getBaseVal().appendItem(yCoord); 160 appendChild(new OMText(data)); 161 } 162}