KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > flow > ExternalGraphic


1 /*
2  * $Id: ExternalGraphic.java,v 1.13.2.7 2003/04/11 00:24:38 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.fo.flow;
52
53 // FOP
54
import org.apache.fop.fo.*;
55 import org.apache.fop.fo.properties.*;
56 import org.apache.fop.layout.*;
57 import org.apache.fop.apps.FOPException;
58 import org.apache.fop.image.*;
59
60 // Java
61
import java.net.MalformedURLException JavaDoc;
62
63 public class ExternalGraphic extends FObj {
64
65     int breakAfter;
66     int breakBefore;
67     int align;
68     int startIndent;
69     int endIndent;
70     int spaceBefore;
71     int spaceAfter;
72     String JavaDoc src;
73     int height;
74     int width;
75     String JavaDoc id;
76
77     ImageArea imageArea;
78
79     public static class Maker extends FObj.Maker {
80         public FObj make(FObj parent, PropertyList propertyList,
81                          String JavaDoc systemId, int line, int column)
82             throws FOPException {
83             return new ExternalGraphic(parent, propertyList,
84                                        systemId, line, column);
85         }
86     }
87
88     public static FObj.Maker maker() {
89         return new ExternalGraphic.Maker();
90     }
91
92     public ExternalGraphic(FObj parent, PropertyList propertyList,
93                            String JavaDoc systemId, int line, int column) {
94         super(parent, propertyList, systemId, line, column);
95     }
96
97     public String JavaDoc getName() {
98         return "fo:external-graphic";
99     }
100
101     public int layout(Area area) throws FOPException {
102
103         if (this.marker == START) {
104
105             // Common Accessibility Properties
106
AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
107
108             // Common Aural Properties
109
AuralProps mAurProps = propMgr.getAuralProps();
110
111             // Common Border, Padding, and Background Properties
112
BorderAndPadding bap = propMgr.getBorderAndPadding();
113             BackgroundProps bProps = propMgr.getBackgroundProps();
114
115             // Common Margin Properties-Inline
116
MarginInlineProps mProps = propMgr.getMarginInlineProps();
117
118             // Common Relative Position Properties
119
RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
120
121             // this.properties.get("alignment-adjust");
122
// this.properties.get("alignment-baseline");
123
// this.properties.get("baseline-shift");
124
// this.properties.get("block-progression-dimension");
125
// this.properties.get("content-height");
126
// this.properties.get("content-type");
127
// this.properties.get("content-width");
128
// this.properties.get("display-align");
129
// this.properties.get("dominant-baseline");
130
// this.properties.get("height");
131
// this.properties.get("id");
132
// this.properties.get("inline-progression-dimension");
133
// this.properties.get("keep-with-next");
134
// this.properties.get("keep-with-previous");
135
// this.properties.get("line-height");
136
// this.properties.get("line-height-shift-adjustment");
137
// this.properties.get("overflow");
138
// this.properties.get("scaling");
139
// this.properties.get("scaling-method");
140
// this.properties.get("src");
141
// this.properties.get("text-align");
142
// this.properties.get("width");
143

144             // FIXME
145
this.align = this.properties.get("text-align").getEnum();
146
147             this.startIndent =
148                 this.properties.get("start-indent").getLength().mvalue();
149             this.endIndent =
150                 this.properties.get("end-indent").getLength().mvalue();
151
152             this.spaceBefore =
153                 this.properties.get("space-before.optimum").getLength().mvalue();
154             this.spaceAfter =
155                 this.properties.get("space-after.optimum").getLength().mvalue();
156
157             this.src = this.properties.get("src").getString();
158
159             this.width = this.properties.get("width").getLength().mvalue();
160
161             this.height = this.properties.get("height").getLength().mvalue();
162
163             this.id = this.properties.get("id").getString();
164
165             try {
166                 area.getIDReferences().createID(id);
167             }
168             catch(FOPException e) {
169                 if (!e.isLocationSet()) {
170                     e.setLocation(systemId, line, column);
171                 }
172                 throw e;
173             }
174             /*
175              * if (area instanceof BlockArea) {
176              * area.end();
177              * }
178              * if (this.isInTableCell) {
179              * startIndent += forcedStartOffset;
180              * endIndent = area.getAllocationWidth() - forcedWidth -
181              * forcedStartOffset;
182              * }
183              */

184             this.marker = 0;
185         }
186
187         try {
188             FopImage img = FopImageFactory.Make(src);
189             // if width / height needs to be computed
190
if ((width == 0) || (height == 0)) {
191                 // aspect ratio
192
double imgWidth = img.getWidth();
193                 double imgHeight = img.getHeight();
194                 if ((width == 0) && (height == 0)) {
195                     width = (int)((imgWidth * 1000d));
196                     height = (int)((imgHeight * 1000d));
197                 } else if (height == 0) {
198                     height = (int)((imgHeight * ((double)width)) / imgWidth);
199                 } else if (width == 0) {
200                     width = (int)((imgWidth * ((double)height)) / imgHeight);
201                 }
202             }
203
204             // scale image if it doesn't fit in the area/page
205
// Need to be more tested...
206
double ratio = ((double)width) / ((double)height);
207             int areaWidth = area.getAllocationWidth() - startIndent
208                             - endIndent;
209             int pageHeight = area.getPage().getBody().getMaxHeight()
210                              - spaceBefore;
211             if (height > pageHeight) {
212                 height = pageHeight;
213                 width = (int)(ratio * ((double)height));
214             }
215             if (width > areaWidth) {
216                 width = areaWidth;
217                 height = (int)(((double)width) / ratio);
218             }
219
220             if (area.spaceLeft() < (height + spaceBefore)) {
221                 return Status.AREA_FULL_NONE;
222             }
223
224             this.imageArea =
225                 new ImageArea(propMgr.getFontState(area.getFontInfo()), img,
226                               area.getAllocationWidth(), width, height,
227                               startIndent, endIndent, align);
228
229             if ((spaceBefore != 0) && (this.marker == 0)) {
230                 area.addDisplaySpace(spaceBefore);
231             }
232
233             if (marker == 0) {
234                 // configure id
235
area.getIDReferences().configureID(id, area);
236             }
237
238             imageArea.start();
239             imageArea.end();
240             // area.addChild(imageArea);
241
// area.increaseHeight(imageArea.getHeight());
242

243             if (spaceAfter != 0) {
244                 area.addDisplaySpace(spaceAfter);
245             }
246             if (breakBefore == BreakBefore.PAGE
247                     || ((spaceBefore + imageArea.getHeight())
248                         > area.spaceLeft())) {
249                 return Status.FORCE_PAGE_BREAK;
250             }
251
252             if (breakBefore == BreakBefore.ODD_PAGE) {
253                 return Status.FORCE_PAGE_BREAK_ODD;
254             }
255
256             if (breakBefore == BreakBefore.EVEN_PAGE) {
257                 return Status.FORCE_PAGE_BREAK_EVEN;
258             }
259
260
261             if (area instanceof BlockArea) {
262                 BlockArea ba = (BlockArea)area;
263                 LineArea la = ba.getCurrentLineArea();
264                 if (la == null) {
265                     return Status.AREA_FULL_NONE;
266                 }
267                 la.addPending();
268                 if (imageArea.getContentWidth() > la.getRemainingWidth()) {
269                     la = ba.createNextLineArea();
270                     if (la == null) {
271                         return Status.AREA_FULL_NONE;
272                     }
273                 }
274                 la.addInlineArea(imageArea, this.getLinkSet());
275             } else {
276                 area.addChild(imageArea);
277                 area.increaseHeight(imageArea.getContentHeight());
278             }
279             imageArea.setPage(area.getPage());
280
281             if (breakAfter == BreakAfter.PAGE) {
282                 this.marker = BREAK_AFTER;
283                 return Status.FORCE_PAGE_BREAK;
284             }
285
286             if (breakAfter == BreakAfter.ODD_PAGE) {
287                 this.marker = BREAK_AFTER;
288                 return Status.FORCE_PAGE_BREAK_ODD;
289             }
290
291             if (breakAfter == BreakAfter.EVEN_PAGE) {
292                 this.marker = BREAK_AFTER;
293                 return Status.FORCE_PAGE_BREAK_EVEN;
294             }
295
296         } catch (MalformedURLException JavaDoc urlex) {
297             // bad URL
298
log.error("Error while creating area : "
299                                    + urlex.getMessage());
300         } catch (FopImageException imgex) {
301             // image error
302
log.error("Error while creating area : "
303                                    + imgex.getMessage());
304         }
305
306         // if (area instanceof BlockArea) {
307
// area.start();
308
// }
309

310         return Status.OK;
311     }
312
313 }
314
315
Popular Tags