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 9.2KB

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