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 return (OMSVGElement)convert(((SVGTextElement)ot).getNearestViewportElement());
69 }
70 /**
71 * The farthest ancestor <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
72 * title='svg element specification'>svg</a> element. Null if the current
73 * element is the outermost <a href='http://www.w3.org/TR/SVG11/struct.html#SVGElement'
74 * title='svg element specification'>svg</a> element.
75 */
76 public final OMSVGElement getFarthestViewportElement() {
77 return (OMSVGElement)convert(((SVGTextElement)ot).getFarthestViewportElement());
78 }
79 /**
80 * Returns the tight bounding box in current user space (i.e., after application
81 * of the <code>transform</code> attribute, if any) on the geometry of all
82 * contained graphics elements, exclusive of stroking, clipping, masking and
83 * filter effects). Note that getBBox must return the actual bounding box
84 * at the time the method was called, even in case the element has not yet
85 * been rendered.
86 * @return An {@link org.vectomatic.dom.svg.OMSVGRect} object that defines
87 * the bounding box.
88 */
89 public final OMSVGRect getBBox() {
90 return ((SVGTextElement)ot).getBBox();
91 }
92 /**
93 * Returns the transformation matrix from current user units (i.e., after
94 * application of the <code>transform</code> attribute, if any) to the viewport
95 * coordinate system for the {@link org.vectomatic.dom.svg.itf.ISVGLocatable#getNearestViewportElement()}.
96 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
97 * the CTM.
98 */
99 public final OMSVGMatrix getCTM() {
100 return ((SVGTextElement)ot).getCTM();
101 }
102 /**
103 * Returns the transformation matrix from current user units (i.e., after
104 * application of the <code>transform</code> attribute, if any) to the parent
105 * user agent's notice of a "pixel". For display devices, ideally this represents
106 * a physical screen pixel. For other devices or environments where physical
107 * pixel sizes are not known, then an algorithm similar to the CSS2 definition
108 * of a "pixel" can be used instead. Note that null is returned if this element
109 * is not hooked into the document tree. This method would have been more
110 * aptly named as <code>getClientCTM</code>, but the name <code>getScreenCTM</code>
111 * is kept for historical reasons.
112 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
113 * the given transformation matrix.
114 */
115 public final OMSVGMatrix getScreenCTM() {
116 return ((SVGTextElement)ot).getScreenCTM();
117 }
118 /**
119 * Returns the transformation matrix from the user coordinate system on the
120 * current element (after application of the <code>transform</code> attribute,
121 * if any) to the user coordinate system on parameter <var>element</var> (after
122 * application of its <code>transform</code> attribute, if any).
123 * @param element The target element.
124 * @return An {@link org.vectomatic.dom.svg.OMSVGMatrix} object that defines
125 * the transformation.
126 * @throws SVGException(SVG_MATRIX_NOT_INVERTABLE) Raised if the currently
127 * defined transformation matrices make it impossible to compute the given
128 * matrix (e.g., because one of the transformations is singular).
129 */
130 public final OMSVGMatrix getTransformToElement(OMSVGElement element) throws JavaScriptException {
131 return ((SVGTextElement)ot).getTransformToElement(((SVGElement)element.ot));
132 }
133
134 // Implementation of the svg::SVGTransformable W3C IDL interface
135 /**
136 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTransformable#getTransform()}
137 * on the given element.
138 */
139 public final OMSVGAnimatedTransformList getTransform() {
140 return ((SVGTextElement)ot).getTransform();
141 }
142
143 // Helper methods
144 /**
145 * Constructor.
146 * @param x the X coordinate of the text in specified units
147 * @param y the Y coordinate of the text in specified units
148 * @param unitType the unit type used to specify the text coordinates
149 * @param data the string represented by the text object.
150 */
151 public OMSVGTextElement(float x, float y, short unitType, String data) {
152 this();
153 OMSVGSVGElement svg = new OMSVGSVGElement();
154 OMSVGLength xCoord = svg.createSVGLength(unitType, x);
155 getX().getBaseVal().appendItem(xCoord);
156 OMSVGLength yCoord = svg.createSVGLength(unitType, y);
157 getY().getBaseVal().appendItem(yCoord);
158 appendChild(new OMText(data));
159 }
160 }