aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/area/Trait.java
blob: a58d527a997838b638dcad52de58bab2df970820 (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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
 * Copyright 1999-2006 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.area;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.fop.datatypes.ColorType;
import org.apache.fop.fo.Constants;
import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.image.FopImage;
import org.apache.fop.traits.BorderProps;

import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

// properties should be serialized by the holder
/**
 * Area traits used for rendering.
 * This class represents an area trait that specifies a value for rendering.
 */
public class Trait implements Serializable {

    /** Logger instance */
    private static Log log = LogFactory.getLog(Trait.class);
    
    /**
     * Id reference line, not resolved.
     * not sure if this is needed.
     */
    public static final Integer ID_LINK = new Integer(0);

    /**
     * Internal link trait.
     * This is resolved and provides a link to an internal area.
     */
    public static final Integer INTERNAL_LINK = new Integer(1); //resolved

    /**
     * External link. A URL link to an external resource.
     */
    public static final Integer EXTERNAL_LINK = new Integer(2);

    /**
     * The font triplet for the current font.
     */
    public static final Integer FONT = new Integer(3);

    /**
     * Font size for the current font.
     */
    public static final Integer FONT_SIZE = new Integer(4);

    /**
     * The current colour.
     */
    public static final Integer COLOR = new Integer(7);

    /**
     * The ID of the FO that produced an area.
     */
    public static final Integer PROD_ID = new Integer(8);

    /**
     * Background trait for an area.
     */
    public static final Integer BACKGROUND = new Integer(9);

    /**
     * Underline trait used when rendering inline parent.
     */
    public static final Integer UNDERLINE = new Integer(10);

    /**
     * Overline trait used when rendering inline parent.
     */
    public static final Integer OVERLINE = new Integer(11);

    /**
     * Linethrough trait used when rendering inline parent.
     */
    public static final Integer LINETHROUGH = new Integer(12);

    /**
     * Shadow offset.
     */
    public static final Integer OFFSET = new Integer(13);

    /**
     * The shadow for text.
     */
    public static final Integer SHADOW = new Integer(14);

    /**
     * The border start.
     */
    public static final Integer BORDER_START = new Integer(15);

    /**
     * The border end.
     */
    public static final Integer BORDER_END = new Integer(16);

    /**
     * The border before.
     */
    public static final Integer BORDER_BEFORE = new Integer(17);

    /**
     * The border after.
     */
    public static final Integer BORDER_AFTER = new Integer(18);

    /**
     * The padding start.
     */
    public static final Integer PADDING_START = new Integer(19);

    /**
     * The padding end.
     */
    public static final Integer PADDING_END = new Integer(20);

    /**
     * The padding before.
     */
    public static final Integer PADDING_BEFORE = new Integer(21);

    /**
     * The padding after.
     */
    public static final Integer PADDING_AFTER = new Integer(22);

    /**
     * The space start.
     */
    public static final Integer SPACE_START = new Integer(23);

    /**
     * The space end.
     */
    public static final Integer SPACE_END  = new Integer(24);

    /**
     * break before
     */
    public static final Integer BREAK_BEFORE = new Integer(25);

    /**
     * break after
     */
    public static final Integer BREAK_AFTER = new Integer(26);

    /**
     * The start-indent trait.
     */
    public static final Integer START_INDENT = new Integer(27);

    /**
     * The end-indent trait.
     */
    public static final Integer END_INDENT  = new Integer(28);

    /** The space-before trait. */
    public static final Integer SPACE_BEFORE  = new Integer(29);
    
    /** The space-after trait. */
    public static final Integer SPACE_AFTER  = new Integer(30);
    
    /** The is-reference-area trait. */
    public static final Integer IS_REFERENCE_AREA = new Integer(31);
    
    /** The is-viewport-area trait. */
    public static final Integer IS_VIEWPORT_AREA = new Integer(32);
    
    /** Blinking trait used when rendering inline parent. */
    public static final Integer BLINK = new Integer(33);
    
    /** Trait for color of underline decorations when rendering inline parent. */
    public static final Integer UNDERLINE_COLOR = new Integer(34);
    /** Trait for color of overline decorations when rendering inline parent. */
    public static final Integer OVERLINE_COLOR = new Integer(35);
    /** Trait for color of linethrough decorations when rendering inline parent. */
    public static final Integer LINETHROUGH_COLOR = new Integer(36);
    
    private static final Map TRAIT_INFO = new HashMap();
    
    /** The list of simple traits in order to avoid iterators */
    public static final Object[] TRAIT_LIST;

    private static class TraitInfo {
        private String name;
        private Class clazz; // Class of trait data

        public TraitInfo(String name, Class clazz) {
            this.name = name;
            this.clazz = clazz;
        }

        public String getName() {
            return this.name;
        }

        public Class getClazz() {
            return this.clazz;
        }
    }

    static {
        // Create a hashmap mapping trait code to name for external representation
        TRAIT_INFO.put(ID_LINK, new TraitInfo("id-link", String.class));
        TRAIT_INFO.put(INTERNAL_LINK,
                          new TraitInfo("internal-link", String.class));
        TRAIT_INFO.put(EXTERNAL_LINK,
                          new TraitInfo("external-link", String.class));
        TRAIT_INFO.put(FONT,
                          new TraitInfo("font", FontTriplet.class));
        TRAIT_INFO.put(FONT_SIZE,
                          new TraitInfo("font-size", Integer.class));
        TRAIT_INFO.put(COLOR, new TraitInfo("color", Color.class));
        TRAIT_INFO.put(PROD_ID, new TraitInfo("prod-id", String.class));
        TRAIT_INFO.put(BACKGROUND,
                          new TraitInfo("background", Background.class));
        TRAIT_INFO.put(UNDERLINE,
                          new TraitInfo("underline-score", Boolean.class));
        TRAIT_INFO.put(UNDERLINE_COLOR, new TraitInfo("underline-score-color", Color.class));
        TRAIT_INFO.put(OVERLINE,
                          new TraitInfo("overline-score", Boolean.class));
        TRAIT_INFO.put(OVERLINE_COLOR, new TraitInfo("overline-score-color", Color.class));
        TRAIT_INFO.put(LINETHROUGH,
                          new TraitInfo("through-score", Boolean.class));
        TRAIT_INFO.put(LINETHROUGH_COLOR, new TraitInfo("through-score-color", Color.class));
        TRAIT_INFO.put(BLINK,
                          new TraitInfo("blink", Boolean.class));
        TRAIT_INFO.put(OFFSET, new TraitInfo("offset", Integer.class));
        TRAIT_INFO.put(SHADOW, new TraitInfo("shadow", Integer.class));
        TRAIT_INFO.put(BORDER_START,
                          new TraitInfo("border-start", BorderProps.class));
        TRAIT_INFO.put(BORDER_END,
                          new TraitInfo("border-end", BorderProps.class));
        TRAIT_INFO.put(BORDER_BEFORE,
                          new TraitInfo("border-before", BorderProps.class));
        TRAIT_INFO.put(BORDER_AFTER,
                          new TraitInfo("border-after", BorderProps.class));
        TRAIT_INFO.put(PADDING_START,
                          new TraitInfo("padding-start", Integer.class));
        TRAIT_INFO.put(PADDING_END,
                          new TraitInfo("padding-end", Integer.class));
        TRAIT_INFO.put(PADDING_BEFORE,
                          new TraitInfo("padding-before", Integer.class));
        TRAIT_INFO.put(PADDING_AFTER,
                          new TraitInfo("padding-after", Integer.class));
        TRAIT_INFO.put(SPACE_START,
                          new TraitInfo("space-start", Integer.class));
        TRAIT_INFO.put(SPACE_END,
                          new TraitInfo("space-end", Integer.class));
        TRAIT_INFO.put(BREAK_BEFORE,
                          new TraitInfo("break-before", Integer.class));
        TRAIT_INFO.put(BREAK_AFTER,
                          new TraitInfo("break-after", Integer.class));
        TRAIT_INFO.put(START_INDENT,
                new TraitInfo("start-indent", Integer.class));
        TRAIT_INFO.put(END_INDENT,
                new TraitInfo("end-indent", Integer.class));
        TRAIT_INFO.put(SPACE_BEFORE,
                new TraitInfo("space-before", Integer.class));
        TRAIT_INFO.put(SPACE_AFTER,
                new TraitInfo("space-after", Integer.class));
        TRAIT_INFO.put(IS_REFERENCE_AREA,
                new TraitInfo("is-reference-area", Boolean.class));
        TRAIT_INFO.put(IS_VIEWPORT_AREA,
                new TraitInfo("is-viewport-area", Boolean.class));
        
        TRAIT_LIST = TRAIT_INFO.keySet().toArray();
    }

    /**
     * Get the trait name for a trait code.
     *
     * @param traitCode the trait code to get the name for
     * @return the trait name
     */
    public static String getTraitName(Object traitCode) {
        Object obj = TRAIT_INFO.get(traitCode);
        if (obj != null) {
            return ((TraitInfo) obj).getName();
        } else {
            return "unknown-trait-" + traitCode.toString();
        }
    }

    /**
     * Get the trait code for a trait name.
     *
     * @param sTraitName the name of the trait to find
     * @return the trait code object
     */
    public static Object getTraitCode(String sTraitName) {
        Iterator iter = TRAIT_INFO.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            TraitInfo ti = (TraitInfo) entry.getValue();
            if (ti != null && ti.getName().equals(sTraitName)) {
                return entry.getKey();
            }
        }
        return null;
    }

    /**
     * Get the data storage class for the trait.
     *
     * @param oTraitCode the trait code to lookup
     * @return the class type for the trait
     */
    public static Class getTraitClass(Object oTraitCode) {
        TraitInfo ti = (TraitInfo) TRAIT_INFO.get(oTraitCode);
        return (ti != null ? ti.getClazz() : null);
    }

    /**
     * The type of trait for an area.
     */
    private Object propType;

    /**
     * The data value of the trait.
     */
    private Object data;

    /**
     * Create a new empty trait.
     */
    public Trait() {
        this.propType = null;
        this.data = null;
    }

    /**
     * Create a trait with the value and type.
     *
     * @param propType the type of trait
     * @param data the data value
     */
    public Trait(Object propType, Object data) {
        this.propType = propType;
        this.data = data;
    }

    /**
     * Returns the trait data value.
     * @return the trait data value
     */
    public Object getData() {
        return this.data;
    }

    /**
     * Returns the property type.
     * @return the property type
     */
    public Object getPropType() {
        return this.propType;
    }

    /**
     * Return the string for debugging.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return data.toString();
    }

    /**
     * Make a trait value.
     *
     * @param oCode trait code
     * @param sTraitValue trait value as String
     * @return the trait value as object
     */
    public static Object makeTraitValue(Object oCode, String sTraitValue) {
        // Get the code from the name
        // See what type of object it is
        // Convert string value to an object of that type
        Class tclass = getTraitClass(oCode);
        if (tclass == null) {
            return null;
        }
        if (tclass.equals(String.class)) {
            return sTraitValue;
        }
        if (tclass.equals(Integer.class)) {
            return new Integer(sTraitValue);
        }
        // See if the class has a constructor from string or can read from a string
        try {
            Object o = tclass.newInstance();
            //return o.fromString(sTraitValue);
        } catch (IllegalAccessException e1) {
            log.error("Can't create instance of "
                               + tclass.getName());
            return null;
        } catch (InstantiationException e2) {
            log.error("Can't create instance of "
                               + tclass.getName());
            return null;
        }


        return null;
    }

    /**
     * Serializable ColorType implementation for the area tree.
     * @TODO Think about switching to java.awt.Color entirely!
     */
    public static class Color implements ColorType, Serializable {

        private float red;
        private float green;
        private float blue;
        private float alpha;
        
        /**
         * Creates a new Color instance
         * @param r the red component
         * @param g the green component
         * @param b the blue component
         * @param a the alpha component
         */
        public Color(float r, float g, float b, float a) {
            this.red = r;
            this.green = g;
            this.blue = b;
            this.alpha = a;
        }
        
        /**
         * Copy constructor
         * @param col the ColorType instance which shall be duplicated
         */
        public Color(ColorType col) {
            this(col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha());
        }
        
        /** @see org.apache.fop.datatypes.ColorType#getRed() */
        public float getRed() {
            return this.red;
        }

        /** @see org.apache.fop.datatypes.ColorType#getGreen() */
        public float getGreen() {
            return this.green;
        }

        /** @see org.apache.fop.datatypes.ColorType#getBlue() */
        public float getBlue() {
            return this.blue;
        }

        /** @see org.apache.fop.datatypes.ColorType#getAlpha() */
        public float getAlpha() {
            return this.alpha;
        }

        /** @see org.apache.fop.datatypes.ColorType#getAWTColor() */
        public java.awt.Color getAWTColor() {
            return new java.awt.Color(red, green, blue, alpha);
        }
        
        /**
         * Converts a given color to a serializable instance if necessary.
         * @param col the color
         * @return the serializable color value.
         */
        public static ColorType makeSerializable(ColorType col) {
            if (col == null) {
                return null;
            } else if (col instanceof Serializable) {
                return col;
            } else {
                return new Color(col);
            }
        }
        
        /** @see java.lang.Object#hashCode() */
        public int hashCode() {
            return toString().hashCode();
        }

        /** @see java.lang.Object#equals(java.lang.Object) */
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            } else if (obj == this) {
                return true;
            } else {
                if (obj instanceof ColorType) {
                    ColorType other = (ColorType)obj;
                    return getRed() == other.getRed()
                            && getGreen() == other.getGreen() 
                            && getBlue() == other.getBlue()
                            && getAlpha() == other.getAlpha();
                }
            }
            return false;
        }

        /**
         * Returns a Color represtation of a string of the format "#RRGGBB".
         * @param s the string
         * @return the Color value
         */
        public static Color valueOf(String s) {
            if (s == null) {
                return null;
            }
            if (!s.startsWith("#")) {
                throw new IllegalArgumentException("Color must start with '#'");
            }
            int r = Integer.parseInt(s.substring(1, 3), 16);
            int g = Integer.parseInt(s.substring(3, 5), 16);
            int b = Integer.parseInt(s.substring(5, 7), 16);
            int a = 255;
            if (s.length() > 7) {
                a = Integer.parseInt(s.substring(7, 9), 16);
            }
            return new Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
        }
        
        /** @see java.lang.Object#toString() */
        public String toString() {
            StringBuffer sbuf = new StringBuffer(8);
            sbuf.append('#');
            String s = Integer.toHexString((int)(red * 255.0));
            if (s.length() == 1) {
                sbuf.append('0');
            }
            sbuf.append(s);
            s = Integer.toHexString((int)(green * 255.0));
            if (s.length() == 1) {
                sbuf.append('0');
            }
            sbuf.append(s);
            s = Integer.toHexString((int)(blue * 255.0));
            if (s.length() == 1) {
                sbuf.append('0');
            }
            sbuf.append(s);
            if (alpha != 1) {
                s = Integer.toHexString((int)(alpha * 255.0));
                if (s.length() == 1) {
                    sbuf.append('0');
                }
                sbuf.append(s);
            }
            return sbuf.toString();
        }

    }
    
    /**
     * Background trait structure.
     * Used for storing back trait information which are related.
     */
    public static class Background implements Serializable {

        /** The background color if any. */
        private ColorType color = null;

        /** The background image url if any. */
        private String url = null;
        
        /** The background image if any. */
        private FopImage fopimage = null;

        /** Background repeat enum for images. */
        private int repeat;

        /** Background horizontal offset for images. */
        private int horiz;

        /** Background vertical offset for images. */
        private int vertical;

        /**
         * Returns the background color.
         * @return background color, null if n/a
         */
        public ColorType getColor() {
            return color;
        }

        /**
         * Returns the horizontal offset for images.
         * @return the horizontal offset
         */
        public int getHoriz() {
            return horiz;
        }

        /**
         * Returns the image repetition behaviour for images.
         * @return the image repetition behaviour
         */
        public int getRepeat() {
            return repeat;
        }

        /**
         * Returns the URL to the background image
         * @return URL to the background image, null if n/a
         */
        public String getURL() {
            return url;
        }

        /**
         * Returns the FopImage representing the background image
         * @return the background image, null if n/a
         */
        public FopImage getFopImage() {
            return fopimage;
        }

        /**
         * Returns the vertical offset for images.
         * @return the vertical offset
         */
        public int getVertical() {
            return vertical;
        }

        /**
         * Sets the color.
         * @param color The color to set
         */
        public void setColor(ColorType color) {
            this.color = Color.makeSerializable(color);
        }

        /**
         * Sets the horizontal offset.
         * @param horiz The horizontal offset to set
         */
        public void setHoriz(int horiz) {
            this.horiz = horiz;
        }

        /**
         * Sets the image repetition behaviour for images.
         * @param repeat The image repetition behaviour to set
         */
        public void setRepeat(int repeat) {
            this.repeat = repeat;
        }

        /**
         * Sets the image repetition behaviour for images.
         * @param repeat The image repetition behaviour to set
         */
        public void setRepeat(String repeat) {
            setRepeat(getConstantForRepeat(repeat));
        }

        /**
         * Sets the URL to the background image.
         * @param url The URL to set
         */
        public void setURL(String url) {
            this.url = url;
        }

        /**
         * Sets the FopImage to use as the background image.
         * @param fopimage The FopImage to use
         */
        public void setFopImage(FopImage fopimage) {
            this.fopimage = fopimage;
        }

        /**
         * Sets the vertical offset for images.
         * @param vertical The vertical offset to set
         */
        public void setVertical(int vertical) {
            this.vertical = vertical;
        }

        private String getRepeatString() {
            switch (getRepeat()) {
            case Constants.EN_REPEAT: return "repeat";
            case Constants.EN_REPEATX: return "repeat-x";
            case Constants.EN_REPEATY: return "repeat-y";
            case Constants.EN_NOREPEAT: return "no-repeat";
            default: throw new IllegalStateException("Illegal repeat style: " + getRepeat());
            }
        }

        private static int getConstantForRepeat(String repeat) {
            if ("repeat".equalsIgnoreCase(repeat)) {
                return Constants.EN_REPEAT;
            } else if ("repeat-x".equalsIgnoreCase(repeat)) {
                return Constants.EN_REPEATX;
            } else if ("repeat-y".equalsIgnoreCase(repeat)) {
                return Constants.EN_REPEATY;
            } else if ("no-repeat".equalsIgnoreCase(repeat)) {
                return Constants.EN_NOREPEAT;
            } else {
                throw new IllegalStateException("Illegal repeat style: " + repeat);
            }
        }
        
        /**
         * Return the string for debugging.
         * @see java.lang.Object#toString()
         */
        public String toString() {
            StringBuffer sb = new StringBuffer();
            sb.append("color=").append(color);
            if (url != null) {
                sb.append(",url=").append(url);
                sb.append(",repeat=").append(getRepeatString());
                sb.append(",horiz=").append(horiz);
                sb.append(",vertical=").append(vertical);
            }
            return sb.toString();
        }

    }
}