aboutsummaryrefslogtreecommitdiffstats
path: root/src/sandbox/org/apache/fop/render/afp/modca/AFPDataStream.java
blob: 0babc927acc9b2aaf00d40eb13cbdee258490dfb (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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * Copyright 2006 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$ */

package org.apache.fop.render.afp.modca;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.render.afp.fonts.AFPFont;
import org.apache.fop.render.afp.tools.StringUtils;

/**
 * A data stream is a continuous ordered stream of data elements and objects
 * conforming to a given format. Application programs can generate data streams
 * destined for a presentation service, archive library, presentation device or
 * another application program. The strategic presentation data stream
 * architectures used is Mixed Object Document Content Architecture (MO:DCA�).
 *
 * The MO:DCA architecture defines the data stream used by applications to
 * describe documents and object envelopes for interchange with other
 * applications and application services. Documents defined in the MO:DCA format
 * may be archived in a database, then later retrieved, viewed, annotated and
 * printed in local or distributed systems environments. Presentation fidelity
 * is accommodated by including resource objects in the documents that reference
 * them.
 *
 */
public class AFPDataStream {

    /**
     * Static logging instance
     */
    protected static final Log log = LogFactory.getLog("org.apache.fop.render.afp.modca");

    /**
     * Boolean completion indicator
     */
    private boolean _complete = false;

    /**
     * The application producing the AFP document
     */
    private String _producer = null;

    /**
     * The AFP document object
     */
    private Document _document = null;

    /**
     * The current page group object
     */
    private PageGroup _currentPageGroup = null;

    /**
     * The current page object
     */
    private PageObject _currentPageObject = null;

    /**
     * The current overlay object
     */
    private Overlay _currentOverlay = null;

    /**
     * The current page
     */
    private AbstractPageObject _currentPage = null;

    /**
     * The page count
     */
    private int _pageCount = 0;

    /**
     * The page group count
     */
    private int _pageGroupCount = 0;

    /**
     * The overlay count
     */
    private int _ovlCount = 0;

    /**
     * The portrait rotation
     */
    private int _portraitRotation = 0;

    /**
     * The landscape rotation
     */
    private int _landscapeRotation = 270;

    /**
     * The x offset
     */
    private int _xOffset = 0;

    /**
     * The y offset
     */
    private int _yOffset = 0;

    /**
     * The rotation
     */
    private int _rotation;

    /**
     * The outputstream for the data stream
     */
    private OutputStream _outputStream = null;

    /**
     * Default constructor for the AFPDataStream.
     */
    public AFPDataStream() {
    }

    /**
     * The document is started by invoking this method which creates an instance
     * of the AFP Document object.
     */
    public void startDocument(OutputStream outputStream) {

        if (_document != null) {
            String msg = "Invalid state - document already started.";
            log.warn("startDocument():: " + msg);
            throw new IllegalStateException(msg);
        }

        _document = new Document();
        _outputStream = outputStream;

    }

    /**
     * The document is ended by invoking this method which creates an instance
     * of the AFP Document object and registers the start with a validation map
     * which ensures that methods are not invoked out of the correct sequence.
     */
    public void endDocument()
        throws IOException {

        if (_complete) {
            String msg = "Invalid state - document already ended.";
            log.warn("endDocument():: " + msg);
            throw new IllegalStateException(msg);
        }

        if (_currentPageObject != null) {
            // End the current page if necessary
            endPage();
        }

        if (_currentPageGroup != null) {
            // End the current page group if necessary
            endPageGroup();
        }

        _document.endDocument();
        _document.writeDataStream(_outputStream);
        _outputStream.flush();

        _complete = true;

        _document = null;

        _outputStream = null;
    }

    /**
     * Start a new page. When processing has finished on the current page, the
     * {@link #endPage()}method must be invoked to mark the page ending.
     *
     * @param pageWidth
     *            the width of the page
     * @param pageHeight
     *            the height of the page
     * @param pageRotation
     *            the rotation of the page
     */
    public void startPage(int pageWidth, int pageHeight, int pageRotation) {

        String pageName = "PGN"
            + StringUtils.lpad(String.valueOf(_pageCount++), '0', 5);

        _currentPageObject = new PageObject(pageName, pageWidth, pageHeight, pageRotation);
        _currentPage = _currentPageObject;
        _currentOverlay = null;
        setOffsets(0, 0, 0);
    }

    /**
     * Start a new overlay. When processing has finished on the current overlay, the
     * {@link #endOverlay()}method must be invoked to mark the overlay ending.
     *
     * @param overlayX
     *            the x position of the overlay on the page
     * @param overlayY
     *            the y position of the overlay on the page
     * @param overlayWidth
     *            the width of the overlay
     * @param overlayHeight
     *            the height of the overlay
     * @param overlayRotation
     *            the rotation of the overlay
     */
    public void startOverlay(int overlayX, int overlayY, int overlayWidth, int overlayHeight, int overlayRotation) {

        String overlayName = "OVL"
            + StringUtils.lpad(String.valueOf(_ovlCount++), '0', 5);

        _currentOverlay = new Overlay(overlayName, overlayWidth, overlayHeight, overlayRotation);
        _currentPageObject.addOverlay(_currentOverlay);
        _currentPageObject.createIncludePageOverlay(overlayName, overlayX, overlayY, 0);
        _currentPage = _currentOverlay;
        setOffsets(0, 0, 0);
    }

    /**
     * Helper method to mark the end of the current overlay.
     */
    public void endOverlay() {

        _currentOverlay.endPage();
        _currentOverlay = null;
        _currentPage = _currentPageObject;

    }

    /**
     * Helper method to save the current page.
     */
    public PageObject savePage() {

        PageObject pageObject = _currentPageObject;
        if (_currentPageGroup != null) {
            _currentPageGroup.addPage(_currentPageObject);
        } else {
            _document.addPage(_currentPageObject);
        }
        _currentPageObject = null;
        _currentPage = null;
        return pageObject;

    }

    /**
     * Helper method to restore the current page.
     */
    public void restorePage(PageObject pageObject) {

        _currentPageObject = pageObject;
        _currentPage = pageObject;

    }

    /**
     * Helper method to mark the end of the current page.
     */
    public void endPage()
        throws IOException {

        _currentPageObject.endPage();
        if (_currentPageGroup != null) {
            _currentPageGroup.addPage(_currentPageObject);
        } else {
            _document.addPage(_currentPageObject);
            _document.writeDataStream(_outputStream);
        }

        _currentPageObject = null;
        _currentPage = null;

    }

    /**
     * Sets the offsets to be used for element positioning
     *
     * @param xOffset
     *            the offset in the x direction
     * @param yOffset
     *            the offset in the y direction
     * @param rotation
     *            the rotation
     */
    public void setOffsets(int xOffset, int yOffset, int rotation) {
        _xOffset = xOffset;
        _yOffset = yOffset;
        _rotation = rotation;
    }

    /**
     * Helper method to create a map coded font object on the current page, this
     * method delegates the construction of the map coded font object to the
     * active environment group on the current page.
     *
     * @param fontReference
     *            the font number used as the resource identifier
     * @param font
     *            the font
     * @param size
     *            the point size of the font
     */
    public void createFont(byte fontReference, AFPFont font, int size) {

        _currentPage.createFont(fontReference, font, size);

    }

    /**
     * Helper method to create text on the current page, this method delegates
     * to the current presentation text object in order to construct the text.
     *
     * @param fontNumber
     *            the font number used as the resource identifier
     * @param x
     *            the x coordinate of the text
     * @param y
     *            the y coordinate of the text
     * @param col
     *            the text color
     * @param vsci
     *            The variable space character increment.
     * @param ica
     *            The inter character adjustment.
     * @param data
     *            the text data to create
     */
    public void createText(int fontNumber, int x, int y, Color col, int vsci, int ica, byte[] data) {

        _currentPage.createText(fontNumber, x + _xOffset, y + _yOffset, _rotation, col, vsci, ica, data);

    }

    /**
     * Returns an ImageObject used to create an image in the datastream.
     *
     * @param x
     *            the x position of the image
     * @param y
     *            the y position of the image
     * @param w
     *            the width of the image
     * @param h
     *            the height of the image
     */
    public ImageObject getImageObject(int x, int y, int w, int h) {

        int xOrigin;
        int yOrigin;
        int width;
        int height;
        switch (_rotation) {
            case 90:
                xOrigin = _currentPage.getWidth() - y - _yOffset;
                yOrigin = x + _xOffset;
                width = h;
                height = w;
                break;
            case 180:
                xOrigin = _currentPage.getWidth() - x - _xOffset;
                yOrigin = _currentPage.getHeight() - y - _yOffset;
                width = w;
                height = h;
                break;
            case 270:
                xOrigin = y + _yOffset;
                yOrigin = _currentPage.getHeight() - x - _xOffset;
                width = h;
                height = w;
                break;
            default:
                xOrigin = x + _xOffset;
                yOrigin = y + _yOffset;
                width = w;
                height = h;
                break;
        }
        ImageObject io = _currentPage.getImageObject();
        io.setImageViewport(xOrigin, yOrigin, width, height, _rotation);
        return io;

    }

    /**
     * Method to create a line on the current page.
     *
     * @param x1
     *            the first x coordinate of the line
     * @param y1
     *            the first y coordinate of the line
     * @param x2
     *            the second x coordinate of the line
     * @param y2
     *            the second y coordinate of the line
     * @param thickness
     *            the thickness of the line
     * @param col
     *            The text color.
     */
    public void createLine(int x1, int y1, int x2, int y2, int thickness, Color col) {

        _currentPage.createLine(x1 + _xOffset, y1 + _yOffset, x2 + _xOffset, y2 + _yOffset, thickness, _rotation, col);

    }

    /**
     * Sets the application producing the AFP.
     *
     * @param producer
     *            the application producing the AFP datastream
     */
    public void setProducer(String producer) {
        _producer = producer;
    }

    /**
     * This method will create shading on the page using the specified
     * coordinates (the shading contrast is controlled via the red, green, blue
     * parameters, by converting this to grey scale).
     *
     * @param x
     *            the x coordinate of the shading
     * @param y
     *            the y coordinate of the shading
     * @param w
     *            the width of the shaded area
     * @param h
     *            the height of the shaded area
     * @param red
     *            the red value
     * @param green
     *            the green value
     * @param blue
     *            the blue value
     */
    public void createShading(int x, int y, int w, int h, int red, int green,
        int blue) {

        _currentPage.createShading(x + _xOffset, y + _xOffset, w, h, red, green, blue);

    }

    /**
     * Helper method which allows creation of the MPO object, via the AEG. And
     * the IPO via the Page. (See actual object for descriptions.)
     *
     * @param name
     *            the name of the static overlay
     */
    public void createIncludePageOverlay(String name) {

        _currentPageObject.createIncludePageOverlay(name, 0, 0, _rotation);
        ActiveEnvironmentGroup aeg = _currentPageObject.getActiveEnvironmentGroup();
        aeg.createOverlay(name);

    }

    /**
     * Helper method which allows creation of the IMM object.
     *
     * @param name
     *            the name of the medium map
     */
    public void createInvokeMediumMap(String name) {

        if (_currentPageGroup == null) {
            startPageGroup();
        }
        _currentPageGroup.createInvokeMediumMap(name);

    }

    /**
     * Creates an IncludePageSegment on the current page.
     *
     * @param name
     *            the name of the include page segment
     * @param x
     *            the x coordinate for the overlay
     * @param y
     *            the y coordinate for the overlay
     */
    public void createIncludePageSegment(String name, int x, int y) {

        int xOrigin;
        int yOrigin;
        switch (_rotation) {
            case 90:
                xOrigin = _currentPage.getWidth() - y - _yOffset;
                yOrigin = x + _xOffset;
                break;
            case 180:
                xOrigin = _currentPage.getWidth() - x - _xOffset;
                yOrigin = _currentPage.getHeight() - y - _yOffset;
                break;
            case 270:
                xOrigin = y + _yOffset;
                yOrigin = _currentPage.getHeight() - x - _xOffset;
                break;
            default:
                xOrigin = x + _xOffset;
                yOrigin = y + _yOffset;
                break;
        }
        _currentPage.createIncludePageSegment(name, xOrigin, yOrigin);

    }

    /**
     * Creates a TagLogicalElement on the current page.
     *
     * @param attributes
     *            the array of key value pairs.
     */
    public void createPageTagLogicalElement(TagLogicalElementBean[] attributes) {

        for (int i = 0; i < attributes.length; i++) {
            String name = (String) attributes[i].getKey();
            String value = (String) attributes[i].getValue();
            _currentPage.createTagLogicalElement(name, value);
        }

    }

    /**
     * Creates a TagLogicalElement on the current page group.
     *
     * @param attributes
     *            the array of key value pairs.
     */
    public void createPageGroupTagLogicalElement(
        TagLogicalElementBean[] attributes) {

        for (int i = 0; i < attributes.length; i++) {
            String name = (String) attributes[i].getKey();
            String value = (String) attributes[i].getValue();
            _currentPageGroup.createTagLogicalElement(name, value);
        }

    }

    /**
     * Creates a TagLogicalElement on the current page or page group
     *
     * @param name
     *            The tag name
     * @param value
     *            The tag value
     */
    public void createTagLogicalElement(String name, String value) {

        if (_currentPageGroup != null) {
            _currentPageGroup.createTagLogicalElement(name, value);
        } else {
            _currentPage.createTagLogicalElement(name, value);
        }

    }

    /**
     * Start a new page group. When processing has finished on the current page
     * group the {@link #endPageGroup()}method must be invoked to mark the page
     * group ending.
     *
     * @param name
     *            the name of the page group
     */
    public void startPageGroup() {

        String pageGroupName = "PGP"
            + StringUtils.lpad(String.valueOf(_pageCount++), '0', 5);

        _currentPageGroup = new PageGroup(pageGroupName);

    }

    /**
     * Helper method to mark the end of the page group.
     */
    public void endPageGroup()
        throws IOException {

        _currentPageGroup.endPageGroup();
        _document.addPageGroup(_currentPageGroup);
        _document.writeDataStream(_outputStream);
        _currentPageGroup = null;

    }

    /**
     * Sets the rotation to be used for portrait pages, valid values are 0
     * (default), 90, 180, 270.
     *
     * @param rotation
     *            The rotation in degrees.
     */
    public void setPortraitRotation(int rotation) {

        if (rotation == 0 || rotation == 90 || rotation == 180
            || rotation == 270) {
            _portraitRotation = rotation;
        } else {
            throw new IllegalArgumentException(
                "The portrait rotation must be one of the values 0, 90, 180, 270");
        }

    }

    /**
     * Sets the rotation to be used for landscape pages, valid values are 0, 90,
     * 180, 270 (default).
     *
     * @param rotation
     *            The rotation in degrees.
     */
    public void setLandscapeRotation(int rotation) {

        if (rotation == 0 || rotation == 90 || rotation == 180
            || rotation == 270) {
            _landscapeRotation = rotation;
        } else {
            throw new IllegalArgumentException(
                "The landscape rotation must be one of the values 0, 90, 180, 270");
        }

    }

}