KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfColorTable


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: RtfColorTable.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.util.Vector JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34  * Singelton of the RTF color table.
35  * This class was created for <fo:basic-link> tag processing.
36  * @author <a HREF="mailto:a.putz@skynamics.com">Andreas Putz</a>
37  */

38
39 public final class RtfColorTable {
40     //////////////////////////////////////////////////
41
// @@ Symbolic constants
42
//////////////////////////////////////////////////
43

44     // Defines the bit moving for the colors
45
private static final int RED = 16;
46     private static final int GREEN = 8;
47     private static final int BLUE = 0;
48
49
50     //////////////////////////////////////////////////
51
// @@ Members
52
//////////////////////////////////////////////////
53

54     /** Singelton instance */
55     private static RtfColorTable instance = null;
56
57     /** Index table for the colors */
58     private Hashtable JavaDoc colorIndex = null;
59     /** Used colors to this vector */
60     private Vector JavaDoc colorTable = null;
61         /** Map of color names to color numbers */
62         private Hashtable JavaDoc namedColors = null;
63
64
65     //////////////////////////////////////////////////
66
// @@ Construction
67
//////////////////////////////////////////////////
68

69     /**
70      * Constructor.
71      */

72     private RtfColorTable () {
73         colorTable = new Vector JavaDoc ();
74         colorIndex = new Hashtable JavaDoc ();
75                 namedColors = new Hashtable JavaDoc ();
76
77         init ();
78     }
79
80     /**
81      * Singelton.
82      *
83      * @return The instance of RTFColorTable
84      */

85     public static RtfColorTable getInstance () {
86         if (instance == null) {
87             instance = new RtfColorTable ();
88         }
89
90         return instance;
91     }
92
93
94     //////////////////////////////////////////////////
95
// @@ Initializing
96
//////////////////////////////////////////////////
97

98     /**
99      * Initialize the color table.
100      */

101     private void init () {
102         addNamedColor("black", getColorNumber (0, 0, 0).intValue());
103         addNamedColor("white", getColorNumber (255, 255, 255).intValue());
104         addNamedColor("red", getColorNumber (255, 0, 0).intValue());
105         addNamedColor("green", getColorNumber (0, 255, 0).intValue());
106         addNamedColor("blue", getColorNumber (0, 0, 255).intValue());
107         addNamedColor("cyan", getColorNumber (0, 255, 255).intValue());
108         addNamedColor("magenta", getColorNumber (255, 0, 255).intValue());
109         addNamedColor("yellow", getColorNumber (255, 255, 0).intValue());
110
111         getColorNumber (0, 0, 128);
112         getColorNumber (0, 128, 128);
113         getColorNumber (0, 128, 0);
114         getColorNumber (128, 0, 128);
115         getColorNumber (128, 0, 0);
116         getColorNumber (128, 128, 0);
117         getColorNumber (128, 128, 128);
118
119          // Added by Normand Masse
120
// Gray color added
121
addNamedColor("gray", getColorNumber(128, 128, 128).intValue());
122
123         getColorNumber (192, 192, 192);
124     }
125
126         /** define a named color for getColorNumber(String) */
127         private void addNamedColor(String JavaDoc name, int colorNumber) {
128             namedColors.put(name.toLowerCase(), new Integer JavaDoc(colorNumber));
129         }
130
131     //////////////////////////////////////////////////
132
// @@ Public methods
133
//////////////////////////////////////////////////
134

135         /**
136          * @param name a named color
137          * @return the RTF number of a named color, or null if name not found
138          */

139     public Integer JavaDoc getColorNumber (String JavaDoc name) {
140         return ((Integer JavaDoc)namedColors.get(name.toLowerCase()));
141     }
142
143     /**
144      * Gets the number of color in the color table
145      *
146      * @param red Color level red
147      * @param green Color level green
148      * @param blue Color level blue
149      *
150      * @return The number of the color in the table
151      */

152     public Integer JavaDoc getColorNumber (int red, int green, int blue) {
153         Integer JavaDoc identifier = new Integer JavaDoc (determineIdentifier (red, green, blue));
154         Object JavaDoc o = colorIndex.get (identifier);
155         int retVal;
156
157         if (o == null) {
158             //The color currently does not exist, so add it to the table.
159
//First add it, then read the size as index (to return it).
160
//So the first added color gets index 1. That is OK, because
161
//index 0 is reserved for auto-colored.
162
addColor (identifier);
163
164             retVal = colorTable.size ();
165         } else {
166             //The color was found. Before returning the index, increment
167
//it by one. Because index 0 is reserved for auto-colored, but
168
//is not contained in colorTable.
169
retVal = ((Integer JavaDoc) o).intValue () + 1;
170         }
171
172         return new Integer JavaDoc(retVal);
173     }
174
175     /**
176      * Writes the color table in the header.
177      *
178      * @param header The header container to write in
179      *
180      * @throws IOException On error
181      */

182     public void writeColors (RtfHeader header) throws IOException JavaDoc {
183         if (colorTable == null || colorTable.size () == 0) {
184             return;
185         }
186
187         header.newLine();
188         header.writeGroupMark (true);
189         //Don't use writeControlWord, because it appends a blank,
190
//which may confuse Wordpad.
191
//This also implicitly writes the first color (=index 0), which
192
//is reserved for auto-colored.
193
header.write ("\\colortbl;");
194
195         int len = colorTable.size ();
196
197         for (int i = 0; i < len; i++) {
198             int identifier = ((Integer JavaDoc) colorTable.get (i)).intValue ();
199
200             header.newLine();
201             header.write ("\\red" + determineColorLevel (identifier, RED));
202             header.write ("\\green" + determineColorLevel (identifier, GREEN));
203             header.write ("\\blue" + determineColorLevel (identifier, BLUE) + ";");
204         }
205
206         header.newLine();
207         header.writeGroupMark (false);
208     }
209
210
211     //////////////////////////////////////////////////
212
// @@ Private methods
213
//////////////////////////////////////////////////
214

215     /**
216      * Adds a color to the table.
217      *
218      * @param i Identifier of color
219      */

220     private void addColor (Integer JavaDoc i) {
221         colorIndex.put (i, new Integer JavaDoc (colorTable.size ()));
222         colorTable.addElement (i);
223     }
224
225     /**
226      * Determines a identifier for the color.
227      *
228      * @param red Color level red
229      * @param green Color level green
230      * @param blue Color level blue
231      *
232      * @return Unique identifier of color
233      */

234     private int determineIdentifier (int red, int green, int blue) {
235         int c = red << RED;
236
237         c += green << GREEN;
238         c += blue << BLUE;
239
240         return c;
241     }
242
243     /**
244      * Determines the color level from the identifier.
245      *
246      * @param identifier Unique color identifier
247      * @param color One of the bit moving constants
248      *
249      * @return Color level in byte size
250      */

251     private int determineColorLevel (int identifier, int color) {
252         int retVal = (byte) (identifier >> color);
253
254         return retVal < 0 ? retVal + 256 : retVal;
255     }
256 }
257
Popular Tags