aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/render/AbstractRenderer.java
blob: e6cd863ccf3d6cbe1273979a6b0d03a507ee3463 (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * $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.apps.FOPException;
import org.apache.fop.area.*;
import org.apache.fop.area.inline.*;
import org.apache.fop.area.inline.Character;
import org.apache.fop.fo.FOUserAgent;

// Avalon
import org.apache.avalon.framework.logger.AbstractLogEnabled;

// Java
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;

/**
 * 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 extends AbstractLogEnabled implements Renderer {
    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 setUserAgent(FOUserAgent agent) {
        userAgent = agent;
    }

    public void setOptions(HashMap opt) {
        options = opt;
    }

    /**
     * Check if this renderer supports out of order rendering.
     * If this renderer supports out of order rendering then it
     * means that the pages that are not ready will be prepared
     * and a future page will be rendered.
     */
    public boolean supportsOutOfOrder() {
        return false;
    }

    /**
     * Prepare a page for rendering.
     * This is called if the renderer supports out of order rendering.
     * The renderer should prepare the page so that a page further on
     * in the set of pages can be rendered. The body of the page should
     * not be rendered. The page will be rendered at a later time
     * by the call to render page.
     */
    public void preparePage(PageViewport page) {
    }

    /**
     * Utility method to convert a page sequence title to a string.
     * Some renderers may only be able to use a string title.
     * A title is a sequence of inline areas that this method
     * attempts to convert to an equivalent string.
     */
    public String convertTitleToString(Title title) {
        String str = "";
        List children = title.getInlineAreas();

        for (int count = 0; count < children.size(); count++) {
            InlineArea inline = (InlineArea) children.get(count);
            if (inline instanceof Character) {
                str += ((Character) inline).getChar();
            } else if (inline instanceof Word) {
                str += ((Word) inline).getWord();
            } else {
                str += " ";
            }
        }
        return str.trim();
    }

    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(RegionReference.BEFORE);
        renderRegionViewport(viewport);
        viewport = page.getRegion(RegionReference.START);
        renderRegionViewport(viewport);
        viewport = page.getRegion(RegionReference.BODY);
        renderRegionViewport(viewport);
        viewport = page.getRegion(RegionReference.END);
        renderRegionViewport(viewport);
        viewport = page.getRegion(RegionReference.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) {
            Rectangle2D view = port.getViewArea();
        // The CTM will transform coordinates relative to
        // this region-reference area into page coords, so
        // set origin for the region to 0,0.
            currentBPPosition = 0; // (int) (view.getY() / 1000);
            currentIPPosition = 0; // (int) (view.getX() / 1000);
            currentBlockIPPosition = currentIPPosition;

            RegionReference region = port.getRegion();
        startVParea(region.getCTM());
            if (region.getRegionClass() == RegionReference.BODY) {
                renderBodyRegion((BodyRegion) region);
            } else {
                renderRegion(region);
            }
        endVParea();
        }
    }

    protected void startVParea(CTM ctm) { }

    protected void endVParea() { }

    protected void renderRegion(RegionReference 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) {
        List children = block.getChildAreas();
        if (children == null) {
            // simply move position
        } else {
            renderBlocks(children);
        }
    }

    // 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();
        int saveBP = currentBPPosition;
        currentBPPosition += viewport.getOffset();
        Rectangle2D contpos = viewport.getContentPosition();
        if (content instanceof Image) {
            renderImage((Image) content, contpos);
        } else if (content instanceof Container) {
            renderContainer((Container) content);
        } else if (content instanceof ForeignObject) {
            renderForeignObject((ForeignObject) content, contpos);
        }
        currentBlockIPPosition += viewport.getWidth();
        currentBPPosition = saveBP;
    }

    public void renderImage(Image image, Rectangle2D pos) {
    }

    public void renderContainer(Container cont) {
        int saveIP = currentIPPosition;
        currentIPPosition = currentBlockIPPosition;
        int saveBlockIP = currentBlockIPPosition;
        int saveBP = currentBPPosition;

        List blocks = cont.getBlocks();
        renderBlocks(blocks);
        currentIPPosition = saveIP;
        currentBlockIPPosition = saveBlockIP;
        currentBPPosition = saveBP;
    }

    public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {

    }

    public void renderCharacter(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();
    }

    public void renderInlineParent(InlineParent ip) {
        // currentBlockIPPosition += ip.getWidth();
    Iterator iter = ip.getChildAreas().iterator();
    while (iter.hasNext()) {
            ((InlineArea)iter.next()).render(this);
        }
    }

    protected void renderBlocks(List blocks) {
        for (int count = 0; count < blocks.size(); count++) {
            Object obj = blocks.get(count);
            if(obj instanceof Block) {
                renderBlock((Block)obj);
            } else {
                // a line area is rendered from the top left position
                // of the line, each inline object is offset from there
                LineArea line = (LineArea) obj;
                currentBlockIPPosition = currentIPPosition;
                renderLineArea(line);
                currentBPPosition += line.getHeight();
            }
        }
    }
}