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.events.ActivateEvent;
33 import org.vectomatic.dom.svg.events.ActivateHandler;
34 import org.vectomatic.dom.svg.events.FocusInEvent;
35 import org.vectomatic.dom.svg.events.FocusInHandler;
36 import org.vectomatic.dom.svg.events.FocusOutEvent;
37 import org.vectomatic.dom.svg.events.FocusOutHandler;
38 import org.vectomatic.dom.svg.events.HasGraphicalHandlers;
39 import org.vectomatic.dom.svg.impl.SVGTextContentElement;
40 import org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired;
41 import org.vectomatic.dom.svg.itf.ISVGLangSpace;
42 import org.vectomatic.dom.svg.itf.ISVGStylable;
43 import org.vectomatic.dom.svg.itf.ISVGTests;
44
45 import com.google.gwt.core.client.JavaScriptException;
46 import com.google.gwt.event.dom.client.ClickEvent;
47 import com.google.gwt.event.dom.client.ClickHandler;
48 import com.google.gwt.event.dom.client.LoadEvent;
49 import com.google.gwt.event.dom.client.LoadHandler;
50 import com.google.gwt.event.dom.client.MouseDownEvent;
51 import com.google.gwt.event.dom.client.MouseDownHandler;
52 import com.google.gwt.event.dom.client.MouseMoveEvent;
53 import com.google.gwt.event.dom.client.MouseMoveHandler;
54 import com.google.gwt.event.dom.client.MouseOutEvent;
55 import com.google.gwt.event.dom.client.MouseOutHandler;
56 import com.google.gwt.event.dom.client.MouseOverEvent;
57 import com.google.gwt.event.dom.client.MouseOverHandler;
58 import com.google.gwt.event.dom.client.MouseUpEvent;
59 import com.google.gwt.event.dom.client.MouseUpHandler;
60 import com.google.gwt.event.dom.client.TouchCancelEvent;
61 import com.google.gwt.event.dom.client.TouchCancelHandler;
62 import com.google.gwt.event.dom.client.TouchEndEvent;
63 import com.google.gwt.event.dom.client.TouchEndHandler;
64 import com.google.gwt.event.dom.client.TouchMoveEvent;
65 import com.google.gwt.event.dom.client.TouchMoveHandler;
66 import com.google.gwt.event.dom.client.TouchStartEvent;
67 import com.google.gwt.event.dom.client.TouchStartHandler;
68 import com.google.gwt.event.shared.HandlerRegistration;
69
70 /**
71 * <p>The {@link org.vectomatic.dom.svg.OMSVGTextContentElement} is inherited
72 * by various text-related interfaces, such as {@link org.vectomatic.dom.svg.OMSVGTextElement},
73 * {@link org.vectomatic.dom.svg.OMSVGTSpanElement}, {@link org.vectomatic.dom.svg.OMSVGTRefElement},
74 * {@link org.vectomatic.dom.svg.OMSVGAltGlyphElement} and {@link org.vectomatic.dom.svg.OMSVGTextPathElement}.</p>
75 * <p>For the methods on this interface that refer to an index to a character
76 * or a number of characters, these references are to be interpreted as an
77 * index to a UTF-16 code unit or a number of UTF-16 code units, respectively.
78 * This is for consistency with DOM Level 2 Core, where methods on the <code>CharacterData</code>
79 * interface use UTF-16 code units as indexes and counts within the character
80 * data. Thus for example, if the text content of a <a href='http://www.w3.org/TR/SVG11/text.html#TextElement'
81 * title='text element specification'>text</a> element is a single non-BMP
82 * character, such as U+10000, then invoking {@link org.vectomatic.dom.svg.OMSVGTextContentElement#getNumberOfChars()}
83 * on that element will return 2 since there are two UTF-16 code units (the
84 * surrogate pair) used to represent that one character.</p>
85 */
86 public abstract class OMSVGTextContentElement extends OMSVGElement implements HasGraphicalHandlers, ISVGTests, ISVGLangSpace, ISVGExternalResourcesRequired, ISVGStylable {
87 /**
88 * The enumeration was set to a value that is not one of predefined types.
89 * It is invalid to attempt to define a new value of this type or to attempt
90 * to switch an existing value to this type.
91 */
92 public static final short LENGTHADJUST_UNKNOWN = 0;
93 /**
94 * Corresponds to value <span class='attr-value'>'spacing'</span>.
95 */
96 public static final short LENGTHADJUST_SPACING = 1;
97 /**
98 * Corresponds to value <span class='attr-value'>'spacingAndGlyphs'</span>.
99 */
100 public static final short LENGTHADJUST_SPACINGANDGLYPHS = 2;
101 protected OMSVGTextContentElement(SVGTextContentElement ot) {
102 super(ot);
103 }
104
105 // Implementation of the svg::SVGTextContentElement W3C IDL interface
106 /**
107 * Corresponds to attribute <span class='attr-name'>'textLength'</span> on
108 * the given element.
109 */
110 public final OMSVGAnimatedLength getTextLength() {
111 return ((SVGTextContentElement)ot).getTextLength();
112 }
113 /**
114 * Corresponds to attribute <span class='attr-name'>'lengthAdjust'</span>
115 * on the given element. The value must be one of the length adjust constants
116 * defined on this interface.
117 */
118 public final OMSVGAnimatedEnumeration getLengthAdjust() {
119 return ((SVGTextContentElement)ot).getLengthAdjust();
120 }
121 /**
122 * Returns the total number of characters available for rendering within the
123 * current element, which includes referenced characters from <a href='http://www.w3.org/TR/SVG11/text.html#TRefElement'
124 * title='tref element specification'>tref</a> reference, regardless of whether
125 * they will be rendered. Effectively, this is equivalent to the length of
126 * the <a href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent">Node::textContent</a>
127 * attribute from DOM Level 3 Core ([<a href="refs.html#ref-DOM3">DOM3</a>],
128 * section 1.4), if that attribute also expanded <a href='http://www.w3.org/TR/SVG11/text.html#TRefElement'
129 * title='tref element specification'>tref</a> elements.
130 * @return Total number of characters.
131 */
132 public final int getNumberOfChars() {
133 return ((SVGTextContentElement)ot).getNumberOfChars();
134 }
135 /**
136 * The total sum of all of the advance values from rendering all of the characters
137 * within this element, including the advance value on the glyphs (horizontal
138 * or vertical), the effect of properties <code>kerning</code>, <code>letter-spacing</code>
139 * and <code>word-spacing</code> and adjustments due to attributes <code>tspan/dx</code>
140 * and <code>tspan/dy</code> on <a href='http://www.w3.org/TR/SVG11/text.html#TSpanElement'
141 * title='tspan element specification'>tspan</a> elements. For non-rendering
142 * environments, the user agent shall make reasonable assumptions about glyph
143 * metrics.
144 * @return The text advance distance.
145 */
146 public final float getComputedTextLength() {
147 return ((SVGTextContentElement)ot).getComputedTextLength();
148 }
149 /**
150 * The total sum of all of the advance values from rendering the specified
151 * substring of the characters, including the advance value on the glyphs
152 * (horizontal or vertical), the effect of properties <code>kerning</code>,
153 * <code>letter-spacing</code> and <code>word-spacing</code> and adjustments
154 * due to attributes <code>tspan/dx</code> and <code>tspan/dy</code> on <a
155 * href='http://www.w3.org/TR/SVG11/text.html#TSpanElement' title='tspan element
156 * specification'>tspan</a> elements. For non-rendering environments, the
157 * user agent shall make reasonable assumptions about glyph metrics. If multiple
158 * consecutive characters are rendered inseparably (e.g., as a single glyph
159 * or a sequence of glyphs, or because the range encompasses half of a surrogate
160 * pair), and nchars is greater than 0 then the measured range shall be expanded
161 * so that each of the inseparable characters are included.
162 * @param charnum The index of the first character in the substring, where
163 * the first character has an index of 0.
164 * @param nchars The number of characters in the substring. If nchars specifies
165 * more characters than are available, then the substring will consist of
166 * all characters starting with charnum until the end of the list of characters.
167 * @return The text advance distance.
168 * @throws DOMException(INDEX_SIZE_ERR) Raised if <var>charnum</var> or
169 * <var>nchars</var> is negative or if <var>charnum</var> is greater than
170 * or equal to the number of characters at this node.
171 */
172 public final float getSubStringLength(int charnum, int nchars) throws JavaScriptException {
173 return ((SVGTextContentElement)ot).getSubStringLength(charnum, nchars);
174 }
175 /**
176 * Returns the current text position before rendering the character in the
177 * user coordinate system for rendering the glyph(s) that correspond to the
178 * specified character. The current text position has already taken into account
179 * the effects of any inter-character adjustments due to properties <code>kerning</code>,
180 * <code>letter-spacing</code> and <code>word-spacing</code> and adjustments
181 * due to attributes <span class='attr-name'>'x'</span>, <span class='attr-name'>'y'</span>,
182 * <span class='attr-name'>'dx'</span> and <span class='attr-name'>'dy'</span>.
183 * If multiple consecutive characters are rendered inseparably (e.g., as a
184 * single glyph or a sequence of glyphs), then each of the inseparable characters
185 * will return the start position for the first glyph.
186 * @param charnum The index of the character, where the first character has
187 * an index of 0.
188 * @return The character's start position.
189 * @throws DOMException(INDEX_SIZE_ERR) Raised if the <var>charnum</var> is
190 * negative or if <var>charnum</var> is greater than or equal to the number
191 * of characters at this node.
192 */
193 public final OMSVGPoint getStartPositionOfChar(int charnum) throws JavaScriptException {
194 return ((SVGTextContentElement)ot).getStartPositionOfChar(charnum);
195 }
196 /**
197 * Returns the current text position after rendering the character in the
198 * user coordinate system for rendering the glyph(s) that correspond to the
199 * specified character. This current text position does <em>not</em> take
200 * into account the effects of any inter-character adjustments to prepare
201 * for the next character, such as properties <code>kerning</code>, <code>letter-spacing</code>
202 * and <code>word-spacing</code> and adjustments due to attributes <span class='attr-name'>'x'</span>,
203 * <span class='attr-name'>'y'</span>, <span class='attr-name'>'dx'</span>
204 * and <span class='attr-name'>'dy'</span>. If multiple consecutive characters
205 * are rendered inseparably (e.g., as a single glyph or a sequence of glyphs),
206 * then each of the inseparable characters will return the end position for
207 * the last glyph.
208 * @param charnum The index of the character, where the first character has
209 * an index of 0.
210 * @return The character's end position.
211 * @throws DOMException(INDEX_SIZE_ERR) Raised if the <var>charnum</var> is
212 * negative or if <var>charnum</var> is greater than or equal to the number
213 * of characters at this node.
214 */
215 public final OMSVGPoint getEndPositionOfChar(int charnum) throws JavaScriptException {
216 return ((SVGTextContentElement)ot).getEndPositionOfChar(charnum);
217 }
218 /**
219 * Returns a tightest rectangle which defines the minimum and maximum X and
220 * Y values in the user coordinate system for rendering the glyph(s) that
221 * correspond to the specified character. The calculations assume that all
222 * glyphs occupy the full standard glyph cell for the font. If multiple consecutive
223 * characters are rendered inseparably (e.g., as a single glyph or a sequence
224 * of glyphs), then each of the inseparable characters will return the same
225 * extent.
226 * @param charnum The index of the character, where the first character has
227 * an index of 0.
228 * @return The rectangle which encloses all of the rendered glyph(s).
229 * @throws DOMException(INDEX_SIZE_ERR) Raised if the <var>charnum</var> is
230 * negative or if <var>charnum</var> is greater than or equal to the number
231 * of characters at this node.
232 */
233 public final OMSVGRect getExtentOfChar(int charnum) throws JavaScriptException {
234 return ((SVGTextContentElement)ot).getExtentOfChar(charnum);
235 }
236 /**
237 * Returns the rotation value relative to the current user coordinate system
238 * used to render the glyph(s) corresponding to the specified character. If
239 * multiple glyph(s) are used to render the given character and the glyphs
240 * each have different rotations (e.g., due to text-on-a-path), the user agent
241 * shall return an average value (e.g., the rotation angle at the midpoint
242 * along the path for all glyphs used to render this character). The rotation
243 * value represents the rotation that is supplemental to any rotation due
244 * to properties <code>glyph-orientation-horizontal</code> and <code>glyph-orientation-vertical</code>;
245 * thus, any glyph rotations due to these properties are not included into
246 * the returned rotation value. If multiple consecutive characters are rendered
247 * inseparably (e.g., as a single glyph or a sequence of glyphs), then each
248 * of the inseparable characters will return the same rotation value.
249 * @param charnum The index of the character, where the first character has
250 * an index of 0.
251 * @return The rotation angle.
252 * @throws DOMException(INDEX_SIZE_ERR) Raised if the <var>charnum</var> is
253 * negative or if <var>charnum</var> is greater than or equal to the number
254 * of characters at this node.
255 */
256 public final float getRotationOfChar(int charnum) throws JavaScriptException {
257 return ((SVGTextContentElement)ot).getRotationOfChar(charnum);
258 }
259 /**
260 * Returns the index of the character whose corresponding glyph cell bounding
261 * box contains the specified point. The calculations assume that all glyphs
262 * occupy the full standard glyph cell for the font. If no such character
263 * exists, a value of -1 is returned. If multiple such characters exist, the
264 * character within the element whose glyphs were rendered last (i.e., take
265 * into account any reordering such as for bidirectional text) is used. If
266 * multiple consecutive characters are rendered inseparably (e.g., as a single
267 * glyph or a sequence of glyphs), then the user agent shall allocate an equal
268 * percentage of the text advance amount to each of the contributing characters
269 * in determining which of the characters is chosen.
270 * @param point A point in user space.
271 * @return The index of the character which is at the given point, where
272 * the first character has an index of 0.
273 */
274 public final int getCharNumAtPosition(OMSVGPoint point) {
275 return ((SVGTextContentElement)ot).getCharNumAtPosition(point);
276 }
277 /**
278 * Causes the specified substring to be selected just as if the user selected
279 * the substring interactively.
280 * @param charnum The index of the start character which is at the given
281 * point, where the first character has an index of 0.
282 * @param nchars The number of characters in the substring. If nchars specifies
283 * more characters than are available, then the substring will consist of
284 * all characters starting with charnum until the end of the list of characters.
285 * @throws DOMException(INDEX_SIZE_ERR) Raised if <var>charnum</var> or
286 * <var>nchars</var> is negative or if <var>charnum</var> is greater than
287 * or equal to the number of characters at this node.
288 */
289 public final void selectSubString(int charnum, int nchars) throws JavaScriptException {
290 ((SVGTextContentElement)ot).selectSubString(charnum, nchars);
291 }
292
293 // Implementation of the svg::SVGExternalResourcesRequired W3C IDL interface
294 /**
295 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
296 * on the given element. Note that the SVG DOM defines the attribute {@link
297 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
298 * as being of type {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean}, whereas
299 * the SVG language definition says that {@link org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
300 * is not animated. Because the SVG language definition states that {@link
301 * org.vectomatic.dom.svg.itf.ISVGExternalResourcesRequired#getExternalResourcesRequired()}
302 * cannot be animated, the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getAnimVal()}
303 * will always be the same as the {@link org.vectomatic.dom.svg.OMSVGAnimatedBoolean#getBaseVal()}.
304 */
305 public final OMSVGAnimatedBoolean getExternalResourcesRequired() {
306 return ((SVGTextContentElement)ot).getExternalResourcesRequired();
307 }
308
309 // Implementation of the svg::SVGLangSpace W3C IDL interface
310 /**
311 * Corresponds to attribute <code>xml:lang</code> on the given element.
312 */
313 public final String getXmllang() {
314 return ((SVGTextContentElement)ot).getXmllang();
315 }
316 /**
317 * Corresponds to attribute <code>xml:lang</code> on the given element.
318 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt
319 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only
320 * attribute</a>.
321 */
322 public final void setXmllang(java.lang.String value) throws JavaScriptException {
323 ((SVGTextContentElement)ot).setXmllang(value);
324 }
325 /**
326 * Corresponds to attribute <code>xml:space</code> on the given element.
327 */
328 public final String getXmlspace() {
329 return ((SVGTextContentElement)ot).getXmlspace();
330 }
331 /**
332 * Corresponds to attribute <code>xml:space</code> on the given element.
333 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt
334 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only
335 * attribute</a>.
336 */
337 public final void setXmlspace(java.lang.String value) throws JavaScriptException {
338 ((SVGTextContentElement)ot).setXmlspace(value);
339 }
340
341 // Implementation of the svg::SVGTests W3C IDL interface
342 /**
343 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredFeatures()}
344 * on the given element.
345 */
346 public final OMSVGStringList getRequiredFeatures() {
347 return ((SVGTextContentElement)ot).getRequiredFeatures();
348 }
349 /**
350 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getRequiredExtensions()}
351 * on the given element.
352 */
353 public final OMSVGStringList getRequiredExtensions() {
354 return ((SVGTextContentElement)ot).getRequiredExtensions();
355 }
356 /**
357 * Corresponds to attribute {@link org.vectomatic.dom.svg.itf.ISVGTests#getSystemLanguage()}
358 * on the given element.
359 */
360 public final OMSVGStringList getSystemLanguage() {
361 return ((SVGTextContentElement)ot).getSystemLanguage();
362 }
363 /**
364 * Returns true if the user agent supports the given extension, specified
365 * by a URI.
366 * @param extension The name of the extension, expressed as a URI.
367 * @return True or false, depending on whether the given extension is supported.
368 */
369 public final boolean hasExtension(String extension) {
370 return ((SVGTextContentElement)ot).hasExtension(extension);
371 }
372
373 @Override
374 public final HandlerRegistration addClickHandler(ClickHandler handler) {
375 return addDomHandler(handler, ClickEvent.getType());
376 }
377 @Override
378 public final HandlerRegistration addLoadHandler(LoadHandler handler) {
379 return addDomHandler(handler, LoadEvent.getType());
380 }
381 @Override
382 public final HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
383 return addDomHandler(handler, MouseDownEvent.getType());
384 }
385 @Override
386 public final HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
387 return addDomHandler(handler, MouseMoveEvent.getType());
388 }
389 @Override
390 public final HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
391 return addDomHandler(handler, MouseOutEvent.getType());
392 }
393 @Override
394 public final HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
395 return addDomHandler(handler, MouseOverEvent.getType());
396 }
397 @Override
398 public final HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
399 return addDomHandler(handler, MouseUpEvent.getType());
400 }
401 @Override
402 public final HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
403 return addDomHandler(handler, TouchCancelEvent.getType());
404 }
405 @Override
406 public final HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
407 return addDomHandler(handler, TouchEndEvent.getType());
408 }
409 @Override
410 public final HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
411 return addDomHandler(handler, TouchMoveEvent.getType());
412 }
413 @Override
414 public final HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
415 return addDomHandler(handler, TouchStartEvent.getType());
416 }
417 @Override
418 public final HandlerRegistration addActivateHandler(ActivateHandler handler) {
419 return addDomHandler(handler, ActivateEvent.getType());
420 }
421 @Override
422 public final HandlerRegistration addFocusInHandler(FocusInHandler handler) {
423 return addDomHandler(handler, FocusInEvent.getType());
424 }
425 @Override
426 public final HandlerRegistration addFocusOutHandler(FocusOutHandler handler) {
427 return addDomHandler(handler, FocusOutEvent.getType());
428 }
429 }