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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/*
* 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.afp;
import java.awt.Color;
import java.awt.Point;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.afp.fonts.AFPFont;
import org.apache.fop.afp.fonts.AFPFontAttributes;
import org.apache.fop.afp.fonts.CharacterSet;
import org.apache.fop.afp.modca.AbstractPageObject;
import org.apache.fop.afp.modca.Document;
import org.apache.fop.afp.modca.InterchangeSet;
import org.apache.fop.afp.modca.Overlay;
import org.apache.fop.afp.modca.PageGroup;
import org.apache.fop.afp.modca.PageObject;
import org.apache.fop.afp.modca.ResourceGroup;
import org.apache.fop.afp.modca.TagLogicalElementBean;
import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
import org.apache.fop.afp.ptoca.PtocaBuilder;
import org.apache.fop.afp.ptoca.PtocaProducer;
import org.apache.fop.fonts.Font;
import org.apache.fop.util.CharUtilities;
/**
* 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 DataStream {
/** Static logging instance */
protected static final Log LOG = LogFactory.getLog("org.apache.xmlgraphics.afp");
/** Boolean completion indicator */
private boolean complete = false;
/** 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;
/** Sequence number for TLE's.*/
private int tleSequence = 0;
/** The MO:DCA interchange set in use (default to MO:DCA-P IS/2 set) */
private InterchangeSet interchangeSet
= InterchangeSet.valueOf(InterchangeSet.MODCA_PRESENTATION_INTERCHANGE_SET_2);
private final Factory factory;
private OutputStream outputStream;
/** the afp painting state */
private final AFPPaintingState paintingState;
/**
* Default constructor for the AFPDocumentStream.
*
* @param factory the resource factory
* @param paintingState the AFP painting state
* @param outputStream the outputstream to write to
*/
public DataStream(Factory factory, AFPPaintingState paintingState, OutputStream outputStream) {
this.paintingState = paintingState;
this.factory = factory;
this.outputStream = outputStream;
}
/**
* Returns the outputstream
*
* @return the outputstream
*/
public OutputStream getOutputStream() {
return this.outputStream;
}
/**
* Returns the document object
*
* @return the document object
*/
private Document getDocument() {
return this.document;
}
/**
* Returns the current page
*
* @return the current page
*/
public AbstractPageObject getCurrentPage() {
return this.currentPage;
}
/**
* The document is started by invoking this method which creates an instance
* of the AFP Document object.
*
* @param name
* the name of this document.
*/
public void setDocumentName(String name) {
if (name != null) {
getDocument().setFullyQualifiedName(
FullyQualifiedNameTriplet.TYPE_BEGIN_DOCUMENT_REF,
FullyQualifiedNameTriplet.FORMAT_CHARSTR, name);
}
}
/**
* Helper method to mark the end of the current document.
*
* @throws IOException thrown if an I/O exception of some sort has occurred
*/
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();
}
// Write out document
if (document != null) {
document.endDocument();
document.writeToStream(this.outputStream);
}
this.outputStream.flush();
this.complete = true;
this.document = null;
this.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
* @param pageWidthRes
* the width resolution of the page
* @param pageHeightRes
* the height resolution of the page
*/
public void startPage(int pageWidth, int pageHeight, int pageRotation,
int pageWidthRes, int pageHeightRes) {
currentPageObject = factory.createPage(pageWidth, pageHeight,
pageRotation, pageWidthRes, pageHeightRes);
currentPage = currentPageObject;
currentOverlay = null;
}
/**
* 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 x
* the x position of the overlay on the page
* @param y
* the y position of the overlay on the page
* @param width
* the width of the overlay
* @param height
* the height of the overlay
* @param widthRes
* the width resolution of the overlay
* @param heightRes
* the height resolution of the overlay
* @param overlayRotation
* the rotation of the overlay
*/
public void startOverlay(int x, int y, int width, int height, int widthRes,
int heightRes, int overlayRotation) {
this.currentOverlay = factory.createOverlay(
width, height, widthRes, heightRes, overlayRotation);
String overlayName = currentOverlay.getName();
currentPageObject.createIncludePageOverlay(overlayName, x, y, 0);
currentPage = currentOverlay;
}
/**
* Helper method to mark the end of the current overlay.
*
* @throws IOException thrown if an I/O exception of some sort has occurred
*/
public void endOverlay() throws IOException {
if (currentOverlay != null) {
currentOverlay.endPage();
currentOverlay = null;
currentPage = currentPageObject;
}
}
/**
* Helper method to save the current page.
*
* @return current page object that was saved
*/
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.
*
* @param pageObject
* page object
*/
public void restorePage(PageObject pageObject) {
currentPageObject = pageObject;
currentPage = pageObject;
}
/**
* Helper method to mark the end of the current page.
*
* @throws IOException thrown if an I/O exception of some sort has occurred
*/
public void endPage() throws IOException {
if (currentPageObject != null) {
currentPageObject.endPage();
if (currentPageGroup != null) {
currentPageGroup.addPage(currentPageObject);
currentPageGroup.writeToStream(this.outputStream);
} else {
document.addPage(currentPageObject);
document.writeToStream(this.outputStream);
}
currentPageObject = null;
currentPage = null;
}
}
/**
* Creates the given page fonts in the current page
*
* @param pageFonts
* a collection of AFP font attributes
*/
public void addFontsToCurrentPage(Map pageFonts) {
Iterator iter = pageFonts.values().iterator();
while (iter.hasNext()) {
AFPFontAttributes afpFontAttributes = (AFPFontAttributes) iter
.next();
createFont(afpFontAttributes.getFontReference(), afpFontAttributes
.getFont(), afpFontAttributes.getPointSize());
}
}
/**
* 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(int fontReference, AFPFont font, int size) {
currentPage.createFont(fontReference, font, size);
}
/**
* Returns a point on the current page
*
* @param x the X-coordinate
* @param y the Y-coordinate
* @return a point on the current page
*/
private Point getPoint(int x, int y) {
return paintingState.getPoint(x, y);
}
/**
* 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 textDataInfo the afp text data
* @param letterSpacing letter spacing to draw text with
* @param wordSpacing word Spacing to draw text with
* @param font is the font to draw text with
* @param charSet is the AFP Character Set to use with the text
* @throws UnsupportedEncodingException thrown if character encoding is not supported
*/
public void createText(final AFPTextDataInfo textDataInfo, final int letterSpacing,
final int wordSpacing, final Font font, final CharacterSet charSet)
throws UnsupportedEncodingException {
int rotation = paintingState.getRotation();
if (rotation != 0) {
textDataInfo.setRotation(rotation);
Point p = getPoint(textDataInfo.getX(), textDataInfo.getY());
textDataInfo.setX(p.x);
textDataInfo.setY(p.y);
}
// use PtocaProducer to create PTX records
PtocaProducer producer = new PtocaProducer() {
public void produce(PtocaBuilder builder) throws IOException {
builder.setTextOrientation(textDataInfo.getRotation());
builder.absoluteMoveBaseline(textDataInfo.getY());
builder.absoluteMoveInline(textDataInfo.getX());
builder.setExtendedTextColor(textDataInfo.getColor());
builder.setCodedFont((byte)textDataInfo.getFontReference());
int l = textDataInfo.getString().length();
StringBuffer sb = new StringBuffer();
int interCharacterAdjustment = 0;
AFPUnitConverter unitConv = paintingState.getUnitConverter();
if (letterSpacing != 0) {
interCharacterAdjustment = Math.round(unitConv.mpt2units(letterSpacing));
}
builder.setInterCharacterAdjustment(interCharacterAdjustment);
int spaceWidth = font.getCharWidth(CharUtilities.SPACE);
int spacing = spaceWidth + letterSpacing;
int fixedSpaceCharacterIncrement = Math.round(unitConv.mpt2units(spacing));
int varSpaceCharacterIncrement = fixedSpaceCharacterIncrement;
if (wordSpacing != 0) {
varSpaceCharacterIncrement = Math.round(unitConv.mpt2units(
spaceWidth + wordSpacing + letterSpacing));
}
builder.setVariableSpaceCharacterIncrement(varSpaceCharacterIncrement);
boolean fixedSpaceMode = false;
for (int i = 0; i < l; i++) {
char orgChar = textDataInfo.getString().charAt(i);
float glyphAdjust = 0;
if (CharUtilities.isFixedWidthSpace(orgChar)) {
flushText(builder, sb, charSet);
builder.setVariableSpaceCharacterIncrement(
fixedSpaceCharacterIncrement);
fixedSpaceMode = true;
sb.append(CharUtilities.SPACE);
int charWidth = font.getCharWidth(orgChar);
glyphAdjust += (charWidth - spaceWidth);
} else {
if (fixedSpaceMode) {
flushText(builder, sb, charSet);
builder.setVariableSpaceCharacterIncrement(
varSpaceCharacterIncrement);
fixedSpaceMode = false;
}
char ch;
if (orgChar == CharUtilities.NBSPACE) {
ch = ' '; //converted to normal space to allow word spacing
} else {
ch = orgChar;
}
sb.append(ch);
}
if (glyphAdjust != 0) {
flushText(builder, sb, charSet);
int increment = Math.round(unitConv.mpt2units(glyphAdjust));
builder.relativeMoveInline(increment);
}
}
flushText(builder, sb, charSet);
}
private void flushText(PtocaBuilder builder, StringBuffer sb,
final CharacterSet charSet) throws IOException {
if (sb.length() > 0) {
builder.addTransparentData(charSet.encodeChars(sb));
sb.setLength(0);
}
}
};
currentPage.createText(producer);
}
/**
* Method to create a line on the current page.
*
* @param lineDataInfo the line data information.
*/
public void createLine(AFPLineDataInfo lineDataInfo) {
currentPage.createLine(lineDataInfo);
}
/**
* 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 col
* the shading color
*/
public void createShading(int x, int y, int w, int h, Color col) {
currentPageObject.createShading(x, y, w, h, col.getRed(), col.getGreen(), col.getBlue());
}
/**
* 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
* @param x x-coordinate
* @param y y-coordinate
*/
public void createIncludePageOverlay(String name, int x, int y) {
currentPageObject.createIncludePageOverlay(name, x, y, paintingState.getRotation());
currentPageObject.getActiveEnvironmentGroup().createOverlay(name);
}
/**
* Helper method which allows creation of the IMM object.
*
* @param name
* the name of the medium map
*/
public void createInvokeMediumMap(String name) {
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
* @param width
* the width of the image
* @param height
* the height of the image
*/
public void createIncludePageSegment(String name, int x, int y, int width, int height) {
int xOrigin;
int yOrigin;
int orientation = paintingState.getRotation();
switch (orientation) {
case 90:
xOrigin = x - height;
yOrigin = y;
break;
case 180:
xOrigin = x - width;
yOrigin = y - height;
break;
case 270:
xOrigin = x;
yOrigin = y - width;
break;
default:
xOrigin = x;
yOrigin = y;
break;
}
boolean createHardPageSegments = true;
currentPage.createIncludePageSegment(name, xOrigin, yOrigin, createHardPageSegments);
}
/**
* 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 = attributes[i].getKey();
String value = attributes[i].getValue();
currentPage.createTagLogicalElement(name, value, tleSequence++);
}
}
/**
* 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 = attributes[i].getKey();
String value = 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 (currentPage != null) {
currentPage.createTagLogicalElement(name, value, tleSequence++);
} else {
currentPageGroup.createTagLogicalElement(name, value);
}
}
/**
* Creates a NoOperation item
*
* @param content
* byte data
*/
public void createNoOperation(String content) {
if (currentPage != null) {
currentPage.createNoOperation(content);
} else if (currentPageGroup != null) {
currentPageGroup.createNoOperation(content);
} else {
document.createNoOperation(content);
}
}
/**
* Returns the current page group
*
* @return the current page group
*/
public PageGroup getCurrentPageGroup() {
return this.currentPageGroup;
}
/**
* Start a new document.
*
* @throws IOException thrown if an I/O exception of some sort has occurred
*/
public void startDocument() throws IOException {
this.document = factory.createDocument();
document.writeToStream(this.outputStream);
}
/**
* 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.
*
* @throws IOException thrown if an I/O exception of some sort has occurred
*/
public void startPageGroup() throws IOException {
endPageGroup();
this.currentPageGroup = factory.createPageGroup(tleSequence);
}
/**
* Helper method to mark the end of the page group.
*
* @throws IOException thrown if an I/O exception of some sort has occurred
*/
public void endPageGroup() throws IOException {
if (currentPageGroup != null) {
currentPageGroup.endPageGroup();
tleSequence = currentPageGroup.getTleSequence();
document.addPageGroup(currentPageGroup);
currentPageGroup = null;
}
document.writeToStream(outputStream); //Flush objects
}
/**
* Sets the MO:DCA interchange set to use
*
* @param interchangeSet the MO:DCA interchange set
*/
public void setInterchangeSet(InterchangeSet interchangeSet) {
this.interchangeSet = interchangeSet;
}
/**
* Returns the MO:DCA interchange set in use
*
* @return the MO:DCA interchange set in use
*/
public InterchangeSet getInterchangeSet() {
return this.interchangeSet;
}
/**
* Returns the resource group for a given resource info
*
* @param level a resource level
* @return a resource group for the given resource info
*/
public ResourceGroup getResourceGroup(AFPResourceLevel level) {
ResourceGroup resourceGroup = null;
if (level.isDocument()) {
resourceGroup = document.getResourceGroup();
} else if (level.isPageGroup()) {
resourceGroup = currentPageGroup.getResourceGroup();
} else if (level.isPage()) {
resourceGroup = currentPageObject.getResourceGroup();
}
return resourceGroup;
}
}
|