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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/*
* Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed 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.layoutmgr;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.traits.BorderProps;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.area.Area;
import org.apache.fop.area.Trait;
import org.apache.fop.datatypes.LengthBase;
import org.apache.fop.datatypes.PercentBaseContext;
import org.apache.fop.datatypes.SimplePercentBaseContext;
import org.apache.fop.fo.Constants;
import org.apache.fop.fo.properties.CommonMarginBlock;
import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
import org.apache.fop.fo.properties.CommonTextDecoration;
/**
* This is a helper class used for setting common traits on areas.
*/
public class TraitSetter {
/** logger */
protected static Log log = LogFactory.getLog(TraitSetter.class);
/**
* Sets border and padding traits on areas.
* @param area area to set the traits on
* @param bpProps border and padding properties
* @param bNotFirst True if the area is not the first area
* @param bNotLast True if the area is not the last area
* @param context Property evaluation context
*/
public static void setBorderPaddingTraits(Area area,
CommonBorderPaddingBackground bpProps, boolean bNotFirst, boolean bNotLast,
PercentBaseContext context) {
int iBP;
iBP = bpProps.getPadding(CommonBorderPaddingBackground.START, bNotFirst, context);
if (iBP > 0) {
area.addTrait(Trait.PADDING_START, new Integer(iBP));
}
iBP = bpProps.getPadding(CommonBorderPaddingBackground.END, bNotLast, context);
if (iBP > 0) {
area.addTrait(Trait.PADDING_END, new Integer(iBP));
}
iBP = bpProps.getPadding(CommonBorderPaddingBackground.BEFORE, false, context);
if (iBP > 0) {
area.addTrait(Trait.PADDING_BEFORE, new Integer(iBP));
}
iBP = bpProps.getPadding(CommonBorderPaddingBackground.AFTER, false, context);
if (iBP > 0) {
area.addTrait(Trait.PADDING_AFTER, new Integer(iBP));
}
addBorderTrait(area, bpProps, bNotFirst,
CommonBorderPaddingBackground.START,
BorderProps.SEPARATE, Trait.BORDER_START);
addBorderTrait(area, bpProps, bNotLast,
CommonBorderPaddingBackground.END,
BorderProps.SEPARATE, Trait.BORDER_END);
addBorderTrait(area, bpProps, false,
CommonBorderPaddingBackground.BEFORE,
BorderProps.SEPARATE, Trait.BORDER_BEFORE);
addBorderTrait(area, bpProps, false,
CommonBorderPaddingBackground.AFTER,
BorderProps.SEPARATE, Trait.BORDER_AFTER);
}
/**
* Sets border traits on an area.
* @param area area to set the traits on
* @param bpProps border and padding properties
* @param mode the border paint mode (see BorderProps)
*/
private static void addBorderTrait(Area area,
CommonBorderPaddingBackground bpProps,
boolean bDiscard, int iSide, int mode,
Object oTrait) {
int iBP = bpProps.getBorderWidth(iSide, bDiscard);
if (iBP > 0) {
area.addTrait(oTrait,
new BorderProps(bpProps.getBorderStyle(iSide),
iBP, bpProps.getBorderColor(iSide),
mode));
}
}
/**
* Add borders to an area. Note: this method also adds unconditional padding. Don't use!
* Layout managers that create areas with borders can use this to
* add the borders to the area.
* @param area the area to set the traits on.
* @param bordProps border properties
* @param context Property evaluation context
* @deprecated Call the other addBorders() method and addPadding separately.
*/
public static void addBorders(Area area, CommonBorderPaddingBackground bordProps,
PercentBaseContext context) {
BorderProps bps = getBorderProps(bordProps, CommonBorderPaddingBackground.BEFORE);
if (bps != null) {
area.addTrait(Trait.BORDER_BEFORE, bps);
}
bps = getBorderProps(bordProps, CommonBorderPaddingBackground.AFTER);
if (bps != null) {
area.addTrait(Trait.BORDER_AFTER, bps);
}
bps = getBorderProps(bordProps, CommonBorderPaddingBackground.START);
if (bps != null) {
area.addTrait(Trait.BORDER_START, bps);
}
bps = getBorderProps(bordProps, CommonBorderPaddingBackground.END);
if (bps != null) {
area.addTrait(Trait.BORDER_END, bps);
}
addPadding(area, bordProps, context);
}
/**
* Add borders to an area.
* Layout managers that create areas with borders can use this to
* add the borders to the area.
* @param area the area to set the traits on.
* @param bordProps border properties
* @param discardBefore true if the before border should be discarded
* @param discardAfter true if the after border should be discarded
* @param discardStart true if the start border should be discarded
* @param discardEnd true if the end border should be discarded
* @param context Property evaluation context
*/
public static void addBorders(Area area, CommonBorderPaddingBackground bordProps,
boolean discardBefore, boolean discardAfter,
boolean discardStart, boolean discardEnd,
PercentBaseContext context) {
BorderProps bps = getBorderProps(bordProps, CommonBorderPaddingBackground.BEFORE);
if (bps != null && !discardBefore) {
area.addTrait(Trait.BORDER_BEFORE, bps);
}
bps = getBorderProps(bordProps, CommonBorderPaddingBackground.AFTER);
if (bps != null && !discardAfter) {
area.addTrait(Trait.BORDER_AFTER, bps);
}
bps = getBorderProps(bordProps, CommonBorderPaddingBackground.START);
if (bps != null && !discardStart) {
area.addTrait(Trait.BORDER_START, bps);
}
bps = getBorderProps(bordProps, CommonBorderPaddingBackground.END);
if (bps != null && !discardEnd) {
area.addTrait(Trait.BORDER_END, bps);
}
}
/**
* Add borders to an area for the collapsing border model in tables.
* Layout managers that create areas with borders can use this to
* add the borders to the area.
* @param area the area to set the traits on.
* @param bordProps border properties
* @param outer 4 boolean values indicating if the side represents the
* table's outer border. Order: before, after, start, end
* @param context Property evaluation context
*/
public static void addCollapsingBorders(Area area,
CommonBorderPaddingBackground bordProps,
boolean[] outer,
PercentBaseContext context) {
BorderProps bps = getCollapsingBorderProps(bordProps,
CommonBorderPaddingBackground.BEFORE, outer[0]);
if (bps != null) {
area.addTrait(Trait.BORDER_BEFORE, bps);
}
bps = getCollapsingBorderProps(bordProps,
CommonBorderPaddingBackground.AFTER, outer[1]);
if (bps != null) {
area.addTrait(Trait.BORDER_AFTER, bps);
}
bps = getCollapsingBorderProps(bordProps,
CommonBorderPaddingBackground.START, outer[2]);
if (bps != null) {
area.addTrait(Trait.BORDER_START, bps);
}
bps = getCollapsingBorderProps(bordProps,
CommonBorderPaddingBackground.END, outer[3]);
if (bps != null) {
area.addTrait(Trait.BORDER_END, bps);
}
addPadding(area, bordProps, context);
}
private static void addPadding(Area area, CommonBorderPaddingBackground bordProps,
PercentBaseContext context) {
addPadding(area, bordProps, false, false, false, false, context);
}
/**
* Add padding to an area.
* Layout managers that create areas with padding can use this to
* add the borders to the area.
* @param area the area to set the traits on.
* @param bordProps border and padding properties
* @param discardBefore true if the before padding should be discarded
* @param discardAfter true if the after padding should be discarded
* @param discardStart true if the start padding should be discarded
* @param discardEnd true if the end padding should be discarded
* @param context Property evaluation context
*/
public static void addPadding(Area area, CommonBorderPaddingBackground bordProps,
boolean discardBefore, boolean discardAfter,
boolean discardStart, boolean discardEnd,
PercentBaseContext context) {
int padding = bordProps.getPadding(CommonBorderPaddingBackground.BEFORE,
discardBefore, context);
if (padding != 0) {
area.addTrait(Trait.PADDING_BEFORE, new java.lang.Integer(padding));
}
padding = bordProps.getPadding(CommonBorderPaddingBackground.AFTER,
discardAfter, context);
if (padding != 0) {
area.addTrait(Trait.PADDING_AFTER, new java.lang.Integer(padding));
}
padding = bordProps.getPadding(CommonBorderPaddingBackground.START,
discardStart, context);
if (padding != 0) {
area.addTrait(Trait.PADDING_START, new java.lang.Integer(padding));
}
padding = bordProps.getPadding(CommonBorderPaddingBackground.END,
discardEnd, context);
if (padding != 0) {
area.addTrait(Trait.PADDING_END, new java.lang.Integer(padding));
}
}
private static BorderProps getBorderProps(CommonBorderPaddingBackground bordProps, int side) {
int width = bordProps.getBorderWidth(side, false);
if (width != 0) {
BorderProps bps;
bps = new BorderProps(bordProps.getBorderStyle(side),
width,
bordProps.getBorderColor(side),
BorderProps.SEPARATE);
return bps;
} else {
return null;
}
}
private static BorderProps getCollapsingBorderProps(
CommonBorderPaddingBackground bordProps, int side, boolean outer) {
int width = bordProps.getBorderWidth(side, false);
if (width != 0) {
BorderProps bps;
bps = new BorderProps(bordProps.getBorderStyle(side),
width, bordProps.getBorderColor(side),
(outer ? BorderProps.COLLAPSE_OUTER : BorderProps.COLLAPSE_INNER));
return bps;
} else {
return null;
}
}
/**
* Add background to an area.
* Layout managers that create areas with a background can use this to
* add the background to the area.
* Note: The area's IPD and BPD must be set before calling this method.
* @param area the area to set the traits on
* @param backProps the background properties
* @param context Property evaluation context
*/
public static void addBackground(Area area,
CommonBorderPaddingBackground backProps,
PercentBaseContext context) {
if (!backProps.hasBackground()) {
return;
}
Trait.Background back = new Trait.Background();
back.setColor(backProps.backgroundColor);
if (backProps.getFopImage() != null) {
back.setURL(backProps.backgroundImage);
back.setFopImage(backProps.getFopImage());
back.setRepeat(backProps.backgroundRepeat);
if (backProps.backgroundPositionHorizontal != null) {
if (back.getRepeat() == Constants.EN_NOREPEAT
|| back.getRepeat() == Constants.EN_REPEATY) {
if (area.getIPD() > 0) {
int width = area.getIPD();
width += backProps.getPaddingStart(false, context);
width += backProps.getPaddingEnd(false, context);
back.setHoriz(backProps.backgroundPositionHorizontal.getValue(
new SimplePercentBaseContext(context,
LengthBase.IMAGE_BACKGROUND_POSITION_HORIZONTAL,
(width - back.getFopImage().getIntrinsicWidth())
)
));
} else {
//TODO Area IPD has to be set for this to work
log.warn("Horizontal background image positioning ignored"
+ " because the IPD was not set on the area."
+ " (Yes, it's a bug in FOP)");
}
}
}
if (backProps.backgroundPositionVertical != null) {
if (back.getRepeat() == Constants.EN_NOREPEAT
|| back.getRepeat() == Constants.EN_REPEATX) {
if (area.getBPD() > 0) {
int height = area.getBPD();
height += backProps.getPaddingBefore(false, context);
height += backProps.getPaddingAfter(false, context);
back.setVertical(backProps.backgroundPositionVertical.getValue(
new SimplePercentBaseContext(context,
LengthBase.IMAGE_BACKGROUND_POSITION_VERTICAL,
(height - back.getFopImage().getIntrinsicHeight())
)
));
} else {
//TODO Area BPD has to be set for this to work
log.warn("Vertical background image positioning ignored"
+ " because the BPD was not set on the area."
+ " (Yes, it's a bug in FOP)");
}
}
}
}
area.addTrait(Trait.BACKGROUND, back);
}
/**
* Add space to a block area.
* Layout managers that create block areas can use this to add space
* outside of the border rectangle to the area.
* @param area the area to set the traits on.
* @param bpProps the border, padding and background properties
* @param marginProps the margin properties.
* @param context the context for evaluation of percentages
*/
public static void addMargins(Area area,
CommonBorderPaddingBackground bpProps,
CommonMarginBlock marginProps,
PercentBaseContext context) {
int startIndent = marginProps.startIndent.getValue(context);
if (startIndent != 0) {
area.addTrait(Trait.START_INDENT, new Integer(startIndent));
}
int spaceStart = marginProps.startIndent.getValue(context)
- bpProps.getBorderStartWidth(false)
- bpProps.getPaddingStart(false, context);
if (spaceStart != 0) {
area.addTrait(Trait.SPACE_START, new Integer(spaceStart));
}
int endIndent = marginProps.endIndent.getValue(context);
if (endIndent != 0) {
area.addTrait(Trait.END_INDENT, new Integer(endIndent));
}
int spaceEnd = marginProps.endIndent.getValue(context)
- bpProps.getBorderEndWidth(false)
- bpProps.getPaddingEnd(false, context);
if (spaceEnd != 0) {
area.addTrait(Trait.SPACE_END, new Integer(spaceEnd));
}
}
public static int getEffectiveSpace(double adjust, MinOptMax space) {
if (space == null) {
return 0;
}
int sp = space.opt;
if (adjust > 0) {
sp = sp + (int)(adjust * (space.max - space.opt));
} else {
sp = sp + (int)(adjust * (space.opt - space.min));
}
return sp;
}
public static void addSpaceBeforeAfter(Area area, double adjust,
MinOptMax spaceBefore, MinOptMax spaceAfter) {
int space;
space = getEffectiveSpace(adjust, spaceBefore);
if (space != 0) {
area.addTrait(Trait.SPACE_BEFORE, new Integer(space));
}
space = getEffectiveSpace(adjust, spaceAfter);
if (space != 0) {
area.addTrait(Trait.SPACE_AFTER, new Integer(space));
}
}
/**
* Sets the traits for breaks on an area.
* @param area the area to set the traits on.
* @param breakBefore the value for break-before
* @param breakAfter the value for break-after
*/
public static void addBreaks(Area area, int breakBefore, int breakAfter) {
area.addTrait(Trait.BREAK_AFTER, new Integer(breakAfter));
area.addTrait(Trait.BREAK_BEFORE, new Integer(breakBefore));
}
/**
* Adds the text-decoration traits to the area.
* @param area the area to set the traits on
* @param deco the text decorations
*/
public static void addTextDecoration(Area area, CommonTextDecoration deco) {
//TODO Finish text-decoration
if (deco != null) {
if (deco.hasUnderline()) {
area.addTrait(Trait.UNDERLINE, Boolean.TRUE);
area.addTrait(Trait.UNDERLINE_COLOR, deco.getUnderlineColor());
}
if (deco.hasOverline()) {
area.addTrait(Trait.OVERLINE, Boolean.TRUE);
area.addTrait(Trait.OVERLINE_COLOR, deco.getOverlineColor());
}
if (deco.hasLineThrough()) {
area.addTrait(Trait.LINETHROUGH, Boolean.TRUE);
area.addTrait(Trait.LINETHROUGH_COLOR, deco.getLineThroughColor());
}
if (deco.isBlinking()) {
area.addTrait(Trait.BLINK, Boolean.TRUE);
}
}
}
/**
* Sets the producer's ID as a trait on the area. This can be used to track back the
* generating FO node.
* @param area the area to set the traits on
* @param id the ID to set
*/
public static void setProducerID(Area area, String id) {
if (id != null && id.length() > 0) {
area.addTrait(Trait.PROD_ID, id);
}
}
}
|