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.

Character.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo.flow;
  19. import java.awt.Color;
  20. import java.util.NoSuchElementException;
  21. import org.xml.sax.Locator;
  22. import org.apache.fop.accessibility.StructureTreeElement;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.datatypes.Length;
  25. import org.apache.fop.fo.CharIterator;
  26. import org.apache.fop.fo.FONode;
  27. import org.apache.fop.fo.FObj;
  28. import org.apache.fop.fo.PropertyList;
  29. import org.apache.fop.fo.ValidationException;
  30. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  31. import org.apache.fop.fo.properties.CommonFont;
  32. import org.apache.fop.fo.properties.CommonHyphenation;
  33. import org.apache.fop.fo.properties.CommonTextDecoration;
  34. import org.apache.fop.fo.properties.KeepProperty;
  35. import org.apache.fop.fo.properties.Property;
  36. import org.apache.fop.fo.properties.SpaceProperty;
  37. import org.apache.fop.fo.properties.StructureTreeElementHolder;
  38. /**
  39. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_character">
  40. * <code>fo:character</code></a> object.
  41. */
  42. public class Character extends FObj implements StructureTreeElementHolder {
  43. // The value of properties relevant for fo:character.
  44. private CommonBorderPaddingBackground commonBorderPaddingBackground;
  45. private CommonFont commonFont;
  46. private CommonHyphenation commonHyphenation;
  47. private Length alignmentAdjust;
  48. private int alignmentBaseline;
  49. private Length baselineShift;
  50. private char character;
  51. private Color color;
  52. private int dominantBaseline;
  53. private KeepProperty keepWithNext;
  54. private KeepProperty keepWithPrevious;
  55. private Property letterSpacing;
  56. private SpaceProperty lineHeight;
  57. /** Holds the text decoration values. May be null */
  58. private CommonTextDecoration textDecoration;
  59. // private ToBeImplementedProperty textShadow;
  60. private Property wordSpacing;
  61. private StructureTreeElement structureTreeElement;
  62. // Unused but valid items, commented out for performance:
  63. // private CommonAural commonAural;
  64. // private CommonMarginInline commonMarginInline;
  65. // private CommonRelativePosition commonRelativePosition;
  66. // private ToBeImplementedProperty glyphOrientationHorizontal;
  67. // private ToBeImplementedProperty glyphOrientationVertical;
  68. // private int treatAsWordSpace;
  69. // private Length textDepth;
  70. // private Length textAltitude;
  71. // private int scoreSpaces;
  72. // private int suppressAtLineBreak;
  73. // private int textTransform;
  74. // private int visibility;
  75. // End of property values
  76. /** constant indicating that the character is OK */
  77. public static final int OK = 0;
  78. /** constant indicating that the character does not fit */
  79. public static final int DOESNOT_FIT = 1;
  80. /**
  81. * @param parent {@link FONode} that is the parent of this object
  82. */
  83. public Character(FONode parent) {
  84. super(parent);
  85. }
  86. /** {@inheritDoc} */
  87. public void bind(PropertyList pList) throws FOPException {
  88. super.bind(pList);
  89. commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
  90. commonFont = pList.getFontProps();
  91. commonHyphenation = pList.getHyphenationProps();
  92. alignmentAdjust = pList.get(PR_ALIGNMENT_ADJUST).getLength();
  93. alignmentBaseline = pList.get(PR_ALIGNMENT_BASELINE).getEnum();
  94. baselineShift = pList.get(PR_BASELINE_SHIFT).getLength();
  95. character = pList.get(PR_CHARACTER).getCharacter();
  96. color = pList.get(PR_COLOR).getColor(getUserAgent());
  97. dominantBaseline = pList.get(PR_DOMINANT_BASELINE).getEnum();
  98. keepWithNext = pList.get(PR_KEEP_WITH_NEXT).getKeep();
  99. keepWithPrevious = pList.get(PR_KEEP_WITH_PREVIOUS).getKeep();
  100. letterSpacing = pList.get(PR_LETTER_SPACING);
  101. lineHeight = pList.get(PR_LINE_HEIGHT).getSpace();
  102. textDecoration = pList.getTextDecorationProps();
  103. wordSpacing = pList.get(PR_WORD_SPACING);
  104. }
  105. /** {@inheritDoc} */
  106. protected void startOfNode() throws FOPException {
  107. super.startOfNode();
  108. getFOEventHandler().character(this);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. * <br>XSL Content Model: empty
  113. */
  114. protected void validateChildNode(Locator loc, String nsURI, String localName)
  115. throws ValidationException {
  116. if (FO_URI.equals(nsURI)) {
  117. invalidChildError(loc, nsURI, localName);
  118. }
  119. }
  120. /** {@inheritDoc} */
  121. public CharIterator charIterator() {
  122. return new FOCharIterator(this);
  123. }
  124. /** @return the Common Border, Padding, and Background Properties */
  125. public CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  126. return commonBorderPaddingBackground;
  127. }
  128. /** @return the Common Font Properties */
  129. public CommonFont getCommonFont() {
  130. return commonFont;
  131. }
  132. /** @return the Common Hyphenation Properties */
  133. public CommonHyphenation getCommonHyphenation() {
  134. return commonHyphenation;
  135. }
  136. /** @return the "character" property */
  137. public char getCharacter() {
  138. return character;
  139. }
  140. /** @return the "color" property */
  141. public Color getColor() {
  142. return color;
  143. }
  144. /** @return the "alignment-adjust" property */
  145. public Length getAlignmentAdjust() {
  146. return alignmentAdjust;
  147. }
  148. /** @return the "alignment-baseline" property */
  149. public int getAlignmentBaseline() {
  150. return alignmentBaseline;
  151. }
  152. /** @return the "baseline-shift" property */
  153. public Length getBaselineShift() {
  154. return baselineShift;
  155. }
  156. /** @return the "dominant-baseline" property */
  157. public int getDominantBaseline() {
  158. return dominantBaseline;
  159. }
  160. /** @return the "letter-spacing" property */
  161. public Property getLetterSpacing() {
  162. return letterSpacing;
  163. }
  164. /** @return the "line-height" property */
  165. public SpaceProperty getLineHeight() {
  166. return lineHeight;
  167. }
  168. /** @return the "text-decoration" property. */
  169. public CommonTextDecoration getTextDecoration() {
  170. return textDecoration;
  171. }
  172. /** @return the "word-spacing" property */
  173. public Property getWordSpacing() {
  174. return wordSpacing;
  175. }
  176. /** @return the "keep-with-next" property */
  177. public KeepProperty getKeepWithNext() {
  178. return keepWithNext;
  179. }
  180. /** @return the "keep-with-previous" property */
  181. public KeepProperty getKeepWithPrevious() {
  182. return keepWithPrevious;
  183. }
  184. @Override
  185. public void setStructureTreeElement(StructureTreeElement structureTreeElement) {
  186. this.structureTreeElement = structureTreeElement;
  187. }
  188. /** {@inheritDoc} */
  189. public StructureTreeElement getStructureTreeElement() {
  190. return structureTreeElement;
  191. }
  192. /** {@inheritDoc} */
  193. public String getLocalName() {
  194. return "character";
  195. }
  196. /**
  197. * {@inheritDoc}
  198. * @return {@link org.apache.fop.fo.Constants#FO_CHARACTER}
  199. */
  200. public int getNameId() {
  201. return FO_CHARACTER;
  202. }
  203. private class FOCharIterator extends CharIterator {
  204. private boolean bFirst = true;
  205. private Character foChar;
  206. FOCharIterator(Character foChar) {
  207. this.foChar = foChar;
  208. }
  209. public boolean hasNext() {
  210. return bFirst;
  211. }
  212. public char nextChar() {
  213. if (bFirst) {
  214. bFirst = false;
  215. return foChar.character;
  216. } else {
  217. throw new NoSuchElementException();
  218. }
  219. }
  220. public void remove() {
  221. foChar.parent.removeChild(foChar);
  222. }
  223. public void replaceChar(char c) {
  224. foChar.character = c;
  225. }
  226. }
  227. }