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.

PageNumberLayoutManager.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Trait;
  20. import org.apache.fop.area.inline.InlineArea;
  21. import org.apache.fop.area.inline.TextArea;
  22. import org.apache.fop.fo.flow.PageNumber;
  23. import org.apache.fop.fonts.Font;
  24. import org.apache.fop.fonts.FontInfo;
  25. import org.apache.fop.fonts.FontTriplet;
  26. import org.apache.fop.layoutmgr.LayoutContext;
  27. import org.apache.fop.layoutmgr.TraitSetter;
  28. import org.apache.fop.traits.MinOptMax;
  29. /**
  30. * LayoutManager for the fo:page-number formatting object
  31. */
  32. public class PageNumberLayoutManager extends LeafNodeLayoutManager {
  33. private PageNumber fobj;
  34. private Font font;
  35. /**
  36. * Constructor
  37. *
  38. * @param node the fo:page-number formatting object that creates the area
  39. * TODO better null checking of node, font
  40. */
  41. public PageNumberLayoutManager(PageNumber node) {
  42. super(node);
  43. fobj = node;
  44. }
  45. /** {@inheritDoc} */
  46. public void initialize() {
  47. FontInfo fi = fobj.getFOEventHandler().getFontInfo();
  48. FontTriplet[] fontkeys = fobj.getCommonFont().getFontState(fi);
  49. font = fi.getFontInstance(fontkeys[0], fobj.getCommonFont().fontSize.getValue(this));
  50. setCommonBorderPaddingBackground(fobj.getCommonBorderPaddingBackground());
  51. }
  52. /**
  53. * {@inheritDoc}
  54. * #makeAlignmentContext(LayoutContext)
  55. */
  56. protected AlignmentContext makeAlignmentContext(LayoutContext context) {
  57. return new AlignmentContext(
  58. font
  59. , fobj.getLineHeight().getOptimum(this).getLength().getValue(this)
  60. , fobj.getAlignmentAdjust()
  61. , fobj.getAlignmentBaseline()
  62. , fobj.getBaselineShift()
  63. , fobj.getDominantBaseline()
  64. , context.getAlignmentContext()
  65. );
  66. }
  67. /** {@inheritDoc} */
  68. public InlineArea get(LayoutContext context) {
  69. // get page string from parent, build area
  70. TextArea text = new TextArea();
  71. String str = getCurrentPV().getPageNumberString();
  72. int width = getStringWidth(str);
  73. text.addWord(str, 0);
  74. text.setIPD(width);
  75. text.setBPD(font.getAscender() - font.getDescender());
  76. text.setBaselineOffset(font.getAscender());
  77. TraitSetter.addFontTraits(text, font);
  78. text.addTrait(Trait.COLOR, fobj.getColor());
  79. TraitSetter.addStructureTreeElement(text, fobj.getStructureTreeElement());
  80. TraitSetter.addTextDecoration(text, fobj.getTextDecoration());
  81. return text;
  82. }
  83. /** {@inheritDoc} */
  84. protected InlineArea getEffectiveArea() {
  85. TextArea baseArea = (TextArea)curArea;
  86. //TODO Maybe replace that with a clone() call or better, a copy constructor
  87. //TODO or even better: delay area creation until addAreas() stage
  88. //TextArea is cloned because the LM is reused in static areas and the area can't be.
  89. TextArea ta = new TextArea();
  90. TraitSetter.setProducerID(ta, fobj.getId());
  91. ta.setIPD(baseArea.getIPD());
  92. ta.setBPD(baseArea.getBPD());
  93. ta.setOffset(baseArea.getOffset());
  94. ta.setBaselineOffset(baseArea.getBaselineOffset());
  95. ta.addTrait(Trait.COLOR, fobj.getColor()); //only to initialize the trait map
  96. ta.getTraits().putAll(baseArea.getTraits());
  97. updateContent(ta);
  98. return ta;
  99. }
  100. private void updateContent(TextArea area) {
  101. // get the page number of the page actually being built
  102. area.removeText();
  103. area.addWord(getCurrentPV().getPageNumberString(), 0);
  104. // update the ipd of the area
  105. area.handleIPDVariation(getStringWidth(area.getText()) - area.getIPD());
  106. // update the width stored in the AreaInfo object
  107. areaInfo.ipdArea = MinOptMax.getInstance(area.getIPD());
  108. }
  109. /**
  110. * @param str string to be measured
  111. * @return width of the string
  112. */
  113. private int getStringWidth(String str) {
  114. int width = 0;
  115. for (int count = 0; count < str.length(); count++) {
  116. width += font.getCharWidth(str.charAt(count));
  117. }
  118. return width;
  119. }
  120. }