aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/render/PrintRenderer.java
blob: efc9b8d56244e384f348855808bc9240f4539fac (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* $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."
 */

// PrintRenderer is an abstract base class for renderers that produce printed type output.
// Subclasses would be PDFRenderer, PCLRenderer and similar renderers.

package org.apache.fop.render;

// FOP
import org.apache.fop.pdf.PDFPathPaint;
import org.apache.fop.pdf.PDFColor;
//import org.apache.fop.render.Renderer;
//import org.apache.fop.messaging.MessageHandler;
import org.apache.fop.image.ImageArea;
//import org.apache.fop.image.FopImage;
import org.apache.fop.apps.FOPException;
import org.apache.fop.fo.properties.*;
import org.apache.fop.layout.*;
import org.apache.fop.layout.inline.*;
import org.apache.fop.datatypes.*;
//import org.apache.fop.configuration.Configuration;
//import org.apache.fop.extensions.*;
//import org.apache.fop.datatypes.IDReferences;
import org.apache.fop.render.pdf.FontSetup;

import org.apache.fop.dom.svg.*;

// Java
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

/**
 * Abstract base class of "Print" type renderers.
 */
public abstract class PrintRenderer implements Renderer
{
	// vvv These are not currently referenced by the PrintRenderer, but are common to PCL and PDF renderers - so declare here.
	    /** the current (internal) font name */
	    protected String currentFontName;
	    /** the current font size in millipoints */
	    protected int currentFontSize;
	    /** the current color/gradient for borders, letters, etc. */
	    protected PDFPathPaint currentStroke = null;
	    /** the current color/gradient to fill shapes with */
	    protected PDFPathPaint currentFill = null;
	    /** the current colour's red component */
	    //protected float currentRed = 0;
	    /** the current colour's green component */
	    //protected float currentGreen = 0;
	    /** the current colour's blue component */
	    //protected float currentBlue = 0;
	// ^^^

    /** the current vertical position in millipoints from bottom */
    protected int currentYPosition = 0;

    /** the current horizontal position in millipoints from left */
    protected int currentXPosition = 0;

    /** the horizontal position of the current area container */
    protected int currentAreaContainerXPosition = 0;

    // previous values used for text-decoration drawing
    protected int prevUnderlineXEndPos;
    protected int prevUnderlineYEndPos;
    protected int prevUnderlineSize;
    protected PDFColor prevUnderlineColor;
    protected int prevOverlineXEndPos;
    protected int prevOverlineYEndPos;
    protected int prevOverlineSize;
    protected PDFColor prevOverlineColor;
    protected int prevLineThroughXEndPos;
    protected int prevLineThroughYEndPos;
    protected int prevLineThroughSize;
    protected PDFColor prevLineThroughColor;

    protected FontInfo fontInfo;

    /** the IDReferences for this document */
    protected IDReferences idReferences;

    /**
       * set the document's producer
       *
       * @param producer string indicating application producing PDF
       */
    public abstract void setProducer(String producer);

    /**
       * render the areas
       *
       * @param areaTree the laid-out area tree
       * @param stream the OutputStream to write to
       */
    public abstract void render(AreaTree areaTree,
                       OutputStream stream) throws IOException, FOPException;

    /**
       * add a line to the current stream
       *
       * @param x1 the start x location in millipoints
       * @param y1 the start y location in millipoints
       * @param x2 the end x location in millipoints
       * @param y2 the end y location in millipoints
       * @param th the thickness in millipoints
       * @param r the red component
       * @param g the green component
       * @param b the blue component
       */
    protected abstract void addLine(int x1, int y1, int x2, int y2, int th,
                           PDFPathPaint stroke);

    /**
      * add a line to the current stream
      *
      * @param x1 the start x location in millipoints
      * @param y1 the start y location in millipoints
      * @param x2 the end x location in millipoints
      * @param y2 the end y location in millipoints
      * @param th the thickness in millipoints
      * @param rs the rule style
      * @param r the red component
      * @param g the green component
      * @param b the blue component
      */
    protected abstract void addLine(int x1, int y1, int x2, int y2, int th,
                           int rs, PDFPathPaint stroke);
    /**
       * add a rectangle to the current stream
       *
       * @param x the x position of left edge in millipoints
       * @param y the y position of top edge in millipoints
       * @param w the width in millipoints
       * @param h the height in millipoints
       * @param stroke the stroke color/gradient
       */
    protected abstract void addRect(int x, int y, int w, int h,
                           PDFPathPaint stroke);

    /**
       * add a filled rectangle to the current stream
       *
       * @param x the x position of left edge in millipoints
       * @param y the y position of top edge in millipoints
       * @param w the width in millipoints
       * @param h the height in millipoints
       * @param fill the fill color/gradient
       * @param stroke the stroke color/gradient
       */
    protected abstract void addRect(int x, int y, int w, int h,
                           PDFPathPaint stroke, PDFPathPaint fill);

    /**
       * render area container
       *
       * @param area the area container to render
       */
    public void renderAreaContainer(AreaContainer area) {

        int saveY = this.currentYPosition;
        int saveX = this.currentAreaContainerXPosition;

        if (area.getPosition() == Position.ABSOLUTE) {
	    // XPosition and YPosition give the content rectangle position
            this.currentYPosition = area.getYPosition() ;
            this.currentAreaContainerXPosition = area.getXPosition();
        } else if (area.getPosition() == Position.RELATIVE) {
            this.currentYPosition -= area.getYPosition();
            this.currentAreaContainerXPosition += area.getXPosition();
        } else if (area.getPosition() == Position.STATIC) {
            this.currentYPosition -=
              area.getPaddingTop() + area.getBorderTopWidth();
            this.currentAreaContainerXPosition +=
              area.getPaddingLeft() + area.getBorderLeftWidth();
        }

        this.currentXPosition = this.currentAreaContainerXPosition;
        doFrame(area);

        Enumeration e = area.getChildren().elements();
        while (e.hasMoreElements()) {
            Box b = (Box) e.nextElement();
            b.render(this);
        }
	// Restore previous origin
	this.currentYPosition = saveY;
	this.currentAreaContainerXPosition = saveX;
        if (area.getPosition() == Position.STATIC) {
            this.currentYPosition -= area.getHeight();
	}

	/****
        if (area.getPosition() != Position.STATIC) {
            this.currentYPosition = saveY;
            this.currentAreaContainerXPosition = saveX;
        } else
            this.currentYPosition -= area.getHeight();
	*****/
    }

	public void renderBodyAreaContainer(BodyAreaContainer area) {
        int saveY = this.currentYPosition;
        int saveX = this.currentAreaContainerXPosition;

        if (area.getPosition() == Position.ABSOLUTE) {
            // Y position is computed assuming positive Y axis, adjust for negative postscript one
            this.currentYPosition = area.getYPosition();
            this.currentAreaContainerXPosition = area.getXPosition();
        } else if (area.getPosition() == Position.RELATIVE) {
            this.currentYPosition -= area.getYPosition();
            this.currentAreaContainerXPosition += area.getXPosition();
        }

        this.currentXPosition = this.currentAreaContainerXPosition;
        int w, h;
        int rx = this.currentAreaContainerXPosition;
        w = area.getContentWidth();
        h = area.getContentHeight();
        int ry = this.currentYPosition;
        ColorType bg = area.getBackgroundColor();

        // I'm not sure I should have to check for bg being null
        // but I do
        if ((bg != null) && (bg.alpha() == 0)) {
            this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
        }

		// floats & footnotes stuff
		renderAreaContainer(area.getBeforeFloatReferenceArea());
  		renderAreaContainer(area.getFootnoteReferenceArea());

		// main reference area
		Enumeration e = area.getMainReferenceArea().getChildren().elements();
		while (e.hasMoreElements()) {
			Box b = (Box) e.nextElement();
			b.render(this);	// span areas
		}

        if (area.getPosition() != Position.STATIC) {
            this.currentYPosition = saveY;
            this.currentAreaContainerXPosition = saveX;
        } else
            this.currentYPosition -= area.getHeight();

	}

	public void renderSpanArea(SpanArea area) {
		Enumeration e = area.getChildren().elements();
		while (e.hasMoreElements()) {
			Box b = (Box) e.nextElement();
			b.render(this);	// column areas
		}
	}

    private void doFrame(Area area) {
        int w, h;
        int rx = this.currentAreaContainerXPosition;
        w = area.getContentWidth();
        if (area instanceof BlockArea)
            rx += ((BlockArea) area).getStartIndent();
        h = area.getContentHeight();
        int ry = this.currentYPosition;
        ColorType bg = area.getBackgroundColor();

        rx = rx - area.getPaddingLeft();
        ry = ry + area.getPaddingTop();
        w = w + area.getPaddingLeft() + area.getPaddingRight();
        h = h + area.getPaddingTop() + area.getPaddingBottom();

        // I'm not sure I should have to check for bg being null
        // but I do
        if ((bg != null) && (bg.alpha() == 0)) {
            this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
        }

        //rx = rx - area.getBorderLeftWidth();
        //ry = ry + area.getBorderTopWidth();
        //w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
        //h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();

	// Handle line style
	// Offset for haft the line width!
	BorderAndPadding bp = area.getBorderAndPadding();
	int left = rx - area.getBorderLeftWidth() / 2;
	int right = rx + w + area.getBorderRightWidth() / 2;
	int top = ry + area.getBorderTopWidth() / 2;
	int bottom = ry - h - area.getBorderBottomWidth() / 2;
        if (area.getBorderTopWidth() != 0)
            addLine(left, top, right, top, area.getBorderTopWidth(),
                    new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
        if (area.getBorderLeftWidth() != 0)
            addLine(left, ry + area.getBorderTopWidth(), left, bottom, area.getBorderLeftWidth(),
                    new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
        if (area.getBorderRightWidth() != 0)
            addLine(right, ry + area.getBorderTopWidth(), right, bottom, area.getBorderRightWidth(),
                    new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
        if (area.getBorderBottomWidth() != 0)
            addLine(rx - area.getBorderLeftWidth(), bottom, rx + w + area.getBorderRightWidth(), bottom, area.getBorderBottomWidth(),
                    new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));

    }


    /**
       * render block area
       *
       * @param area the block area to render
       */
    public void renderBlockArea(BlockArea area) {
      // KLease: Temporary test to fix block positioning
      // Offset ypos by padding and border widths
        this.currentYPosition -= (area.getPaddingTop() + area.getBorderTopWidth());
        doFrame(area);
        Enumeration e = area.getChildren().elements();
        while (e.hasMoreElements()) {
            Box b = (Box) e.nextElement();
            b.render(this);
        }
	this.currentYPosition -= (area.getPaddingBottom() + area.getBorderBottomWidth());
    }

    /**
       * render display space
       *
       * @param space the display space to render
       */
    public void renderDisplaySpace(DisplaySpace space) {
        int d = space.getSize();
        this.currentYPosition -= d;
    }

    /**
       * render image area
       *
       * @param area the image area to render
       */
    public abstract void renderImageArea(ImageArea area);

    /** render a foreign object area */
    public abstract void renderForeignObjectArea(ForeignObjectArea area);

    /**
       * render SVG area
       *
       * @param area the SVG area to render
       */
    public abstract void renderSVGArea(SVGArea area);

    /**
       * render inline area
       *
       * @param area inline area to render
       */
    public abstract void renderWordArea(WordArea area);

	protected void addWordLines(WordArea area, int rx, int bl, int size, PDFColor theAreaColor)
	{
        if (area.getUnderlined()) {
            int yPos = bl - size/10;
            addLine(rx, yPos, rx + area.getContentWidth(),
                    yPos, size/14, theAreaColor);
            // save position for underlining a following InlineSpace
            prevUnderlineXEndPos = rx + area.getContentWidth();
            prevUnderlineYEndPos = yPos;
            prevUnderlineSize = size/14;
            prevUnderlineColor = theAreaColor;
        }

        if (area.getOverlined()) {
            int yPos = bl + area.getFontState().getAscender() + size/10;
            addLine(rx, yPos, rx + area.getContentWidth(),
                    yPos, size/14, theAreaColor);
            prevOverlineXEndPos = rx + area.getContentWidth();
            prevOverlineYEndPos = yPos;
            prevOverlineSize = size/14;
            prevOverlineColor = theAreaColor;
        }

        if (area.getLineThrough()) {
            int yPos = bl + area.getFontState().getAscender() * 3/8;
            addLine(rx, yPos, rx + area.getContentWidth(),
                    yPos, size/14, theAreaColor);
            prevLineThroughXEndPos = rx + area.getContentWidth();
            prevLineThroughYEndPos = yPos;
            prevLineThroughSize = size/14;
            prevLineThroughColor = theAreaColor;
        }
	}
    
    /**
       * render inline space
       *
       * @param space space to render
       */
    public void renderInlineSpace(InlineSpace space) {
        this.currentXPosition += space.getSize();
        if (space.getUnderlined()) {
            if (prevUnderlineColor != null) {
               addLine(prevUnderlineXEndPos, prevUnderlineYEndPos,
                       prevUnderlineXEndPos + space.getSize(),
                       prevUnderlineYEndPos, prevUnderlineSize, prevUnderlineColor);
            }
        }
        if (space.getOverlined()) {
            if (prevOverlineColor != null) {
                addLine(prevOverlineXEndPos, prevOverlineYEndPos,
                        prevOverlineXEndPos + space.getSize(),
                        prevOverlineYEndPos, prevOverlineSize, prevOverlineColor);
            }
        }
        if (space.getLineThrough()) {
            if (prevLineThroughColor != null) {
                addLine(prevLineThroughXEndPos, prevLineThroughYEndPos,
                        prevLineThroughXEndPos + space.getSize(),
                        prevLineThroughYEndPos, prevLineThroughSize, prevLineThroughColor);
            }
         }
    }

    /**
       * render line area
       *
       * @param area area to render
       */
    public void renderLineArea(LineArea area) {
        int rx = this.currentAreaContainerXPosition + area.getStartIndent();
        int ry = this.currentYPosition;
        int w = area.getContentWidth();
        int h = area.getHeight();

        this.currentYPosition -= area.getPlacementOffset();
        this.currentXPosition = rx;

        int bl = this.currentYPosition;

        Enumeration e = area.getChildren().elements();
        while (e.hasMoreElements()) {
            Box b = (Box) e.nextElement();
            if(b instanceof InlineArea) {
                InlineArea ia = (InlineArea)b;
                this.currentYPosition = ry - ia.getYOffset();
            } else {
                this.currentYPosition = ry - area.getPlacementOffset();
            }
            b.render(this);
        }

        this.currentYPosition = ry - h;
        this.currentXPosition = rx;
    }

    /**
       * render page
       *
       * @param page page to render
       */
    public abstract void renderPage(Page page);

    /**
       * render leader area
       *
       * @param area area to render
       */
    public void renderLeaderArea(LeaderArea area) {
        int rx = this.currentXPosition;
        int ry = this.currentYPosition;
        int w = area.getContentWidth();
        int h = area.getHeight();
        int th = area.getRuleThickness();
        int st = area.getRuleStyle();

        //checks whether thickness is = 0, because of bug in pdf (or where?),
        //a line with thickness 0 is still displayed
        if (th != 0) {
            switch (st) {
                case org.apache.fop.fo.properties.RuleStyle.DOUBLE:
                    addLine(rx, ry, rx + w, ry, th / 3, st,
                            new PDFColor(area.getRed(),
                                         area.getGreen(), area.getBlue()));
                    addLine(rx, ry + (2 * th / 3), rx + w,
                            ry + (2 * th / 3), th / 3, st,
                            new PDFColor(area.getRed(),
                                         area.getGreen(), area.getBlue()));
                    break;
                case org.apache.fop.fo.properties.RuleStyle.GROOVE:
                    addLine(rx, ry, rx + w, ry, th / 2, st,
                            new PDFColor(area.getRed(),
                                         area.getGreen(), area.getBlue()));
                    addLine(rx, ry + (th / 2), rx + w, ry + (th / 2),
                            th / 2, st, new PDFColor(255, 255, 255));
                    break;
                case org.apache.fop.fo.properties.RuleStyle.RIDGE:
                    addLine(rx, ry, rx + w, ry, th / 2, st,
                            new PDFColor(255, 255, 255));
                    addLine(rx, ry + (th / 2), rx + w, ry + (th / 2),
                            th / 2, st,
                            new PDFColor(area.getRed(),
                                         area.getGreen(), area.getBlue()));
                    break;
                default:
                    addLine(rx, ry, rx + w, ry, th, st,
                            new PDFColor(area.getRed(),
                                         area.getGreen(), area.getBlue()));
            }
            this.currentXPosition += area.getContentWidth();
            this.currentYPosition += th;
        }
    }

    /**
       * set up the font info
       *
       * @param fontInfo font info to set up
       */
    public void setupFontInfo(FontInfo fontInfo) {
        this.fontInfo = fontInfo;
        FontSetup.setup(fontInfo);
    }
}