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.

CharacterLayoutManager.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.layoutmgr.inline;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import org.apache.fop.area.Trait;
  22. import org.apache.fop.area.inline.TextArea;
  23. import org.apache.fop.fo.flow.Character;
  24. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  25. import org.apache.fop.fonts.Font;
  26. import org.apache.fop.fonts.FontSelector;
  27. import org.apache.fop.layoutmgr.InlineKnuthSequence;
  28. import org.apache.fop.layoutmgr.KnuthElement;
  29. import org.apache.fop.layoutmgr.KnuthGlue;
  30. import org.apache.fop.layoutmgr.KnuthPenalty;
  31. import org.apache.fop.layoutmgr.KnuthSequence;
  32. import org.apache.fop.layoutmgr.LayoutContext;
  33. import org.apache.fop.layoutmgr.LeafPosition;
  34. import org.apache.fop.layoutmgr.Position;
  35. import org.apache.fop.layoutmgr.TraitSetter;
  36. import org.apache.fop.traits.MinOptMax;
  37. import org.apache.fop.traits.SpaceVal;
  38. import org.apache.fop.util.CharUtilities;
  39. /**
  40. * LayoutManager for the fo:character formatting object
  41. */
  42. public class CharacterLayoutManager extends LeafNodeLayoutManager {
  43. private MinOptMax letterSpaceIPD;
  44. private int hyphIPD;
  45. private Font font;
  46. private CommonBorderPaddingBackground borderProps = null;
  47. /**
  48. * Constructor
  49. *
  50. * @param node the fo:character formatting object
  51. */
  52. public CharacterLayoutManager(Character node) {
  53. super(node);
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public void initialize() {
  58. Character fobj = (Character)this.fobj;
  59. font = FontSelector.selectFontForCharacter(fobj, this);
  60. SpaceVal ls = SpaceVal.makeLetterSpacing(fobj.getLetterSpacing());
  61. letterSpaceIPD = ls.getSpace();
  62. hyphIPD = fobj.getCommonHyphenation().getHyphIPD(font);
  63. borderProps = fobj.getCommonBorderPaddingBackground();
  64. setCommonBorderPaddingBackground(borderProps);
  65. TextArea chArea = getCharacterInlineArea(fobj);
  66. chArea.setBaselineOffset(font.getAscender());
  67. setCurrentArea(chArea);
  68. }
  69. private TextArea getCharacterInlineArea(Character node) {
  70. TextArea text = new TextArea();
  71. char ch = node.getCharacter();
  72. if (CharUtilities.isAnySpace(ch)) {
  73. // add space unless it's zero-width:
  74. if (!CharUtilities.isZeroWidthSpace(ch)) {
  75. text.addSpace(ch, 0, CharUtilities.isAdjustableSpace(ch));
  76. }
  77. } else {
  78. text.addWord(String.valueOf(ch), 0);
  79. }
  80. TraitSetter.setProducerID(text, node.getId());
  81. TraitSetter.addTextDecoration(text, node.getTextDecoration());
  82. TraitSetter.addStructureTreeElement(text, node.getStructureTreeElement());
  83. return text;
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public List getNextKnuthElements(LayoutContext context, int alignment) {
  88. MinOptMax ipd;
  89. curArea = get(context);
  90. KnuthSequence seq = new InlineKnuthSequence();
  91. if (curArea == null) {
  92. setFinished(true);
  93. return null;
  94. }
  95. Character fobj = (Character)this.fobj;
  96. ipd = MinOptMax.getInstance(font.getCharWidth(fobj.getCharacter()));
  97. curArea.setIPD(ipd.getOpt());
  98. curArea.setBPD(font.getAscender() - font.getDescender());
  99. TraitSetter.addFontTraits(curArea, font);
  100. curArea.addTrait(Trait.COLOR, fobj.getColor());
  101. // TODO: may need some special handling for fo:character
  102. alignmentContext = new AlignmentContext(font
  103. , font.getFontSize()
  104. , fobj.getAlignmentAdjust()
  105. , fobj.getAlignmentBaseline()
  106. , fobj.getBaselineShift()
  107. , fobj.getDominantBaseline()
  108. , context.getAlignmentContext());
  109. addKnuthElementsForBorderPaddingStart(seq);
  110. // create the AreaInfo object to store the computed values
  111. areaInfo = new AreaInfo((short) 0, ipd, false, alignmentContext);
  112. // node is a fo:Character
  113. if (letterSpaceIPD.isStiff()) {
  114. // constant letter space, only return a box
  115. seq.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(), areaInfo.alignmentContext,
  116. notifyPos(new LeafPosition(this, 0)), false));
  117. } else {
  118. // adjustable letter space, return a sequence of elements;
  119. // at the moment the character is supposed to have no letter spaces,
  120. // but returning this sequence allows us to change only one element
  121. // if addALetterSpaceTo() is called
  122. seq.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(), areaInfo.alignmentContext,
  123. notifyPos(new LeafPosition(this, 0)), false));
  124. seq.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
  125. new LeafPosition(this, -1), true));
  126. seq.add(new KnuthGlue(0, 0, 0,
  127. new LeafPosition(this, -1), true));
  128. seq.add(new KnuthInlineBox(0, null,
  129. notifyPos(new LeafPosition(this, -1)), true));
  130. }
  131. addKnuthElementsForBorderPaddingEnd(seq);
  132. LinkedList<KnuthSequence> returnList = new LinkedList<KnuthSequence>();
  133. returnList.add(seq);
  134. setFinished(true);
  135. return returnList;
  136. }
  137. /** {@inheritDoc} */
  138. @Override
  139. public String getWordChars(Position pos) {
  140. return ((TextArea) curArea).getText();
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public void hyphenate(Position pos, HyphContext hc) {
  145. if (hc.getNextHyphPoint() == 1) {
  146. // the character ends a syllable
  147. areaInfo.isHyphenated = true;
  148. somethingChanged = true;
  149. } else {
  150. // hc.getNextHyphPoint() returned -1 (no more hyphenation points)
  151. // or a number > 1;
  152. // the character does not end a syllable
  153. }
  154. hc.updateOffset(1);
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public boolean applyChanges(List oldList) {
  159. setFinished(false);
  160. return somethingChanged;
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public List getChangedKnuthElements(List oldList, int alignment) {
  165. if (isFinished()) {
  166. return null;
  167. }
  168. LinkedList<KnuthElement> returnList = new LinkedList<KnuthElement>();
  169. addKnuthElementsForBorderPaddingStart(returnList);
  170. if (letterSpaceIPD.isStiff() || areaInfo.letterSpaces == 0) {
  171. // constant letter space, or no letter space
  172. returnList.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(),
  173. areaInfo.alignmentContext,
  174. notifyPos(new LeafPosition(this, 0)), false));
  175. if (areaInfo.isHyphenated) {
  176. returnList.add(new KnuthPenalty(hyphIPD, KnuthPenalty.FLAGGED_PENALTY, true,
  177. new LeafPosition(this, -1), false));
  178. }
  179. } else {
  180. // adjustable letter space
  181. returnList.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt()
  182. - areaInfo.letterSpaces * letterSpaceIPD.getOpt(), areaInfo.alignmentContext,
  183. notifyPos(new LeafPosition(this, 0)), false));
  184. returnList.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
  185. new LeafPosition(this, -1), true));
  186. returnList.add(new KnuthGlue(letterSpaceIPD.mult(areaInfo.letterSpaces),
  187. new LeafPosition(this, -1), true));
  188. returnList.add (
  189. new KnuthInlineBox(0, null, notifyPos(new LeafPosition(this, -1)), true));
  190. if (areaInfo.isHyphenated) {
  191. returnList.add(new KnuthPenalty(hyphIPD, KnuthPenalty.FLAGGED_PENALTY, true,
  192. new LeafPosition(this, -1), false));
  193. }
  194. }
  195. addKnuthElementsForBorderPaddingEnd(returnList);
  196. setFinished(true);
  197. return returnList;
  198. }
  199. }