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
|
/*
* $Id: RTFHandler.java,v 1.4 2003/03/07 09:47:56 jeremias Exp $
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "FOP" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ============================================================================
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation and was originally created by
* James Tauber <jtauber@jtauber.com>. For more information on the Apache
* Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.fop.rtf.renderer;
// Java
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.FOPException;
import org.apache.fop.datatypes.ColorType;
import org.apache.fop.fo.FOInputHandler;
import org.apache.fop.fo.flow.Block;
import org.apache.fop.fo.flow.ExternalGraphic;
import org.apache.fop.fo.flow.InstreamForeignObject;
import org.apache.fop.fo.flow.Leader;
import org.apache.fop.fo.flow.ListBlock;
import org.apache.fop.fo.flow.ListItem;
import org.apache.fop.fo.flow.Table;
import org.apache.fop.fo.flow.TableColumn;
import org.apache.fop.fo.flow.TableBody;
import org.apache.fop.fo.flow.TableCell;
import org.apache.fop.fo.flow.TableRow;
import org.apache.fop.fo.pagination.Flow;
import org.apache.fop.fo.pagination.PageSequence;
import org.apache.fop.fo.properties.Constants;
import org.apache.fop.fo.Property;
import org.apache.fop.apps.Document;
import org.apache.fop.rtf.rtflib.rtfdoc.IRtfParagraphContainer;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfAttributes;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfColorTable;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfDocumentArea;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfFile;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfParagraph;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfSection;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfText;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfTable;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfTableRow;
import org.apache.fop.rtf.rtflib.rtfdoc.RtfTableCell;
import org.apache.fop.rtf.rtflib.rtfdoc.IRtfTableContainer;
import org.xml.sax.SAXException;
/**
* RTF Handler: generates RTF output using the structure events from
* the FO Tree sent to this structure handler.
*
* @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
* @author Trembicki-Guy, Ed <GuyE@DNB.com>
* @author Boris Poudérous <boris.pouderous@eads-telecom.com>
* @author Peter Herweg <pherweg@web.de>
*/
public class RTFHandler extends FOInputHandler {
private RtfFile rtfFile;
private final OutputStream os;
private final Logger log = new ConsoleLogger();
private RtfSection sect;
private RtfDocumentArea docArea;
private RtfParagraph para;
private boolean warned = false;
private BuilderContext m_context = new BuilderContext(null);
private static final String ALPHA_WARNING = "WARNING: RTF renderer is "
+ "veryveryalpha at this time, see class org.apache.fop.rtf.renderer.RTFHandler";
/**
* Tracks current background color. BG color is not reset automatically
* anywhere, so we need a persistent way to see whether it has changed.
*/
private int currentRTFBackgroundColor = -1;
/**
* Creates a new RTF structure handler.
* @param os OutputStream to write to
*/
public RTFHandler(Document doc, OutputStream os) {
super(doc);
this.os = os;
// use pdf fonts for now, this is only for resolving names
org.apache.fop.render.pdf.FontSetup.setup(doc, null);
log.warn(ALPHA_WARNING);
}
/**
* @see org.apache.fop.fo.FOInputHandler#startDocument()
*/
public void startDocument() throws SAXException {
// FIXME sections should be created
try {
rtfFile = new RtfFile(new OutputStreamWriter(os));
docArea = rtfFile.startDocumentArea();
} catch (IOException ioe) {
// FIXME could we throw Exception in all FOInputHandler events?
throw new SAXException("IOException: " + ioe);
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#endDocument()
*/
public void endDocument() throws SAXException {
try {
rtfFile.flush();
} catch (IOException ioe) {
// FIXME could we throw Exception in all FOInputHandler events?
throw new SAXException("IOException: " + ioe);
}
}
/**
* @see org.apache.fop.fo.FOInputHandler
*/
public void startPageSequence(PageSequence pageSeq) {
try {
sect = docArea.newSection();
m_context.pushContainer(sect);
if (!warned) {
sect.newParagraph().newText(ALPHA_WARNING);
warned = true;
}
} catch (IOException ioe) {
// FIXME could we throw Exception in all FOInputHandler events?
log.error("startPageSequence: " + ioe.getMessage());
throw new Error("IOException: " + ioe);
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#endPageSequence(PageSequence)
*/
public void endPageSequence(PageSequence pageSeq) throws FOPException {
m_context.popContainer();
}
/**
* @see org.apache.fop.fo.FOInputHandler#startFlow(Flow)
*/
public void startFlow(Flow fl) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endFlow(Flow)
*/
public void endFlow(Flow fl) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startBlock(Block)
*/
public void startBlock(Block bl) {
try {
RtfAttributes rtfAttr = new RtfAttributes();
attrBlockTextAlign(bl, rtfAttr);
attrBlockBackgroundColor(bl, rtfAttr);
attrBlockFontSize(bl, rtfAttr);
attrBlockFontWeight(bl, rtfAttr);
IRtfParagraphContainer pc =
(IRtfParagraphContainer)m_context.getContainer
(IRtfParagraphContainer.class, true, null);
para = pc.newParagraph(rtfAttr);
m_context.pushContainer(para);
} catch (IOException ioe) {
// FIXME could we throw Exception in all FOInputHandler events?
log.error("startBlock: " + ioe.getMessage());
throw new Error("IOException: " + ioe);
} catch (Exception e) {
log.error("startBlock: " + e.getMessage());
throw new Error("Exception: " + e);
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#endBlock(Block)
*/
public void endBlock(Block bl) {
m_context.popContainer();
}
/**
* @see org.apache.fop.fo.FOInputHandler#startTable(Table)
*/
public void startTable(Table tbl) {
// create an RtfTable in the current table container
TableContext tableContext = new TableContext(m_context);
RtfAttributes atts = new RtfAttributes();
try {
final IRtfTableContainer tc =
(IRtfTableContainer)m_context.getContainer(IRtfTableContainer.class,
true, null);
m_context.pushContainer(tc.newTable(atts, tableContext));
} catch (Exception e) {
log.error("startTable:" + e.getMessage());
throw new Error(e.getMessage());
}
m_context.pushTableContext(tableContext);
}
/**
* @see org.apache.fop.fo.FOInputHandler#endTable(Table)
*/
public void endTable(Table tbl) {
m_context.popTableContext();
m_context.popContainer();
}
/**
*
* @param th TableColumn that is starting;
*/
public void startColumn(TableColumn tc) {
try {
Integer iWidth = new Integer(tc.getColumnWidth() / 1000);
m_context.getTableContext().setNextColumnWidth(iWidth.toString() + "pt");
m_context.getTableContext().setNextColumnRowSpanning(new Integer(0), null);
} catch (Exception e) {
log.error("startColumn: " + e.getMessage());
throw new Error(e.getMessage());
}
}
/**
*
* @param th TableColumn that is ending;
*/
public void endColumn(TableColumn tc) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startHeader(TableBody)
*/
public void startHeader(TableBody th) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endHeader(TableBody)
*/
public void endHeader(TableBody th) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startFooter(TableBody)
*/
public void startFooter(TableBody tf) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endFooter(TableBody)
*/
public void endFooter(TableBody tf) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startBody(TableBody)
*/
public void startBody(TableBody tb) {
try {
RtfAttributes atts = TableAttributesConverter.convertRowAttributes (tb.properties,
null, null);
RtfTable tbl = (RtfTable)m_context.getContainer(RtfTable.class, true, this);
tbl.setHeaderAttribs(atts);
} catch (Exception e) {
log.error("startBody: " + e.getMessage());
throw new Error(e.getMessage());
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#endBody(TableBody)
*/
public void endBody(TableBody tb) {
try {
RtfTable tbl = (RtfTable)m_context.getContainer(RtfTable.class, true, this);
tbl.setHeaderAttribs(null);
} catch (Exception e) {
log.error("endBody: " + e.getMessage());
throw new Error(e.getMessage());
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#startRow(TableRow)
*/
public void startRow(TableRow tr) {
try {
// create an RtfTableRow in the current RtfTable
final RtfTable tbl = (RtfTable)m_context.getContainer(RtfTable.class,
true, null);
RtfAttributes tblAttribs = tbl.getRtfAttributes();
RtfAttributes tblRowAttribs = new RtfAttributes();
RtfAttributes atts = TableAttributesConverter.convertRowAttributes(tr.properties,
null, tbl.getHeaderAttribs());
m_context.pushContainer(tbl.newTableRow(atts));
// reset column iteration index to correctly access column widths
m_context.getTableContext().selectFirstColumn();
} catch (Exception e) {
log.error("startRow: " + e.getMessage());
throw new Error(e.getMessage());
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#endRow(TableRow)
*/
public void endRow(TableRow tr) {
m_context.popContainer();
m_context.getTableContext().decreaseRowSpannings();
}
/**
* @see org.apache.fop.fo.FOInputHandler#startCell(TableCell)
*/
public void startCell(TableCell tc) {
try {
TableContext tctx = m_context.getTableContext();
final RtfTableRow row = (RtfTableRow)m_context.getContainer(RtfTableRow.class,
true, null);
//while the current column is in row-spanning, act as if
//a vertical merged cell would have been specified.
while (tctx.getNumberOfColumns() > tctx.getColumnIndex()
&& tctx.getColumnRowSpanningNumber().intValue() > 0) {
row.newTableCellMergedVertically((int)tctx.getColumnWidth(),
tctx.getColumnRowSpanningAttrs());
tctx.selectNextColumn();
}
//get the width of the currently started cell
float width = tctx.getColumnWidth();
// create an RtfTableCell in the current RtfTableRow
RtfAttributes atts = TableAttributesConverter.convertCellAttributes(tc.properties,
null);
RtfTableCell cell = row.newTableCell((int)width, atts);
//process number-rows-spanned attribute
Property p = null;
if ((p = tc.properties.get("number-rows-spanned")) != null && false) {
// Start vertical merge
cell.setVMerge(RtfTableCell.MERGE_START);
// set the number of rows spanned
tctx.setCurrentColumnRowSpanning(new Integer(p.getNumber().intValue()),
cell.getRtfAttributes());
} else {
tctx.setCurrentColumnRowSpanning(new Integer(1), null);
}
m_context.pushContainer(cell);
} catch (Exception e) {
log.error("startCell: " + e.getMessage());
throw new Error(e.getMessage());
}
}
/**
* @see org.apache.fop.fo.FOInputHandler#endCell(TableCell)
*/
public void endCell(TableCell tc) {
m_context.popContainer();
m_context.getTableContext().selectNextColumn();
}
// Lists
/**
* @see org.apache.fop.fo.FOInputHandler#startList(ListBlock)
*/
public void startList(ListBlock lb) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endList(ListBlock)
*/
public void endList(ListBlock lb) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startListItem(ListItem)
*/
public void startListItem(ListItem li) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endListItem(ListItem)
*/
public void endListItem(ListItem li) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startListLabel()
*/
public void startListLabel() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endListLabel()
*/
public void endListLabel() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startListBody()
*/
public void startListBody() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endListBody()
*/
public void endListBody() {
}
// Static Regions
/**
* @see org.apache.fop.fo.FOInputHandler#startStatic()
*/
public void startStatic() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endStatic()
*/
public void endStatic() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startMarkup()
*/
public void startMarkup() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endMarkup()
*/
public void endMarkup() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#startLink()
*/
public void startLink() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#endLink()
*/
public void endLink() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#image(ExternalGraphic)
*/
public void image(ExternalGraphic eg) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#pageRef()
*/
public void pageRef() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#foreignObject(InstreamForeignObject)
*/
public void foreignObject(InstreamForeignObject ifo) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#footnote()
*/
public void footnote() {
}
/**
* @see org.apache.fop.fo.FOInputHandler#leader(Leader)
*/
public void leader(Leader l) {
}
/**
* @see org.apache.fop.fo.FOInputHandler#characters(char[], int, int)
*/
public void characters(char data[], int start, int length) {
try {
para.newText(new String(data, start, length));
} catch (IOException ioe) {
// FIXME could we throw Exception in all FOInputHandler events?
log.error("characters: " + ioe.getMessage());
throw new Error("IOException: " + ioe);
}
}
private void attrBlockFontSize(Block bl, RtfAttributes rtfAttr) {
int fopValue = bl.properties.get("font-size").getLength().getValue() / 500;
rtfAttr.set("fs", fopValue);
}
private void attrBlockFontWeight(Block bl, RtfAttributes rtfAttr) {
String fopValue = bl.properties.get("font-weight").getString();
if (fopValue == "bold" || fopValue == "700") {
rtfAttr.set("b", 1);
} else {
rtfAttr.set("b", 0);
}
}
private void attrBlockTextAlign(Block bl, RtfAttributes rtfAttr) {
int fopValue = bl.properties.get("text-align").getEnum();
String rtfValue = null;
switch (fopValue) {
case Constants.CENTER: {
rtfValue = RtfText.ALIGN_CENTER;
break;
}
case Constants.END: {
rtfValue = RtfText.ALIGN_RIGHT;
break;
}
case Constants.JUSTIFY: {
rtfValue = RtfText.ALIGN_JUSTIFIED;
break;
}
default: {
rtfValue = RtfText.ALIGN_LEFT;
break;
}
}
rtfAttr.set(rtfValue);
}
private void attrBlockBackgroundColor(Block bl, RtfAttributes rtfAttr) {
ColorType fopValue = bl.properties.get("background-color").getColorType();
int rtfColor = 0;
/* FOP uses a default background color of "transparent", which is
actually a transparent black, which is generally not suitable as a
default here. Changing FOP's default to "white" causes problems in
PDF output, so we will look for the default here & change it to
white. */
if ((fopValue.getRed() == 0) && (fopValue.getGreen() == 0)
&& (fopValue.getBlue() == 0) && (fopValue.getAlpha() == 0)) {
rtfColor = RtfColorTable.getInstance().getColorNumber("white").intValue();
currentRTFBackgroundColor = -1;
return;
} else {
rtfColor = convertFOPColorToRTF(fopValue);
}
if (rtfColor != currentRTFBackgroundColor) {
rtfAttr.set(RtfText.ATTR_BACKGROUND_COLOR, rtfColor);
currentRTFBackgroundColor = rtfColor;
}
}
/**
* Converts a FOP ColorType to the integer pointing into the RTF color table
* @param fopColor the ColorType object to be converted
* @return integer pointing into the RTF color table
*/
public static int convertFOPColorToRTF(ColorType fopColor) {
int redComponent = ColorType.convertChannelToInteger (fopColor.getRed());
int greenComponent = ColorType.convertChannelToInteger (fopColor.getGreen());
int blueComponent = ColorType.convertChannelToInteger (fopColor.getBlue());
return RtfColorTable.getInstance().getColorNumber(redComponent,
greenComponent, blueComponent).intValue();
}
}
|