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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 java.util.Stack;
  22. import org.xml.sax.Locator;
  23. import org.apache.fop.accessibility.StructureTreeElement;
  24. import org.apache.fop.apps.FOPException;
  25. import org.apache.fop.complexscripts.bidi.DelimitedTextRange;
  26. import org.apache.fop.datatypes.Length;
  27. import org.apache.fop.fo.CharIterator;
  28. import org.apache.fop.fo.FONode;
  29. import org.apache.fop.fo.FObj;
  30. import org.apache.fop.fo.PropertyList;
  31. import org.apache.fop.fo.ValidationException;
  32. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  33. import org.apache.fop.fo.properties.CommonFont;
  34. import org.apache.fop.fo.properties.CommonHyphenation;
  35. import org.apache.fop.fo.properties.CommonTextDecoration;
  36. import org.apache.fop.fo.properties.KeepProperty;
  37. import org.apache.fop.fo.properties.Property;
  38. import org.apache.fop.fo.properties.SpaceProperty;
  39. import org.apache.fop.fo.properties.StructureTreeElementHolder;
  40. /**
  41. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_character">
  42. * <code>fo:character</code></a> object.
  43. */
  44. public class Character extends FObj implements StructureTreeElementHolder {
  45. // The value of properties relevant for fo:character.
  46. private CommonBorderPaddingBackground commonBorderPaddingBackground;
  47. private CommonFont commonFont;
  48. private CommonHyphenation commonHyphenation;
  49. private Length alignmentAdjust;
  50. private int alignmentBaseline;
  51. private Length baselineShift;
  52. private char character;
  53. private Color color;
  54. private int dominantBaseline;
  55. private KeepProperty keepWithNext;
  56. private KeepProperty keepWithPrevious;
  57. private Property letterSpacing;
  58. private SpaceProperty lineHeight;
  59. /** Holds the text decoration values. May be null */
  60. private CommonTextDecoration textDecoration;
  61. // private ToBeImplementedProperty textShadow;
  62. private Property wordSpacing;
  63. private StructureTreeElement structureTreeElement;
  64. // Unused but valid items, commented out for performance:
  65. // private CommonAural commonAural;
  66. // private CommonMarginInline commonMarginInline;
  67. // private CommonRelativePosition commonRelativePosition;
  68. // private ToBeImplementedProperty glyphOrientationHorizontal;
  69. // private ToBeImplementedProperty glyphOrientationVertical;
  70. // private int treatAsWordSpace;
  71. // private Length textDepth;
  72. // private Length textAltitude;
  73. // private int scoreSpaces;
  74. // private int suppressAtLineBreak;
  75. // private int textTransform;
  76. // private int visibility;
  77. // End of property values
  78. /** constant indicating that the character is OK */
  79. public static final int OK = 0;
  80. /** constant indicating that the character does not fit */
  81. public static final int DOESNOT_FIT = 1;
  82. /**
  83. * @param parent {@link FONode} that is the parent of this object
  84. */
  85. public Character(FONode parent) {
  86. super(parent);
  87. }
  88. /** {@inheritDoc} */
  89. public void bind(PropertyList pList) throws FOPException {
  90. super.bind(pList);
  91. commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
  92. commonFont = pList.getFontProps();
  93. commonHyphenation = pList.getHyphenationProps();
  94. alignmentAdjust = pList.get(PR_ALIGNMENT_ADJUST).getLength();
  95. alignmentBaseline = pList.get(PR_ALIGNMENT_BASELINE).getEnum();
  96. baselineShift = pList.get(PR_BASELINE_SHIFT).getLength();
  97. character = pList.get(PR_CHARACTER).getCharacter();
  98. color = pList.get(PR_COLOR).getColor(getUserAgent());
  99. dominantBaseline = pList.get(PR_DOMINANT_BASELINE).getEnum();
  100. keepWithNext = pList.get(PR_KEEP_WITH_NEXT).getKeep();
  101. keepWithPrevious = pList.get(PR_KEEP_WITH_PREVIOUS).getKeep();
  102. letterSpacing = pList.get(PR_LETTER_SPACING);
  103. lineHeight = pList.get(PR_LINE_HEIGHT).getSpace();
  104. textDecoration = pList.getTextDecorationProps();
  105. wordSpacing = pList.get(PR_WORD_SPACING);
  106. }
  107. /** {@inheritDoc} */
  108. protected void startOfNode() throws FOPException {
  109. super.startOfNode();
  110. getFOEventHandler().character(this);
  111. }
  112. /**
  113. * {@inheritDoc}
  114. * <br>XSL Content Model: empty
  115. */
  116. protected void validateChildNode(Locator loc, String nsURI, String localName)
  117. throws ValidationException {
  118. if (FO_URI.equals(nsURI)) {
  119. invalidChildError(loc, nsURI, localName);
  120. }
  121. }
  122. /** {@inheritDoc} */
  123. public CharIterator charIterator() {
  124. return new FOCharIterator(this);
  125. }
  126. /** @return the Common Border, Padding, and Background Properties */
  127. public CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  128. return commonBorderPaddingBackground;
  129. }
  130. /** @return the Common Font Properties */
  131. public CommonFont getCommonFont() {
  132. return commonFont;
  133. }
  134. /** @return the Common Hyphenation Properties */
  135. public CommonHyphenation getCommonHyphenation() {
  136. return commonHyphenation;
  137. }
  138. /** @return the "character" property */
  139. public char getCharacter() {
  140. return character;
  141. }
  142. /** @return the "color" property */
  143. public Color getColor() {
  144. return color;
  145. }
  146. /** @return the "alignment-adjust" property */
  147. public Length getAlignmentAdjust() {
  148. return alignmentAdjust;
  149. }
  150. /** @return the "alignment-baseline" property */
  151. public int getAlignmentBaseline() {
  152. return alignmentBaseline;
  153. }
  154. /** @return the "baseline-shift" property */
  155. public Length getBaselineShift() {
  156. return baselineShift;
  157. }
  158. /** @return the "dominant-baseline" property */
  159. public int getDominantBaseline() {
  160. return dominantBaseline;
  161. }
  162. /** @return the "letter-spacing" property */
  163. public Property getLetterSpacing() {
  164. return letterSpacing;
  165. }
  166. /** @return the "line-height" property */
  167. public SpaceProperty getLineHeight() {
  168. return lineHeight;
  169. }
  170. /** @return the "text-decoration" property. */
  171. public CommonTextDecoration getTextDecoration() {
  172. return textDecoration;
  173. }
  174. /** @return the "word-spacing" property */
  175. public Property getWordSpacing() {
  176. return wordSpacing;
  177. }
  178. /** @return the "keep-with-next" property */
  179. public KeepProperty getKeepWithNext() {
  180. return keepWithNext;
  181. }
  182. /** @return the "keep-with-previous" property */
  183. public KeepProperty getKeepWithPrevious() {
  184. return keepWithPrevious;
  185. }
  186. @Override
  187. public void setStructureTreeElement(StructureTreeElement structureTreeElement) {
  188. this.structureTreeElement = structureTreeElement;
  189. }
  190. /** {@inheritDoc} */
  191. public StructureTreeElement getStructureTreeElement() {
  192. return structureTreeElement;
  193. }
  194. /** {@inheritDoc} */
  195. public String getLocalName() {
  196. return "character";
  197. }
  198. /**
  199. * {@inheritDoc}
  200. * @return {@link org.apache.fop.fo.Constants#FO_CHARACTER}
  201. */
  202. public int getNameId() {
  203. return FO_CHARACTER;
  204. }
  205. @Override
  206. public boolean isDelimitedTextRangeBoundary ( int boundary ) {
  207. return false;
  208. }
  209. @Override
  210. protected Stack collectDelimitedTextRanges ( Stack ranges, DelimitedTextRange currentRange ) {
  211. if ( currentRange != null ) {
  212. currentRange.append ( charIterator(), this );
  213. }
  214. return ranges;
  215. }
  216. private class FOCharIterator extends CharIterator {
  217. private boolean bFirst = true;
  218. private Character foChar;
  219. FOCharIterator(Character foChar) {
  220. this.foChar = foChar;
  221. }
  222. public boolean hasNext() {
  223. return bFirst;
  224. }
  225. public char nextChar() {
  226. if (bFirst) {
  227. bFirst = false;
  228. return foChar.character;
  229. } else {
  230. throw new NoSuchElementException();
  231. }
  232. }
  233. public void remove() {
  234. foChar.parent.removeChild(foChar);
  235. }
  236. public void replaceChar(char c) {
  237. foChar.character = c;
  238. }
  239. }
  240. }