001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PageNumberLayoutManager.java 453920 2006-10-07 14:29:54Z spepping $ */
019:
020: package org.apache.fop.layoutmgr.inline;
021:
022: import org.apache.fop.fo.flow.PageNumber;
023: import org.apache.fop.area.inline.InlineArea;
024: import org.apache.fop.area.inline.TextArea;
025: import org.apache.fop.area.Trait;
026: import org.apache.fop.fonts.Font;
027: import org.apache.fop.layoutmgr.LayoutContext;
028: import org.apache.fop.layoutmgr.TraitSetter;
029: import org.apache.fop.traits.MinOptMax;
030:
031: /**
032: * LayoutManager for the fo:page-number formatting object
033: */
034: public class PageNumberLayoutManager extends LeafNodeLayoutManager {
035:
036: private PageNumber fobj;
037: private Font font;
038:
039: /**
040: * Constructor
041: *
042: * @param node the fo:page-number formatting object that creates the area
043: * @todo better null checking of node, font
044: */
045: public PageNumberLayoutManager(PageNumber node) {
046: super (node);
047: fobj = node;
048: }
049:
050: /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#get(LayoutContext) */
051: public void initialize() {
052: font = fobj.getCommonFont().getFontState(
053: fobj.getFOEventHandler().getFontInfo(), this );
054: setCommonBorderPaddingBackground(fobj
055: .getCommonBorderPaddingBackground());
056: }
057:
058: /**
059: * @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager
060: * #makeAlignmentContext(LayoutContext)
061: */
062: protected AlignmentContext makeAlignmentContext(
063: LayoutContext context) {
064: return new AlignmentContext(font, fobj.getLineHeight()
065: .getOptimum(this ).getLength().getValue(this ), fobj
066: .getAlignmentAdjust(), fobj.getAlignmentBaseline(),
067: fobj.getBaselineShift(), fobj.getDominantBaseline(),
068: context.getAlignmentContext());
069: }
070:
071: /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#get(LayoutContext) */
072: public InlineArea get(LayoutContext context) {
073: // get page string from parent, build area
074: TextArea text = new TextArea();
075: String str = getCurrentPV().getPageNumberString();
076: int width = getStringWidth(str);
077: text.addWord(str, 0);
078: text.setIPD(width);
079: text.setBPD(font.getAscender() - font.getDescender());
080: text.setBaselineOffset(font.getAscender());
081: TraitSetter.addFontTraits(text, font);
082: text.addTrait(Trait.COLOR, fobj.getColor());
083:
084: TraitSetter.addTextDecoration(text, fobj.getTextDecoration());
085:
086: return text;
087: }
088:
089: /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#getEffectiveArea() */
090: protected InlineArea getEffectiveArea() {
091: TextArea baseArea = (TextArea) curArea;
092: //TODO Maybe replace that with a clone() call or better, a copy constructor
093: //TODO or even better: delay area creation until addAreas() stage
094: //TextArea is cloned because the LM is reused in static areas and the area can't be.
095: TextArea ta = new TextArea();
096: TraitSetter.setProducerID(ta, fobj.getId());
097: ta.setIPD(baseArea.getIPD());
098: ta.setBPD(baseArea.getBPD());
099: ta.setOffset(baseArea.getOffset());
100: ta.setBaselineOffset(baseArea.getBaselineOffset());
101: ta.addTrait(Trait.COLOR, fobj.getColor()); //only to initialize the trait map
102: ta.getTraits().putAll(baseArea.getTraits());
103: updateContent(ta);
104: return ta;
105: }
106:
107: private void updateContent(TextArea area) {
108: // get the page number of the page actually being built
109: area.removeText();
110: area.addWord(getCurrentPV().getPageNumberString(), 0);
111: // update the ipd of the area
112: area.handleIPDVariation(getStringWidth(area.getText())
113: - area.getIPD());
114: // update the width stored in the AreaInfo object
115: areaInfo.ipdArea = new MinOptMax(area.getIPD());
116: }
117:
118: /**
119: * @param str string to be measured
120: * @return width of the string
121: */
122: private int getStringWidth(String str) {
123: int width = 0;
124: for (int count = 0; count < str.length(); count++) {
125: width += font.getCharWidth(str.charAt(count));
126: }
127: return width;
128: }
129:
130: /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#addId() */
131: protected void addId() {
132: getPSLM().addIDToPage(fobj.getId());
133: }
134: }
|