aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/fo/PropertyManager.java
blob: 2505c323d17c7d8e399cb9fd8d9481a250b153ee (plain)
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * Copyright 1999-2004 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.fo;

// FOP
import org.apache.fop.apps.FOPException;
import org.apache.fop.fonts.Font;
import org.apache.fop.fo.properties.Property;
import org.apache.fop.fo.properties.CommonBorderAndPadding;
import org.apache.fop.fo.properties.CommonMarginBlock;
import org.apache.fop.fo.properties.CommonMarginInline;
import org.apache.fop.fo.properties.CommonBackground;
import org.apache.fop.fo.properties.CommonAccessibility;
import org.apache.fop.fo.properties.CommonAural;
import org.apache.fop.fo.properties.CommonRelativePosition;
import org.apache.fop.fo.properties.CommonAbsolutePosition;
import org.apache.fop.traits.BlockProps;
import org.apache.fop.traits.InlineProps;
import org.apache.fop.traits.SpaceVal;
import org.apache.fop.traits.LayoutProps; // keep, break, span, space?
import org.apache.fop.fonts.FontMetrics;
import org.apache.fop.fo.properties.CommonHyphenation;
import org.xml.sax.Attributes;

/**
 * Helper class for managing groups of properties.
 */
public class PropertyManager implements Constants {

    private PropertyList propertyList;
    private FOTreeControl foTreeControl = null;
    private Font fontState = null;
    private CommonBorderAndPadding borderAndPadding = null;
    private CommonHyphenation hyphProps = null;
    private TextInfo textInfo = null;
    private static final String NONE = "none";

    private static final int[] SA_BEFORE = new int[] {
        PR_BORDER_BEFORE_COLOR, PR_BORDER_BEFORE_STYLE, PR_BORDER_BEFORE_WIDTH, PR_PADDING_BEFORE};
    private static final int[] SA_AFTER = new int[]{
        PR_BORDER_AFTER_COLOR, PR_BORDER_AFTER_STYLE, PR_BORDER_AFTER_WIDTH, PR_PADDING_AFTER};
    private static final int[] SA_START = new int[]{
        PR_BORDER_START_COLOR, PR_BORDER_START_STYLE, PR_BORDER_START_WIDTH, PR_PADDING_START};
    private static final int[] SA_END = new int[]{
        PR_BORDER_END_COLOR, PR_BORDER_END_STYLE, PR_BORDER_END_WIDTH, PR_PADDING_END};
    
    /**
     * Main constructor
     * @param propList list of properties for the FO, initialized
     * from the attributes in the input source document
     */
    public PropertyManager(PropertyList propList) {
        propertyList = propList;
    }

    /**
     * Sets the Document object telling the property manager which fonts are
     * available.
     * @param foTreeControl foTreeControl implementation containing font
     * information
     */
    public void setFontInfo(FOTreeControl foTreeControl) {
        this.foTreeControl = foTreeControl;
    }


    /**
     * Constructs a FontState object. If it was constructed before it is
     * reused.
     * @param foTreeControl FOTreeControl implementation containing the font
     * information
     * @return a FontState object
     */
    public Font getFontState(FOTreeControl foTreeControl) {
        if (fontState == null) {
            if (foTreeControl == null) {
                foTreeControl = this.foTreeControl;
            } else if (this.foTreeControl == null) {
                this.foTreeControl = foTreeControl;
            }
            /**@todo this is ugly. need to improve. */

            String fontFamily = propertyList.get(PR_FONT_FAMILY).getString();
            String fontStyle = propertyList.get(PR_FONT_STYLE).getString();
            String fw = propertyList.get(PR_FONT_WEIGHT).getString();
            int fontWeight = 400;
            if (fw.equals("bolder")) {
                // +100 from inherited
            } else if (fw.equals("lighter")) {
                // -100 from inherited
            } else {
                try {
                    fontWeight = Integer.parseInt(fw);
                } catch (NumberFormatException nfe) {
                } /** TODO: log that exception */
            }
            fontWeight = ((int) fontWeight / 100) * 100;
            if (fontWeight < 100) {
                fontWeight = 100;
            } else if (fontWeight > 900) {
                fontWeight = 900;
            }

            // NOTE: this is incomplete. font-size may be specified with
            // various kinds of keywords too
            int fontSize = propertyList.get(PR_FONT_SIZE).getLength().getValue();
            //int fontVariant = propertyList.get("font-variant").getEnum();
            String fname = foTreeControl.getFontInfo().fontLookup(fontFamily, fontStyle,
                                               fontWeight);
            FontMetrics metrics = foTreeControl.getFontInfo().getMetricsFor(fname);
            fontState = new Font(fname, metrics, fontSize);
        }
        return fontState;
    }


    /**
     * Constructs a BorderAndPadding object. If it was constructed before it is
     * reused.
     * @return a BorderAndPadding object
     */
    public CommonBorderAndPadding getBorderAndPadding() {
        if (borderAndPadding == null) {
            this.borderAndPadding = new CommonBorderAndPadding();

            initBorderInfo(CommonBorderAndPadding.BEFORE, SA_BEFORE);
            initBorderInfo(CommonBorderAndPadding.AFTER, SA_AFTER);
            initBorderInfo(CommonBorderAndPadding.START, SA_START);
            initBorderInfo(CommonBorderAndPadding.END, SA_END);
        }
        return borderAndPadding;
    }

    private void initBorderInfo(int whichSide, int[] saSide) {
        borderAndPadding.setPadding(whichSide,
                                    propertyList.get(saSide[3]).getCondLength());
        // If style = none, force width to 0, don't get Color (spec 7.7.20)
        int style = propertyList.get(saSide[1]).getEnum();
        if (style != Constants.NONE) {
            borderAndPadding.setBorder(whichSide, style,
                                       propertyList.get(saSide[2]).getCondLength(),
                                       propertyList.get(saSide[0]).getColorType());
        }
    }

    /**
     * Constructs a HyphenationProps objects. If it was constructed before it is
     * reused.
     * @return a HyphenationProps object
     */
    public CommonHyphenation getHyphenationProps() {
        if (hyphProps == null) {
            this.hyphProps = new CommonHyphenation();
            hyphProps.hyphenate =
              this.propertyList.get(PR_HYPHENATE).getEnum();
            hyphProps.hyphenationChar = this.propertyList.get(
                                          PR_HYPHENATION_CHARACTER).getCharacter();
            hyphProps.hyphenationPushCharacterCount = this.propertyList.get(
                      PR_HYPHENATION_PUSH_CHARACTER_COUNT).getNumber().
                    intValue();
            hyphProps.hyphenationRemainCharacterCount = this.propertyList.get(
                      PR_HYPHENATION_REMAIN_CHARACTER_COUNT).getNumber().
                    intValue();
            hyphProps.language =
              this.propertyList.get(PR_LANGUAGE).getString();
            hyphProps.country = this.propertyList.get(PR_COUNTRY).getString();
        }
        return hyphProps;
    }

    /*public int checkBreakBefore(Area area) {
         if (!(area instanceof ColumnArea)) {
             switch (propertyList.get("break-before").getEnum()) {
             case BreakBefore.PAGE:
                 return Status.FORCE_PAGE_BREAK;
             case BreakBefore.ODD_PAGE:
                 return Status.FORCE_PAGE_BREAK_ODD;
             case BreakBefore.EVEN_PAGE:
                 return Status.FORCE_PAGE_BREAK_EVEN;
             case BreakBefore.COLUMN:
                 return Status.FORCE_COLUMN_BREAK;
             default:
                 return Status.OK;
             }
         } else {
             ColumnArea colArea = (ColumnArea)area;
             switch (propertyList.get("break-before").getEnum()) {
             case BreakBefore.PAGE:
                 // if first ColumnArea, and empty, return OK
                 if (!colArea.hasChildren() && (colArea.getColumnIndex() == 1))
                     return Status.OK;
                 else
                     return Status.FORCE_PAGE_BREAK;
             case BreakBefore.ODD_PAGE:
                 // if first ColumnArea, empty, _and_ in odd page,
                 // return OK
                 if (!colArea.hasChildren() && (colArea.getColumnIndex() == 1)
                         && (colArea.getPage().getNumber() % 2 != 0))
                     return Status.OK;
                 else
                     return Status.FORCE_PAGE_BREAK_ODD;
             case BreakBefore.EVEN_PAGE:
                 // if first ColumnArea, empty, _and_ in even page,
                 // return OK
                 if (!colArea.hasChildren() && (colArea.getColumnIndex() == 1)
                         && (colArea.getPage().getNumber() % 2 == 0))
                     return Status.OK;
                 else
                     return Status.FORCE_PAGE_BREAK_EVEN;
             case BreakBefore.COLUMN:
                 // if ColumnArea is empty return OK
                 if (!area.hasChildren())
                     return Status.OK;
                 else
                     return Status.FORCE_COLUMN_BREAK;
             default:
                 return Status.OK;
             }
         }
     }

     public int checkBreakAfter(Area area) {
         switch (propertyList.get("break-after").getEnum()) {
         case BreakAfter.PAGE:
             return Status.FORCE_PAGE_BREAK;
         case BreakAfter.ODD_PAGE:
             return Status.FORCE_PAGE_BREAK_ODD;
         case BreakAfter.EVEN_PAGE:
             return Status.FORCE_PAGE_BREAK_EVEN;
         case BreakAfter.COLUMN:
             return Status.FORCE_COLUMN_BREAK;
         default:
             return Status.OK;
         }
     }*/


    /**
     * Constructs a MarginProps objects. If it was constructed before it is
     * reused.
     * @return a MarginProps object
     */
    public CommonMarginBlock getMarginProps() {
        CommonMarginBlock props = new CommonMarginBlock();

        // Common Margin Properties-Block
        props.marginTop =
          this.propertyList.get(PR_MARGIN_TOP).getLength().getValue();
        props.marginBottom =
          this.propertyList.get(PR_MARGIN_BOTTOM).getLength().getValue();
        props.marginLeft =
          this.propertyList.get(PR_MARGIN_LEFT).getLength().getValue();
        props.marginRight =
          this.propertyList.get(PR_MARGIN_RIGHT).getLength().getValue();

        // For now, we only get the optimum value for space-before and after
        props.spaceBefore = this.propertyList.get(PR_SPACE_BEFORE).
                        getSpace().getOptimum().getLength().getValue();
        props.spaceAfter = this.propertyList.get(PR_SPACE_AFTER).
                        getSpace().getOptimum().getLength().getValue();


        // For now we do the section 5.3.2 calculation here.
        // This is a hack that doesn't deal correctly with:
        // - Reference vs. non-reference areas.
        // - writing mode, it mixes start and left.
        // - inherited values of margins and indents.
        // When the indents properties calculate this values correctly,
        // the block below can be removed and replaced with simple
        // props.startIndent = this.propertyList.get(PR_START_INDENT)
        // props.endIndent = this.propertyList.get(PR_END_INDENT)
        CommonBorderAndPadding bpProps = getBorderAndPadding();

        int startIndent = 0;
        if (props.marginLeft != 0) {
            startIndent = props.marginLeft; 
        } else {
            startIndent = this.propertyList.get(PR_START_INDENT).
                                getLength().getValue(); 
        }
        props.startIndent = startIndent +
                            bpProps.getBorderStartWidth(false) +
                            bpProps.getPaddingStart(false);  

        int endIndent = 0;
        if (props.marginRight != 0) {
            endIndent = props.marginRight; 
        } else {
            endIndent = this.propertyList.get(PR_END_INDENT).
                                getLength().getValue(); 
        }
        props.endIndent = endIndent +
                          bpProps.getBorderEndWidth(false) +
                          bpProps.getPaddingEnd(false);

        return props;
    }

    /**
     * Constructs a BackgroundProps objects. If it was constructed before it is
     * reused.
     * @return a BackgroundProps object
     */
    public CommonBackground getBackgroundProps() {
        CommonBackground bp = new CommonBackground();
        bp.backAttachment = propertyList.get(PR_BACKGROUND_ATTACHMENT).getEnum();
        bp.backColor = propertyList.get(PR_BACKGROUND_COLOR).getColorType();
        if (bp.backColor.getAlpha() == 0) {
            bp.backColor = null;
        }

        bp.backImage = propertyList.get(PR_BACKGROUND_IMAGE).getString();
        if (bp.backImage == null || NONE.equals(bp.backImage)) {
            bp.backImage = null;
        } else {
            bp.backRepeat = propertyList.get(PR_BACKGROUND_REPEAT).getEnum();
            Property prop = propertyList.get(PR_BACKGROUND_POSITION_HORIZONTAL);
            if (prop != null) {
                bp.backPosHorizontal = prop.getLength();
            }
            prop = propertyList.get(PR_BACKGROUND_POSITION_VERTICAL);
            if (prop != null) {
                bp.backPosVertical = prop.getLength();
            }
        }

        return bp;
    }

    /**
     * Constructs a MarginInlineProps objects. If it was constructed before it is
     * reused.
     * @return a MarginInlineProps object
     */
    public CommonMarginInline getMarginInlineProps() {
        CommonMarginInline props = new CommonMarginInline();
        return props;
    }

    /**
     * Constructs a InlineProps objects. If it was constructed before it is
     * reused.
     * @return a InlineProps object
     */
    public InlineProps getInlineProps() {
        InlineProps props = new InlineProps();
        props.spaceStart =  new SpaceVal(propertyList.get(PR_SPACE_START).getSpace());
        props.spaceEnd =    new SpaceVal(propertyList.get(PR_SPACE_END).getSpace());
        return props;
    }

    /**
     * Constructs a AccessibilityProps objects. If it was constructed before it is
     * reused.
     * @return a AccessibilityProps object
     */
    public CommonAccessibility getAccessibilityProps() {
        CommonAccessibility props = new CommonAccessibility();
        String str;
        str = this.propertyList.get(PR_SOURCE_DOCUMENT).getString();
        if (!NONE.equals(str)) {
            props.sourceDoc = str;
        }
        str = this.propertyList.get(PR_ROLE).getString();
        if (!NONE.equals(str)) {
            props.role = str;
        }
        return props;
    }

    /**
     * Constructs a AuralProps objects. If it was constructed before it is
     * reused.
     * @return a AuralProps object
     */
    public CommonAural getAuralProps() {
        CommonAural props = new CommonAural();
        return props;
    }

    /**
     * Constructs a RelativePositionProps objects. If it was constructed before it is
     * reused.
     * @return a RelativePositionProps object
     */
    public CommonRelativePosition getRelativePositionProps() {
        CommonRelativePosition props = new CommonRelativePosition();
        return props;
    }

    /**
     * Constructs a AbsolutePositionProps objects. If it was constructed before
     * it is reused.
     * @return a AbsolutePositionProps object
     */
    public CommonAbsolutePosition getAbsolutePositionProps() {
        CommonAbsolutePosition props = new CommonAbsolutePosition();
        props.absolutePosition =
          this.propertyList.get(PR_ABSOLUTE_POSITION).getEnum();
        props.top = this.propertyList.get(PR_TOP).getLength().getValue();
        props.bottom = this.propertyList.get(PR_BOTTOM).getLength().getValue();
        props.left = this.propertyList.get(PR_LEFT).getLength().getValue();
        props.right = this.propertyList.get(PR_RIGHT).getLength().getValue();
        return props;
    }

    /**
     * Constructs a BlockProps objects. If it was constructed before it is
     * reused.
     * @return a BlockProps object
     */
    public BlockProps getBlockProps() {
        BlockProps props = new BlockProps();
        props.firstIndent = this.propertyList.get(PR_TEXT_INDENT).getLength();
        props.lastIndent = 0;
            /*this.propertyList.get("last-line-end-indent").getLength().mvalue(); */
        props.textAlign = this.propertyList.get(PR_TEXT_ALIGN).getEnum();
        props.textAlignLast = this.propertyList.get(PR_TEXT_ALIGN_LAST).getEnum();
        props.lineStackType = this.propertyList.get(PR_LINE_STACKING_STRATEGY).getEnum();

        return props;
    }

    /**
     * Constructs a LayoutProps objects. If it was constructed before it is
     * reused.
     * @return a LayoutProps object
     */
    public LayoutProps getLayoutProps() {
        LayoutProps props = new LayoutProps();
        props.breakBefore = this.propertyList.get(PR_BREAK_BEFORE).getEnum();
        props.breakAfter = this.propertyList.get(PR_BREAK_AFTER).getEnum();
        props.bIsSpan = (this.propertyList.get(PR_SPAN).getEnum() == Constants.ALL);
        props.spaceBefore = new SpaceVal(
                              this.propertyList.get(PR_SPACE_BEFORE).getSpace());
        props.spaceAfter = new SpaceVal(
                             this.propertyList.get(PR_SPACE_AFTER).getSpace());
        return props;
    }

    /**
     * Constructs a TextInfo objects. If it was constructed before it is
     * reused.
     * @param foTreeControl FOTreeControl implementation containing list of
     * available fonts
     * @return a TextInfo object
     */
    public TextInfo getTextLayoutProps(FOTreeControl foTreeControl) {
        if (textInfo == null) {
            textInfo = new TextInfo();
            textInfo.fs = getFontState(foTreeControl);
            textInfo.color = propertyList.get(PR_COLOR).getColorType();

            textInfo.verticalAlign =
              propertyList.get(PR_VERTICAL_ALIGN).getEnum();

            textInfo.wrapOption = propertyList.get(PR_WRAP_OPTION).getEnum();
            textInfo.bWrap = (textInfo.wrapOption == Constants.WRAP);

            textInfo.wordSpacing = new SpaceVal(
                                     propertyList.get(PR_WORD_SPACING).getSpace());

            /* textInfo.letterSpacing =
               new SpaceVal(propertyList.get("letter-spacing").getSpace());*/

            textInfo.whiteSpaceCollapse =
              propertyList.get(PR_WHITE_SPACE_COLLAPSE).getEnum();

            textInfo.lineHeight = this.propertyList.get(
                                    PR_LINE_HEIGHT).getLength().getValue();

            textInfo.textTransform
                    = this.propertyList.get(PR_TEXT_TRANSFORM).getEnum();

            textInfo.hyphChar = this.propertyList.get(
                                  PR_HYPHENATION_CHARACTER).getCharacter();

        }
        return textInfo;
    }

    /**
     * Calculate absolute reference-orientation relative to media orientation.
     * @return the enumerated reference-orientation
     */
    public int getAbsRefOrient() {
        return propertyList.get(PR_REFERENCE_ORIENTATION).getNumber().intValue();
    }

    /**
     * @return the enumerated writing-mode
     */
    public int getWritingMode() {
        return propertyList.get(PR_WRITING_MODE).getEnum();
    }

}