blob: 1e4c320f46b8b2fc91e45221032b67b54eec429a (
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
|
/*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.mif;
import org.apache.fop.apps.StructureHandler;
import org.apache.fop.layout.FontInfo;
import org.apache.fop.fo.pagination.*;
import org.apache.fop.fo.flow.*;
import org.apache.fop.fo.*;
import org.apache.fop.apps.FOPException;
// Java
import java.io.*;
import java.util.*;
import org.xml.sax.SAXException;
// do we really want every method throwing a SAXException
/**
* The MIF Handler.
* This generates MIF output using the structure events from
* the FO Tree sent to this structure handler.
* This builds an MIF file and writes it to the output.
*/
public class MIFHandler extends StructureHandler {
protected MIFFile mifFile;
protected OutputStream outStream;
private FontInfo fontInfo = new FontInfo();
// current state elements
MIFElement textFlow;
MIFElement para;
/**
*/
public MIFHandler(OutputStream os) {
outStream = os;
// use pdf fonts for now, this is only for resolving names
org.apache.fop.render.pdf.FontSetup.setup(fontInfo, null);
}
public FontInfo getFontInfo() {
return fontInfo;
}
public void startDocument() throws SAXException {
mifFile = new MIFFile();
try {
mifFile.output(outStream);
} catch(IOException ioe) {
throw new SAXException(ioe);
}
}
public void endDocument() throws SAXException {
// finish all open elements
mifFile.finish(true);
try {
mifFile.output(outStream);
outStream.flush();
} catch(IOException ioe) {
throw new SAXException(ioe);
}
}
/**
* Start the page sequence.
* This creates the pages in the MIF document that will be used
* by the following flows and static areas.
*/
public void startPageSequence(PageSequence pageSeq, Title seqTitle, LayoutMasterSet lms) {
// get the layout master set
// setup the pages for this sequence
String name = pageSeq.getProperty("master-reference").getString();
SimplePageMaster spm = lms.getSimplePageMaster(name);
if(spm == null) {
PageSequenceMaster psm = lms.getPageSequenceMaster(name);
} else {
// create simple master with regions
MIFElement prop = new MIFElement("PageType");
prop.setValue("BodyPage");
MIFElement page = new MIFElement("Page");
page.addElement(prop);
prop = new MIFElement("PageBackground");
prop.setValue("'Default'");
page.addElement(prop);
// build regions
MIFElement textRect = new MIFElement("TextRect");
prop = new MIFElement("ID");
prop.setValue("1");
textRect.addElement(prop);
prop = new MIFElement("ShapeRect");
prop.setValue("0.0 841.889 453.543 0.0");
textRect.addElement(prop);
page.addElement(textRect);
textRect = new MIFElement("TextRect");
prop = new MIFElement("ID");
prop.setValue("2");
textRect.addElement(prop);
prop = new MIFElement("ShapeRect");
prop.setValue("0.0 841.889 453.543 187.65");
textRect.addElement(prop);
page.addElement(textRect);
mifFile.addPage(page);
}
}
public void endPageSequence(PageSequence pageSeq) throws FOPException {
}
public void startFlow(Flow fl) {
// start text flow in body region
textFlow = new MIFElement("TextFlow");
}
public void endFlow(Flow fl) {
textFlow.finish(true);
mifFile.addElement(textFlow);
textFlow = null;
}
public void startBlock(Block bl) {
para = new MIFElement("Para");
// get font
textFlow.addElement(para);
}
public void endBlock(Block bl) {
para.finish(true);
para = null;
}
public void characters(char data[], int start, int length) {
if(para != null) {
String str = new String(data, start, length);
str = str.trim();
// break into nice length chunks
if(str.length() == 0) {
return;
}
MIFElement line = new MIFElement("ParaLine");
MIFElement prop = new MIFElement("TextRectID");
prop.setValue("2");
line.addElement(prop);
prop = new MIFElement("String");
prop.setValue("\"" + str + "\"");
line.addElement(prop);
para.addElement(line);
}
}
}
|