/************************************************************************ * * TableFormatter.java * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * Copyright: 2002-2009 by Henrik Just * * All Rights Reserved. * * Version 1.0 (2009-02-19) * */ package writer2latex.latex; import org.w3c.dom.Element; import org.w3c.dom.Node; import writer2latex.util.*; import writer2latex.office.*; import writer2latex.latex.util.BeforeAfter; import writer2latex.latex.util.Context; /** *
This class converts OOo table styles to LaTeX.
*In OOo the table style is distributed on table, column and cell styles. *
In LaTeX we have to rearrange this information slightly, so this class * takes care of that.
*/ public class TableFormatter extends ConverterHelper { //private boolean bApplyCellFormat; private TableReader table; private char[][] cAlign; private char[] cGlobalAlign; private boolean[][] bHBorder; private boolean[][] bVBorder; private boolean[] bGlobalVBorder; private String[] sRowColor; private String[][] sCellColor; private String[] sColumnWidth; private boolean bIsLongtable; private boolean bIsSupertabular; private boolean bIsTabulary; private boolean bIsColortbl; private boolean bIsSimple; /**Constructor: Create from a TableReader.
*/ public TableFormatter(OfficeReader ofr, LaTeXConfig config, ConverterPalette palette, TableReader table, boolean bAllowPageBreak, boolean bIsInTable) { super(ofr,config,palette); this.table = table; //bApplyCellFormat = config.formatting()>=LaTeXConfig.CONVERT_MOST; int nRowCount = table.getRowCount(); int nColCount = table.getColCount(); int nSimpleTableLimit = config.getSimpleTableLimit(); // Step 1: Collect alignment and identify simple tables bIsSimple = true; cAlign = new char[nRowCount][nColCount]; cGlobalAlign = new char[nColCount]; // Keep track of characters to be counted int[] nPendingChars = new int[nRowCount]; int[] nPendingColSpan = new int[nRowCount]; int nTableWidth = 0; for (int nCol=0; nColReturns eg. "\begin{longtable}{m{2cm}|m{4cm}}", "\end{longtable}".
*/ public void applyTableStyle(BeforeAfter ba, BeforeAfter baAlign) { // Read formatting info from table style // Only supported properties are alignment and may-break-between-rows. String sStyleName = table.getTableStyleName(); StyleWithProperties style = ofr.getTableStyle(sStyleName); char cAlign = 'c'; if (style!=null && !table.isSubTable()) { String s = style.getProperty(XMLString.TABLE_ALIGN); if ("left".equals(s)) { cAlign='l'; } else if ("right".equals(s)) { cAlign='r'; } } String sAlign="center"; switch (cAlign) { case 'c': sAlign="center"; break; case 'r': sAlign="flushright"; break; case 'l': sAlign="flushleft"; } // Create table alignment (for supertabular, tabular and tabulary) if (!bIsLongtable && !table.isSubTable()) { baAlign.add("\\begin{"+sAlign+"}\n","\\end{"+sAlign+"}\n"); } // Create table declaration if (bIsLongtable) { ba.add("\\begin{longtable}["+cAlign+"]", "\\end{longtable}"); } else if (bIsSupertabular) { ba.add("\\begin{supertabular}","\\end{supertabular}"); } else if (bIsTabulary) { ba.add("\\begin{tabulary}{"+table.getTableWidth()+"}","\\end{tabulary}"); } else if (!table.isSubTable()) { ba.add("\\begin{tabular}","\\end{tabular}"); } else { // subtables should occupy the entire width, including padding! ba.add("\\hspace*{-\\tabcolsep}\\begin{tabular}", "\\end{tabular}\\hspace*{-\\tabcolsep}"); } // columns ba.add("{",""); if (bGlobalVBorder[0]) { ba.add("|",""); } int nColCount = table.getColCount(); for (int nCol=0; nCol