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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Trait;
  21. import org.apache.fop.area.inline.InlineArea;
  22. import org.apache.fop.area.inline.TextArea;
  23. import org.apache.fop.area.inline.UnresolvedPageNumber;
  24. import org.apache.fop.fo.flow.AbstractPageNumberCitation;
  25. import org.apache.fop.fonts.Font;
  26. import org.apache.fop.fonts.FontInfo;
  27. import org.apache.fop.fonts.FontTriplet;
  28. import org.apache.fop.layoutmgr.LayoutContext;
  29. import org.apache.fop.layoutmgr.TraitSetter;
  30. import org.apache.fop.traits.MinOptMax;
  31. /**
  32. * LayoutManager for the fo:page-number-citation(-last) formatting object
  33. */
  34. public abstract class AbstractPageNumberCitationLayoutManager extends LeafNodeLayoutManager {
  35. /** The page number citation object */
  36. protected AbstractPageNumberCitation citation;
  37. /** Font for the page-number-citation */
  38. protected Font font;
  39. /** Indicates whether the page referred to by the citation has been resolved yet */
  40. private boolean resolved;
  41. private String citationString;
  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. citation = node;
  51. }
  52. /** {@inheritDoc} */
  53. public void initialize() {
  54. FontInfo fi = citation.getFOEventHandler().getFontInfo();
  55. FontTriplet[] fontkeys = citation.getCommonFont().getFontState(fi);
  56. font = fi.getFontInstance(fontkeys[0], citation.getCommonFont().fontSize.getValue(this));
  57. setCommonBorderPaddingBackground(citation.getCommonBorderPaddingBackground());
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. protected AlignmentContext makeAlignmentContext(LayoutContext context) {
  63. return new AlignmentContext(
  64. font
  65. , citation.getLineHeight().getOptimum(this).getLength().getValue(this)
  66. , citation.getAlignmentAdjust()
  67. , citation.getAlignmentBaseline()
  68. , citation.getBaselineShift()
  69. , citation.getDominantBaseline()
  70. , context.getAlignmentContext()
  71. );
  72. }
  73. @Override
  74. protected MinOptMax getAllocationIPD(int refIPD) {
  75. determineCitationString();
  76. int ipd = getStringWidth(citationString);
  77. return MinOptMax.getInstance(ipd);
  78. }
  79. private void determineCitationString() {
  80. assert citationString == null;
  81. PageViewport page = getCitedPage();
  82. if (page != null) {
  83. resolved = true;
  84. citationString = page.getPageNumberString();
  85. } else {
  86. resolved = false;
  87. citationString = "MMM"; // Use a place holder
  88. }
  89. }
  90. private int getStringWidth(String str) {
  91. int width = 0;
  92. for (int count = 0; count < str.length(); count++) {
  93. width += font.getCharWidth(str.charAt(count));
  94. }
  95. return width;
  96. }
  97. protected abstract PageViewport getCitedPage();
  98. @Override
  99. protected InlineArea getEffectiveArea(LayoutContext layoutContext) {
  100. InlineArea area = getPageNumberCitationArea();
  101. if (!layoutContext.treatAsArtifact()) {
  102. TraitSetter.addStructureTreeElement(area, citation.getStructureTreeElement());
  103. }
  104. return area;
  105. }
  106. private InlineArea getPageNumberCitationArea() {
  107. TextArea text;
  108. if (resolved) {
  109. text = new TextArea();
  110. int bidiLevel = getBidiLevel();
  111. text.setBidiLevel(bidiLevel);
  112. text.addWord(citationString, getStringWidth(citationString), null, null, null, 0);
  113. } else {
  114. UnresolvedPageNumber unresolved = new UnresolvedPageNumber(citation.getRefId(), font,
  115. getReferenceType());
  116. getPSLM().addUnresolvedArea(citation.getRefId(), unresolved);
  117. text = unresolved;
  118. }
  119. text.setChangeBarList(getChangeBarList());
  120. setTraits(text);
  121. return text;
  122. }
  123. /**
  124. * @return {@link org.apache.fop.area.inline.UnresolvedPageNumber#FIRST} or
  125. * {@link org.apache.fop.area.inline.UnresolvedPageNumber#LAST}
  126. */
  127. protected abstract boolean getReferenceType();
  128. private void setTraits(TextArea text) {
  129. TraitSetter.setProducerID(text, citation.getId());
  130. int bidiLevel = getBidiLevel();
  131. text.setBidiLevel(bidiLevel);
  132. int width = getStringWidth(citationString); // TODO: [GA] !I18N!
  133. text.setIPD(width); // TODO: [GA] !I18N!
  134. text.setBPD(font.getAscender() - font.getDescender());
  135. text.setBaselineOffset(font.getAscender());
  136. TraitSetter.addFontTraits(text, font);
  137. text.addTrait(Trait.COLOR, citation.getColor());
  138. TraitSetter.addTextDecoration(text, citation.getTextDecoration());
  139. }
  140. /**
  141. * @return bidi level governing abstract page number citation
  142. */
  143. protected int getBidiLevel() {
  144. return citation.getBidiLevel();
  145. }
  146. }