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.

RtfTableCell.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.io.Writer;
  59. import java.io.IOException;
  60. import java.util.Iterator;
  61. import org.apache.fop.render.rtf.rtflib.interfaces.ITableColumnsInfo;
  62. /** A cell in an RTF table, container for paragraphs, lists, etc.
  63. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  64. */
  65. public class RtfTableCell
  66. extends RtfContainer
  67. implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
  68. IRtfExternalGraphicContainer, IRtfTextrunContainer {
  69. private RtfParagraph paragraph;
  70. private RtfList list;
  71. private RtfTable table;
  72. private RtfExternalGraphic externalGraphic;
  73. private final RtfTableRow parentRow;
  74. private boolean setCenter;
  75. private boolean setRight;
  76. private int id;
  77. /** default cell width (in twips ??) */
  78. public static final int DEFAULT_CELL_WIDTH = 2000;
  79. /** cell width in twips */
  80. private int cellWidth;
  81. private int widthOffset;
  82. /** cell merging has three states */
  83. private int vMerge = NO_MERGE;
  84. private int hMerge = NO_MERGE;
  85. /** cell merging: this cell is not merged */
  86. public static final int NO_MERGE = 0;
  87. /** cell merging: this cell is the start of a range of merged cells */
  88. public static final int MERGE_START = 1;
  89. /** cell merging: this cell is part of (but not the start of) a range of merged cells */
  90. public static final int MERGE_WITH_PREVIOUS = 2;
  91. /** Create an RTF element as a child of given container */
  92. RtfTableCell(RtfTableRow parent, Writer w, int cellWidth, int idNum) throws IOException {
  93. super(parent, w);
  94. id = idNum;
  95. parentRow = parent;
  96. this.cellWidth = cellWidth;
  97. setCenter = setRight = false;
  98. }
  99. /** Create an RTF element as a child of given container */
  100. RtfTableCell(RtfTableRow parent, Writer w, int cellWidth, RtfAttributes attrs,
  101. int idNum) throws IOException {
  102. super(parent, w, attrs);
  103. id = idNum;
  104. parentRow = parent;
  105. this.cellWidth = cellWidth;
  106. /** Added by Boris Poudérous on 07/22/2002 in order to process
  107. * number-columns-spanned attribute */
  108. // If the cell is spanned horizontally
  109. if (attrs.getValue("number-columns-spanned") != null) {
  110. // Start horizontal merge
  111. this.setHMerge(MERGE_START);
  112. // Get the number of columns spanned
  113. int nbMergedCells = ((Integer)attrs.getValue("number-columns-spanned")).intValue();
  114. if (parent.parent instanceof RtfTable) {
  115. // Get the context of the current table in order to get the width of each column
  116. ITableColumnsInfo tableColumnsInfo =
  117. ((RtfTable)parent.parent).getITableColumnsInfo();
  118. tableColumnsInfo.selectFirstColumn();
  119. // Reach the column index in table context corresponding to the current column cell
  120. // id is the index of the current cell (it begins at 1)
  121. // getColumnIndex() is the index of the current column in table context (it begins at 0)
  122. // => so we must widthdraw 1 when comparing these two variables.
  123. while ((this.id - 1) != tableColumnsInfo.getColumnIndex()) {
  124. tableColumnsInfo.selectNextColumn();
  125. }
  126. // We widthdraw one cell because the first cell is already created
  127. // (it's the current cell) !
  128. int i = nbMergedCells - 1;
  129. while (i > 0) {
  130. tableColumnsInfo.selectNextColumn();
  131. // Added by Normand Masse
  132. // Pass in the current cell's attributes so the 'merged' cell has the
  133. // same display attributes.
  134. parent.newTableCellMergedHorizontally((int)tableColumnsInfo.getColumnWidth(),
  135. attrs);
  136. i--;
  137. }
  138. }
  139. }
  140. /** - end - */
  141. }
  142. /**
  143. * Start a new paragraph after closing current current paragraph, list and table
  144. * @param attrs attributes of new RtfParagraph
  145. * @return new RtfParagraph object
  146. * @throws IOException for I/O problems
  147. */
  148. public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException {
  149. closeAll();
  150. // in tables, RtfParagraph must have the intbl attribute
  151. if (attrs == null) {
  152. attrs = new RtfAttributes();
  153. }
  154. attrs.set("intbl");
  155. paragraph = new RtfParagraph(this, writer, attrs);
  156. if (paragraph.attrib.isSet("qc")) {
  157. setCenter = true;
  158. attrs.set("qc");
  159. } else if (paragraph.attrib.isSet("qr")) {
  160. setRight = true;
  161. attrs.set("qr");
  162. } else {
  163. attrs.set("ql");
  164. }
  165. attrs.set("intbl");
  166. //lines modified by Chris Scott, Westinghouse
  167. return paragraph;
  168. }
  169. /**
  170. * Start a new external graphic after closing current paragraph, list and table
  171. * @throws IOException for I/O problems
  172. * @return new RtfExternalGraphic object
  173. */
  174. public RtfExternalGraphic newImage() throws IOException {
  175. closeAll();
  176. externalGraphic = new RtfExternalGraphic(this, writer);
  177. return externalGraphic;
  178. }
  179. /**
  180. * Start a new paragraph with default attributes after closing current
  181. * paragraph, list and table
  182. * @return new RtfParagraph object
  183. * @throws IOException for I/O problems
  184. */
  185. public RtfParagraph newParagraph() throws IOException {
  186. return newParagraph(null);
  187. }
  188. /**
  189. * Start a new list after closing current paragraph, list and table
  190. * @param attrib attributes for new RtfList
  191. * @return new RtfList object
  192. * @throws IOException for I/O problems
  193. */
  194. public RtfList newList(RtfAttributes attrib) throws IOException {
  195. closeAll();
  196. list = new RtfList(this, writer, attrib);
  197. return list;
  198. }
  199. /**
  200. * Start a new nested table after closing current paragraph, list and table
  201. * @param tc table column info for new RtfTable
  202. * @return new RtfTable object
  203. * @throws IOException for I/O problems
  204. */
  205. public RtfTable newTable(ITableColumnsInfo tc) throws IOException {
  206. closeAll();
  207. table = new RtfTable(this, writer, tc);
  208. return table;
  209. }
  210. /**
  211. * Start a new nested table after closing current paragraph, list and table
  212. * @param attrs attributes of new RtfTable
  213. * @param tc table column info for new RtfTable
  214. * @return new RtfTable object
  215. * @throws IOException for I/O problems
  216. */
  217. // Modified by Boris Poudérous on 07/22/2002
  218. public RtfTable newTable(RtfAttributes attrs, ITableColumnsInfo tc) throws IOException {
  219. closeAll();
  220. table = new RtfTable(this, writer, attrs, tc); // Added tc Boris Poudérous 07/22/2002
  221. return table;
  222. }
  223. /** used by RtfTableRow to write the <celldef> cell definition control words
  224. * @param widthOffset sum of the widths of preceeding cells in same row
  225. * @return widthOffset + width of this cell
  226. */
  227. int writeCellDef(int widthOffset) throws IOException {
  228. this.widthOffset = widthOffset;
  229. // vertical cell merge codes
  230. if (vMerge == MERGE_START) {
  231. writeControlWord("clvmgf");
  232. } else if (vMerge == MERGE_WITH_PREVIOUS) {
  233. writeControlWord("clvmrg");
  234. }
  235. // horizontal cell merge codes
  236. if (hMerge == MERGE_START) {
  237. writeControlWord("clmgf");
  238. } else if (hMerge == MERGE_WITH_PREVIOUS) {
  239. writeControlWord("clmrg");
  240. }
  241. /**
  242. * Added by Boris POUDEROUS on 2002/06/26
  243. */
  244. // Cell background color processing :
  245. writeAttributes (attrib, ITableAttributes.CELL_COLOR);
  246. /** - end - */
  247. writeAttributes (attrib, ITableAttributes.ATTRIB_CELL_PADDING);
  248. writeAttributes (attrib, ITableAttributes.CELL_BORDER);
  249. writeAttributes (attrib, BorderAttributesConverter.BORDERS);
  250. // cell width
  251. final int xPos = widthOffset + this.cellWidth;
  252. //these lines added by Chris Scott, Westinghouse
  253. //some attributes need to be writting before opening block
  254. if (setCenter) {
  255. writeControlWord("qc");
  256. } else if (setRight) {
  257. writeControlWord("qr");
  258. } else {
  259. writeControlWord("ql");
  260. }
  261. writeControlWord("cellx" + xPos);
  262. writeControlWord("ql");
  263. return xPos;
  264. }
  265. /**
  266. * The "cell" control word marks the end of a cell
  267. * @throws IOException for I/O problems
  268. */
  269. protected void writeRtfSuffix() throws IOException {
  270. // word97 hangs if cell does not contain at least one "par" control word
  271. // TODO this is what causes the extra spaces in nested table of test
  272. // 004-spacing-in-tables.fo,
  273. // but if is not here we generate invalid RTF for word97
  274. if (setCenter) {
  275. writeControlWord("qc");
  276. } else if (setRight) {
  277. writeControlWord("qr");
  278. } else {
  279. writeControlWord("ql");
  280. }
  281. if (!containsText()) {
  282. writeControlWord("intbl");
  283. //R.Marra this create useless paragraph
  284. //Seem working into Word97 with the "intbl" only
  285. // writeControlWord("par");
  286. }
  287. writeControlWord("cell");
  288. }
  289. //modified by Chris Scott, Westinghouse
  290. private void closeCurrentParagraph() throws IOException {
  291. if (paragraph != null) {
  292. paragraph.close();
  293. }
  294. }
  295. private void closeCurrentList() throws IOException {
  296. if (list != null) {
  297. list.close();
  298. }
  299. }
  300. private void closeCurrentTable() throws IOException {
  301. if (table != null) {
  302. table.close();
  303. }
  304. }
  305. private void closeCurrentExternalGraphic() throws IOException {
  306. if (externalGraphic != null) {
  307. externalGraphic.close();
  308. }
  309. }
  310. private void closeAll()
  311. throws IOException {
  312. closeCurrentTable();
  313. closeCurrentParagraph();
  314. closeCurrentList();
  315. closeCurrentExternalGraphic();
  316. }
  317. /**
  318. * @param mergeStatus vertical cell merging status to set
  319. */
  320. public void setVMerge(int mergeStatus) { this.vMerge = mergeStatus; }
  321. /**
  322. * @return vertical cell merging status
  323. */
  324. public int getVMerge() { return this.vMerge; }
  325. /**
  326. * Set horizontal cell merging status
  327. * @param mergeStatus mergeStatus to set
  328. */
  329. public void setHMerge(int mergeStatus) {
  330. this.hMerge = mergeStatus;
  331. }
  332. /**
  333. * @return horizontal cell merging status
  334. */
  335. public int getHMerge() {
  336. return this.hMerge;
  337. }
  338. /** get cell width */
  339. int getCellWidth() { return this.cellWidth; }
  340. /**
  341. * Overridden so that nested tables cause extra rows to be added after the row
  342. * that contains this cell
  343. * disabled for V0.3 - nested table support is not done yet
  344. * @throws IOException for I/O problems
  345. */
  346. protected void writeRtfContent()
  347. throws IOException {
  348. int extraRowIndex = 0;
  349. RtfTableCell extraCell = null;
  350. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  351. final RtfElement e = (RtfElement)it.next();
  352. if (e instanceof RtfTable) {
  353. // nested table - render its cells in supplementary rows after current row,
  354. // and put the remaining content of this cell in a new cell after nested table
  355. // Line added by Boris Poudérous
  356. parentRow.getExtraRowSet().setParentITableColumnsInfo(
  357. ((RtfTable)this.getParentOfClass(e.getClass())).getITableColumnsInfo());
  358. extraRowIndex = parentRow.getExtraRowSet().addTable((RtfTable)e,
  359. extraRowIndex, widthOffset);
  360. // Boris Poudérous added the passing of the current cell
  361. // attributes to the new cells (in order not to have cell without
  362. // border for example)
  363. extraCell = parentRow.getExtraRowSet().createExtraCell(extraRowIndex,
  364. widthOffset, this.getCellWidth(), attrib);
  365. extraRowIndex++;
  366. } else if (extraCell != null) {
  367. // we are after a nested table, add elements to the extra cell created for them
  368. extraCell.addChild(e);
  369. } else {
  370. // before a nested table, normal rendering
  371. e.writeRtf();
  372. }
  373. }
  374. }
  375. /**
  376. * A table cell always contains "useful" content, as it is here to take some
  377. * space in a row.
  378. * Use containsText() to find out if there is really some useful content in the cell.
  379. * TODO: containsText could use the original isEmpty implementation?
  380. * @return false (always)
  381. */
  382. public boolean isEmpty() {
  383. return false;
  384. }
  385. /** true if the "par" control word must be written for given RtfParagraph
  386. * (which is not the case for the last non-empty paragraph of the cell)
  387. */
  388. boolean paragraphNeedsPar(RtfParagraph p) {
  389. // true if there is at least one non-empty paragraph after p in our children
  390. boolean pFound = false;
  391. boolean result = false;
  392. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  393. final Object o = it.next();
  394. if (!pFound) {
  395. // set pFound when p is found in the list
  396. pFound = (o == p);
  397. } else {
  398. if (o instanceof RtfParagraph) {
  399. final RtfParagraph p2 = (RtfParagraph)o;
  400. if (!p2.isEmpty()) {
  401. // found a non-empty paragraph after p
  402. result = true;
  403. break;
  404. }
  405. } else if (o instanceof RtfTable) {
  406. break;
  407. }
  408. }
  409. }
  410. return result;
  411. }
  412. public RtfTextrun getTextrun()
  413. throws IOException {
  414. RtfAttributes attrs = new RtfAttributes();
  415. attrs.set("intbl");
  416. return RtfTextrun.getTextrun(this, writer, attrs);
  417. }
  418. }