KickJava   Java API By Example, From Geeks To Geeks.

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


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: PageNumberCitationLayoutManager.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import org.apache.fop.fo.flow.PageNumberCitation;
23 import org.apache.fop.area.PageViewport;
24 import org.apache.fop.area.Resolvable;
25 import org.apache.fop.area.Trait;
26 import org.apache.fop.area.inline.InlineArea;
27 import org.apache.fop.area.inline.UnresolvedPageNumber;
28 import org.apache.fop.area.inline.TextArea;
29 import org.apache.fop.fonts.Font;
30 import org.apache.fop.layoutmgr.LayoutContext;
31 import org.apache.fop.layoutmgr.LayoutManager;
32 import org.apache.fop.layoutmgr.PositionIterator;
33 import org.apache.fop.layoutmgr.TraitSetter;
34
35 /**
36  * LayoutManager for the fo:page-number-citation formatting object
37  */

38 public class PageNumberCitationLayoutManager extends LeafNodeLayoutManager {
39
40     private PageNumberCitation fobj;
41     /** Font for the page-number-citation */
42     protected Font font;
43     
44     /** Indicates whether the page referred to by the citation has been resolved yet */
45     protected boolean resolved = false;
46     
47     /**
48      * Constructor
49      *
50      * @param node the formatting object that creates this area
51      * @todo better retrieval of font info
52      */

53     public PageNumberCitationLayoutManager(PageNumberCitation node) {
54         super(node);
55         fobj = node;
56     }
57     
58     /** @see org.apache.fop.layoutmgr.LayoutManager#initialize */
59     public void initialize() {
60         font = fobj.getCommonFont().getFontState(fobj.getFOEventHandler().getFontInfo(), this);
61         setCommonBorderPaddingBackground(fobj.getCommonBorderPaddingBackground());
62     }
63
64     /**
65      * @see LeafNodeLayoutManager#makeAlignmentContext(LayoutContext)
66      */

67     protected AlignmentContext makeAlignmentContext(LayoutContext context) {
68         return new AlignmentContext(
69                 font
70                 , fobj.getLineHeight().getOptimum(this).getLength().getValue(this)
71                 , fobj.getAlignmentAdjust()
72                 , fobj.getAlignmentBaseline()
73                 , fobj.getBaselineShift()
74                 , fobj.getDominantBaseline()
75                 , context.getAlignmentContext()
76             );
77     }
78
79     /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#get(LayoutContext) */
80     public InlineArea get(LayoutContext context) {
81         curArea = getPageNumberCitationInlineArea(parentLM);
82         return curArea;
83     }
84     
85     /**
86      * @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#addAreas(PositionIterator
87      * , LayoutContext)
88      */

89     public void addAreas(PositionIterator posIter, LayoutContext context) {
90         super.addAreas(posIter, context);
91         if (!resolved) {
92             getPSLM().addUnresolvedArea(fobj.getRefId(), (Resolvable) curArea);
93         }
94     }
95     
96     /**
97      * if id can be resolved then simply return a word, otherwise
98      * return a resolvable area
99      */

100     private InlineArea getPageNumberCitationInlineArea(LayoutManager parentLM) {
101         PageViewport page = getPSLM().getFirstPVWithID(fobj.getRefId());
102         TextArea text = null;
103         if (page != null) {
104             String JavaDoc str = page.getPageNumberString();
105             // get page string from parent, build area
106
text = new TextArea();
107             int width = getStringWidth(str);
108             text.addWord(str, 0);
109             text.setIPD(width);
110             resolved = true;
111         } else {
112             resolved = false;
113             text = new UnresolvedPageNumber(fobj.getRefId(), font);
114             String JavaDoc str = "MMM"; // reserve three spaces for page number
115
int width = getStringWidth(str);
116             text.setIPD(width);
117         }
118         updateTextAreaTraits(text);
119         
120         return text;
121     }
122     
123     /**
124      * Updates the traits for the generated text area.
125      * @param text the text area
126      */

127     protected void updateTextAreaTraits(TextArea text) {
128         TraitSetter.setProducerID(text, fobj.getId());
129         text.setBPD(font.getAscender() - font.getDescender());
130         text.setBaselineOffset(font.getAscender());
131         TraitSetter.addFontTraits(text, font);
132         text.addTrait(Trait.COLOR, fobj.getColor());
133         TraitSetter.addTextDecoration(text, fobj.getTextDecoration());
134     }
135     
136     /**
137      * @param str string to be measured
138      * @return width (in millipoints ??) of the string
139      */

140     protected int getStringWidth(String JavaDoc str) {
141         int width = 0;
142         for (int count = 0; count < str.length(); count++) {
143             width += font.getCharWidth(str.charAt(count));
144         }
145         return width;
146     }
147
148     /** @see org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager#addId() */
149     protected void addId() {
150         getPSLM().addIDToPage(fobj.getId());
151     }
152 }
153
154
Popular Tags