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.

AbstractPageNumberCitationLayoutManager.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 org.apache.fop.area.PageViewport;
  20. import org.apache.fop.area.Resolvable;
  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.area.inline.UnresolvedPageNumber;
  25. import org.apache.fop.fo.flow.AbstractPageNumberCitation;
  26. import org.apache.fop.fonts.Font;
  27. import org.apache.fop.fonts.FontInfo;
  28. import org.apache.fop.fonts.FontTriplet;
  29. import org.apache.fop.layoutmgr.LayoutContext;
  30. import org.apache.fop.layoutmgr.PositionIterator;
  31. import org.apache.fop.layoutmgr.TraitSetter;
  32. /**
  33. * LayoutManager for the fo:page-number-citation(-last) formatting object
  34. */
  35. public abstract class AbstractPageNumberCitationLayoutManager extends LeafNodeLayoutManager {
  36. /** The page number citation object */
  37. protected AbstractPageNumberCitation fobj;
  38. /** Font for the page-number-citation */
  39. protected Font font;
  40. /** Indicates whether the page referred to by the citation has been resolved yet */
  41. protected boolean resolved = false;
  42. /**
  43. * Constructor
  44. *
  45. * @param node the formatting object that creates this area
  46. * TODO better retrieval of font info
  47. */
  48. public AbstractPageNumberCitationLayoutManager(AbstractPageNumberCitation node) {
  49. super(node);
  50. fobj = node;
  51. }
  52. /** {@inheritDoc} */
  53. public void initialize() {
  54. FontInfo fi = fobj.getFOEventHandler().getFontInfo();
  55. FontTriplet[] fontkeys = fobj.getCommonFont().getFontState(fi);
  56. font = fi.getFontInstance(fontkeys[0], fobj.getCommonFont().fontSize.getValue(this));
  57. setCommonBorderPaddingBackground(fobj.getCommonBorderPaddingBackground());
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. protected AlignmentContext makeAlignmentContext(LayoutContext context) {
  63. return new AlignmentContext(
  64. font
  65. , fobj.getLineHeight().getOptimum(this).getLength().getValue(this)
  66. , fobj.getAlignmentAdjust()
  67. , fobj.getAlignmentBaseline()
  68. , fobj.getBaselineShift()
  69. , fobj.getDominantBaseline()
  70. , context.getAlignmentContext()
  71. );
  72. }
  73. /** {@inheritDoc} */
  74. public InlineArea get(LayoutContext context) {
  75. curArea = getPageNumberCitationInlineArea();
  76. return curArea;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. * , LayoutContext)
  81. */
  82. public void addAreas(PositionIterator posIter, LayoutContext context) {
  83. super.addAreas(posIter, context);
  84. if (!resolved) {
  85. getPSLM().addUnresolvedArea(fobj.getRefId(), (Resolvable) curArea);
  86. }
  87. }
  88. /**
  89. * If id can be resolved then simply return a text area, otherwise
  90. * return a resolvable area
  91. *
  92. * @return a corresponding InlineArea
  93. */
  94. private InlineArea getPageNumberCitationInlineArea() {
  95. PageViewport page = getPSLM().getFirstPVWithID(fobj.getRefId());
  96. TextArea text;
  97. if (page != null) {
  98. String str = page.getPageNumberString();
  99. // get page string from parent, build area
  100. text = new TextArea();
  101. int width = getStringWidth(str);
  102. text.addWord(str, 0);
  103. text.setIPD(width);
  104. resolved = true;
  105. } else {
  106. resolved = false;
  107. text = new UnresolvedPageNumber(fobj.getRefId(), font);
  108. String str = "MMM"; // reserve three spaces for page number
  109. int width = getStringWidth(str);
  110. text.setIPD(width);
  111. }
  112. updateTextAreaTraits(text);
  113. return text;
  114. }
  115. /**
  116. * Updates the traits for the generated text area.
  117. * @param text the text area
  118. */
  119. protected void updateTextAreaTraits(TextArea text) {
  120. TraitSetter.setProducerID(text, fobj.getId());
  121. text.setBPD(font.getAscender() - font.getDescender());
  122. text.setBaselineOffset(font.getAscender());
  123. TraitSetter.addFontTraits(text, font);
  124. text.addTrait(Trait.COLOR, fobj.getColor());
  125. TraitSetter.addStructureTreeElement(text, fobj.getStructureTreeElement());
  126. TraitSetter.addTextDecoration(text, fobj.getTextDecoration());
  127. }
  128. /**
  129. * @param str string to be measured
  130. * @return width (in millipoints ??) of the string
  131. */
  132. protected int getStringWidth(String str) {
  133. int width = 0;
  134. for (int count = 0; count < str.length(); count++) {
  135. width += font.getCharWidth(str.charAt(count));
  136. }
  137. return width;
  138. }
  139. }