001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: DashedBorderElement.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.txt.border;
021:
022: import java.util.Arrays;
023:
024: /**
025: * This class is responsible for managing of dashed border elements.
026: */
027: public class DashedBorderElement extends AbstractBorderElement {
028:
029: private static final char DASH_HORIZONTAL = '-';
030:
031: private static final char DASH_VERTICAL = '|';
032:
033: private static final char UNDEFINED = '?';
034:
035: private static final int UP2 = 1;
036:
037: private static final int RIGHT2 = 2;
038:
039: private static final int DOWN2 = 4;
040:
041: private static final int LEFT2 = 8;
042:
043: private static char[] map = new char[20];
044:
045: static {
046: Arrays.fill(map, UNDEFINED);
047: map[0] = ' ';
048: map[UP2] = DASH_VERTICAL;
049: map[DOWN2] = DASH_VERTICAL;
050: map[UP2 + DOWN2] = DASH_VERTICAL;
051:
052: map[LEFT2] = DASH_HORIZONTAL;
053: map[RIGHT2] = DASH_HORIZONTAL;
054: map[LEFT2 + RIGHT2] = DASH_HORIZONTAL;
055: }
056:
057: /**
058: * Constructs a newly allocated <code>DashedBorderElement</code> object.
059: * Fills <code>data</code> using superclass constructor.
060: *
061: * @param type binary representation of type gives <code>data</code>
062: */
063: public DashedBorderElement(int type) {
064: super (type);
065: }
066:
067: /**
068: * Merges dashed border element with instance of solid and double border
069: * element, returns instance of <code>SolidAndDoubleBorderElement</code>.
070: *
071: * @param sdb instance of <code>SolidAndDoubleBorderElement</code> to merge
072: * @return merged border element
073: */
074: private AbstractBorderElement mergeSolid(
075: SolidAndDoubleBorderElement sdb) {
076: AbstractBorderElement e = new SolidAndDoubleBorderElement(
077: EN_SOLID, 0);
078: for (int i = 0; i < 4; i++) {
079: e.setData(i, Math.max(data[i], sdb.getData(i)));
080: }
081: return e;
082: }
083:
084: /**
085: * Merges dashed border element with dashed border element and returns
086: * instance of <code>DashedBorderElement</code>.
087: *
088: * @param dbe instance of <code>DashedBorderElement</code> to merge
089: * @return merged border element
090: */
091: private AbstractBorderElement mergeDashed(DashedBorderElement dbe) {
092: for (int i = 0; i < 4; i++) {
093: data[i] = Math.max(data[i], dbe.getData(i));
094: }
095: return this ;
096: }
097:
098: /**
099: * Converts dashed border element to
100: * <code>SolidAndDoubleBorderElement</code>.
101: *
102: * @return converted instance of <code>SolidAndDoubleBorderElement</code>
103: */
104: private AbstractBorderElement toSolidAndDouble() {
105: AbstractBorderElement e = new SolidAndDoubleBorderElement(
106: EN_SOLID, 0);
107: for (int i = 0; i < 4; i++) {
108: e.setData(i, data[i]);
109: }
110: return e;
111: }
112:
113: /**
114: * Merges with border element.
115: * @param e instance of AbstractBorderElement
116: * @return instance of AbstractBorderElement
117: */
118: public AbstractBorderElement merge(AbstractBorderElement e) {
119: AbstractBorderElement abe = this ;
120: if (e instanceof SolidAndDoubleBorderElement) {
121: abe = mergeSolid((SolidAndDoubleBorderElement) e);
122: } else if (e instanceof DashedBorderElement) {
123: abe = mergeDashed((DashedBorderElement) e);
124: } else {
125: abe = e;
126: }
127: return abe;
128: }
129:
130: /**
131: * @see org.apache.fop.render.txt.border.AbstractBorderElement#convert2Char()
132: */
133: public char convert2Char() {
134: int key = 0;
135: key += data[UP] * UP2;
136: key += data[DOWN] * DOWN2;
137: key += data[LEFT] * LEFT2;
138: key += data[RIGHT] * RIGHT2;
139: char ch = map[key];
140: if (ch == UNDEFINED) {
141: ch = toSolidAndDouble().convert2Char();
142: }
143: return ch;
144: }
145: }
|