aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/layout/BorderAndPadding.java
blob: d42b82cf67a9f2c4e9467a844c2727d036eb4e3b (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
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.layout;

import org.apache.fop.datatypes.ColorType;
import org.apache.fop.datatypes.CondLength;
import org.apache.fop.fo.properties.Constants;

public class BorderAndPadding implements Cloneable {

    public static final int BEFORE = 0;
    public static final int AFTER = 1;
    public static final int START = 2;
    public static final int END = 3;

    public static final int TOP = BEFORE;
    public static final int BOTTOM = AFTER;
    public static final int LEFT = START;
    public static final int RIGHT = END;

    private static class ResolvedCondLength implements Cloneable {
        int iLength;    // Resolved length value
        boolean bDiscard;

        ResolvedCondLength(CondLength length) {
            bDiscard = length.isDiscard();
            iLength = length.mvalue();
        }

	public Object clone() throws CloneNotSupportedException {
	    return super.clone();
	}

    }

    /**
     * Return a full copy of the BorderAndPadding information. This clones all
     * padding and border information.
     * @return The copy.
     */
    public Object clone() throws CloneNotSupportedException {
	BorderAndPadding bp = (BorderAndPadding) super.clone();
	bp.padding = (ResolvedCondLength[])padding.clone();
	bp.borderInfo = (BorderInfo[])borderInfo.clone();
	for (int i=0; i<padding.length; i++) {
	    if (padding[i] != null) {
		bp.padding[i]=(ResolvedCondLength)padding[i].clone();
	    }
	    if (borderInfo[i] != null) {
		bp.borderInfo[i]=(BorderInfo)borderInfo[i].clone();
	    }
	}
	return bp;
    }

    public static class BorderInfo implements Cloneable {
        private int mStyle;          // Enum for border style
        private ColorType mColor;    // Border color
        private ResolvedCondLength mWidth;

        BorderInfo(int style, CondLength width, ColorType color) {
            mStyle = style;
            mWidth = new ResolvedCondLength(width);
            mColor = color;
        }

	public Object clone() throws CloneNotSupportedException {
	    BorderInfo bi = (BorderInfo) super.clone();
	    bi.mWidth = (ResolvedCondLength)mWidth.clone();
	    // do we need to clone the Color too???
	    return bi;
	}
    }

    private BorderInfo[] borderInfo = new BorderInfo[4];
    private ResolvedCondLength[] padding = new ResolvedCondLength[4];

    public BorderAndPadding() {}

    public void setBorder(int side, int style, CondLength width,
                          ColorType color) {
        borderInfo[side] = new BorderInfo(style, width, color);
    }

    public void setPadding(int side, CondLength width) {
        padding[side] = new ResolvedCondLength(width);
    }

    public void setPaddingLength(int side, int iLength) {
        padding[side].iLength = iLength;
    }

    public void setBorderLength(int side, int iLength) {
        borderInfo[side].mWidth.iLength = iLength;
    }

    public int getBorderLeftWidth(boolean bDiscard) {
        return getBorderWidth(LEFT, bDiscard);
    }

    public int getBorderRightWidth(boolean bDiscard) {
        return getBorderWidth(RIGHT, bDiscard);
    }

    public int getBorderTopWidth(boolean bDiscard) {
        return getBorderWidth(TOP, bDiscard);
    }

    public int getBorderBottomWidth(boolean bDiscard) {
        return getBorderWidth(BOTTOM, bDiscard);
    }

    public int getPaddingLeft(boolean bDiscard) {
        return getPadding(LEFT, bDiscard);
    }

    public int getPaddingRight(boolean bDiscard) {
        return getPadding(RIGHT, bDiscard);
    }

    public int getPaddingBottom(boolean bDiscard) {
        return getPadding(BOTTOM, bDiscard);
    }

    public int getPaddingTop(boolean bDiscard) {
        return getPadding(TOP, bDiscard);
    }


    public int getBorderWidth(int side, boolean bDiscard) {
        if ((borderInfo[side] == null) ||
	    (borderInfo[side].mStyle == Constants.NONE) ||
	    (bDiscard && borderInfo[side].mWidth.bDiscard)) {
            return 0;
        } else {
            return borderInfo[side].mWidth.iLength;
	}
    }

    public ColorType getBorderColor(int side) {
        if (borderInfo[side] != null) {
            return borderInfo[side].mColor;
        } else
            return null;
    }

    public int getBorderStyle(int side) {
        if (borderInfo[side] != null) {
            return borderInfo[side].mStyle;
        } else
            return 0;
    }

    public int getPadding(int side, boolean bDiscard) {
        if ((padding[side] == null) || (bDiscard && padding[side].bDiscard)) {
            return 0;
        } else
            return padding[side].iLength;
    }

}