1 /**********************************************
2 * Copyright (C) 2010-2011 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29
30 package org.vectomatic.dom.svg;
31
32 import org.vectomatic.dom.svg.impl.SVGElement;
33 import org.vectomatic.dom.svg.impl.SVGTextElement;
34 import org.vectomatic.dom.svg.itf.ISVGGraphicsElement;
35 import org.vectomatic.dom.svg.itf.ISVGTransformable;
36 import org.vectomatic.dom.svg.utils.DOMHelper;
37 import org.vectomatic.dom.svg.utils.SVGConstants;
38
39 import com.google.gwt.core.client.JavaScriptException;
40 import com.google.gwt.dom.client.TagName;
41
42 /**
43 * The {@link org.vectomatic.dom.svg.OMSVGTextElement} interface corresponds
44 * to the <a href='http://www.w3.org/TR/SVG11/text.html#TextElement' title='text
45 * element specification'>text</a> element.
46 */
47 @TagName("text")
48 public class OMSVGTextElement extends OMSVGTextPositioningElement implements ISVGTransformable, ISVGGraphicsElement {
49 public OMSVGTextElement() {
50 this((SVGTextElement)DOMHelper.createElementNS(DOMHelper.getCurrentDocument(), SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_TEXT_TAG).cast());
51 }
52
53 protected OMSVGTextElement(SVGTextElement ot) {
54 super(ot);
55 }
56
57 // Implementation of the svg::SVGTextElement W3C IDL interface
58
59 // Implementation of the svg::SVGLocatable W3C IDL interface
60 /**
61 * The element which established the current viewport. Often, the nearest
62 * ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement' title='svg
63 * element specification'>svg</a> element. Null if the current element is
64 * the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
65 * title='svg element specification'>svg</a> element.
66 */
67 public final OMSVGElement getNearestViewportElement() {
68 SVGElement elt = ((SVGTextElement)ot).getNearestViewportElement();
69 return elt != null ? (OMSVGElement)convert(elt) : null;
70 }
71 /**
72 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
73 * title='svg element specification'>svg</a> element. Null if the current
74 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
75 * title='svg element specification'>svg</a> element.
76 */
77 public final OMSVGElement getFarthestViewportElement() {
78 SVGElement elt = ((SVGTextElement)ot).getFarthestViewportElement();
79 return elt != null ? (OMSVGElement)convert(elt) : null;
80 }
81 /**
82 * Returns the tight bounding box in current user space (i.e., after application
83 * of the <code>transform</code> attribute, if any) on the geometry of all
84 * contained graphics elements, exclusive of stroking, clipping, masking and
85 * filter effects). Note that getBBox must return the actual bounding box
86 * at the time the method was called, even in case the element has not yet
87 * been rendered.
88 * @return An {@link org.vectomatic.dom.svg.OMSVGRect} object that defines
89 * the bounding box.
90 */
91 public final OMSVGRect getBBox() {
92 return ((SVGTextElement)ot).getBBox();
93 }
94 /**
95 * Returns the transformation matrix from current user units (i.e., after
96 * application of the <code>transform</code> attribute, if any) to the viewport
97 * coordinate system for the {@link org.vectomatic.dom.svg.itf.ISVGLocatable#getNearestViewportElement()}.
98 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
99 * 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 }