KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > PropertyManager


1 /*
2  * $Id: PropertyManager.java,v 1.7.2.6 2003/02/25 12:56:55 jeremias 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;
52
53 import java.lang.StringBuffer JavaDoc;
54 import java.net.MalformedURLException JavaDoc;
55
56 import org.apache.fop.apps.FOPException;
57 import org.apache.fop.fo.flow.AbstractFlow;
58 import org.apache.fop.fo.properties.BreakAfter;
59 import org.apache.fop.fo.properties.BreakBefore;
60 import org.apache.fop.fo.properties.Constants;
61 import org.apache.fop.fo.properties.TextDecoration;
62 import org.apache.fop.image.FopImageFactory;
63 import org.apache.fop.image.FopImageException;
64 import org.apache.fop.layout.AbsolutePositionProps;
65 import org.apache.fop.layout.AccessibilityProps;
66 import org.apache.fop.layout.Area;
67 import org.apache.fop.layout.AuralProps;
68 import org.apache.fop.layout.BackgroundProps;
69 import org.apache.fop.layout.BorderAndPadding;
70 import org.apache.fop.layout.ColumnArea;
71 import org.apache.fop.layout.FontInfo;
72 import org.apache.fop.layout.FontState;
73 import org.apache.fop.layout.HyphenationProps;
74 import org.apache.fop.layout.MarginInlineProps;
75 import org.apache.fop.layout.MarginProps;
76 import org.apache.fop.layout.RelativePositionProps;
77 import org.apache.fop.layout.TextState;
78
79 public class PropertyManager {
80
81     private PropertyList properties;
82     private FontState fontState = null;
83     private BorderAndPadding borderAndPadding = null;
84     private HyphenationProps hyphProps = null;
85     private BackgroundProps bgProps = null;
86
87     private String JavaDoc[] saLeft;
88     private String JavaDoc[] saRight;
89     private String JavaDoc[] saTop;
90     private String JavaDoc[] saBottom;
91
92     public PropertyManager(PropertyList pList) {
93         this.properties = pList;
94     }
95
96     private void initDirections() {
97         saLeft = new String JavaDoc[1];
98         saRight = new String JavaDoc[1];
99         saTop = new String JavaDoc[1];
100         saBottom = new String JavaDoc[1];
101         saTop[0] = properties.wmAbsToRel(PropertyList.TOP);
102         saBottom[0] = properties.wmAbsToRel(PropertyList.BOTTOM);
103         saLeft[0] = properties.wmAbsToRel(PropertyList.LEFT);
104         saRight[0] = properties.wmAbsToRel(PropertyList.RIGHT);
105     }
106
107     public FontState getFontState(FontInfo fontInfo) throws FOPException {
108         if (fontState == null) {
109             String JavaDoc fontFamily = properties.get("font-family").getString();
110             String JavaDoc fontStyle = properties.get("font-style").getString();
111             String JavaDoc fontWeight = properties.get("font-weight").getString();
112             // NOTE: this is incomplete. font-size may be specified with
113
// various kinds of keywords too
114
int fontSize = properties.get("font-size").getLength().mvalue();
115             int fontVariant = properties.get("font-variant").getEnum();
116             // fontInfo is same for the whole FOP run but set in all FontState
117
fontState = new FontState(fontInfo, fontFamily, fontStyle,
118                                       fontWeight, fontSize, fontVariant);
119         }
120         return fontState;
121     }
122
123
124     public BorderAndPadding getBorderAndPadding() {
125         if (borderAndPadding == null) {
126             this.borderAndPadding = new BorderAndPadding();
127             initDirections();
128
129             initBorderInfo(BorderAndPadding.TOP, saTop);
130             initBorderInfo(BorderAndPadding.BOTTOM, saBottom);
131             initBorderInfo(BorderAndPadding.LEFT, saLeft);
132             initBorderInfo(BorderAndPadding.RIGHT, saRight);
133         }
134         return borderAndPadding;
135     }
136
137     private void initBorderInfo(int whichSide, String JavaDoc[] saSide) {
138         borderAndPadding.setPadding(whichSide,
139                                     properties.get(formatPadding(saSide)).getCondLength());
140
141         // If style = none, force width to 0, don't get Color
142
int style = properties.get(formatStyle(saSide)).getEnum();
143         if (style != Constants.NONE) {
144             borderAndPadding.setBorder(whichSide, style,
145                                        properties.get(formatWidth(saSide)).getCondLength(),
146                                        properties.get(formatColor(saSide)).getColorType());
147         }
148     }
149
150     private static final String JavaDoc padding = new String JavaDoc("padding-");
151     private static final String JavaDoc border = new String JavaDoc("border-");
152     private static final String JavaDoc width = new String JavaDoc("-width");
153     private static final String JavaDoc color = new String JavaDoc("-color");
154     private static final String JavaDoc style = new String JavaDoc("-style");
155
156     private String JavaDoc formatPadding(String JavaDoc[] saSide) {
157         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(14);
158         return sb.append(padding).append(saSide[0]).toString();
159     }
160
161     private String JavaDoc formatColor(String JavaDoc[] saSide) {
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(19);
163         return sb.append(border).append(saSide[0]).append(color).toString();
164     }
165     private String JavaDoc formatWidth(String JavaDoc[] saSide) {
166         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(19);
167         return sb.append(border).append(saSide[0]).append(width).toString();
168     }
169
170     private String JavaDoc formatStyle(String JavaDoc[] saSide) {
171         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(19);
172         return sb.append(border).append(saSide[0]).append(style).toString();
173     }
174
175     public HyphenationProps getHyphenationProps() {
176         if (hyphProps == null) {
177             this.hyphProps = new HyphenationProps();
178             hyphProps.hyphenate = this.properties.get("hyphenate").getEnum();
179             hyphProps.hyphenationChar =
180                 this.properties.get("hyphenation-character").getCharacter();
181             hyphProps.hyphenationPushCharacterCount =
182                 this.properties.get("hyphenation-push-character-count").getNumber().intValue();
183             hyphProps.hyphenationRemainCharacterCount =
184                 this.properties.get("hyphenation-remain-character-count").getNumber().intValue();
185             hyphProps.language = this.properties.get("language").getString();
186             hyphProps.country = this.properties.get("country").getString();
187         }
188         return hyphProps;
189     }
190
191     public int checkBreakBefore(Area area) {
192         if (!(area instanceof ColumnArea)) {
193             switch (properties.get("break-before").getEnum()) {
194             case BreakBefore.PAGE:
195                 return Status.FORCE_PAGE_BREAK;
196             case BreakBefore.ODD_PAGE:
197                 return Status.FORCE_PAGE_BREAK_ODD;
198             case BreakBefore.EVEN_PAGE:
199                 return Status.FORCE_PAGE_BREAK_EVEN;
200             case BreakBefore.COLUMN:
201                 return Status.FORCE_COLUMN_BREAK;
202             default:
203                 return Status.OK;
204             }
205         } else {
206             ColumnArea colArea = (ColumnArea)area;
207             switch (properties.get("break-before").getEnum()) {
208             case BreakBefore.PAGE:
209                 // if first ColumnArea, and empty, return OK
210
if (!colArea.hasChildren() && (colArea.getColumnIndex() == 1))
211                     return Status.OK;
212                 else
213                     return Status.FORCE_PAGE_BREAK;
214             case BreakBefore.ODD_PAGE:
215                 // if first ColumnArea, empty, _and_ in odd page,
216
// return OK
217
if (!colArea.hasChildren() && (colArea.getColumnIndex() == 1)
218                         && (colArea.getPage().getNumber() % 2 != 0))
219                     return Status.OK;
220                 else
221                     return Status.FORCE_PAGE_BREAK_ODD;
222             case BreakBefore.EVEN_PAGE:
223                 // if first ColumnArea, empty, _and_ in even page,
224
// return OK
225
if (!colArea.hasChildren() && (colArea.getColumnIndex() == 1)
226                         && (colArea.getPage().getNumber() % 2 == 0))
227                     return Status.OK;
228                 else
229                     return Status.FORCE_PAGE_BREAK_EVEN;
230             case BreakBefore.COLUMN:
231                 // if ColumnArea is empty return OK
232
if (!area.hasChildren())
233                     return Status.OK;
234                 else
235                     return Status.FORCE_COLUMN_BREAK;
236             default:
237                 return Status.OK;
238             }
239         }
240     }
241
242     public int checkBreakAfter(Area area) {
243         switch (properties.get("break-after").getEnum()) {
244         case BreakAfter.PAGE:
245             return Status.FORCE_PAGE_BREAK;
246         case BreakAfter.ODD_PAGE:
247             return Status.FORCE_PAGE_BREAK_ODD;
248         case BreakAfter.EVEN_PAGE:
249             return Status.FORCE_PAGE_BREAK_EVEN;
250         case BreakAfter.COLUMN:
251             return Status.FORCE_COLUMN_BREAK;
252         default:
253             return Status.OK;
254         }
255     }
256
257     public MarginProps getMarginProps() {
258         MarginProps props = new MarginProps();
259
260         // Common Margin Properties-Block
261
props.marginTop =
262             this.properties.get("margin-top").getLength().mvalue();
263         props.marginBottom =
264             this.properties.get("margin-bottom").getLength().mvalue();
265         props.marginLeft =
266             this.properties.get("margin-left").getLength().mvalue();
267         props.marginRight =
268             this.properties.get("margin-right").getLength().mvalue();
269         /*
270          * // need to get opt, min and max
271          * props.spaceBefore = this.properties.get("space-before").getLength().mvalue();
272          * props.spaceAfter = this.properties.get("space-after").getLength().mvalue();
273          * props.startIndent = this.properties.get("start-indent").getLength().mvalue();
274          * props.endIndent = this.properties.get("end-indent").getLength().mvalue();
275          */

276         return props;
277     }
278
279     public BackgroundProps getBackgroundProps() {
280         if (bgProps == null) {
281             this.bgProps = new BackgroundProps();
282             // bgProps.backAttachment = this.properties.get("background-attachment").getEnum();
283
bgProps.backColor =
284             this.properties.get("background-color").getColorType();
285
286             String JavaDoc src = this.properties.get("background-image").getString();
287             if (src.equalsIgnoreCase("none")) {
288                 bgProps.backImage = null;
289             } else if (src.equalsIgnoreCase("inherit")) {
290                 // XXX: implement this
291
bgProps.backImage = null;
292             } else {
293                 try {
294                     bgProps.backImage = FopImageFactory.Make(src);
295                 } catch (MalformedURLException JavaDoc urlex) {
296                     bgProps.backImage = null;
297                     // XXX: use a logger instead
298
System.out.println("Error creating background image: "
299                             + urlex.getMessage());
300                 } catch (FopImageException imgex) {
301                     bgProps.backImage = null;
302                     // XXX: use a logger instead
303
System.out.println("Error creating background image: "
304                             + imgex.getMessage());
305                 }
306             }
307
308             bgProps.backRepeat = this.properties.get("background-repeat").getEnum();
309
310
311             // bgProps.backPosHorizontal = this.properties.get("background-position-horizontal").getLength();
312
// bgProps.backPosVertical = this.properties.get("background-position-vertical").getLength();
313
}
314         return bgProps;
315     }
316
317     public MarginInlineProps getMarginInlineProps() {
318         MarginInlineProps props = new MarginInlineProps();
319         return props;
320     }
321
322     public AccessibilityProps getAccessibilityProps() {
323         AccessibilityProps props = new AccessibilityProps();
324         String JavaDoc str;
325         str = this.properties.get("source-document").getString();
326         if(!"none".equals(str)) {
327             props.sourceDoc = str;
328         }
329         str = this.properties.get("role").getString();
330         if(!"none".equals(str)) {
331             props.role = str;
332         }
333         return props;
334     }
335
336     public AuralProps getAuralProps() {
337         AuralProps props = new AuralProps();
338         return props;
339     }
340
341     public RelativePositionProps getRelativePositionProps() {
342         RelativePositionProps props = new RelativePositionProps();
343         return props;
344     }
345
346     public AbsolutePositionProps getAbsolutePositionProps() {
347         AbsolutePositionProps props = new AbsolutePositionProps();
348         return props;
349     }
350
351     public TextState getTextDecoration(FObj parent) throws FOPException {
352
353         // TextState from parent Block/Inline
354
TextState tsp = null;
355         boolean found = false;
356
357         do {
358             if (parent instanceof AbstractFlow) {
359                 found = true;
360             } else if (parent instanceof FObjMixed) {
361                 FObjMixed fom = (FObjMixed) parent;
362                 tsp = fom.getTextState();
363                 found = true;
364             }
365             parent = parent.getParent();
366         } while (!found);
367
368         TextState ts = new TextState();
369
370         if (tsp != null) {
371             ts.setUnderlined(tsp.getUnderlined());
372             ts.setOverlined(tsp.getOverlined());
373             ts.setLineThrough(tsp.getLineThrough());
374         }
375
376         int textDecoration = this.properties.get("text-decoration").getEnum();
377
378         if (textDecoration == TextDecoration.UNDERLINE) {
379             ts.setUnderlined(true);
380         }
381         if (textDecoration == TextDecoration.OVERLINE) {
382             ts.setOverlined(true);
383         }
384         if (textDecoration == TextDecoration.LINE_THROUGH) {
385             ts.setLineThrough(true);
386         }
387         if (textDecoration == TextDecoration.NO_UNDERLINE) {
388             ts.setUnderlined(false);
389         }
390         if (textDecoration == TextDecoration.NO_OVERLINE) {
391             ts.setOverlined(false);
392         }
393         if (textDecoration == TextDecoration.NO_LINE_THROUGH) {
394             ts.setLineThrough(false);
395         }
396
397         return ts;
398     }
399
400 }
401
Popular Tags