You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShadowEffect.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * ShadowEffect.java
  3. * $Id$
  4. *
  5. *
  6. * Copyright 1999-2003 The Apache Software Foundation.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. *
  21. * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  22. * @version $Revision$ $Name$
  23. */
  24. package org.apache.fop.datatypes;
  25. import java.util.Iterator;
  26. import org.apache.fop.fo.PropNames;
  27. import org.apache.fop.fo.expr.PropertyException;
  28. /**
  29. * Class to represent ShadowEffect values. This class is a holder for a
  30. * set of PropertyValue objects, and will be placed in a PropertyValueList,
  31. * as text shadow effects are specified in a list. See 7.16.5.
  32. */
  33. public class ShadowEffect extends AbstractPropertyValue {
  34. private static final String tag = "$Name$";
  35. private static final String revision = "$Revision$";
  36. /**
  37. * The shadow's "horizontal distance to the right of the text" (mandatory).
  38. */
  39. private Numeric inlineOffset;
  40. /**
  41. * The shadow's "vertical distance below the text" (mandatory).
  42. */
  43. private Numeric blockOffset;
  44. /**
  45. * The shadow's blur radius (optional)
  46. */
  47. private Numeric blurRadius;
  48. /**
  49. * The shadow's color (optional)
  50. */
  51. private ColorType color;
  52. /**
  53. * Construct a <i>ShadowEffect</i> from a given <tt>PropertyValueList</tt>.
  54. * An individual shadow effect is specified as a list comprising a
  55. * mandatory pair of <tt>Length</tt>s, the inline-progression offset and
  56. * the block-progression offset, with an otpional third <tt>Length</tt>
  57. * for the blur radius. The shadow effect may optionally include a
  58. * color value, specified as a <tt>ColorType</tt>. The <tt>ColorType</tt>
  59. * may precede or follow the <tt>Length</tt> specifiers.
  60. * @param property the index of the property with which this value
  61. * is associated.
  62. * @param list the <tt>PropertyValueList</tt> containing details of one
  63. * shadow effect
  64. */
  65. public ShadowEffect(int property, PropertyValueList list)
  66. throws PropertyException
  67. {
  68. super(property, PropertyValue.SHADOW_EFFECT);
  69. Object entry;
  70. Iterator entries = list.iterator();
  71. switch (list.size()) {
  72. case 2:
  73. // Must be the inline and block offsets
  74. setInlineAndBlock(entries);
  75. break;
  76. case 3:
  77. // Must have inline and block offsets; may have blur radius or
  78. // a colour specifier
  79. if (list.getFirst() instanceof Numeric) {
  80. if (list.getLast() instanceof Numeric) {
  81. setInlineBlockAndBlur(entries);
  82. } else { // last element must be a color
  83. setInlineAndBlock(entries);
  84. setColor(entries);
  85. }
  86. }
  87. else { // First entry is not Numeric; has to be color
  88. setColor(entries);
  89. setInlineAndBlock(entries);
  90. }
  91. break;
  92. case 4:
  93. // Must have inline and block offsets, blur radius and colour
  94. // specifier
  95. if (list.getFirst() instanceof Numeric) {
  96. setInlineBlockAndBlur(entries);
  97. setColor(entries);
  98. }
  99. else { // First entry is not Numeric; has to be color
  100. setColor(entries);
  101. setInlineBlockAndBlur(entries);
  102. }
  103. break;
  104. default:
  105. throw new PropertyException
  106. ("Invalid number of elements in ShadowEffect: "
  107. + list.size());
  108. }
  109. }
  110. /**
  111. * Construct a <i>ShadowEffect</i> from a given <tt>PropertyValueList</tt>.
  112. * @param propertyName the name of the property with which this value
  113. * is associated.
  114. * @param list the <tt>PropertyValueList</tt> containing details of one
  115. * shadow effect
  116. */
  117. public ShadowEffect(String propertyName, PropertyValueList list)
  118. throws PropertyException
  119. {
  120. this(PropNames.getPropertyIndex(propertyName), list);
  121. }
  122. /**
  123. * Pick up two <tt>Numeric</tt> entries through the <tt>Iterator</tt>
  124. * and assign them to the inlineOffset and blockOffset
  125. * @param entries an <tt>Iterator</tt> already initialised elsewhere
  126. */
  127. private void setInlineAndBlock(Iterator entries)
  128. throws PropertyException
  129. {
  130. Object entry;
  131. entry = entries.next();
  132. if (! (entry instanceof Numeric))
  133. throw new PropertyException
  134. ("Numeric value expected for text-shadow");
  135. inlineOffset = (Numeric)entry;
  136. entry = entries.next();
  137. if (! (entry instanceof Numeric))
  138. throw new PropertyException
  139. ("Numeric value expected for text-shadow");
  140. blockOffset = (Numeric)entry;
  141. }
  142. /**
  143. * Pick up three <tt>Numeric</tt> entries through the <tt>Iterator</tt>
  144. * and assign them to the inlineOffset, blockOffset and blurRadius
  145. * @param entries an <tt>Iterator</tt> already initialised elsewhere
  146. */
  147. private void setInlineBlockAndBlur(Iterator entries)
  148. throws PropertyException
  149. {
  150. Object entry;
  151. setInlineAndBlock(entries);
  152. entry = entries.next();
  153. if (! (entry instanceof Numeric))
  154. throw new PropertyException
  155. ("Numeric blur radius value expected for text-shadow");
  156. blurRadius = (Numeric)entry;
  157. }
  158. /**
  159. * Set the shadow color from the next entry returned by the entries
  160. * iterator. A color entry must be either a <tt>ColorType</tt> already,
  161. * or an <tt>NCName</tt> containing one of the standard XSL color
  162. * keywords.
  163. * @param entries an <tt>Iterator</tt>.
  164. */
  165. private void setColor(Iterator entries) throws PropertyException {
  166. Object entry;
  167. entry = entries.next();
  168. if (entry instanceof ColorType) {
  169. color = (ColorType)entry;
  170. } else if (entry instanceof NCName) {
  171. color = new ColorType
  172. (property,
  173. propertyConsts.getEnumIndex
  174. (PropNames.TEXT_SHADOW, ((NCName)entry).getNCName()));
  175. }
  176. }
  177. /**
  178. * Validate this <i>ShadowEffect</i>. Check that it is allowed on the
  179. * associated property. A <i>ShadowEffect</i> may also encode a single
  180. * character; i.e. a <tt>&lt;character&gt;</tt> type. If the
  181. * validation against <i>LITERAL</i> fails, try <i>CHARACTER_T</i>.
  182. */
  183. public void validate() throws PropertyException {
  184. if (property != PropNames.TEXT_SHADOW)
  185. throw new PropertyException
  186. ("ShadowEffects only valid for text-shadow'");
  187. }
  188. }