KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > txt > border > AbstractBorderElement


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: AbstractBorderElement.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.txt.border;
21
22 import java.awt.Point JavaDoc;
23 import java.util.Arrays JavaDoc;
24
25 import org.apache.fop.area.CTM;
26 import org.apache.fop.fo.Constants;
27 import org.apache.fop.render.txt.TXTState;
28
29 /**
30  * This class keeps information about abstract border element, i.e. specifies
31  * border element for one text position.
32  */

33 public abstract class AbstractBorderElement implements Constants {
34
35     /**
36      * Constant for a line segment, directing from a center of symbol up
37      * the the symbol boundary.
38      */

39     public static final int UP = 0;
40
41     /**
42      * Constant for a line segment, directing from a center of symbol right
43      * the the symbol boundary.
44      */

45     public static final int RIGHT = 1;
46
47     /**
48      * Constant for a line segment, directing from a center of symbol down
49      * the the symbol boundary.
50      */

51     public static final int DOWN = 2;
52
53     /**
54      * Constant for a line segment, directing from a center of symbol left
55      * the the symbol boundary.
56      */

57     public static final int LEFT = 3;
58
59     /**
60      * I-th element of this array specify, if there line from center of symbol
61      * to corresponding side (UP, RIGHT, DOWN, LEFT).
62      */

63     protected int[] data = {0, 0, 0, 0};
64
65     /**
66      * Initializes a newly created <code>AbstractBorderElement</code> object
67      * so that it represents an empty border element.
68      */

69     public AbstractBorderElement() {
70     }
71
72     /**
73      * Constructs a newly allocated <code>AbstractBorderElement</code> object.
74      * Fills array <code>data</code> using binary representation of
75      * <code>type</code>.
76      *
77      * @param type binary representation of type gives <code>data</code>
78      */

79     public AbstractBorderElement(int type) {
80         for (int i = 0; i < 4; i++) {
81             data[i] = (type >> i) & 1;
82         }
83     }
84
85     /**
86      * Returns value of side's element of <code>data</code>.
87      *
88      * @param side integer, representing side
89      * @return value of side's element
90      */

91     public int getData(int side) {
92         return data[side];
93     }
94
95     /**
96      * Sets a value for <code>data[side]</code>.
97      *
98      * @param side integer, representing side
99      * @param value a new value for <code>data[side]</code>
100      */

101     public void setData(int side, int value) {
102         data[side] = value;
103     }
104
105     /**
106      * Transform border element in according with <code>state</code>.
107      * @param state instance of TXTState
108      */

109     public void transformElement(TXTState state) {
110         // here we'll get CTM^-1 without shift
111
double[] da = state.getResultCTM().toArray();
112         CTM ctm = new CTM(da[0], -da[1], -da[2], da[3], 0, 0);
113
114         Point JavaDoc[] pa = new Point JavaDoc[4];
115         pa[0] = new Point JavaDoc(0, data[UP]);
116         pa[1] = new Point JavaDoc(data[RIGHT], 0);
117         pa[2] = new Point JavaDoc(0, -data[DOWN]);
118         pa[3] = new Point JavaDoc(-data[LEFT], 0);
119
120         Arrays.fill(data, 0);
121         for (int i = 0; i < 4; i++) {
122             Point JavaDoc p = state.transformPoint(pa[i], ctm);
123
124             int length = (int) p.distance(0, 0);
125             if (p.x == 0 && p.y > 0) {
126                 data[UP] = length;
127             } else if (p.x == 0 && p.y < 0) {
128                 data[DOWN] = length;
129             } else if (p.x > 0 && p.y == 0) {
130                 data[RIGHT] = length;
131             } else if (p.x < 0 && p.y == 0) {
132                 data[LEFT] = length;
133             }
134         }
135     }
136
137     /**
138      * Merges with border element.
139      * @param e instance of AbstractBorderElement
140      * @return instance of AbstractBorderElement
141      */

142     public abstract AbstractBorderElement merge(AbstractBorderElement e);
143
144     /**
145      * Convert internal representation of border element to char.
146      * @return corresponding char
147      */

148     public abstract char convert2Char();
149 }
150
Popular Tags