aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/afp/AFPState.java
blob: 2f01e837173a15f962fd86ba4f6312b3d1803905 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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;

import java.awt.geom.AffineTransform;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.render.AbstractState;

/**
 * This keeps information about the current state when writing to an AFP datastream.
 */
public class AFPState extends org.apache.fop.render.AbstractState implements Cloneable {

    private static final long serialVersionUID = 8206711712452344473L;

    private static Log log = LogFactory.getLog("org.apache.fop.render.afp.AFPState");

    /** the portrait rotation */
    private int portraitRotation = 0;

    /** the landscape rotation */
    private int landscapeRotation = 270;

    /** color image support */
    private boolean colorImages = false;

    /** images are supported in this AFP environment */
    private boolean nativeImages;

    /** default value for image depth */
    private int bitsPerPixel = 8;

    /** the output resolution */
    private int resolution = 240; // 240 dpi

    /** the current page */
    private AFPPageState pageState = new AFPPageState();

    /** a unit converter */
    private final transient AFPUnitConverter unitConv = new AFPUnitConverter(this);


    /**
     * Sets the rotation to be used for portrait pages, valid values are 0
     * (default), 90, 180, 270.
     *
     * @param rotation
     *            The rotation in degrees.
     */
    protected 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");

        }
    }

    /**
     * Returns the rotation to be used for portrait pages
     *
     * @return the rotation to be used for portrait pages
     */
    protected int getPortraitRotation() {
        return this.portraitRotation;
    }

    /**
     * Sets the rotation to be used for landscape pages, valid values are 0, 90,
     * 180, 270 (default).
     *
     * @param rotation
     *            The rotation in degrees.
     */
    protected 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");
        }
    }

    /**
     * Returns the landscape rotation
     *
     * @return the landscape rotation
     */
    protected int getLandscapeRotation() {
        return this.landscapeRotation;
    }

    /**
     * Sets the number of bits used per pixel
     *
     * @param bitsPerPixel
     *            number of bits per pixel
     */
    public void setBitsPerPixel(int bitsPerPixel) {
        switch (bitsPerPixel) {
        case 1:
        case 4:
        case 8:
            this.bitsPerPixel = bitsPerPixel;
            break;
        default:
            log.warn("Invalid bits_per_pixel value, must be 1, 4 or 8.");
            this.bitsPerPixel = 8;
            break;
        }
    }

    /**
     * Returns the number of bits per pixel
     *
     * @return the number of bits per pixel
     */
    public int getBitsPerPixel() {
        return this.bitsPerPixel;
    }

    /**
     * Sets whether images are color or not
     *
     * @param colorImages
     *            color image output
     */
    public void setColorImages(boolean colorImages) {
        this.colorImages = colorImages;
    }

    /**
     * Returns true if color images are to be used
     *
     * @return true if color images are to be used
     */
    protected boolean isColorImages() {
        return this.colorImages;
    }

    /**
     * Sets whether images are natively supported or not in the AFP environment
     *
     * @param nativeImages true if images are natively supported in this AFP environment
     */
    public void setNativeImages(boolean nativeImages) {
        this.nativeImages = nativeImages;
    }

    /**
     * Returns true if images are supported natively in this AFP environment
     *
     * @return true if images are supported natively in this AFP environment
     */
    protected boolean isNativeImages() {
        return this.nativeImages;
    }

    /**
     * Sets the output/device resolution
     *
     * @param resolution
     *            the output resolution (dpi)
     */
    public void setResolution(int resolution) {
        if (log.isDebugEnabled()) {
            log.debug("renderer-resolution set to: " + resolution + "dpi");
        }
        this.resolution = resolution;
    }

    /**
     * Returns the output/device resolution.
     *
     * @return the resolution in dpi
     */
    protected int getResolution() {
        return this.resolution;
    }

    /** {@inheritDoc} */
    protected AbstractData instantiateData() {
        return new AFPData();
    }

    /** {@inheritDoc} */
    protected AbstractState instantiateState() {
        return new AFPState();
    }

    /**
     * Returns the state of the current page
     *
     * @return the state of the current page
     */
    protected AFPPageState getPageState() {
        return this.pageState;
    }

    /**
     * Sets if the current painted shape is to be filled
     *
     * @param fill true if the current painted shape is to be filled
     * @return true if the fill value has changed
     */
    protected boolean setFill(boolean fill) {
        if (fill != ((AFPData)getData()).filled) {
            ((AFPData)getData()).filled = fill;
            return true;
        }
        return false;
    }

    /**
     * Gets the current page fonts
     *
     * @return the current page fonts
     */
    protected AFPPageFonts getPageFonts() {
        return pageState.getFonts();
    }

    /**
     * Increments and returns the page font count
     *
     * @return the page font count
     */
    public int incrementPageFontCount() {
        return pageState.incrementFontCount();
    }

    /**
     * Sets the page width
     *
     * @param pageWidth the page width
     */
    public void setPageWidth(int pageWidth) {
        pageState.setWidth(pageWidth);
    }

    /**
     * Returns the page width
     *
     * @return the page width
     */
    public int getPageWidth() {
        return pageState.getWidth();
    }

    /**
     * Sets the page height
     *
     * @param pageHeight the page height
     */
    public void setPageHeight(int pageHeight) {
        pageState.setHeight(pageHeight);
    }

    /**
     * Returns the page height
     *
     * @return the page height
     */
    public int getPageHeight() {
        return pageState.getHeight();
    }

    /**
     * Sets the uri of the current image
     *
     * @param uri the uri of the current image
     */
    protected void setImageUri(String uri) {
        ((AFPData)getData()).imageUri = uri;
    }

    /**
     * Gets the uri of the current image
     *
     * @return the uri of the current image
     */
    public String getImageUri() {
        return ((AFPData)getData()).imageUri;
    }

    /**
     * Returns the current orientation
     *
     * @return the current orientation
     */
    public int getOrientation() {
        AffineTransform at = getData().getTransform();
        int orientation = 0;
        if (at.getScaleX() == 0 && at.getScaleY() == 0
                && at.getShearX() == 1 && at.getShearY() == -1) {
            orientation = 90;
        } else if (at.getScaleX() == -1 && at.getScaleY() == -1
                && at.getShearX() == 0 && at.getShearY() == 0) {
            orientation = 180;
        } else if (at.getScaleX() == 0 && at.getScaleY() == 0
                && at.getShearX() == -1 && at.getShearY() == 1) {
            orientation = 270;
        }
        return orientation;
    }

    /**
     * Returns the unit converter
     *
     * @return the unit converter
     */
    public AFPUnitConverter getUnitConverter() {
        return this.unitConv;
    }

    /** {@inheritDoc} */
    public Object clone() {
        AFPState state = (AFPState)super.clone();
        state.pageState = (AFPPageState)this.pageState.clone();
        state.portraitRotation = this.portraitRotation;
        state.landscapeRotation = this.landscapeRotation;
        state.bitsPerPixel = this.bitsPerPixel;
        state.colorImages = this.colorImages;
        state.resolution = this.resolution;
        return state;
    }

    /** {@inheritDoc} */
    public String toString() {
        return "AFPState{" + "portraitRotation=" + portraitRotation
        + ", landscapeRotation=" + landscapeRotation
        + ", colorImages=" + colorImages
        + ", bitsPerPixel=" + bitsPerPixel
        + ", resolution=" + resolution
        + ", pageState=" + pageState
        + super.toString()
        + "}";
    }

    /**
     * Page level state data
     */
    private class AFPPageState implements Cloneable {
        /** The current page width */
        private int width = 0;

        /** The current page height */
        private int height = 0;

        /** The current page fonts */
        private AFPPageFonts fonts = new AFPPageFonts();

        /** The current page font count */
        private int fontCount = 0;

        /**
         * Returns the page width
         *
         * @return the page width
         */
        protected int getWidth() {
            return width;
        }

        /**
         * Sets the page width
         *
         * @param width the page width
         */
        protected void setWidth(int width) {
            this.width = width;
        }

        /**
         * Returns the page height
         *
         * @return the page height
         */
        protected int getHeight() {
            return height;
        }

        /**
         * Sets the page height
         *
         * @param height the page height
         */
        protected void setHeight(int height) {
            this.height = height;
        }

        /**
         * Returns the page fonts
         *
         * @return the page fonts
         */
        protected AFPPageFonts getFonts() {
            return fonts;
        }

        /**
         * Sets the current page fonts
         *
         * @param fonts the current page fonts
         */
        protected void setFonts(AFPPageFonts fonts) {
            this.fonts = fonts;
        }

        /**
         * Increments and returns the current page font count
         *
         * @return increment and return the current page font count
         */
        protected int incrementFontCount() {
            return ++fontCount;
        }

        /** {@inheritDoc} */
        public Object clone() {
            AFPPageState state = new AFPPageState();
            state.fonts = new AFPPageFonts(this.fonts);
            state.height = this.height;
            state.width = this.width;
            state.fontCount = this.fontCount;
            return state;
        }

        /** {@inheritDoc} */
        public String toString() {
            return "AFPPageState{width=" + width
            + ", height=" + height
            + ", fonts=" + fonts
            + ", fontCount=" + fontCount
            + "}";
        }
    }

    /**
     * Block level state data
     */
    private class AFPData extends org.apache.fop.render.AbstractState.AbstractData {
        private static final long serialVersionUID = -1789481244175275686L;

        /** The current fill status */
        private boolean filled = false;

        private String imageUri = null;

        /** {@inheritDoc} */
        public Object clone() {
            AFPData obj = (AFPData)super.clone();
            obj.filled = this.filled;
            obj.imageUri = this.imageUri;
            return obj;
        }

        /** {@inheritDoc} */
        public String toString() {
            return "AFPData{" + super.toString()
            + ", filled=" + filled
            + ", imageUri=" + imageUri
            + "}";
        }
    }
}