001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.model;
027:
028: import com.sun.perseus.j2d.GraphicsProperties;
029: import com.sun.perseus.j2d.RenderGraphics;
030:
031: import com.sun.perseus.util.SVGConstants;
032:
033: import org.w3c.dom.DOMException;
034:
035: import org.w3c.dom.svg.SVGRect;
036:
037: import com.sun.perseus.j2d.Box;
038: import com.sun.perseus.j2d.PathSupport;
039: import com.sun.perseus.j2d.Transform;
040:
041: /**
042: * <code>Rect</code> represents and SVG Tiny <code><rect></code>
043: * element.
044: * <br />
045: * Negative width, height, rx or ry value is illegal. A value of zero
046: * for the rectangle's width or height disables its rendering.
047: *
048: * @version $Id: Rect.java,v 1.11 2006/06/29 10:47:33 ln156897 Exp $
049: */
050: public class Rect extends AbstractShapeNode {
051: /**
052: * width and height are required on <rect>
053: */
054: static final String[] REQUIRED_TRAITS = {
055: SVGConstants.SVG_WIDTH_ATTRIBUTE,
056: SVGConstants.SVG_HEIGHT_ATTRIBUTE };
057:
058: /**
059: * The rect's width.
060: */
061: protected float width = 0;
062:
063: /**
064: * The rect's height.
065: */
066: protected float height = 0;
067:
068: /**
069: * The rect's x-axis origin.
070: */
071: protected float x = 0;
072:
073: /**
074: * The rect's y-axis origin.
075: */
076: protected float y = 0;
077:
078: /**
079: * The rect's x-axis arcwidth.
080: */
081: protected float aw = 0;
082:
083: /**
084: * The rect's y-axis archeight.
085: */
086: protected float ah = 0;
087:
088: /**
089: * Constructor.
090: *
091: * @param ownerDocument this element's owner <code>DocumentNode</code>
092: */
093: public Rect(final DocumentNode ownerDocument) {
094: super (ownerDocument);
095:
096: // Initially, the rect's width and height are zero, so we
097: // set the corresponding bits accordingly.
098: canRenderState |= CAN_RENDER_ZERO_WIDTH_BIT;
099: canRenderState |= CAN_RENDER_ZERO_HEIGHT_BIT;
100: }
101:
102: /**
103: * @return the SVGConstants.SVG_RECT_TAG value
104: */
105: public String getLocalName() {
106: return SVGConstants.SVG_RECT_TAG;
107: }
108:
109: /**
110: * Used by <code>DocumentNode</code> to create a new instance from
111: * a prototype <code>Rect</code>.
112: *
113: * @param doc the <code>DocumentNode</code> for which a new node is
114: * should be created.
115: * @return a new <code>Rect</code> for the requested document.
116: */
117: public ElementNode newInstance(final DocumentNode doc) {
118: return new Rect(doc);
119: }
120:
121: /**
122: * @return this rectangle's x-axis origin
123: */
124: public float getX() {
125: return x;
126: }
127:
128: /**
129: * @return this rectangle's y-axis origin
130: */
131: public float getY() {
132: return y;
133: }
134:
135: /**
136: * @return this rectangle's width
137: */
138: public float getWidth() {
139: return width;
140: }
141:
142: /**
143: * @return this rectangle's height
144: */
145: public float getHeight() {
146: return height;
147: }
148:
149: /**
150: * @return x-axis corner radius
151: */
152: public float getRx() {
153: return aw / 2;
154: }
155:
156: /**
157: * @return y-axis corner radius
158: */
159: public float getRy() {
160: return ah / 2;
161: }
162:
163: /**
164: * @param x new rectangle x-axis origin
165: */
166: public void setX(final float x) {
167: if (this .x == x) {
168: return;
169: }
170:
171: modifyingNode();
172: renderingDirty();
173: this .x = x;
174: modifiedNode();
175: }
176:
177: /**
178: * @param y new rectangle y-axis origin
179: */
180: public void setY(final float y) {
181: if (this .y == y) {
182: return;
183: }
184: modifyingNode();
185: renderingDirty();
186: this .y = y;
187: modifiedNode();
188: }
189:
190: /**
191: * @param width new rectangle width. Should be strictly positive.
192: */
193: public void setWidth(final float width) {
194: if (width < 0) {
195: throw new IllegalArgumentException();
196: }
197:
198: if (this .width == width) {
199: return;
200: }
201:
202: modifyingNode();
203: renderingDirty();
204: this .width = width;
205: computeCanRenderWidthBit(width);
206: modifiedNode();
207: }
208:
209: /**
210: * @param height new rectangle height. Should be strictly positive.
211: */
212: public void setHeight(final float height) {
213: if (height < 0) {
214: throw new IllegalArgumentException();
215: }
216:
217: if (this .height == height) {
218: return;
219: }
220:
221: modifyingNode();
222: renderingDirty();
223: this .height = height;
224: computeCanRenderHeightBit(height);
225: modifiedNode();
226: }
227:
228: /**
229: * @param rx new x-axis corner radius. Should be strictly positive.
230: */
231: public void setRx(final float rx) {
232: if (rx < 0) {
233: throw new IllegalArgumentException();
234: }
235:
236: if (2 * rx == aw) {
237: return;
238: }
239:
240: modifyingNode();
241: renderingDirty();
242:
243: if (rx > 0) {
244: aw = 2 * rx;
245: } else {
246: aw = 0;
247: }
248: modifiedNode();
249: }
250:
251: /**
252: * @param ry new y-axis radius. Shoud be strictly positive.
253: */
254: public void setRy(final float ry) {
255: if (ry < 0) {
256: throw new IllegalArgumentException();
257: }
258:
259: if (2 * ry == ah) {
260: return;
261: }
262:
263: modifyingNode();
264: renderingDirty();
265:
266: if (ry > 0) {
267: ah = 2 * ry;
268: } else {
269: ah = 0;
270: }
271: modifiedNode();
272: }
273:
274: /**
275: * @param x new x-axis origin
276: * @param y new y-axis origin
277: * @param width new width
278: * @param height new height
279: */
280: public void setRect(final float x, final float y,
281: final float width, final float height) {
282: if (width < 0 || height < 0) {
283: throw new IllegalArgumentException();
284: }
285:
286: if (this .x == x && this .y == y && this .width == width
287: && this .height == height) {
288: return;
289: }
290:
291: modifyingNode();
292: renderingDirty();
293:
294: this .x = x;
295: this .y = y;
296: this .width = width;
297: this .height = height;
298:
299: computeCanRenderWidthBit(width);
300: computeCanRenderHeightBit(height);
301: modifiedNode();
302: }
303:
304: /**
305: * @param rg the RenderGraphics on which to fill the shape.
306: */
307: public void fillShape(final RenderGraphics rg) {
308: rg.fillRect(x, y, width, height, aw, ah);
309: }
310:
311: /**
312: * @param rg the RenderGraphics on which to draw the shape.
313: */
314: public void drawShape(final RenderGraphics rg) {
315: rg.drawRect(x, y, width, height, aw, ah);
316: }
317:
318: /**
319: * @param x the hit point coordinate along the x-axis, in user space.
320: * @param y the hit point coordinate along the y-axis, in user space.
321: * @param fillRule the fillRule to apply when testing for containment.
322: * @return true if the hit point is contained within the shape.
323: */
324: public boolean contains(float x, float y, final int fillRule) {
325: if (aw == 0 && ah == 0) {
326: return x >= this .x && y >= this .y && x < this .x + width
327: && y < this .y + height;
328: } else {
329: // This code is derived from the java.awt.geom.Rectangle2D and
330: // java.awt.geom.RoundRectangle2D
331: float rrx0 = this .x;
332: float rry0 = this .y;
333: float rrx1 = rrx0 + width;
334: float rry1 = rry0 + height;
335:
336: // Check for trivial rejection - point is outside bounding rectangle
337: if (x < rrx0 || y < rry0 || x >= rrx1 || y >= rry1) {
338: return false;
339: }
340:
341: float aw = Math.min(width, this .aw) / 2.0f;
342: float ah = Math.min(height, this .ah) / 2.0f;
343:
344: // Check which corner point is in and do circular containment
345: // test - otherwise simple acceptance
346: if (x >= (rrx0 += aw) && x < (rrx0 = rrx1 - aw)) {
347: return true;
348: }
349:
350: if (y >= (rry0 += ah) && y < (rry0 = rry1 - ah)) {
351: return true;
352: }
353:
354: x = (x - rrx0) / aw;
355: y = (y - rry0) / ah;
356: return (x * x + y * y <= 1.0);
357: }
358: }
359:
360: /**
361: * Returns the stroked shape, using the given stroke properties.
362: *
363: * @param gp the <code>GraphicsProperties</code> defining the rendering
364: * context.
365: * @return the shape's stroked path.
366: */
367: Object getStrokedPath(final GraphicsProperties gp) {
368: if (aw > 0 || ah > 0) {
369: return PathSupport.getStrokedRect(x, y, width, height, aw,
370: ah, gp);
371: }
372:
373: return PathSupport.getStrokedRect(x, y, width, height, gp);
374: }
375:
376: /**
377: * Rect handles x, y, rx, ry, width and height traits.
378: *
379: * @param traitName the name of the trait which the element may support.
380: * @return true if this element supports the given trait in one of the
381: * trait accessor methods.
382: */
383: boolean supportsTrait(final String traitName) {
384: if (SVGConstants.SVG_X_ATTRIBUTE == traitName
385: || SVGConstants.SVG_Y_ATTRIBUTE == traitName
386: || SVGConstants.SVG_RX_ATTRIBUTE == traitName
387: || SVGConstants.SVG_RY_ATTRIBUTE == traitName
388: || SVGConstants.SVG_WIDTH_ATTRIBUTE == traitName
389: || SVGConstants.SVG_HEIGHT_ATTRIBUTE == traitName) {
390: return true;
391: } else {
392: return super .supportsTrait(traitName);
393: }
394: }
395:
396: /**
397: * @return an array of traits that are required by this element.
398: */
399: public String[] getRequiredTraits() {
400: return REQUIRED_TRAITS;
401: }
402:
403: /**
404: * @return an array of trait aliases. These are used when the
405: * value of a trait can be used to set the value of another trait.
406: * For example, on a <rect>, if the rx trait is not specified in the
407: * original XML document, the value fot eh ry trait should be used.
408: */
409: public String[][] getTraitAliases() {
410: return new String[][] {
411: { SVGConstants.SVG_RX_ATTRIBUTE,
412: SVGConstants.SVG_RY_ATTRIBUTE },
413: { SVGConstants.SVG_RY_ATTRIBUTE,
414: SVGConstants.SVG_RX_ATTRIBUTE } };
415: }
416:
417: /**
418: * Rect handles x, y, rx, ry, width and height traits as
419: * FloatTraitAnims
420: *
421: * @param traitName the trait name.
422: */
423: TraitAnim createTraitAnimImpl(final String traitName) {
424: if (SVGConstants.SVG_X_ATTRIBUTE == traitName
425: || SVGConstants.SVG_Y_ATTRIBUTE == traitName
426: || SVGConstants.SVG_RX_ATTRIBUTE == traitName
427: || SVGConstants.SVG_RY_ATTRIBUTE == traitName
428: || SVGConstants.SVG_WIDTH_ATTRIBUTE == traitName
429: || SVGConstants.SVG_HEIGHT_ATTRIBUTE == traitName) {
430: return new FloatTraitAnim(this , traitName, TRAIT_TYPE_FLOAT);
431: } else {
432: return super .createTraitAnimImpl(traitName);
433: }
434: }
435:
436: /**
437: * Rect handles x, y, rx, ry, width and height traits.
438: * Other traits are handled by the super class.
439: *
440: * @param name the requested trait name (e.g., "ry")
441: * @return the trait's value, as a string.
442: *
443: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
444: * trait is not supported on this element or null.
445: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
446: * trait's computed value cannot be converted to a String (SVG Tiny only).
447: */
448: public String getTraitImpl(final String name) throws DOMException {
449: if (SVGConstants.SVG_X_ATTRIBUTE == name) {
450: return Float.toString(x);
451: } else if (SVGConstants.SVG_Y_ATTRIBUTE == name) {
452: return Float.toString(y);
453: } else if (SVGConstants.SVG_RX_ATTRIBUTE == name) {
454: return Float.toString(aw / 2);
455: } else if (SVGConstants.SVG_RY_ATTRIBUTE == name) {
456: return Float.toString(ah / 2);
457: } else if (SVGConstants.SVG_WIDTH_ATTRIBUTE == name) {
458: return Float.toString(width);
459: } else if (SVGConstants.SVG_HEIGHT_ATTRIBUTE == name) {
460: return Float.toString(height);
461: } else {
462: return super .getTraitImpl(name);
463: }
464: }
465:
466: /**
467: * Rect handles x, y, rx, ry, width and height traits.
468: * Other attributes are handled by the super class.
469: *
470: * @param name the requested trait name (e.g., "y")
471: * @return the requested trait value
472: *
473: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
474: * trait is not supported on this element or null.
475: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
476: * trait's computed value cannot be converted to a float
477: * @throws SecurityException if the application does not have the necessary
478: * privilege rights to access this (SVG) content.
479: */
480: float getFloatTraitImpl(final String name) throws DOMException {
481: if (SVGConstants.SVG_X_ATTRIBUTE == name) {
482: return x;
483: } else if (SVGConstants.SVG_Y_ATTRIBUTE == name) {
484: return y;
485: } else if (SVGConstants.SVG_RX_ATTRIBUTE == name) {
486: return aw / 2;
487: } else if (SVGConstants.SVG_RY_ATTRIBUTE == name) {
488: return ah / 2;
489: } else if (SVGConstants.SVG_WIDTH_ATTRIBUTE == name) {
490: return width;
491: } else if (SVGConstants.SVG_HEIGHT_ATTRIBUTE == name) {
492: return height;
493: } else {
494: return super .getFloatTraitImpl(name);
495: }
496: }
497:
498: /**
499: * Validates the input trait value.
500: *
501: * @param traitName the name of the trait to be validated.
502: * @param value the value to be validated
503: * @param reqNamespaceURI the namespace of the element requesting
504: * validation.
505: * @param reqLocalName the local name of the element requesting validation.
506: * @param reqTraitNamespace the namespace of the trait which has the values
507: * value on the requesting element.
508: * @param reqTraitName the name of the trait which has the values value on
509: * the requesting element.
510: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
511: * value is incompatible with the given trait.
512: */
513: public float[][] validateFloatArrayTrait(final String traitName,
514: final String value, final String reqNamespaceURI,
515: final String reqLocalName, final String reqTraitNamespace,
516: final String reqTraitName) throws DOMException {
517: if (SVGConstants.SVG_X_ATTRIBUTE == traitName
518: || SVGConstants.SVG_Y_ATTRIBUTE == traitName) {
519: return new float[][] { { parseFloatTrait(traitName, value) } };
520: } else if (SVGConstants.SVG_RX_ATTRIBUTE == traitName
521: || SVGConstants.SVG_RY_ATTRIBUTE == traitName
522: || SVGConstants.SVG_WIDTH_ATTRIBUTE == traitName
523: || SVGConstants.SVG_HEIGHT_ATTRIBUTE == traitName) {
524: return new float[][] { { parsePositiveFloatTrait(traitName,
525: value) } };
526: } else {
527: return super .validateFloatArrayTrait(traitName, value,
528: reqNamespaceURI, reqLocalName, reqTraitNamespace,
529: reqTraitName);
530: }
531: }
532:
533: /**
534: * Rect handles x, y, rx, ry, width and height traits.
535: * Other traits are handled by the super class.
536: *
537: * @param name the trait's name (e.g., "rx")
538: * @param value the new trait string value (e.g., "10")
539: *
540: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
541: * trait is not supported on this element or null.
542: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
543: * trait's value cannot be specified as a String
544: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
545: * value is an invalid value for the given trait or null.
546: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
547: * attempt is made to change readonly trait.
548: */
549: public void setTraitImpl(final String name, final String value)
550: throws DOMException {
551: if (SVGConstants.SVG_X_ATTRIBUTE == name) {
552: setX(parseFloatTrait(name, value));
553: } else if (SVGConstants.SVG_Y_ATTRIBUTE == name) {
554: setY(parseFloatTrait(name, value));
555: } else if (SVGConstants.SVG_RX_ATTRIBUTE == name) {
556: setRx(parsePositiveFloatTrait(name, value));
557: } else if (SVGConstants.SVG_RY_ATTRIBUTE == name) {
558: setRy(parsePositiveFloatTrait(name, value));
559: } else if (SVGConstants.SVG_WIDTH_ATTRIBUTE == name) {
560: setWidth(parsePositiveFloatTrait(name, value));
561: } else if (SVGConstants.SVG_HEIGHT_ATTRIBUTE == name) {
562: setHeight(parsePositiveFloatTrait(name, value));
563: } else {
564: super .setTraitImpl(name, value);
565: }
566: }
567:
568: /**
569: * Rect handles x, y, rx, ry, width and height traits.
570: * Other traits are handled by the super class.
571: *
572: * @param name the trait's name (e.g., "x")
573: * @param value the new trait value (e.g., 20f)
574: *
575: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
576: * trait is not supported on this element.
577: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
578: * trait's value cannot be specified as a float
579: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
580: * value is an invalid value for the given trait.
581: * @throws SecurityException if the application does not have the necessary
582: * privilege rights to access this (SVG) content.
583: */
584: public void setFloatTraitImpl(final String name, final float value)
585: throws DOMException {
586: if (SVGConstants.SVG_X_ATTRIBUTE == name) {
587: setX(value);
588: } else if (SVGConstants.SVG_Y_ATTRIBUTE == name) {
589: setY(value);
590: } else if (SVGConstants.SVG_RX_ATTRIBUTE == name) {
591: checkPositive(name, value);
592: setRx(value);
593: } else if (SVGConstants.SVG_RY_ATTRIBUTE == name) {
594: checkPositive(name, value);
595: setRy(value);
596: } else if (SVGConstants.SVG_WIDTH_ATTRIBUTE == name) {
597: checkPositive(name, value);
598: setWidth(value);
599: } else if (SVGConstants.SVG_HEIGHT_ATTRIBUTE == name) {
600: checkPositive(name, value);
601: setHeight(value);
602: } else {
603: super .setFloatTraitImpl(name, value);
604: }
605: }
606:
607: /**
608: * @param name the name of the trait to convert.
609: * @param value the float trait value to convert.
610: */
611: String toStringTrait(final String name, final float[][] value) {
612: if (SVGConstants.SVG_X_ATTRIBUTE == name
613: || SVGConstants.SVG_Y_ATTRIBUTE == name
614: || SVGConstants.SVG_RX_ATTRIBUTE == name
615: || SVGConstants.SVG_RY_ATTRIBUTE == name
616: || SVGConstants.SVG_WIDTH_ATTRIBUTE == name
617: || SVGConstants.SVG_HEIGHT_ATTRIBUTE == name) {
618: return Float.toString(value[0][0]);
619: } else {
620: return super .toStringTrait(name, value);
621: }
622: }
623:
624: /**
625: * Set the trait value as float.
626: *
627: * @param name the trait's name.
628: * @param value the trait's value.
629: *
630: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
631: * trait is not supported on this element.
632: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
633: * trait's value cannot be specified as a float
634: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
635: * value is an invalid value for the given trait.
636: */
637: void setFloatArrayTrait(final String name, final float[][] value)
638: throws DOMException {
639: if (SVGConstants.SVG_X_ATTRIBUTE == name) {
640: setX(value[0][0]);
641: } else if (SVGConstants.SVG_Y_ATTRIBUTE == name) {
642: setY(value[0][0]);
643: } else if (SVGConstants.SVG_RX_ATTRIBUTE == name) {
644: checkPositive(name, value[0][0]);
645: setRx(value[0][0]);
646: } else if (SVGConstants.SVG_RY_ATTRIBUTE == name) {
647: checkPositive(name, value[0][0]);
648: setRy(value[0][0]);
649: } else if (SVGConstants.SVG_WIDTH_ATTRIBUTE == name) {
650: checkPositive(name, value[0][0]);
651: setWidth(value[0][0]);
652: } else if (SVGConstants.SVG_HEIGHT_ATTRIBUTE == name) {
653: checkPositive(name, value[0][0]);
654: setHeight(value[0][0]);
655: } else {
656: super .setFloatArrayTrait(name, value);
657: }
658: }
659:
660: /**
661: * @param bbox the bounding box to which this node's bounding box should be
662: * appended. That bounding box is in the target coordinate space. It
663: * may be null, in which case this node should create a new one.
664: * @param t the transform from the node coordinate system to the coordinate
665: * system into which the bounds should be computed.
666: * @return the bounding box of this node, in the target coordinate space,
667: */
668: Box addNodeBBox(final Box bbox, final Transform t) {
669: return addTransformedBBox(bbox, x, y, width, height, t);
670: }
671:
672: /**
673: * Debug helper.
674: *
675: * @return a textual description of the rectangle including id
676: * and geometry information.
677: */
678: /*
679: public String toString() {
680: if (isRounded) {
681: return "RoundedRect[id(" + getId() + ") " + rr.getX() + ", "
682: + rr.getY() + ", " + rr.getWidth() + ", " + rr.getHeight()
683: + ", aw(" + rr.getArcWidth() + ") ah(" + rr.getArcHeight()
684: + ")]";
685: } else {
686: return "Rect[id(" + getId() + ") " + r.getX() + ", "
687: + r.getY() + ", " + r.getWidth() + ", " + r.getHeight()
688: + "]";
689: }
690: }
691: */
692:
693: }
|