1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: Page.java,v 1.4 2004/02/27 17:41:26 jeremias Exp $ */
package org.apache.fop.area;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.fop.datatypes.FODimension;
import org.apache.fop.datatypes.LengthBase;
import org.apache.fop.datatypes.PercentBaseContext;
import org.apache.fop.datatypes.SimplePercentBaseContext;
import org.apache.fop.fo.Constants;
import org.apache.fop.fo.pagination.Region;
import org.apache.fop.fo.pagination.RegionBody;
import org.apache.fop.fo.pagination.SimplePageMaster;
import org.apache.fop.fo.properties.CommonMarginBlock;
import org.apache.fop.layoutmgr.TraitSetter;
/**
* The page.
* This holds the contents of the page. Each region is added.
* The unresolved references area added so that if the page is
* serialized then it will handle the resolving properly after
* being reloaded.
* This is serializable so it can be saved to cache to save
* memory if there are forward references.
* The page is cloneable so the page master can make copies of
* the top level page and regions.
*/
public class Page implements Serializable, Cloneable {
// contains before, start, body, end and after regions
private RegionViewport regionBefore = null;
private RegionViewport regionStart = null;
private RegionViewport regionBody = null;
private RegionViewport regionEnd = null;
private RegionViewport regionAfter = null;
// temporary map of unresolved objects used when serializing the page
private HashMap unresolved = null;
/** Set to true to make this page behave as if it were not empty. */
private boolean fakeNonEmpty = false;
/**
* Empty constructor, for cloning
*/
public Page() {
}
/**
* Constructor
* @param spm SimplePageMaster containing the dimensions for this
* page-reference-area
*/
public Page(SimplePageMaster spm) {
// Width and Height of the page view port
FODimension pageViewPortDims = new FODimension(spm.getPageWidth().getValue()
, spm.getPageHeight().getValue());
// Get absolute margin properties (top, left, bottom, right)
CommonMarginBlock mProps = spm.getCommonMarginBlock();
/*
* Create the page reference area rectangle (0,0 is at top left
* of the "page media" and y increases
* when moving towards the bottom of the page.
* The media rectangle itself is (0,0,pageWidth,pageHeight).
*/
/* Special rules apply to resolving margins in the page context.
* Contrary to normal margins in this case top and bottom margin
* are resolved relative to the height. In the property subsystem
* all margin properties are configured to using BLOCK_WIDTH.
* That's why we 'cheat' here and setup a context for the height but
* use the LengthBase.BLOCK_WIDTH.
*/
SimplePercentBaseContext pageWidthContext
= new SimplePercentBaseContext(null, LengthBase.CONTAINING_BLOCK_WIDTH, pageViewPortDims.ipd);
SimplePercentBaseContext pageHeightContext
= new SimplePercentBaseContext(null, LengthBase.CONTAINING_BLOCK_WIDTH, pageViewPortDims.bpd);
Rectangle pageRefRect =
new Rectangle(mProps.marginLeft.getValue(pageWidthContext), mProps.marginTop.getValue(pageHeightContext),
pageViewPortDims.ipd - mProps.marginLeft.getValue(pageWidthContext) - mProps.marginRight.getValue(pageWidthContext),
pageViewPortDims.bpd - mProps.marginTop.getValue(pageHeightContext) - mProps.marginBottom.getValue(pageHeightContext));
// Set up the CTM on the page reference area based on writing-mode
// and reference-orientation
FODimension reldims = new FODimension(0, 0);
CTM pageCTM = CTM.getCTMandRelDims(spm.getReferenceOrientation(),
spm.getWritingMode(), pageRefRect, reldims);
// Create a RegionViewport/ reference area pair for each page region
RegionReference rr = null;
for (Iterator regenum = spm.getRegions().values().iterator();
regenum.hasNext();) {
Region r = (Region)regenum.next();
RegionViewport rvp = makeRegionViewport(r, reldims, pageCTM, pageViewPortDims);
if (r.getNameId() == Constants.FO_REGION_BODY) {
rr = new BodyRegion((RegionBody) r, rvp);
} else {
rr = new RegionReference(r, rvp);
}
setRegionReferencePosition(rr, r, rvp.getViewArea());
rvp.setRegionReference(rr);
setRegionViewport(r.getNameId(), rvp);
}
}
/**
* Call this method to force this page to pretend not to be empty.
*/
public void fakeNonEmpty() {
this.fakeNonEmpty = true;
}
/**
* Creates a RegionViewport Area object for this pagination Region.
* @param reldims relative dimensions
* @param pageCTM page coordinate transformation matrix
* @return the new region viewport
*/
private RegionViewport makeRegionViewport(Region r, FODimension reldims, CTM pageCTM,
FODimension pageViewPortDims) {
Rectangle2D relRegionRect = r.getViewportRectangle(reldims, pageViewPortDims);
Rectangle2D absRegionRect = pageCTM.transform(relRegionRect);
// Get the region viewport rectangle in absolute coords by
// transforming it using the page CTM
RegionViewport rv = new RegionViewport(absRegionRect);
rv.setBPD((int)relRegionRect.getHeight());
rv.setIPD((int)relRegionRect.getWidth());
TraitSetter.addBackground(rv, r.getCommonBorderPaddingBackground(), null);
rv.setClip(r.getOverflow() == Constants.EN_HIDDEN
|| r.getOverflow() == Constants.EN_ERROR_IF_OVERFLOW);
return rv;
}
/**
* Set the region reference position within the region viewport.
* This sets the transform that is used to place the contents of
* the region reference.
*
* @param rr the region reference area
* @param r the region-xxx formatting object
* @param absRegVPRect The region viewport rectangle in "absolute" coordinates
* where x=distance from left, y=distance from bottom, width=right-left
* height=top-bottom
*/
private void setRegionReferencePosition(RegionReference rr, Region r,
Rectangle2D absRegVPRect) {
FODimension reldims = new FODimension(0, 0);
rr.setCTM(CTM.getCTMandRelDims(r.getReferenceOrientation(),
r.getWritingMode(), absRegVPRect, reldims));
rr.setIPD(reldims.ipd);
rr.setBPD(reldims.bpd);
}
/**
* Set the region on this page.
*
* @param areaclass the area class of the region to set
* @param port the region viewport to set
*/
private void setRegionViewport(int areaclass, RegionViewport port) {
if (areaclass == Constants.FO_REGION_BEFORE) {
regionBefore = port;
} else if (areaclass == Constants.FO_REGION_START) {
regionStart = port;
} else if (areaclass == Constants.FO_REGION_BODY) {
regionBody = port;
} else if (areaclass == Constants.FO_REGION_END) {
regionEnd = port;
} else if (areaclass == Constants.FO_REGION_AFTER) {
regionAfter = port;
}
}
/**
* Get the region from this page.
*
* @param areaclass the region area class
* @return the region viewport or null if none
*/
public RegionViewport getRegionViewport(int areaclass) {
if (areaclass == Constants.FO_REGION_BEFORE) {
return regionBefore;
} else if (areaclass == Constants.FO_REGION_START) {
return regionStart;
} else if (areaclass == Constants.FO_REGION_BODY) {
return regionBody;
} else if (areaclass == Constants.FO_REGION_END) {
return regionEnd;
} else if (areaclass == Constants.FO_REGION_AFTER) {
return regionAfter;
}
throw new IllegalArgumentException("No such area class with ID = "
+ areaclass);
}
/**
* indicates whether any FOs have been added to the body region
*
* @return whether any FOs have been added to the body region
*/
public boolean isEmpty() {
if (fakeNonEmpty) {
return false;
} else if (regionBody == null) {
return true;
} else {
BodyRegion body = (BodyRegion)regionBody.getRegionReference();
return body.isEmpty();
}
}
/**
* Clone this page.
* This returns a new page with a clone of all the regions.
*
* @return a new clone of this page
*/
public Object clone() {
Page p = new Page();
if (regionBefore != null) {
p.regionBefore = (RegionViewport)regionBefore.clone();
}
if (regionStart != null) {
p.regionStart = (RegionViewport)regionStart.clone();
}
if (regionBody != null) {
p.regionBody = (RegionViewport)regionBody.clone();
}
if (regionEnd != null) {
p.regionEnd = (RegionViewport)regionEnd.clone();
}
if (regionAfter != null) {
p.regionAfter = (RegionViewport)regionAfter.clone();
}
return p;
}
/**
* Set the unresolved references on this page for serializing.
*
* @param unres the HashMap of unresolved objects
*/
public void setUnresolvedReferences(HashMap unres) {
unresolved = unres;
}
/**
* Get the map unresolved references from this page.
* This should be called after deserializing to retrieve
* the map of unresolved references that were serialized.
*
* @return the de-serialized HashMap of unresolved objects
*/
public HashMap getUnresolvedReferences() {
return unresolved;
}
}
|