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.

TextAttributesConverter.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * ============================================================================
  3. * The Apache Software License, Version 1.1
  4. * ============================================================================
  5. *
  6. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modifica-
  9. * tion, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. *
  18. * 3. The end-user documentation included with the redistribution, if any, must
  19. * include the following acknowledgment: "This product includes software
  20. * developed by the Apache Software Foundation (http://www.apache.org/)."
  21. * Alternately, this acknowledgment may appear in the software itself, if
  22. * and wherever such third-party acknowledgments normally appear.
  23. *
  24. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  25. * endorse or promote products derived from this software without prior
  26. * written permission. For written permission, please contact
  27. * apache@apache.org.
  28. *
  29. * 5. Products derived from this software may not be called "Apache", nor may
  30. * "Apache" appear in their name, without prior written permission of the
  31. * Apache Software Foundation.
  32. *
  33. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  35. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  38. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  39. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  40. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  42. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. * ============================================================================
  44. *
  45. * This software consists of voluntary contributions made by many individuals
  46. * on behalf of the Apache Software Foundation and was originally created by
  47. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  48. * Software Foundation, please see <http://www.apache.org/>.
  49. */
  50. package org.apache.fop.render.rtf;
  51. //XML
  52. import org.xml.sax.Attributes;
  53. //FOP
  54. import org.apache.avalon.framework.logger.Logger;
  55. import org.apache.avalon.framework.logger.ConsoleLogger;
  56. import org.apache.fop.apps.FOPException;
  57. import org.apache.fop.fo.EnumProperty;
  58. import org.apache.fop.fo.expr.NCnameProperty;
  59. import org.apache.fop.fo.properties.Constants;
  60. import org.apache.fop.fo.LengthProperty;
  61. import org.apache.fop.fo.ListProperty;
  62. import org.apache.fop.fo.PropertyList;
  63. import org.apache.fop.fo.Property;
  64. import org.apache.fop.fo.SpaceProperty;
  65. import org.apache.fop.fo.ColorTypeProperty;
  66. import org.apache.fop.fo.NumberProperty;
  67. import org.apache.fop.datatypes.ColorType;
  68. //RTF
  69. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
  70. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfColorTable;
  71. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfFontManager;
  72. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfText;
  73. /** Converts FO properties to RtfAttributes
  74. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  75. * @author Andreas Putz a.putz@skynamics.com
  76. * @author Boris Poudérous boris.pouderous@eads-telecom.com
  77. * @author Peter Herweg, pherweg@web.de
  78. * @author Normand Massé
  79. * @author Chris Scott
  80. * @author rmarra
  81. */
  82. class TextAttributesConverter {
  83. private static Logger log = new ConsoleLogger();
  84. /**
  85. * Converts all known text FO properties to RtfAttributes
  86. * @param props list of FO properites, which are to be converted
  87. * @param props list of default FO properites (usally null)
  88. */
  89. public static RtfAttributes convertAttributes(PropertyList props, PropertyList defProps)
  90. throws FOPException {
  91. RtfAttributes attrib = null;
  92. if (defProps != null) {
  93. attrib = convertAttributes(defProps, null);
  94. } else {
  95. attrib = new RtfAttributes();
  96. }
  97. attrBlockFontFamily(props, attrib);
  98. attrBlockFontWeight(props, attrib);
  99. attrBlockFontSize(props, attrib);
  100. attrBlockFontColor(props, attrib);
  101. attrBlockFontItalic(props, attrib);
  102. attrBlockFontUnderline(props, attrib);
  103. attrBlockBackgroundColor(props, attrib);
  104. attrBlockSpaceBeforeAfter(props, attrib);
  105. attrBlockMargins(props, attrib);
  106. attrBlockTextAlign(props, attrib);
  107. return attrib;
  108. }
  109. /**
  110. * Converts all character related FO properties to RtfAttributes.
  111. * @param props list of FO properites, which are to be converted
  112. * @param props list of default FO properites (usally null)
  113. */
  114. public static RtfAttributes convertCharacterAttributes(PropertyList props, PropertyList defProps)
  115. throws FOPException {
  116. RtfAttributes attrib = null;
  117. if (defProps != null) {
  118. attrib = convertCharacterAttributes(defProps, null);
  119. } else {
  120. attrib = new RtfAttributes();
  121. }
  122. attrBlockFontFamily(props, attrib);
  123. attrBlockFontWeight(props, attrib);
  124. attrBlockFontSize(props, attrib);
  125. attrBlockFontColor(props, attrib);
  126. attrBlockFontItalic(props, attrib);
  127. attrBlockFontUnderline(props, attrib);
  128. attrBlockBackgroundColor(props, attrib);
  129. return attrib;
  130. }
  131. private static void attrBlockFontFamily(PropertyList properties, RtfAttributes rtfAttr) {
  132. String fopValue = properties.get("font-family").getString();
  133. if (fopValue != null) {
  134. rtfAttr.set(RtfText.ATTR_FONT_FAMILY, RtfFontManager.getInstance().getFontNumber(fopValue));
  135. }
  136. }
  137. private static void attrBlockFontSize(PropertyList properties, RtfAttributes rtfAttr) {
  138. int fopValue = properties.get("font-size").getLength().getValue() / 500;
  139. rtfAttr.set("fs", fopValue);
  140. }
  141. private static void attrBlockFontColor(PropertyList properties, RtfAttributes rtfAttr) {
  142. // Cell background color
  143. ColorTypeProperty colorTypeProp=(ColorTypeProperty)properties.get("color");
  144. if(colorTypeProp != null) {
  145. ColorType colorType = colorTypeProp.getColorType();
  146. if (colorType != null) {
  147. if (colorType.getAlpha() != 0
  148. || colorType.getRed() != 0
  149. || colorType.getGreen() != 0
  150. || colorType.getBlue() != 0) {
  151. rtfAttr.set(
  152. RtfText.ATTR_FONT_COLOR,
  153. convertFOPColorToRTF(colorType));
  154. }
  155. } else {
  156. log.warn("Named color '" + colorTypeProp.toString() + "' not found. ");
  157. }
  158. }
  159. }
  160. private static void attrBlockFontWeight(PropertyList properties, RtfAttributes rtfAttr) {
  161. String fopValue = properties.get("font-weight").getString();
  162. if (fopValue == "bold" || fopValue == "700") {
  163. rtfAttr.set("b", 1);
  164. } else {
  165. rtfAttr.set("b", 0);
  166. }
  167. }
  168. private static void attrBlockFontItalic(PropertyList properties, RtfAttributes rtfAttr) {
  169. String fopValue = properties.get("font-style").getString();
  170. if(fopValue.equals("italic")) {
  171. rtfAttr.set(RtfText.ATTR_ITALIC, 1);
  172. } else {
  173. rtfAttr.set(RtfText.ATTR_ITALIC, 0);
  174. }
  175. }
  176. private static void attrBlockFontUnderline(PropertyList properties, RtfAttributes rtfAttr) {
  177. EnumProperty enumProp=(EnumProperty)properties.get("text-decoration");
  178. if(enumProp.getEnum()==Constants.UNDERLINE) {
  179. rtfAttr.set(RtfText.ATTR_UNDERLINE, 1);
  180. } else {
  181. rtfAttr.set(RtfText.ATTR_UNDERLINE, 0);
  182. }
  183. }
  184. private static void attrBlockSpaceBeforeAfter(PropertyList properties, RtfAttributes rtfAttr) {
  185. SpaceProperty spaceProp=null;
  186. //space-before
  187. spaceProp=(SpaceProperty)properties.get("space-before");
  188. if(spaceProp!=null) {
  189. Float f = new Float(spaceProp.getLengthRange().getOptimum().getLength().getValue() / 1000f);
  190. String sValue = f.toString() + "pt";
  191. try {
  192. rtfAttr.set(
  193. RtfText.SPACE_BEFORE,
  194. (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
  195. } catch(FOPException fe) {
  196. log.warn("attrBlockSpaceBeforeAfter: " + fe.getMessage());
  197. }
  198. }
  199. //space-after
  200. spaceProp=(SpaceProperty)properties.get("space-after");
  201. if(spaceProp!=null) {
  202. Float f = new Float(spaceProp.getLengthRange().getOptimum().getLength().getValue() / 1000f);
  203. String sValue = f.toString() + "pt";
  204. try {
  205. rtfAttr.set(
  206. RtfText.SPACE_AFTER,
  207. (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
  208. } catch(FOPException fe) {
  209. log.warn("attrBlockSpaceBeforeAfter: " + fe.getMessage());
  210. }
  211. }
  212. }
  213. private static void attrBlockMargins(PropertyList properties, RtfAttributes rtfAttr) {
  214. try {
  215. LengthProperty lengthProp=null;
  216. // margin-left
  217. lengthProp=(LengthProperty)properties.get("margin-left");
  218. if (lengthProp != null) {
  219. Float f = new Float(lengthProp.getLength().getValue() / 1000f);
  220. String sValue = f.toString() + "pt";
  221. rtfAttr.set(
  222. RtfText.LEFT_INDENT_BODY,
  223. (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
  224. } else {
  225. rtfAttr.set(RtfText.LEFT_INDENT_BODY, 0);
  226. }
  227. // margin-right
  228. lengthProp=(LengthProperty)properties.get("margin-right");
  229. if (lengthProp != null) {
  230. Float f = new Float(lengthProp.getLength().getValue() / 1000f);
  231. String sValue = f.toString() + "pt";
  232. rtfAttr.set(
  233. RtfText.LEFT_INDENT_BODY,
  234. (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
  235. } else {
  236. rtfAttr.set(RtfText.RIGHT_INDENT_BODY, 0);
  237. }
  238. } catch(FOPException fe) {
  239. log.warn("attrBlockSpaceBeforeAfter: " + fe.getMessage());
  240. }
  241. }
  242. private static void attrBlockTextAlign(PropertyList properties, RtfAttributes rtfAttr) {
  243. int fopValue = properties.get("text-align").getEnum();
  244. String rtfValue = null;
  245. switch (fopValue) {
  246. case Constants.CENTER: {
  247. rtfValue = RtfText.ALIGN_CENTER;
  248. break;
  249. }
  250. case Constants.END: {
  251. rtfValue = RtfText.ALIGN_RIGHT;
  252. break;
  253. }
  254. case Constants.JUSTIFY: {
  255. rtfValue = RtfText.ALIGN_JUSTIFIED;
  256. break;
  257. }
  258. default: {
  259. rtfValue = RtfText.ALIGN_LEFT;
  260. break;
  261. }
  262. }
  263. rtfAttr.set(rtfValue);
  264. }
  265. /**
  266. * Reads background-color from bl and writes it to rtfAttr.
  267. *
  268. * @param bl the Block object the properties are read from
  269. * @param rtfAttr the RtfAttributes object the attributes are written to
  270. */
  271. private static void attrBlockBackgroundColor(PropertyList properties, RtfAttributes rtfAttr) {
  272. ColorType fopValue = properties.get("background-color").getColorType();
  273. int rtfColor = 0;
  274. /* FOP uses a default background color of "transparent", which is
  275. actually a transparent black, which is generally not suitable as a
  276. default here. Changing FOP's default to "white" causes problems in
  277. PDF output, so we will look for the default here & change it to
  278. "auto". */
  279. if ((fopValue.getRed() == 0)
  280. && (fopValue.getGreen() == 0)
  281. && (fopValue.getBlue() == 0)
  282. && (fopValue.getAlpha() == 0)) {
  283. rtfColor = 0; //=auto
  284. } else {
  285. rtfColor = convertFOPColorToRTF(fopValue);
  286. }
  287. rtfAttr.set(RtfText.ATTR_BACKGROUND_COLOR, rtfColor);
  288. }
  289. /**
  290. * Converts a FOP ColorType to the integer pointing into the RTF color table
  291. * @param fopColor the ColorType object to be converted
  292. * @return integer pointing into the RTF color table
  293. */
  294. public static int convertFOPColorToRTF(ColorType fopColor) {
  295. int redComponent = ColorType.convertChannelToInteger (fopColor.getRed());
  296. int greenComponent = ColorType.convertChannelToInteger (fopColor.getGreen());
  297. int blueComponent = ColorType.convertChannelToInteger (fopColor.getBlue());
  298. return RtfColorTable.getInstance().getColorNumber(redComponent,
  299. greenComponent, blueComponent).intValue();
  300. }
  301. }