KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > Page


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: Page.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.area;
21
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.fop.datatypes.FODimension;
29 import org.apache.fop.datatypes.LengthBase;
30 import org.apache.fop.datatypes.SimplePercentBaseContext;
31 import org.apache.fop.fo.Constants;
32 import org.apache.fop.fo.pagination.Region;
33 import org.apache.fop.fo.pagination.RegionBody;
34 import org.apache.fop.fo.pagination.SimplePageMaster;
35 import org.apache.fop.fo.properties.CommonMarginBlock;
36 import org.apache.fop.layoutmgr.TraitSetter;
37
38 /**
39  * The page.
40  * This holds the contents of the page. Each region is added.
41  * The unresolved references area added so that if the page is
42  * serialized then it will handle the resolving properly after
43  * being reloaded.
44  * This is serializable so it can be saved to cache to save
45  * memory if there are forward references.
46  * The page is cloneable so the page master can make copies of
47  * the top level page and regions.
48  */

49 public class Page extends AreaTreeObject implements Serializable JavaDoc, Cloneable JavaDoc {
50     // contains before, start, body, end and after regions
51
private RegionViewport regionBefore = null;
52     private RegionViewport regionStart = null;
53     private RegionViewport regionBody = null;
54     private RegionViewport regionEnd = null;
55     private RegionViewport regionAfter = null;
56
57     // temporary map of unresolved objects used when serializing the page
58
private Map JavaDoc unresolved = null;
59
60     /** Set to true to make this page behave as if it were not empty. */
61     private boolean fakeNonEmpty = false;
62     
63     /**
64      * Empty constructor, for cloning
65      */

66     public Page() {
67     }
68
69     /**
70      * Constructor
71      * @param spm SimplePageMaster containing the dimensions for this
72      * page-reference-area
73      */

74     public Page(SimplePageMaster spm) {
75         // Width and Height of the page view port
76
FODimension pageViewPortDims = new FODimension(spm.getPageWidth().getValue()
77                             , spm.getPageHeight().getValue());
78
79         // Get absolute margin properties (top, left, bottom, right)
80
CommonMarginBlock mProps = spm.getCommonMarginBlock();
81         
82         /*
83          * Create the page reference area rectangle (0,0 is at top left
84          * of the "page media" and y increases
85          * when moving towards the bottom of the page.
86          * The media rectangle itself is (0,0,pageWidth,pageHeight).
87          */

88         /* Special rules apply to resolving margins in the page context.
89          * Contrary to normal margins in this case top and bottom margin
90          * are resolved relative to the height. In the property subsystem
91          * all margin properties are configured to using BLOCK_WIDTH.
92          * That's why we 'cheat' here and setup a context for the height but
93          * use the LengthBase.BLOCK_WIDTH.
94          */

95         SimplePercentBaseContext pageWidthContext
96             = new SimplePercentBaseContext(null, LengthBase.CONTAINING_BLOCK_WIDTH
97                                             , pageViewPortDims.ipd);
98         SimplePercentBaseContext pageHeightContext
99             = new SimplePercentBaseContext(null, LengthBase.CONTAINING_BLOCK_WIDTH
100                                             , pageViewPortDims.bpd);
101
102         Rectangle JavaDoc pageRefRect
103             = new Rectangle JavaDoc(mProps.marginLeft.getValue(pageWidthContext)
104                             , mProps.marginTop.getValue(pageHeightContext)
105                             , pageViewPortDims.ipd
106                                 - mProps.marginLeft.getValue(pageWidthContext)
107                                 - mProps.marginRight.getValue(pageWidthContext)
108                             , pageViewPortDims.bpd
109                                 - mProps.marginTop.getValue(pageHeightContext)
110                                 - mProps.marginBottom.getValue(pageHeightContext));
111
112         // Set up the CTM on the page reference area based on writing-mode
113
// and reference-orientation
114
FODimension reldims = new FODimension(0, 0);
115         CTM pageCTM = CTM.getCTMandRelDims(spm.getReferenceOrientation(),
116             spm.getWritingMode(), pageRefRect, reldims);
117
118         // Create a RegionViewport/ reference area pair for each page region
119
RegionReference rr = null;
120         for (Iterator JavaDoc regenum = spm.getRegions().values().iterator();
121             regenum.hasNext();) {
122             Region r = (Region)regenum.next();
123             RegionViewport rvp = makeRegionViewport(r, reldims, pageCTM, spm);
124             if (r.getNameId() == Constants.FO_REGION_BODY) {
125                 rr = new BodyRegion((RegionBody) r, rvp);
126             } else {
127                 rr = new RegionReference(r, rvp);
128             }
129             setRegionReferencePosition(rr, r, rvp.getViewArea());
130             rvp.setRegionReference(rr);
131             setRegionViewport(r.getNameId(), rvp);
132        }
133     }
134
135     /**
136      * Call this method to force this page to pretend not to be empty.
137      */

138     public void fakeNonEmpty() {
139         this.fakeNonEmpty = true;
140     }
141     
142     /**
143      * Creates a RegionViewport Area object for this pagination Region.
144      * @param r the region the viewport is to be created for
145      * @param reldims relative dimensions
146      * @param pageCTM page coordinate transformation matrix
147      * @param spm the simple-page-master for this page
148      * @return the new region viewport
149      */

150     private RegionViewport makeRegionViewport(Region r, FODimension reldims, CTM pageCTM,
151         SimplePageMaster spm) {
152         Rectangle2D JavaDoc relRegionRect = r.getViewportRectangle(reldims, spm);
153         Rectangle2D JavaDoc absRegionRect = pageCTM.transform(relRegionRect);
154         // Get the region viewport rectangle in absolute coords by
155
// transforming it using the page CTM
156
RegionViewport rv = new RegionViewport(absRegionRect);
157         rv.setBPD((int)relRegionRect.getHeight());
158         rv.setIPD((int)relRegionRect.getWidth());
159         TraitSetter.addBackground(rv, r.getCommonBorderPaddingBackground(), null);
160         rv.setClip(r.getOverflow() == Constants.EN_HIDDEN
161                 || r.getOverflow() == Constants.EN_ERROR_IF_OVERFLOW);
162         return rv;
163     }
164    
165     /**
166      * Set the region reference position within the region viewport.
167      * This sets the transform that is used to place the contents of
168      * the region reference.
169      *
170      * @param rr the region reference area
171      * @param r the region-xxx formatting object
172      * @param absRegVPRect The region viewport rectangle in "absolute" coordinates
173      * where x=distance from left, y=distance from bottom, width=right-left
174      * height=top-bottom
175      */

176     private void setRegionReferencePosition(RegionReference rr, Region r,
177                                   Rectangle2D JavaDoc absRegVPRect) {
178         FODimension reldims = new FODimension(0, 0);
179         rr.setCTM(CTM.getCTMandRelDims(r.getReferenceOrientation(),
180                 r.getWritingMode(), absRegVPRect, reldims));
181         rr.setIPD(reldims.ipd);
182         rr.setBPD(reldims.bpd);
183     }
184     
185     /**
186      * Set the region on this page.
187      *
188      * @param areaclass the area class of the region to set
189      * @param port the region viewport to set
190      */

191     public void setRegionViewport(int areaclass, RegionViewport port) {
192         if (areaclass == Constants.FO_REGION_BEFORE) {
193             regionBefore = port;
194         } else if (areaclass == Constants.FO_REGION_START) {
195             regionStart = port;
196         } else if (areaclass == Constants.FO_REGION_BODY) {
197             regionBody = port;
198         } else if (areaclass == Constants.FO_REGION_END) {
199             regionEnd = port;
200         } else if (areaclass == Constants.FO_REGION_AFTER) {
201             regionAfter = port;
202         }
203     }
204
205     /**
206      * Get the region from this page.
207      *
208      * @param areaclass the region area class
209      * @return the region viewport or null if none
210      */

211     public RegionViewport getRegionViewport(int areaclass) {
212         if (areaclass == Constants.FO_REGION_BEFORE) {
213             return regionBefore;
214         } else if (areaclass == Constants.FO_REGION_START) {
215             return regionStart;
216         } else if (areaclass == Constants.FO_REGION_BODY) {
217             return regionBody;
218         } else if (areaclass == Constants.FO_REGION_END) {
219             return regionEnd;
220         } else if (areaclass == Constants.FO_REGION_AFTER) {
221             return regionAfter;
222         }
223         throw new IllegalArgumentException JavaDoc("No such area class with ID = "
224             + areaclass);
225     }
226
227     /**
228      * indicates whether any FOs have been added to the body region
229      *
230      * @return whether any FOs have been added to the body region
231      */

232     public boolean isEmpty() {
233         if (fakeNonEmpty) {
234             return false;
235         } else if (regionBody == null) {
236             return true;
237         } else {
238             BodyRegion body = (BodyRegion)regionBody.getRegionReference();
239             return body.isEmpty();
240         }
241     }
242
243     /**
244      * Clone this page.
245      * This returns a new page with a clone of all the regions.
246      *
247      * @return a new clone of this page
248      */

249     public Object JavaDoc clone() {
250         Page p = new Page();
251         if (regionBefore != null) {
252             p.regionBefore = (RegionViewport)regionBefore.clone();
253         }
254         if (regionStart != null) {
255             p.regionStart = (RegionViewport)regionStart.clone();
256         }
257         if (regionBody != null) {
258             p.regionBody = (RegionViewport)regionBody.clone();
259         }
260         if (regionEnd != null) {
261             p.regionEnd = (RegionViewport)regionEnd.clone();
262         }
263         if (regionAfter != null) {
264             p.regionAfter = (RegionViewport)regionAfter.clone();
265         }
266
267         return p;
268     }
269
270     /**
271      * Set the unresolved references on this page for serializing.
272      *
273      * @param unres the Map of unresolved objects
274      */

275     public void setUnresolvedReferences(Map JavaDoc unres) {
276         unresolved = unres;
277     }
278
279     /**
280      * Get the map unresolved references from this page.
281      * This should be called after deserializing to retrieve
282      * the map of unresolved references that were serialized.
283      *
284      * @return the de-serialized HashMap of unresolved objects
285      */

286     public Map JavaDoc getUnresolvedReferences() {
287         return unresolved;
288     }
289
290 }
291
292
Popular Tags