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.

TableAttributesConverter.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. package org.apache.fop.render.rtf;
  52. //RTF
  53. import org.apache.fop.render.rtf.rtflib.rtfdoc.BorderAttributesConverter;
  54. //FOP
  55. import org.apache.avalon.framework.logger.Logger;
  56. import org.apache.avalon.framework.logger.ConsoleLogger;
  57. import org.apache.fop.apps.FOPException;
  58. import org.apache.fop.fo.EnumProperty;
  59. import org.apache.fop.fo.expr.NCnameProperty;
  60. import org.apache.fop.fo.properties.Constants;
  61. import org.apache.fop.fo.LengthProperty;
  62. import org.apache.fop.fo.ListProperty;
  63. import org.apache.fop.fo.PropertyList;
  64. import org.apache.fop.fo.Property;
  65. import org.apache.fop.fo.ColorTypeProperty;
  66. import org.apache.fop.fo.NumberProperty;
  67. import org.apache.fop.datatypes.ColorType;
  68. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
  69. import org.apache.fop.render.rtf.rtflib.rtfdoc.ITableAttributes;
  70. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfColorTable;
  71. /**
  72. * Contributor(s):
  73. * @author Roberto Marra <roberto@link-u.com>
  74. * @author Boris Poudérous <boris.pouderous@eads-telecom.com>
  75. * @author Normand Massé
  76. * @author Peter Herweg <pherweg@web.de>
  77. *
  78. * This class was originally developed for the JFOR project and
  79. * is now integrated into FOP.
  80. -----------------------------------------------------------------------------*/
  81. /**
  82. * Provides methods to convert the attributes to RtfAttributes.
  83. */
  84. public class TableAttributesConverter {
  85. private static Logger log = new ConsoleLogger();
  86. //////////////////////////////////////////////////
  87. // @@ Construction
  88. //////////////////////////////////////////////////
  89. /**
  90. * Constructor.
  91. */
  92. private TableAttributesConverter() {
  93. }
  94. //////////////////////////////////////////////////
  95. // @@ Static converter methods
  96. //////////////////////////////////////////////////
  97. /**
  98. * Converts cell attributes to rtf attributes.
  99. * @param attrs Given attributes
  100. * @param defaultAttributes Default rtf attributes
  101. *
  102. * @return All valid rtf attributes together
  103. *
  104. * @throws ConverterException On convertion error
  105. */
  106. static RtfAttributes convertCellAttributes(PropertyList props, PropertyList defProps)
  107. throws FOPException {
  108. Property p;
  109. EnumProperty ep;
  110. RtfColorTable colorTable = RtfColorTable.getInstance();
  111. RtfAttributes attrib = null;
  112. if (defProps != null) {
  113. attrib = convertCellAttributes(defProps, null);
  114. } else {
  115. attrib = new RtfAttributes();
  116. }
  117. boolean isBorderPresent = false;
  118. // Cell background color
  119. if ((p = props.getNearestSpecified("background-color")) != null) {
  120. ColorType color = p.getColorType();
  121. if (color != null) {
  122. if (color.getAlpha() != 0
  123. || color.getRed() != 0
  124. || color.getGreen() != 0
  125. || color.getBlue() != 0) {
  126. attrib.set(
  127. ITableAttributes.CELL_COLOR_BACKGROUND,
  128. TextAttributesConverter.convertFOPColorToRTF(color));
  129. }
  130. } else {
  131. log.warn("Named color '" + p.toString() + "' not found. ");
  132. }
  133. }
  134. // Cell borders :
  135. if ((p = props.getExplicit("border-color")) != null) {
  136. ListProperty listprop = (ListProperty)p;
  137. ColorType color = null;
  138. if (listprop.getList().get(0) instanceof NCnameProperty) {
  139. color = new ColorType(((NCnameProperty)listprop.getList().get(0)).getNCname());
  140. } else if (listprop.getList().get(0) instanceof ColorTypeProperty) {
  141. color = ((ColorTypeProperty)listprop.getList().get(0)).getColorType();
  142. }
  143. attrib.set(
  144. BorderAttributesConverter.BORDER_COLOR,
  145. colorTable.getColorNumber((int)color.getRed(), (int)color.getGreen(),
  146. (int)color.getBlue()).intValue());
  147. }
  148. if ((p = props.getExplicit("border-top-color")) != null) {
  149. ColorType color = p.getColorType();
  150. attrib.set(
  151. BorderAttributesConverter.BORDER_COLOR,
  152. colorTable.getColorNumber((int)color.getRed(), (int)color.getGreen(),
  153. (int)color.getBlue()).intValue());
  154. }
  155. if ((p = props.getExplicit("border-bottom-color")) != null) {
  156. ColorType color = p.getColorType();
  157. attrib.set(
  158. BorderAttributesConverter.BORDER_COLOR,
  159. colorTable.getColorNumber((int)color.getRed(), (int)color.getGreen(),
  160. (int)color.getBlue()).intValue());
  161. }
  162. if ((p = props.getExplicit("border-left-color")) != null) {
  163. ColorType color = p.getColorType();
  164. attrib.set(
  165. BorderAttributesConverter.BORDER_COLOR,
  166. colorTable.getColorNumber((int)color.getRed(), (int)color.getGreen(),
  167. (int)color.getBlue()).intValue());
  168. }
  169. if ((p = props.getExplicit("border-right-color")) != null) {
  170. ColorType color = p.getColorType();
  171. attrib.set(
  172. BorderAttributesConverter.BORDER_COLOR,
  173. colorTable.getColorNumber((int)color.getRed(), (int)color.getGreen(),
  174. (int)color.getBlue()).intValue());
  175. }
  176. // Border styles do not inherit from parent
  177. if ((p = props.get("border-style")) != null) {
  178. log.warn("border-style not implemented. Please use border-style-left, "
  179. + "...-right, ...-top or ...-bottom");
  180. /*
  181. attrib.set(ITableAttributes.CELL_BORDER_LEFT, "\\"+convertAttributetoRtf(e.getEnum()));
  182. attrib.set(ITableAttributes.CELL_BORDER_RIGHT, "\\"+convertAttributetoRtf(e.getEnum()));
  183. attrib.set(ITableAttributes.CELL_BORDER_BOTTOM,"\\"+convertAttributetoRtf(e.getEnum()));
  184. attrib.set(ITableAttributes.CELL_BORDER_TOP, "\\"+convertAttributetoRtf(e.getEnum()));
  185. isBorderPresent=true;
  186. */
  187. }
  188. ep = (EnumProperty)props.get("border-top-style");
  189. if (ep != null && ep.getEnum() != Constants.NONE) {
  190. attrib.set(ITableAttributes.CELL_BORDER_TOP, "\\"
  191. + convertAttributetoRtf(ep.getEnum()));
  192. isBorderPresent = true;
  193. }
  194. ep = (EnumProperty)props.get("border-bottom-style");
  195. if (ep != null && ep.getEnum() != Constants.NONE) {
  196. attrib.set(ITableAttributes.CELL_BORDER_BOTTOM, "\\"
  197. + convertAttributetoRtf(ep.getEnum()));
  198. isBorderPresent = true;
  199. }
  200. ep = (EnumProperty)props.get("border-left-style");
  201. if (ep != null && ep.getEnum() != Constants.NONE) {
  202. attrib.set(ITableAttributes.CELL_BORDER_LEFT, "\\"
  203. + convertAttributetoRtf(ep.getEnum()));
  204. isBorderPresent = true;
  205. }
  206. ep = (EnumProperty)props.get("border-right-style");
  207. if (ep != null && ep.getEnum() != Constants.NONE) {
  208. attrib.set(ITableAttributes.CELL_BORDER_RIGHT, "\\"
  209. + convertAttributetoRtf(ep.getEnum()));
  210. isBorderPresent = true;
  211. }
  212. if ((p = props.get("border-width")) != null) {
  213. ListProperty listprop = (ListProperty)p;
  214. LengthProperty lengthprop = (LengthProperty)listprop.getList().get(0);
  215. Float f = new Float(lengthprop.getLength().getValue() / 1000f);
  216. String sValue = f.toString() + "pt";
  217. attrib.set(BorderAttributesConverter.BORDER_WIDTH,
  218. (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
  219. } else if (isBorderPresent) {
  220. //if not defined, set default border width
  221. //note 20 twips = 1 point
  222. attrib.set(BorderAttributesConverter.BORDER_WIDTH,
  223. (int)FoUnitsConverter.getInstance().convertToTwips("1pt"));
  224. }
  225. // Column spanning :
  226. NumberProperty n = (NumberProperty)props.get("number-columns-spanned");
  227. if (n != null && n.getNumber().intValue() > 1) {
  228. attrib.set(ITableAttributes.COLUMN_SPAN, n.getNumber().intValue());
  229. }
  230. return attrib;
  231. }
  232. /**
  233. * Converts table and row attributes to rtf attributes.
  234. *
  235. * @param attrs Given attributes
  236. * @param defaultAttributes Default rtf attributes
  237. *
  238. * @return All valid rtf attributes together
  239. *
  240. * @throws ConverterException On convertion error
  241. */
  242. static RtfAttributes convertRowAttributes(PropertyList props,
  243. PropertyList defProps, RtfAttributes rtfatts)
  244. throws FOPException {
  245. Property p;
  246. EnumProperty ep;
  247. RtfColorTable colorTable = RtfColorTable.getInstance();
  248. RtfAttributes attrib = null;
  249. if (defProps != null) {
  250. attrib = convertRowAttributes(defProps, null, rtfatts);
  251. } else {
  252. if (rtfatts == null) {
  253. attrib = new RtfAttributes();
  254. } else {
  255. attrib = rtfatts;
  256. }
  257. }
  258. String attrValue;
  259. boolean isBorderPresent = false;
  260. //need to set a default width
  261. //check for keep-together row attribute
  262. if ((p = props.get("keep-together.within-page")) != null) {
  263. attrib.set(ITableAttributes.ROW_KEEP_TOGETHER);
  264. }
  265. if ((p = props.get("keep-together")) != null) {
  266. attrib.set(ITableAttributes.ROW_KEEP_TOGETHER);
  267. }
  268. //Check for keep-with-next row attribute.
  269. if ((p = props.get("keep-together")) != null) {
  270. attrib.set(ITableAttributes.ROW_KEEP_WITH_NEXT);
  271. }
  272. //Check for keep-with-previous row attribute.
  273. if ((p = props.get("keep-with-previous")) != null) {
  274. attrib.set(ITableAttributes.ROW_KEEP_WITH_PREVIOUS);
  275. }
  276. //Check for height row attribute.
  277. if ((p = props.get("height")) != null) {
  278. Float f = new Float(p.getLength().getValue() / 1000);
  279. attrValue = f.toString() + "pt";
  280. attrib.set(ITableAttributes.ROW_HEIGHT,
  281. (int)FoUnitsConverter.getInstance().convertToTwips(attrValue));
  282. }
  283. /* to write a border to a side of a cell one must write the directional
  284. * side (ie. left, right) and the inside value if one needs to be taken
  285. * out ie if the cell lies on the edge of a table or not, the offending
  286. * value will be taken out by RtfTableRow. This is because you can't
  287. * say BORDER_TOP and BORDER_HORIZONTAL if the cell lies at the top of
  288. * the table. Similarly using BORDER_BOTTOM and BORDER_HORIZONTAL will
  289. * not work if the cell lies at th bottom of the table. The same rules
  290. * apply for left right and vertical.
  291. * Also, the border type must be written after every control word. Thus
  292. * it is implemented that the border type is the value of the border
  293. * place.
  294. */
  295. if ((p = props.get("border-style")) != null) {
  296. log.warn("border-style not implemented. Please use border-style-left, "
  297. + "...-right, ...-top or ...-bottom");
  298. /*
  299. attrValue = new String(AbstractBuilder.getValue( attrs, "border-style", defAttrs ));
  300. attrib.set(ITableAttributes.ROW_BORDER_LEFT,"\\"
  301. + BorderAttributesConverter.convertAttributetoRtf(attrValue));
  302. attrib.set(ITableAttributes.ROW_BORDER_RIGHT,"\\"
  303. + BorderAttributesConverter.convertAttributetoRtf(attrValue));
  304. attrib.set(ITableAttributes.ROW_BORDER_HORIZONTAL,"\\"
  305. + BorderAttributesConverter.convertAttributetoRtf(attrValue));
  306. attrib.set(ITableAttributes.ROW_BORDER_VERTICAL,"\\"
  307. + BorderAttributesConverter.convertAttributetoRtf(attrValue));
  308. attrib.set(ITableAttributes.ROW_BORDER_BOTTOM,"\\"
  309. + BorderAttributesConverter.convertAttributetoRtf(attrValue));
  310. attrib.set(ITableAttributes.ROW_BORDER_TOP,"\\"
  311. + BorderAttributesConverter.convertAttributetoRtf(attrValue));
  312. isBorderPresent=true;
  313. */
  314. }
  315. ep = (EnumProperty)props.get("border-top-style");
  316. if (ep != null && ep.getEnum() != Constants.NONE) {
  317. attrib.set(ITableAttributes.ROW_BORDER_TOP, "\\"
  318. + convertAttributetoRtf(ep.getEnum()));
  319. attrib.set(ITableAttributes.ROW_BORDER_HORIZONTAL, "\\"
  320. + convertAttributetoRtf(ep.getEnum()));
  321. isBorderPresent = true;
  322. }
  323. ep = (EnumProperty)props.get("border-bottom-style");
  324. if (ep != null && ep.getEnum() != Constants.NONE) {
  325. attrib.set(ITableAttributes.ROW_BORDER_BOTTOM, "\\"
  326. + convertAttributetoRtf(ep.getEnum()));
  327. attrib.set(ITableAttributes.ROW_BORDER_HORIZONTAL, "\\"
  328. + convertAttributetoRtf(ep.getEnum()));
  329. isBorderPresent = true;
  330. }
  331. ep = (EnumProperty)props.get("border-left-style");
  332. if (ep != null && ep.getEnum() != Constants.NONE) {
  333. attrib.set(ITableAttributes.ROW_BORDER_LEFT, "\\"
  334. + convertAttributetoRtf(ep.getEnum()));
  335. attrib.set(ITableAttributes.ROW_BORDER_VERTICAL, "\\"
  336. + convertAttributetoRtf(ep.getEnum()));
  337. isBorderPresent = true;
  338. }
  339. ep = (EnumProperty)props.get("border-right-style");
  340. if (ep != null && ep.getEnum() != Constants.NONE) {
  341. attrib.set(ITableAttributes.ROW_BORDER_RIGHT, "\\"
  342. + convertAttributetoRtf(ep.getEnum()));
  343. attrib.set(ITableAttributes.ROW_BORDER_VERTICAL, "\\"
  344. + convertAttributetoRtf(ep.getEnum()));
  345. isBorderPresent = true;
  346. }
  347. ep = (EnumProperty)props.get("border-horizontal-style");
  348. if (ep != null && ep.getEnum() != Constants.NONE) {
  349. attrib.set(ITableAttributes.ROW_BORDER_HORIZONTAL, "\\"
  350. + convertAttributetoRtf(ep.getEnum()));
  351. attrib.set(ITableAttributes.ROW_BORDER_TOP, "\\"
  352. + convertAttributetoRtf(ep.getEnum()));
  353. attrib.set(ITableAttributes.ROW_BORDER_BOTTOM, "\\"
  354. + convertAttributetoRtf(ep.getEnum()));
  355. isBorderPresent = true;
  356. }
  357. ep = (EnumProperty)props.get("border-vertical-style");
  358. if (ep != null && ep.getEnum() != Constants.NONE) {
  359. attrib.set(ITableAttributes.ROW_BORDER_VERTICAL, "\\"
  360. + convertAttributetoRtf(ep.getEnum()));
  361. attrib.set(ITableAttributes.ROW_BORDER_LEFT, "\\"
  362. + convertAttributetoRtf(ep.getEnum()));
  363. attrib.set(ITableAttributes.ROW_BORDER_RIGHT, "\\"
  364. + convertAttributetoRtf(ep.getEnum()));
  365. isBorderPresent = true;
  366. }
  367. if ((p = props.get("border-width")) != null) {
  368. ListProperty listprop = (ListProperty)p;
  369. LengthProperty lengthprop = (LengthProperty)listprop.getList().get(0);
  370. Float f = new Float(lengthprop.getLength().getValue() / 1000f);
  371. String sValue = f.toString() + "pt";
  372. attrib.set(BorderAttributesConverter.BORDER_WIDTH,
  373. (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
  374. } else if (isBorderPresent) {
  375. //if not defined, set default border width
  376. //note 20 twips = 1 point
  377. attrib.set(BorderAttributesConverter.BORDER_WIDTH,
  378. (int)FoUnitsConverter.getInstance().convertToTwips("1pt"));
  379. }
  380. return attrib;
  381. }
  382. /**
  383. *
  384. * @param iBorderStyle the border style to be converted
  385. * @return String with the converted border style
  386. */
  387. public static String convertAttributetoRtf(int iBorderStyle) {
  388. // Added by Normand Masse
  389. // "solid" is interpreted like "thin"
  390. if (iBorderStyle == Constants.SOLID) {
  391. return BorderAttributesConverter.BORDER_SINGLE_THICKNESS;
  392. /* } else if (iBorderStyle==Constants.THIN) {
  393. return BorderAttributesConverter.BORDER_SINGLE_THICKNESS;
  394. } else if (iBorderStyle==Constants.THICK) {
  395. return BorderAttributesConverter.BORDER_DOUBLE_THICKNESS;
  396. } else if (iBorderStyle==Constants. value.equals("shadowed")) {
  397. return BorderAttributesConverter.BORDER_SHADOWED;*/
  398. } else if (iBorderStyle == Constants.DOUBLE) {
  399. return BorderAttributesConverter.BORDER_DOUBLE;
  400. } else if (iBorderStyle == Constants.DOTTED) {
  401. return BorderAttributesConverter.BORDER_DOTTED;
  402. } else if (iBorderStyle == Constants.DASHED) {
  403. return BorderAttributesConverter.BORDER_DASH;
  404. /* } else if (iBorderStyle==Constants value.equals("hairline")) {
  405. return BorderAttributesConverter.BORDER_HAIRLINE;*/
  406. /* } else if (iBorderStyle==Constant value.equals("dot-dash")) {
  407. return BorderAttributesConverter.BORDER_DOT_DASH;
  408. } else if (iBorderStyle==Constant value.equals("dot-dot-dash")) {
  409. return BorderAttributesConverter.BORDER_DOT_DOT_DASH;
  410. } else if (iBorderStyle==Constant value.equals("triple")) {
  411. return BorderAttributesConverter.BORDER_TRIPLE;
  412. } else if (iBorderStyle==Constant value.equals("wavy")) {
  413. return BorderAttributesConverter.BORDER_WAVY;
  414. } else if (iBorderStyle==Constant value.equals("wavy-double")) {
  415. return BorderAttributesConverter.BORDER_WAVY_DOUBLE;
  416. } else if (iBorderStyle==Constant value.equals("striped")) {
  417. return BorderAttributesConverter.BORDER_STRIPED;
  418. } else if (iBorderStyle==Constant value.equals("emboss")) {
  419. return BorderAttributesConverter.BORDER_EMBOSS;
  420. } else if (iBorderStyle==Constant value.equals("engrave")) {
  421. return BorderAttributesConverter.BORDER_ENGRAVE;*/
  422. } else {
  423. return null;
  424. }
  425. }
  426. }