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.fo.flow.PageNumber;
  20. import org.apache.fop.area.inline.InlineArea;
  21. import org.apache.fop.area.inline.TextArea;
  22. import org.apache.fop.area.Trait;
  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.addTextDecoration(text, fobj.getTextDecoration());
  80. return text;
  81. }
  82. /** {@inheritDoc} */
  83. protected InlineArea getEffectiveArea() {
  84. TextArea baseArea = (TextArea)curArea;
  85. //TODO Maybe replace that with a clone() call or better, a copy constructor
  86. //TODO or even better: delay area creation until addAreas() stage
  87. //TextArea is cloned because the LM is reused in static areas and the area can't be.
  88. TextArea ta = new TextArea();
  89. TraitSetter.setProducerID(ta, fobj.getId());
  90. ta.setIPD(baseArea.getIPD());
  91. ta.setBPD(baseArea.getBPD());
  92. ta.setOffset(baseArea.getOffset());
  93. ta.setBaselineOffset(baseArea.getBaselineOffset());
  94. ta.addTrait(Trait.COLOR, fobj.getColor()); //only to initialize the trait map
  95. ta.getTraits().putAll(baseArea.getTraits());
  96. updateContent(ta);
  97. return ta;
  98. }
  99. private void updateContent(TextArea area) {
  100. // get the page number of the page actually being built
  101. area.removeText();
  102. area.addWord(getCurrentPV().getPageNumberString(), 0);
  103. // update the ipd of the area
  104. area.handleIPDVariation(getStringWidth(area.getText()) - area.getIPD());
  105. // update the width stored in the AreaInfo object
  106. areaInfo.ipdArea = new MinOptMax(area.getIPD());
  107. }
  108. /**
  109. * @param str string to be measured
  110. * @return width of the string
  111. */
  112. private int getStringWidth(String str) {
  113. int width = 0;
  114. for (int count = 0; count < str.length(); count++) {
  115. width += font.getCharWidth(str.charAt(count));
  116. }
  117. return width;
  118. }
  119. }