KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > TraitSetter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: TraitSetter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.fop.traits.BorderProps;
25 import org.apache.fop.traits.MinOptMax;
26 import org.apache.fop.area.Area;
27 import org.apache.fop.area.Trait;
28 import org.apache.fop.datatypes.LengthBase;
29 import org.apache.fop.datatypes.PercentBaseContext;
30 import org.apache.fop.datatypes.SimplePercentBaseContext;
31 import org.apache.fop.fo.Constants;
32 import org.apache.fop.fo.properties.CommonMarginBlock;
33 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
34 import org.apache.fop.fo.properties.CommonTextDecoration;
35 import org.apache.fop.fonts.Font;
36 import org.apache.fop.fonts.FontInfo;
37 import org.apache.fop.fonts.FontTriplet;
38
39 /**
40  * This is a helper class used for setting common traits on areas.
41  */

42 public class TraitSetter {
43
44     /** logger */
45     protected static Log log = LogFactory.getLog(TraitSetter.class);
46     
47     /**
48      * Sets border and padding traits on areas.
49      * @param area area to set the traits on
50      * @param bpProps border and padding properties
51      * @param bNotFirst True if the area is not the first area
52      * @param bNotLast True if the area is not the last area
53      * @param context Property evaluation context
54      */

55     public static void setBorderPaddingTraits(Area area,
56             CommonBorderPaddingBackground bpProps, boolean bNotFirst, boolean bNotLast,
57             PercentBaseContext context) {
58         int iBP;
59         iBP = bpProps.getPadding(CommonBorderPaddingBackground.START, bNotFirst, context);
60         if (iBP > 0) {
61             area.addTrait(Trait.PADDING_START, new Integer JavaDoc(iBP));
62         }
63         iBP = bpProps.getPadding(CommonBorderPaddingBackground.END, bNotLast, context);
64         if (iBP > 0) {
65             area.addTrait(Trait.PADDING_END, new Integer JavaDoc(iBP));
66         }
67         iBP = bpProps.getPadding(CommonBorderPaddingBackground.BEFORE, false, context);
68         if (iBP > 0) {
69             area.addTrait(Trait.PADDING_BEFORE, new Integer JavaDoc(iBP));
70         }
71         iBP = bpProps.getPadding(CommonBorderPaddingBackground.AFTER, false, context);
72         if (iBP > 0) {
73             area.addTrait(Trait.PADDING_AFTER, new Integer JavaDoc(iBP));
74         }
75
76         addBorderTrait(area, bpProps, bNotFirst,
77                        CommonBorderPaddingBackground.START,
78                        BorderProps.SEPARATE, Trait.BORDER_START);
79
80         addBorderTrait(area, bpProps, bNotLast,
81                        CommonBorderPaddingBackground.END,
82                        BorderProps.SEPARATE, Trait.BORDER_END);
83
84         addBorderTrait(area, bpProps, false,
85                        CommonBorderPaddingBackground.BEFORE,
86                        BorderProps.SEPARATE, Trait.BORDER_BEFORE);
87
88         addBorderTrait(area, bpProps, false,
89                        CommonBorderPaddingBackground.AFTER,
90                        BorderProps.SEPARATE, Trait.BORDER_AFTER);
91     }
92
93     /**
94      * Sets border traits on an area.
95      * @param area area to set the traits on
96      * @param bpProps border and padding properties
97      * @param mode the border paint mode (see BorderProps)
98      */

99     private static void addBorderTrait(Area area,
100                                        CommonBorderPaddingBackground bpProps,
101                                        boolean bDiscard, int iSide, int mode,
102                                        Object JavaDoc oTrait) {
103         int iBP = bpProps.getBorderWidth(iSide, bDiscard);
104         if (iBP > 0) {
105             area.addTrait(oTrait,
106                           new BorderProps(bpProps.getBorderStyle(iSide),
107                                           iBP, bpProps.getBorderColor(iSide),
108                                           mode));
109         }
110     }
111
112     /**
113      * Add borders to an area. Note: this method also adds unconditional padding. Don't use!
114      * Layout managers that create areas with borders can use this to
115      * add the borders to the area.
116      * @param area the area to set the traits on.
117      * @param bordProps border properties
118      * @param context Property evaluation context
119      * @deprecated Call the other addBorders() method and addPadding separately.
120      */

121     public static void addBorders(Area area, CommonBorderPaddingBackground bordProps,
122                                 PercentBaseContext context) {
123         BorderProps bps = getBorderProps(bordProps, CommonBorderPaddingBackground.BEFORE);
124         if (bps != null) {
125             area.addTrait(Trait.BORDER_BEFORE, bps);
126         }
127         bps = getBorderProps(bordProps, CommonBorderPaddingBackground.AFTER);
128         if (bps != null) {
129             area.addTrait(Trait.BORDER_AFTER, bps);
130         }
131         bps = getBorderProps(bordProps, CommonBorderPaddingBackground.START);
132         if (bps != null) {
133             area.addTrait(Trait.BORDER_START, bps);
134         }
135         bps = getBorderProps(bordProps, CommonBorderPaddingBackground.END);
136         if (bps != null) {
137             area.addTrait(Trait.BORDER_END, bps);
138         }
139
140         addPadding(area, bordProps, context);
141     }
142
143     /**
144      * Add borders to an area.
145      * Layout managers that create areas with borders can use this to
146      * add the borders to the area.
147      * @param area the area to set the traits on.
148      * @param bordProps border properties
149      * @param discardBefore true if the before border should be discarded
150      * @param discardAfter true if the after border should be discarded
151      * @param discardStart true if the start border should be discarded
152      * @param discardEnd true if the end border should be discarded
153      * @param context Property evaluation context
154      */

155     public static void addBorders(Area area, CommonBorderPaddingBackground bordProps,
156                 boolean discardBefore, boolean discardAfter,
157                 boolean discardStart, boolean discardEnd,
158                 PercentBaseContext context) {
159         BorderProps bps = getBorderProps(bordProps, CommonBorderPaddingBackground.BEFORE);
160         if (bps != null && !discardBefore) {
161             area.addTrait(Trait.BORDER_BEFORE, bps);
162         }
163         bps = getBorderProps(bordProps, CommonBorderPaddingBackground.AFTER);
164         if (bps != null && !discardAfter) {
165             area.addTrait(Trait.BORDER_AFTER, bps);
166         }
167         bps = getBorderProps(bordProps, CommonBorderPaddingBackground.START);
168         if (bps != null && !discardStart) {
169             area.addTrait(Trait.BORDER_START, bps);
170         }
171         bps = getBorderProps(bordProps, CommonBorderPaddingBackground.END);
172         if (bps != null && !discardEnd) {
173             area.addTrait(Trait.BORDER_END, bps);
174         }
175     }
176
177     /**
178      * Add borders to an area for the collapsing border model in tables.
179      * Layout managers that create areas with borders can use this to
180      * add the borders to the area.
181      * @param area the area to set the traits on.
182      * @param bordProps border properties
183      * @param outer 4 boolean values indicating if the side represents the
184      * table's outer border. Order: before, after, start, end
185      * @param context Property evaluation context
186      */

187     public static void addCollapsingBorders(Area area,
188             CommonBorderPaddingBackground bordProps,
189             boolean[] outer,
190             PercentBaseContext context) {
191         BorderProps bps = getCollapsingBorderProps(bordProps,
192                 CommonBorderPaddingBackground.BEFORE, outer[0]);
193         if (bps != null) {
194             area.addTrait(Trait.BORDER_BEFORE, bps);
195         }
196         bps = getCollapsingBorderProps(bordProps,
197                 CommonBorderPaddingBackground.AFTER, outer[1]);
198         if (bps != null) {
199             area.addTrait(Trait.BORDER_AFTER, bps);
200         }
201         bps = getCollapsingBorderProps(bordProps,
202                 CommonBorderPaddingBackground.START, outer[2]);
203         if (bps != null) {
204             area.addTrait(Trait.BORDER_START, bps);
205         }
206         bps = getCollapsingBorderProps(bordProps,
207                 CommonBorderPaddingBackground.END, outer[3]);
208         if (bps != null) {
209             area.addTrait(Trait.BORDER_END, bps);
210         }
211
212         addPadding(area, bordProps, context);
213     }
214
215     private static void addPadding(Area area, CommonBorderPaddingBackground bordProps,
216                                 PercentBaseContext context) {
217         addPadding(area, bordProps, false, false, false, false, context);
218     }
219
220     /**
221      * Add padding to an area.
222      * Layout managers that create areas with padding can use this to
223      * add the borders to the area.
224      * @param area the area to set the traits on.
225      * @param bordProps border and padding properties
226      * @param discardBefore true if the before padding should be discarded
227      * @param discardAfter true if the after padding should be discarded
228      * @param discardStart true if the start padding should be discarded
229      * @param discardEnd true if the end padding should be discarded
230      * @param context Property evaluation context
231      */

232     public static void addPadding(Area area, CommonBorderPaddingBackground bordProps,
233                 boolean discardBefore, boolean discardAfter,
234                 boolean discardStart, boolean discardEnd,
235                 PercentBaseContext context) {
236         int padding = bordProps.getPadding(CommonBorderPaddingBackground.BEFORE,
237                 discardBefore, context);
238         if (padding != 0) {
239             area.addTrait(Trait.PADDING_BEFORE, new java.lang.Integer JavaDoc(padding));
240         }
241
242         padding = bordProps.getPadding(CommonBorderPaddingBackground.AFTER,
243                 discardAfter, context);
244         if (padding != 0) {
245             area.addTrait(Trait.PADDING_AFTER, new java.lang.Integer JavaDoc(padding));
246         }
247
248         padding = bordProps.getPadding(CommonBorderPaddingBackground.START,
249                 discardStart, context);
250         if (padding != 0) {
251             area.addTrait(Trait.PADDING_START, new java.lang.Integer JavaDoc(padding));
252         }
253
254         padding = bordProps.getPadding(CommonBorderPaddingBackground.END,
255                 discardEnd, context);
256         if (padding != 0) {
257             area.addTrait(Trait.PADDING_END, new java.lang.Integer JavaDoc(padding));
258         }
259
260     }
261     
262     private static BorderProps getBorderProps(CommonBorderPaddingBackground bordProps, int side) {
263         int width = bordProps.getBorderWidth(side, false);
264         if (width != 0) {
265             BorderProps bps;
266             bps = new BorderProps(bordProps.getBorderStyle(side),
267                                   width,
268                                   bordProps.getBorderColor(side),
269                                   BorderProps.SEPARATE);
270             return bps;
271         } else {
272             return null;
273         }
274     }
275
276     private static BorderProps getCollapsingBorderProps(
277             CommonBorderPaddingBackground bordProps, int side, boolean outer) {
278         int width = bordProps.getBorderWidth(side, false);
279         if (width != 0) {
280             BorderProps bps;
281             bps = new BorderProps(bordProps.getBorderStyle(side),
282                     width, bordProps.getBorderColor(side),
283                     (outer ? BorderProps.COLLAPSE_OUTER : BorderProps.COLLAPSE_INNER));
284             return bps;
285         } else {
286             return null;
287         }
288     }
289
290     /**
291      * Add background to an area.
292      * Layout managers that create areas with a background can use this to
293      * add the background to the area.
294      * Note: The area's IPD and BPD must be set before calling this method.
295      * @param area the area to set the traits on
296      * @param backProps the background properties
297      * @param context Property evaluation context
298      */

299     public static void addBackground(Area area,
300                                      CommonBorderPaddingBackground backProps,
301                                      PercentBaseContext context) {
302         if (!backProps.hasBackground()) {
303             return;
304         }
305         Trait.Background back = new Trait.Background();
306         back.setColor(backProps.backgroundColor);
307
308         if (backProps.getFopImage() != null) {
309             back.setURL(backProps.backgroundImage);
310             back.setFopImage(backProps.getFopImage());
311             back.setRepeat(backProps.backgroundRepeat);
312             if (backProps.backgroundPositionHorizontal != null) {
313                 if (back.getRepeat() == Constants.EN_NOREPEAT
314                         || back.getRepeat() == Constants.EN_REPEATY) {
315                     if (area.getIPD() > 0) {
316                         int width = area.getIPD();
317                         width += backProps.getPaddingStart(false, context);
318                         width += backProps.getPaddingEnd(false, context);
319                         back.setHoriz(backProps.backgroundPositionHorizontal.getValue(
320                                 new SimplePercentBaseContext(context,
321                                     LengthBase.IMAGE_BACKGROUND_POSITION_HORIZONTAL,
322                                     (width - back.getFopImage().getIntrinsicWidth())
323                                 )
324                             ));
325                     } else {
326                         //TODO Area IPD has to be set for this to work
327
log.warn("Horizontal background image positioning ignored"
328                                 + " because the IPD was not set on the area."
329                                 + " (Yes, it's a bug in FOP)");
330                     }
331                 }
332             }
333             if (backProps.backgroundPositionVertical != null) {
334                 if (back.getRepeat() == Constants.EN_NOREPEAT
335                         || back.getRepeat() == Constants.EN_REPEATX) {
336                     if (area.getBPD() > 0) {
337                         int height = area.getBPD();
338                         height += backProps.getPaddingBefore(false, context);
339                         height += backProps.getPaddingAfter(false, context);
340                         back.setVertical(backProps.backgroundPositionVertical.getValue(
341                                 new SimplePercentBaseContext(context,
342                                      LengthBase.IMAGE_BACKGROUND_POSITION_VERTICAL,
343                                      (height - back.getFopImage().getIntrinsicHeight())
344                                 )
345                             ));
346                     } else {
347                         //TODO Area BPD has to be set for this to work
348
log.warn("Vertical background image positioning ignored"
349                                 + " because the BPD was not set on the area."
350                                 + " (Yes, it's a bug in FOP)");
351                     }
352                 }
353             }
354         }
355
356         area.addTrait(Trait.BACKGROUND, back);
357     }
358
359     /**
360      * Add space to a block area.
361      * Layout managers that create block areas can use this to add space
362      * outside of the border rectangle to the area.
363      * @param area the area to set the traits on.
364      * @param bpProps the border, padding and background properties
365      * @param startIndent the effective start-indent value
366      * @param endIndent the effective end-indent value
367      * @param context the context for evaluation of percentages
368      */

369     public static void addMargins(Area area,
370                                   CommonBorderPaddingBackground bpProps,
371                                   int startIndent, int endIndent,
372                                   PercentBaseContext context) {
373         if (startIndent != 0) {
374             area.addTrait(Trait.START_INDENT, new Integer JavaDoc(startIndent));
375         }
376         
377         int spaceStart = startIndent
378                             - bpProps.getBorderStartWidth(false)
379                             - bpProps.getPaddingStart(false, context);
380         if (spaceStart != 0) {
381             area.addTrait(Trait.SPACE_START, new Integer JavaDoc(spaceStart));
382         }
383
384         if (endIndent != 0) {
385             area.addTrait(Trait.END_INDENT, new Integer JavaDoc(endIndent));
386         }
387         int spaceEnd = endIndent
388                             - bpProps.getBorderEndWidth(false)
389                             - bpProps.getPaddingEnd(false, context);
390         if (spaceEnd != 0) {
391             area.addTrait(Trait.SPACE_END, new Integer JavaDoc(spaceEnd));
392         }
393     }
394
395     /**
396      * Add space to a block area.
397      * Layout managers that create block areas can use this to add space
398      * outside of the border rectangle to the area.
399      * @param area the area to set the traits on.
400      * @param bpProps the border, padding and background properties
401      * @param marginProps the margin properties.
402      * @param context the context for evaluation of percentages
403      */

404     public static void addMargins(Area area,
405                                   CommonBorderPaddingBackground bpProps,
406                                   CommonMarginBlock marginProps,
407                                   PercentBaseContext context) {
408         int startIndent = marginProps.startIndent.getValue(context);
409         int endIndent = marginProps.endIndent.getValue(context);
410         addMargins(area, bpProps, startIndent, endIndent, context);
411     }
412
413     /**
414      * Returns the effective space length of a resolved space specifier based on the adjustment
415      * value.
416      * @param adjust the adjustment value
417      * @param space the space specifier
418      * @return the effective space length
419      */

420     public static int getEffectiveSpace(double adjust, MinOptMax space) {
421         if (space == null) {
422             return 0;
423         }
424         int sp = space.opt;
425         if (adjust > 0) {
426             sp = sp + (int)(adjust * (space.max - space.opt));
427         } else {
428             sp = sp + (int)(adjust * (space.opt - space.min));
429         }
430         return sp;
431     }
432     
433     /**
434      * Adds traits for space-before and space-after to an area.
435      * @param area the target area
436      * @param adjust the adjustment value
437      * @param spaceBefore the space-before space specifier
438      * @param spaceAfter the space-after space specifier
439      */

440     public static void addSpaceBeforeAfter(Area area, double adjust,
441             MinOptMax spaceBefore, MinOptMax spaceAfter) {
442         int space;
443         space = getEffectiveSpace(adjust, spaceBefore);
444         if (space != 0) {
445             area.addTrait(Trait.SPACE_BEFORE, new Integer JavaDoc(space));
446         }
447         space = getEffectiveSpace(adjust, spaceAfter);
448         if (space != 0) {
449             area.addTrait(Trait.SPACE_AFTER, new Integer JavaDoc(space));
450         }
451     }
452     
453     /**
454      * Sets the traits for breaks on an area.
455      * @param area the area to set the traits on.
456      * @param breakBefore the value for break-before
457      * @param breakAfter the value for break-after
458      */

459     public static void addBreaks(Area area, int breakBefore, int breakAfter) {
460         /* Currently disabled as these traits are never used by the renderers
461         area.addTrait(Trait.BREAK_AFTER, new Integer(breakAfter));
462         area.addTrait(Trait.BREAK_BEFORE, new Integer(breakBefore));
463         */

464     }
465     
466     /**
467      * Adds font traits to an area
468      * @param area the target are
469      * @param font the font to use
470      */

471     public static void addFontTraits(Area area, Font font) {
472         area.addTrait(Trait.FONT, font.getFontTriplet());
473         area.addTrait(Trait.FONT_SIZE, new Integer JavaDoc(font.getFontSize()));
474     }
475     
476     /**
477      * Adds the text-decoration traits to the area.
478      * @param area the area to set the traits on
479      * @param deco the text decorations
480      */

481     public static void addTextDecoration(Area area, CommonTextDecoration deco) {
482         //TODO Finish text-decoration
483
if (deco != null) {
484             if (deco.hasUnderline()) {
485                 area.addTrait(Trait.UNDERLINE, Boolean.TRUE);
486                 area.addTrait(Trait.UNDERLINE_COLOR, deco.getUnderlineColor());
487             }
488             if (deco.hasOverline()) {
489                 area.addTrait(Trait.OVERLINE, Boolean.TRUE);
490                 area.addTrait(Trait.OVERLINE_COLOR, deco.getOverlineColor());
491             }
492             if (deco.hasLineThrough()) {
493                 area.addTrait(Trait.LINETHROUGH, Boolean.TRUE);
494                 area.addTrait(Trait.LINETHROUGH_COLOR, deco.getLineThroughColor());
495             }
496             if (deco.isBlinking()) {
497                 area.addTrait(Trait.BLINK, Boolean.TRUE);
498             }
499         }
500     }
501     
502     /**
503      * Sets the producer's ID as a trait on the area. This can be used to track back the
504      * generating FO node.
505      * @param area the area to set the traits on
506      * @param id the ID to set
507      */

508     public static void setProducerID(Area area, String JavaDoc id) {
509         if (id != null && id.length() > 0) {
510             area.addTrait(Trait.PROD_ID, id);
511         }
512     }
513 }
514
Popular Tags