aboutsummaryrefslogtreecommitdiffstats
path: root/src/sandbox/org/apache/fop/render/afp/modca/PresentationTextData.java
blob: bd659c59d99e01afb5fab6e6ac2871f10baa8f26 (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
/*
 * 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.fop.render.afp.tools.BinaryUtils;

/**
 * Presentation text data contains the graphic characters and the control
 * sequences necessary to position the characters within the object space. The
 * data consists of: - graphic characters to be presented - control sequences
 * that position them - modal control sequences that adjust the positions by
 * small amounts - other functions causing text to be presented with differences
 * in appearance.
 *
 * The graphic characters are expected to conform to a coded font representation
 * so that they can be translated from the code point in the object data to the
 * character in the coded font. The units of measure for linear displacements
 * are derived from the PresentationTextDescriptor or from the hierarchical
 * defaults.
 *
 * In addition to graphic character code points, Presentation Text data can
 * contain embedded control sequences. These are strings of two or more bytes
 * which signal an alternate mode of processing for the content of the current
 * Presentation Text data.
 *
 */
public class PresentationTextData extends AbstractAFPObject {

    /**
     * The maximum size of the presentation text data.
     */
    private static final int MAX_SIZE = 8192;

    /**
     * The afp data relating to this presentaion text data.
     */
    private ByteArrayOutputStream _baos = new ByteArrayOutputStream(1024);

    /**
     * The current x coordinate.
     */
    private int _currentXCoordinate = -1;

    /**
     * The current y cooridnate
     */
    private int _currentYCoordinate = -1;

    /**
     * The current font
     */
    private String _currentFont = "";

    /**
     * The current orientation
     */
    private int _currentOrientation = 0;

    /**
     * The current color
     */
    private Color _currentColor = new Color(0, 0, 0);

    /**
     * The current variable space increment
     */
    private int _currentVariableSpaceCharacterIncrement = 0;

    /**
     * The current inter character adjustment
     */
    private int _currentInterCharacterAdjustment = 0;

    /**
     * Default constructor for the PresentationTextData.
     */
    public PresentationTextData() {

        this(false);

    }

    /**
     * Constructor for the PresentationTextData, the boolean flag indicate
     * whether the control sequence prefix should be set to indicate the start
     * of a new control sequence.
     *
     * @param controlInd
     *            The control sequence indicator.
     */
    public PresentationTextData(boolean controlInd) {

        _baos.write(new byte[] { 0x5A, // Structured field identifier
            0x00, // Record length byte 1
            0x00, // Record length byte 2
            (byte) 0xD3, // PresentationTextData identifier byte 1
            (byte) 0xEE, // PresentationTextData identifier byte 2
            (byte) 0x9B, // PresentationTextData identifier byte 3
            0x00, // Flag
            0x00, // Reserved
            0x00, // Reserved
        }, 0, 9);

        if (controlInd) {
            _baos.write(new byte[] { 0x2B, (byte) 0xD3 }, 0, 2);
        }

    }

    /**
     * The Set Coded Font Local control sequence activates a coded font and
     * specifies the character attributes to be used. This is a modal control
     * sequence.
     *
     * @param font
     *            The font local identifier.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void setCodedFont(byte font, ByteArrayOutputStream afpdata) {

        // Avoid unnecessary specification of the font
        if (String.valueOf(font).equals(_currentFont)) {
            return;
        } else {
            _currentFont = String.valueOf(font);
        }

        afpdata.write(new byte[] { 0x03, (byte) 0xF1, font, }, 0, 3);

    }

    /**
     * Establishes the current presentation position on the baseline at a new
     * I-axis coordinate, which is a specified number of measurement units from
     * the B-axis. There is no change to the current B-axis coordinate.
     *
     * @param coordinate
     *            The coordinate for the inline move.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void absoluteMoveInline(int coordinate,
        ByteArrayOutputStream afpdata) {

        byte[] b = BinaryUtils.convert(coordinate, 2);

        afpdata.write(new byte[] { 0x04, (byte) 0xC7, b[0], b[1], }, 0, 4);

        _currentXCoordinate = coordinate;

    }

    /**
     * Establishes the baseline and the current presentation position at a new
     * B-axis coordinate, which is a specified number of measurement units from
     * the I-axis. There is no change to the current I-axis coordinate.
     *
     * @param coordinate
     *            The coordinate for the baseline move.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void absoluteMoveBaseline(int coordinate,
        ByteArrayOutputStream afpdata) {

        byte[] b = BinaryUtils.convert(coordinate, 2);

        afpdata.write(new byte[] { 0x04, (byte) 0xD3, b[0], b[1], }, 0, 4);

        _currentYCoordinate = coordinate;

    }

    /**
     * The Transparent Data control sequence contains a sequence of code points
     * that are presented without a scan for embedded control sequences.
     *
     * @param data
     *            The text data to add.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void addTransparentData(byte[] data, ByteArrayOutputStream afpdata) {

        // Calculate the length
        int l = data.length + 2;

        if (l > 255) {
            // Check that we are not exceeding the maximum length
            throw new IllegalArgumentException(
                "Transparent data is longer than 253 bytes: " + data);
        }

        afpdata.write(new byte[] { BinaryUtils.convert(l)[0], (byte) 0xDB, },
            0, 2);

        afpdata.write(data, 0, data.length);

    }

    /**
     * Draws a line of specified length and specified width in the B-direction
     * from the current presentation position. The location of the current
     * presentation position is unchanged.
     *
     * @param length
     *            The length of the rule.
     * @param width
     *            The width of the rule.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void drawBaxisRule(int length, int width,
        ByteArrayOutputStream afpdata) {

        afpdata.write(new byte[] { 0x07, // Length
            (byte) 0xE7, // Type
        }, 0, 2);

        // Rule length
        byte[] data1 = BinaryUtils.shortToByteArray((short) length);
        afpdata.write(data1, 0, data1.length);
        // Rule width
        byte[] data2 = BinaryUtils.shortToByteArray((short) width);
        afpdata.write(data2, 0, data2.length);
        // Rule width fraction
        afpdata.write(0x00);

    }

    /**
     * Draws a line of specified length and specified width in the I-direction
     * from the current presentation position. The location of the current
     * presentation position is unchanged.
     *
     * @param length
     *            The length of the rule.
     * @param width
     *            The width of the rule.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void drawIaxisRule(int length, int width,
        ByteArrayOutputStream afpdata) {

        afpdata.write(new byte[] { 0x07, // Length
            (byte) 0xE5, // Type
        }, 0, 2);

        // Rule length
        byte[] data1 = BinaryUtils.shortToByteArray((short) length);
        afpdata.write(data1, 0, data1.length);
        // Rule width
        byte[] data2 = BinaryUtils.shortToByteArray((short) width);
        afpdata.write(data2, 0, data2.length);
        // Rule width fraction
        afpdata.write(0x00);

    }

    /**
     * Create the presentation text data for the byte array of data.
     *
     * @param fontNumber
     *            The font resource identifier.
     * @param x
     *            The x coordinate for the text data.
     * @param y
     *            The y coordinate for the text data.
     * @param orientation
     *            The orientation of the text data.
     * @param col
     *            The text color.
     * @param vsci
     *            The variable space character increment.
     * @param ica
     *            The inter character adjustment.
     * @param data
     *            The text data to be created.
     * @throws MaximumSizeExceededException
     */
    public void createTextData(int fontNumber, int x, int y, int orientation,
        Color col, int vsci, int ica, byte[] data)
        throws MaximumSizeExceededException {

        ByteArrayOutputStream afpdata = new ByteArrayOutputStream();

        if (_currentOrientation != orientation) {
            setTextOrientation(orientation, afpdata);
            _currentOrientation = orientation;
            _currentXCoordinate = -1;
            _currentYCoordinate = -1;
        }

        // Avoid unnecessary specification of the Y co-ordinate
        if (y != _currentYCoordinate) {
            absoluteMoveBaseline(y, afpdata);
            _currentXCoordinate = -1;
        }

        // Avoid unnecessary specification of the X co-ordinate
        if (x != _currentXCoordinate) {
            absoluteMoveInline(x, afpdata);
        }

        // Avoid unnecessary specification of the variable space increment
        if (vsci != _currentVariableSpaceCharacterIncrement) {
            setVariableSpaceCharacterIncrement(vsci, afpdata);
            _currentVariableSpaceCharacterIncrement = vsci;
        }

        // Avoid unnecessary specification of the inter character adjustment
        if (ica != _currentInterCharacterAdjustment) {
            setInterCharacterAdjustment(ica, afpdata);
            _currentInterCharacterAdjustment = ica;
        }

        // Avoid unnecessary specification of the text color
        if (!col.equals(_currentColor)) {
            setExtendedTextColor(col, afpdata);
            _currentColor = col;
        }

        setCodedFont(BinaryUtils.convert(fontNumber)[0], afpdata);
        addTransparentData(data, afpdata);
        _currentXCoordinate = -1;

        int s = afpdata.size();

        if (_baos.size() + s > MAX_SIZE) {
            _currentXCoordinate = -1;
            _currentYCoordinate = -1;
            throw new MaximumSizeExceededException();
        }

        byte[] outputdata = afpdata.toByteArray();
        _baos.write(outputdata, 0, outputdata.length);

    }

    /**
     * Drawing of lines using the starting and ending coordinates, thickness and
     * colour arguments.
     *
     * @param x1
     *            The starting X coordinate.
     * @param y1
     *            The starting Y coordinate.
     * @param x2
     *            The ending X coordinate.
     * @param y2
     *            The ending Y coordinate.
     * @param thickness
     *            The line thickness.
     * @param orientation
     *            The orientation of the text data.
     * @param col
     *            The text color.
     */
    public void createLineData(int x1, int y1, int x2, int y2, int thickness,
        int orientation, Color col) throws MaximumSizeExceededException {

        ByteArrayOutputStream afpdata = new ByteArrayOutputStream();

        if (_currentOrientation != orientation) {
            setTextOrientation(orientation, afpdata);
            _currentOrientation = orientation;
        }

        // Avoid unnecessary specification of the Y coordinate
        if (y1 != _currentYCoordinate) {
            absoluteMoveBaseline(y1, afpdata);
        }

        // Avoid unnecessary specification of the X coordinate
        if (x1 != _currentXCoordinate) {
            absoluteMoveInline(x1, afpdata);
        }

        if (!col.equals(_currentColor)) {
            setExtendedTextColor(col, afpdata);
            _currentColor = col;
        }

        if (y1 == y2) {
            drawIaxisRule(x2 - x1, thickness, afpdata);
        } else if (x1 == x2) {
            drawBaxisRule(y2 - y1, thickness, afpdata);
        } else {
            return;
        }

        int s = afpdata.size();

        if (_baos.size() + s > MAX_SIZE) {
            _currentXCoordinate = -1;
            _currentYCoordinate = -1;
            throw new MaximumSizeExceededException();
        }

        byte[] outputdata = afpdata.toByteArray();
        _baos.write(outputdata, 0, outputdata.length);

    }

    /**
     * The Set Text Orientation control sequence establishes the I-direction and
     * B-direction for the subsequent text. This is a modal control sequence.
     *
     * Semantics: This control sequence specifies the I-axis and B-axis
     * orientations with respect to the Xp-axis for the current Presentation
     * Text object. The orientations are rotational values expressed in degrees
     * and minutes.
     *
     * @param orientation
     *            The text orientation (0,90, 180, 270).
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void setTextOrientation(int orientation,
        ByteArrayOutputStream afpdata) {

        afpdata.write(new byte[] { 0x06, (byte) 0xF7, }, 0, 2);

        switch (orientation) {
            case 90:
                afpdata.write(0x2D);
                afpdata.write(0x00);
                afpdata.write(0x5A);
                afpdata.write(0x00);
                break;
            case 180:
                afpdata.write(0x5A);
                afpdata.write(0x00);
                afpdata.write(0x87);
                afpdata.write(0x00);
                break;
            case 270:
                afpdata.write(0x87);
                afpdata.write(0x00);
                afpdata.write(0x00);
                afpdata.write(0x00);
                break;
            default:
                afpdata.write(0x00);
                afpdata.write(0x00);
                afpdata.write(0x2D);
                afpdata.write(0x00);
                break;
        }

    }

    /**
     * The Set Extended Text Color control sequence specifies a color value and
     * defines the color space and encoding for that value.  The specified color
     * value is applied to foreground areas of the text presentation space.
     * This is a modal control sequence.
     *
     * @param col
     *            The color to be set.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void setExtendedTextColor(Color col,
        ByteArrayOutputStream afpdata) {

        afpdata.write(new byte[] {
              15                // Control sequence length
            , (byte)0x81        // Control sequence function type
            , 0x00              // Reserved; must be zero
            , 0x01              // Color space - 0x01 = RGB
            , 0x00              // Reserved; must be zero
            , 0x00              // Reserved; must be zero
            , 0x00              // Reserved; must be zero
            , 0x00              // Reserved; must be zero
            , 8                 // Number of bits in component 1
            , 8                 // Number of bits in component 2
            , 8                 // Number of bits in component 3
            , 0                 // Number of bits in component 4
            , (byte)(col.getRed())   // Red intensity
            , (byte)(col.getGreen()) // Green intensity
            , (byte)(col.getBlue())  // Blue intensity
        }, 0, 15);

    }

    /**
     * //TODO
     * This is a modal control sequence.
     *
     * @param incr
     *            The increment to be set.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void setVariableSpaceCharacterIncrement(int incr,
        ByteArrayOutputStream afpdata) {

        byte[] b = BinaryUtils.convert(incr, 2);

        afpdata.write(new byte[] {
              4                  // Control sequence length
            , (byte)0xC5         // Control sequence function type
            , b[0]
            , b[1]
        }, 0, 4);

    }

    /**
     * //TODO
     * This is a modal control sequence.
     *
     * @param incr
     *            The increment to be set.
     * @param afpdata
     *            The output stream to which data should be written.
     */
    private void setInterCharacterAdjustment(int incr,
        ByteArrayOutputStream afpdata) {

        byte[] b = BinaryUtils.convert(Math.abs(incr), 2);

        afpdata.write(new byte[] {
              5                  // Control sequence length
            , (byte)0xC3         // Control sequence function type
            , b[0]
            , b[1]
            , (byte)(incr >= 0 ? 0 : 1) // Direction
        }, 0, 5);

    }

    /**
     * Accessor method to write the AFP datastream for
     * the text data.
     * @param os The stream to write to
     * @throws java.io.IOException
     */
    public void writeDataStream(OutputStream os)
        throws IOException {

        byte[] data = _baos.toByteArray();
        byte[] size = BinaryUtils.convert(data.length - 1, 2);
        data[1] = size[0];
        data[2] = size[1];

        os.write(data);

    }

    /**
     * A control sequence is a sequence of bytes that specifies a control
     * function. A control sequence consists of a control sequence introducer
     * and zero or more parameters. The control sequence can extend multiple
     * presentation text data objects, but must eventually be terminated. This
     * method terminates the control sequence.
     *
     * @throws MaximumSizeExceededException
     */
    public void endControlSequence() throws MaximumSizeExceededException {

        byte[] data = new byte[2];
        data[0] = 0x02;
        data[1] = (byte) 0xF8;

        if (data.length + _baos.size() > MAX_SIZE) {
            throw new MaximumSizeExceededException();
        }

        _baos.write(data, 0, data.length);

    }

}