blob: a76a32f5e202153e5434a99eb53afec629c5bb2b (
plain)
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
|
/*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.render;
// FOP
import org.apache.fop.image.ImageArea;
import org.apache.fop.apps.FOPException;
import org.apache.fop.area.*;
import org.apache.fop.area.Span;
import org.apache.fop.area.inline.*;
import org.apache.fop.area.inline.Space;
import org.apache.fop.fo.FOUserAgent;
import org.apache.log.Logger;
// Java
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
/**
* Abstract base class for all renderers.
* The Abstract renderer does all the top level processing
* of the area tree and adds some abstract methods to handle
* viewports. This keeps track of the current block and inline
* position.
*/
public abstract class AbstractRenderer implements Renderer {
protected Logger log;
protected FOUserAgent userAgent;
protected HashMap options;
// block progression position
protected int currentBPPosition = 0;
// inline progression position
protected int currentIPPosition = 0;
protected int currentBlockIPPosition = 0;
public void setLogger(Logger logger) {
log = logger;
}
public void setUserAgent(FOUserAgent agent) {
userAgent = agent;
}
public void setOptions(HashMap opt) {
options = opt;
}
public void startPageSequence(Title seqTitle) {
}
// normally this would be overriden to create a page in the
// output
public void renderPage(PageViewport page) throws IOException,
FOPException {
Page p = page.getPage();
renderPageAreas(p);
}
protected void renderPageAreas(Page page) {
RegionViewport viewport;
viewport = page.getRegion(Region.BEFORE);
renderRegionViewport(viewport);
viewport = page.getRegion(Region.START);
renderRegionViewport(viewport);
viewport = page.getRegion(Region.BODY);
renderRegionViewport(viewport);
viewport = page.getRegion(Region.END);
renderRegionViewport(viewport);
viewport = page.getRegion(Region.AFTER);
renderRegionViewport(viewport);
}
// the region may clip the area and it establishes
// a position from where the region is placed
protected void renderRegionViewport(RegionViewport port) {
if (port != null) {
Region region = port.getRegion();
if (region.getRegionClass() == Region.BODY) {
renderBodyRegion((BodyRegion) region);
} else {
renderRegion(region);
}
}
}
protected void renderRegion(Region region) {
List blocks = region.getBlocks();
renderBlocks(blocks);
}
protected void renderBodyRegion(BodyRegion region) {
BeforeFloat bf = region.getBeforeFloat();
if (bf != null) {
renderBeforeFloat(bf);
}
MainReference mr = region.getMainReference();
if (mr != null) {
renderMainReference(mr);
}
Footnote foot = region.getFootnote();
if (foot != null) {
renderFootnote(foot);
}
}
protected void renderBeforeFloat(BeforeFloat bf) {
List blocks = bf.getBlocks();
if (blocks != null) {
renderBlocks(blocks);
Block sep = bf.getSeparator();
if (sep != null) {
renderBlock(sep);
}
}
}
protected void renderFootnote(Footnote footnote) {
List blocks = footnote.getBlocks();
if (blocks != null) {
Block sep = footnote.getSeparator();
if (sep != null) {
renderBlock(sep);
}
renderBlocks(blocks);
}
}
// the main reference area contains a list of spans that are
// stacked on the page
// the spans contain a list of normal flow reference areas
// that are positioned into columns.
protected void renderMainReference(MainReference mr) {
int saveIPPos = currentIPPosition;
Span span = null;
List spans = mr.getSpans();
for (int count = 0; count < spans.size(); count++) {
span = (Span) spans.get(count);
int offset = (mr.getWidth() -
(span.getColumnCount() - 1) * mr.getColumnGap()) /
span.getColumnCount() + mr.getColumnGap();
for (int c = 0; c < span.getColumnCount(); c++) {
Flow flow = (Flow) span.getFlow(c);
renderFlow(flow);
currentIPPosition += offset;
}
currentIPPosition = saveIPPos;
currentBPPosition += span.getHeight();
}
}
// the normal flow reference area contains stacked blocks
protected void renderFlow(Flow flow) {
List blocks = flow.getBlocks();
renderBlocks(blocks);
}
protected void renderBlock(Block block) {
boolean childrenblocks = block.isChildrenBlocks();
List children = block.getChildAreas();
if (childrenblocks) {
renderBlocks(children);
} else {
if (children == null) {
// simply move position
} else {
// a line area is rendered from the top left position
// of the line, each inline object is offset from there
for (int count = 0; count < children.size(); count++) {
LineArea line = (LineArea) children.get(count);
renderLineArea(line);
}
}
}
}
// a line area may have grouped styling for its children
// such as underline, background
protected void renderLineArea(LineArea line) {
List children = line.getInlineAreas();
for (int count = 0; count < children.size(); count++) {
InlineArea inline = (InlineArea) children.get(count);
inline.render(this);
}
}
public void renderViewport(Viewport viewport) {
Area content = viewport.getContent();
if (content instanceof Image) {
renderImage((Image) content);
} else if (content instanceof Container) {
renderContainer((Container) content);
} else if (content instanceof ForeignObject) {
renderForeignObject((ForeignObject) content);
}
}
public void renderImage(Image image) {
}
public void renderContainer(Container cont) {
List blocks = cont.getBlocks();
renderBlocks(blocks);
}
public void renderForeignObject(ForeignObject fo) {
}
public void renderCharacter(org.apache.fop.area.inline.Character ch) {
currentBlockIPPosition += ch.getWidth();
}
// an inline space moves the inline progression position
// for the current block by the width or height of the space
// it may also have styling (only on this object) that needs
// handling
public void renderInlineSpace(Space space) {
currentBlockIPPosition += space.getWidth();
}
public void renderLeader(Leader area) {
currentBlockIPPosition += area.getWidth();
}
public void renderWord(Word word) {
currentBlockIPPosition += word.getWidth();
}
protected void renderBlocks(List blocks) {
for (int count = 0; count < blocks.size(); count++) {
Block block = (Block) blocks.get(count);
renderBlock(block);
}
}
}
|