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
|
/*
* 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.geom.Rectangle2D;
import org.apache.fop.afp.ioca.ImageContent;
import org.apache.fop.afp.modca.AbstractDataObject;
import org.apache.fop.afp.modca.AbstractNamedAFPObject;
import org.apache.fop.afp.modca.Document;
import org.apache.fop.afp.modca.GraphicsObject;
import org.apache.fop.afp.modca.ImageObject;
import org.apache.fop.afp.modca.IncludeObject;
import org.apache.fop.afp.modca.ObjectContainer;
import org.apache.fop.afp.modca.Overlay;
import org.apache.fop.afp.modca.PageSegment;
import org.apache.fop.afp.modca.Registry;
import org.apache.fop.afp.modca.ResourceObject;
import org.apache.fop.afp.modca.triplets.MappingOptionTriplet;
import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet;
import org.apache.xmlgraphics.image.codec.tiff.TIFFImage;
import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
/**
* Factory for high level data objects (Image/Graphics etc)
*/
public class AFPDataObjectFactory {
private final Factory factory;
/**
* Main constructor
*
* @param factory an object factory
*/
public AFPDataObjectFactory(Factory factory) {
this.factory = factory;
}
/**
* Creates and configures an ObjectContainer.
*
* @param dataObjectInfo the object container info
* @return a newly created Object Container
*/
public ObjectContainer createObjectContainer(AFPDataObjectInfo dataObjectInfo) {
ObjectContainer objectContainer = factory.createObjectContainer();
// set data object viewport (i.e. position, rotation, dimension, resolution)
objectContainer.setViewport(dataObjectInfo);
// set object classification
Registry.ObjectType objectType = dataObjectInfo.getObjectType();
AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
AFPResourceLevel resourceLevel = resourceInfo.getLevel();
final boolean dataInContainer = true;
final boolean containerHasOEG = resourceLevel.isInline();
final boolean dataInOCD = true;
objectContainer.setObjectClassification(
ObjectClassificationTriplet.CLASS_TIME_INVARIANT_PAGINATED_PRESENTATION_OBJECT,
objectType, dataInContainer, containerHasOEG, dataInOCD);
objectContainer.setData(dataObjectInfo.getData());
return objectContainer;
}
/**
* Creates and configures an IOCA Image Object.
*
* @param imageObjectInfo the image object info
* @return a newly created IOCA Image Object
*/
public ImageObject createImage(AFPImageObjectInfo imageObjectInfo) {
// IOCA bitmap image
ImageObject imageObj = factory.createImageObject();
// set data object viewport (i.e. position, rotation, dimension, resolution)
imageObj.setViewport(imageObjectInfo);
if (imageObjectInfo.hasCompression()) {
int compression = imageObjectInfo.getCompression();
switch (compression) {
case TIFFImage.COMP_FAX_G3_1D:
imageObj.setEncoding(ImageContent.COMPID_G3_MH);
break;
case TIFFImage.COMP_FAX_G3_2D:
imageObj.setEncoding(ImageContent.COMPID_G3_MR);
break;
case TIFFImage.COMP_FAX_G4_2D:
imageObj.setEncoding(ImageContent.COMPID_G3_MMR);
break;
default:
throw new IllegalStateException(
"Invalid compression scheme: " + compression);
}
}
if (imageObjectInfo.isColor()) {
imageObj.setIDESize((byte) 24);
} else {
imageObj.setIDESize((byte) imageObjectInfo.getBitsPerPixel());
}
imageObj.setData(imageObjectInfo.getData());
return imageObj;
}
/**
* Creates and returns a new graphics object.
*
* @param graphicsObjectInfo the graphics object info
* @return a new graphics object
*/
public GraphicsObject createGraphic(AFPGraphicsObjectInfo graphicsObjectInfo) {
// set newly created graphics object in g2d
GraphicsObject graphicsObj = factory.createGraphicsObject();
// set data object viewport (i.e. position, rotation, dimension, resolution)
graphicsObj.setViewport(graphicsObjectInfo);
AFPGraphics2D g2d = graphicsObjectInfo.getGraphics2D();
g2d.setGraphicsObject(graphicsObj);
// paint to graphics object
Graphics2DImagePainter painter = graphicsObjectInfo.getPainter();
Rectangle2D area = graphicsObjectInfo.getArea();
g2d.scale(1, -1);
g2d.translate(0, -area.getHeight());
painter.paint(g2d, area);
graphicsObj.setComplete(true);
// return painted graphics object
return graphicsObj;
}
/**
* Creates and returns a new include object.
*
* @param includeName the include name
* @param dataObjectInfo a data object info
*
* @return a new include object
*/
public IncludeObject createInclude(String includeName, AFPDataObjectInfo dataObjectInfo) {
IncludeObject includeObj = factory.createInclude(includeName);
if (dataObjectInfo instanceof AFPImageObjectInfo) {
// IOCA image object
includeObj.setObjectType(IncludeObject.TYPE_IMAGE);
} else if (dataObjectInfo instanceof AFPGraphicsObjectInfo) {
// graphics object
includeObj.setObjectType(IncludeObject.TYPE_GRAPHIC);
} else {
// object container
includeObj.setObjectType(IncludeObject.TYPE_OTHER);
// set mandatory object classification (type other)
Registry.ObjectType objectType = dataObjectInfo.getObjectType();
if (objectType != null) {
// set object classification
final boolean dataInContainer = true;
final boolean containerHasOEG = false; // environment parameters set in include
final boolean dataInOCD = true;
includeObj.setObjectClassification(
// object scope not defined
ObjectClassificationTriplet.CLASS_TIME_VARIANT_PRESENTATION_OBJECT,
objectType, dataInContainer, containerHasOEG, dataInOCD);
} else {
throw new IllegalStateException(
"Failed to set Object Classification Triplet on Object Container.");
}
}
AFPObjectAreaInfo objectAreaInfo = dataObjectInfo.getObjectAreaInfo();
int xOffset = objectAreaInfo.getX();
int yOffset = objectAreaInfo.getY();
includeObj.setObjectAreaOffset(xOffset, yOffset);
int width = objectAreaInfo.getWidth();
int height = objectAreaInfo.getHeight();
includeObj.setObjectAreaSize(width, height);
int rotation = objectAreaInfo.getRotation();
includeObj.setObjectAreaOrientation(rotation);
int widthRes = objectAreaInfo.getWidthRes();
int heightRes = objectAreaInfo.getHeightRes();
includeObj.setMeasurementUnits(widthRes, heightRes);
includeObj.setMappingOption(MappingOptionTriplet.SCALE_TO_FIT);
return includeObj;
}
/**
* Creates a resource object wrapper for named includable data objects
*
* @param namedObj an named object
* @param resourceInfo resource information
* @param objectType the object type
* @return a new resource object wrapper
*/
public ResourceObject createResource(AbstractNamedAFPObject namedObj,
AFPResourceInfo resourceInfo, Registry.ObjectType objectType) {
ResourceObject resourceObj = null;
String resourceName = resourceInfo.getName();
if (resourceName != null) {
resourceObj = factory.createResource(resourceName);
} else {
resourceObj = factory.createResource();
}
if (namedObj instanceof Document) {
resourceObj.setType(ResourceObject.TYPE_DOCUMENT);
} else if (namedObj instanceof PageSegment) {
resourceObj.setType(ResourceObject.TYPE_PAGE_SEGMENT);
} else if (namedObj instanceof Overlay) {
resourceObj.setType(ResourceObject.TYPE_OVERLAY_OBJECT);
} else if (namedObj instanceof AbstractDataObject) {
AbstractDataObject dataObj = (AbstractDataObject)namedObj;
if (namedObj instanceof ObjectContainer) {
resourceObj.setType(ResourceObject.TYPE_OBJECT_CONTAINER);
// set object classification
final boolean dataInContainer = true;
final boolean containerHasOEG = false; // must be included
final boolean dataInOCD = true;
// mandatory triplet for object container
resourceObj.setObjectClassification(
ObjectClassificationTriplet.CLASS_TIME_INVARIANT_PAGINATED_PRESENTATION_OBJECT,
objectType, dataInContainer, containerHasOEG, dataInOCD);
} else if (namedObj instanceof ImageObject) {
// ioca image type
resourceObj.setType(ResourceObject.TYPE_IMAGE);
} else if (namedObj instanceof GraphicsObject) {
resourceObj.setType(ResourceObject.TYPE_GRAPHIC);
} else {
throw new UnsupportedOperationException(
"Unsupported resource object for data object type " + dataObj);
}
} else {
throw new UnsupportedOperationException(
"Unsupported resource object type " + namedObj);
}
// set the resource information/classification on the data object
resourceObj.setDataObject(namedObj);
return resourceObj;
}
}
|