KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > inline > PageNumberLayoutManager


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
18 /* $Id: PageNumberLayoutManager.java 453920 2006-10-07 14:29:54Z spepping $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import org.apache.fop.fo.flow.PageNumber;
23 import org.apache.fop.area.inline.InlineArea;
24 import org.apache.fop.area.inline.TextArea;
25 import org.apache.fop.area.Trait;
26 import org.apache.fop.fonts.Font;
27 import org.apache.fop.layoutmgr.LayoutContext;
28 import org.apache.fop.layoutmgr.TraitSetter;
29 import org.apache.fop.traits.MinOptMax;
30
31 /**
32  * LayoutManager for the fo:page-number formatting object
33  */

34 public class PageNumberLayoutManager extends LeafNodeLayoutManager {
35     
36     private PageNumber fobj;
37     private Font font;
38     
39     /**
40      * Constructor
41      *
42      * @param node the fo:page-number formatting object that creates the area
43      * @todo better null checking of node, font
44      */

45     public PageNumberLayoutManager(PageNumber node) {
46         super(node);
47         fobj = node;
48     }
49     
50     /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#get(LayoutContext) */
51     public void initialize() {
52         font = fobj.getCommonFont().getFontState(fobj.getFOEventHandler().getFontInfo(), this);
53         setCommonBorderPaddingBackground(fobj.getCommonBorderPaddingBackground());
54     }
55
56     /**
57      * @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager
58      * #makeAlignmentContext(LayoutContext)
59      */

60     protected AlignmentContext makeAlignmentContext(LayoutContext context) {
61         return new AlignmentContext(
62                 font
63                 , fobj.getLineHeight().getOptimum(this).getLength().getValue(this)
64                 , fobj.getAlignmentAdjust()
65                 , fobj.getAlignmentBaseline()
66                 , fobj.getBaselineShift()
67                 , fobj.getDominantBaseline()
68                 , context.getAlignmentContext()
69             );
70     }
71
72     /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#get(LayoutContext) */
73     public InlineArea get(LayoutContext context) {
74         // get page string from parent, build area
75
TextArea text = new TextArea();
76         String JavaDoc str = getCurrentPV().getPageNumberString();
77         int width = getStringWidth(str);
78         text.addWord(str, 0);
79         text.setIPD(width);
80         text.setBPD(font.getAscender() - font.getDescender());
81         text.setBaselineOffset(font.getAscender());
82         TraitSetter.addFontTraits(text, font);
83         text.addTrait(Trait.COLOR, fobj.getColor());
84
85         TraitSetter.addTextDecoration(text, fobj.getTextDecoration());
86
87         return text;
88     }
89     
90     /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#getEffectiveArea() */
91     protected InlineArea getEffectiveArea() {
92         TextArea baseArea = (TextArea)curArea;
93         //TODO Maybe replace that with a clone() call or better, a copy constructor
94
//TODO or even better: delay area creation until addAreas() stage
95
//TextArea is cloned because the LM is reused in static areas and the area can't be.
96
TextArea ta = new TextArea();
97         TraitSetter.setProducerID(ta, fobj.getId());
98         ta.setIPD(baseArea.getIPD());
99         ta.setBPD(baseArea.getBPD());
100         ta.setOffset(baseArea.getOffset());
101         ta.setBaselineOffset(baseArea.getBaselineOffset());
102         ta.addTrait(Trait.COLOR, fobj.getColor()); //only to initialize the trait map
103
ta.getTraits().putAll(baseArea.getTraits());
104         updateContent(ta);
105         return ta;
106     }
107     
108     private void updateContent(TextArea area) {
109         // get the page number of the page actually being built
110
area.removeText();
111         area.addWord(getCurrentPV().getPageNumberString(), 0);
112         // update the ipd of the area
113
area.handleIPDVariation(getStringWidth(area.getText()) - 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 JavaDoc 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 }
135
136
Popular Tags