You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfColorTable.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. /*
  52. * This file is part of the RTF library of the FOP project, which was originally
  53. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  54. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  55. * the FOP project.
  56. */
  57. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  58. import java.util.Vector;
  59. import java.util.Hashtable;
  60. import java.io.IOException;
  61. /**
  62. * Singelton of the RTF color table.
  63. * This class was created for <fo:basic-link> tag processing.
  64. * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
  65. */
  66. public class RtfColorTable {
  67. //////////////////////////////////////////////////
  68. // @@ Symbolic constants
  69. //////////////////////////////////////////////////
  70. // Defines the bit moving for the colors
  71. private static final int RED = 16;
  72. private static final int GREEN = 8;
  73. private static final int BLUE = 0;
  74. //////////////////////////////////////////////////
  75. // @@ Members
  76. //////////////////////////////////////////////////
  77. /** Singelton instance */
  78. private static RtfColorTable instance = null;
  79. /** Index table for the colors */
  80. private Hashtable colorIndex = null;
  81. /** Used colors to this vector */
  82. private Vector colorTable = null;
  83. /** Map of color names to color numbers */
  84. private Hashtable namedColors = null;
  85. //////////////////////////////////////////////////
  86. // @@ Construction
  87. //////////////////////////////////////////////////
  88. /**
  89. * Constructor.
  90. */
  91. private RtfColorTable () {
  92. colorTable = new Vector ();
  93. colorIndex = new Hashtable ();
  94. namedColors = new Hashtable ();
  95. init ();
  96. }
  97. /**
  98. * Singelton.
  99. *
  100. * @return The instance of RTFColorTable
  101. */
  102. public static RtfColorTable getInstance () {
  103. if (instance == null) {
  104. instance = new RtfColorTable ();
  105. }
  106. return instance;
  107. }
  108. //////////////////////////////////////////////////
  109. // @@ Initializing
  110. //////////////////////////////////////////////////
  111. /**
  112. * Initialize the color table.
  113. */
  114. private void init () {
  115. addNamedColor("black", getColorNumber (0, 0, 0).intValue());
  116. addNamedColor("white", getColorNumber (255, 255, 255).intValue());
  117. addNamedColor("red", getColorNumber (255, 0, 0).intValue());
  118. addNamedColor("green", getColorNumber (0, 255, 0).intValue());
  119. addNamedColor("blue", getColorNumber (0, 0, 255).intValue());
  120. addNamedColor("cyan", getColorNumber (0, 255, 255).intValue());
  121. addNamedColor("magenta", getColorNumber (255, 0, 255).intValue());
  122. addNamedColor("yellow", getColorNumber (255, 255, 0).intValue());
  123. getColorNumber (0, 0, 128);
  124. getColorNumber (0, 128, 128);
  125. getColorNumber (0, 128, 0);
  126. getColorNumber (128, 0, 128);
  127. getColorNumber (128, 0, 0);
  128. getColorNumber (128, 128, 0);
  129. getColorNumber (128, 128, 128);
  130. // Added by Normand Masse
  131. // Gray color added
  132. addNamedColor("gray", getColorNumber(128, 128, 128).intValue());
  133. getColorNumber (192, 192, 192);
  134. }
  135. /** define a named color for getColorNumber(String) */
  136. private void addNamedColor(String name, int colorNumber) {
  137. namedColors.put(name.toLowerCase(), new Integer(colorNumber));
  138. }
  139. //////////////////////////////////////////////////
  140. // @@ Public methods
  141. //////////////////////////////////////////////////
  142. /**
  143. * @param name a named color
  144. * @return the RTF number of a named color, or null if name not found
  145. */
  146. public Integer getColorNumber (String name) {
  147. return ((Integer)namedColors.get(name.toLowerCase()));
  148. }
  149. /**
  150. * Gets the number of color in the color table
  151. *
  152. * @param red Color level red
  153. * @param green Color level green
  154. * @param blue Color level blue
  155. *
  156. * @return The number of the color in the table
  157. */
  158. public Integer getColorNumber (int red, int green, int blue) {
  159. Integer identifier = new Integer (determineIdentifier (red, green, blue));
  160. Object o = colorIndex.get (identifier);
  161. int retVal;
  162. if (o == null) {
  163. addColor (identifier);
  164. retVal = colorTable.size ();
  165. } else {
  166. retVal = ((Integer) o).intValue ();
  167. }
  168. return new Integer(retVal + 1);
  169. }
  170. /**
  171. * Writes the color table in the header.
  172. *
  173. * @param header The header container to write in
  174. *
  175. * @throws IOException On error
  176. */
  177. public void writeColors (RtfHeader header) throws IOException {
  178. if (colorTable == null || colorTable.size () == 0) {
  179. return;
  180. }
  181. header.writeGroupMark (true);
  182. header.writeControlWord ("colortbl;");
  183. int len = colorTable.size ();
  184. for (int i = 0; i < len; i++) {
  185. int identifier = ((Integer) colorTable.get (i)).intValue ();
  186. header.write ("\\red" + determineColorLevel (identifier, RED));
  187. header.write ("\\green" + determineColorLevel (identifier, GREEN));
  188. header.write ("\\blue" + determineColorLevel (identifier, BLUE) + ";");
  189. }
  190. header.writeGroupMark (false);
  191. }
  192. //////////////////////////////////////////////////
  193. // @@ Private methods
  194. //////////////////////////////////////////////////
  195. /**
  196. * Adds a color to the table.
  197. *
  198. * @param i Identifier of color
  199. */
  200. private void addColor (Integer i) {
  201. colorIndex.put (i, new Integer (colorTable.size () + 1));
  202. colorTable.addElement (i);
  203. }
  204. /**
  205. * Determines a identifier for the color.
  206. *
  207. * @param red Color level red
  208. * @param green Color level green
  209. * @param blue Color level blue
  210. *
  211. * @return Unique identifier of color
  212. */
  213. private int determineIdentifier (int red, int green, int blue) {
  214. int c = red << RED;
  215. c += green << GREEN;
  216. c += blue << BLUE;
  217. return c;
  218. }
  219. /**
  220. * Determines the color level from the identifier.
  221. *
  222. * @param identifier Unique color identifier
  223. * @param color One of the bit moving constants
  224. *
  225. * @return Color level in byte size
  226. */
  227. private int determineColorLevel (int identifier, int color) {
  228. int retVal = (byte) (identifier >> color);
  229. return retVal < 0 ? retVal + 256 : retVal;
  230. }
  231. }