X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ftex2lyx%2Ftable.cpp;h=98f3e03e3a7a37c87aad13758b3e677d901f032b;hb=639aa354448188f45e69d6bd1e0ef3dfcc938c5f;hp=06ad747dd0e9493c95843d65f819fd13bb59aa00;hpb=528c984a571550045fb67f2d437bb71f6a4d45c4;p=lyx.git diff --git a/src/tex2lyx/table.cpp b/src/tex2lyx/table.cpp index 06ad747dd0..98f3e03e3a 100644 --- a/src/tex2lyx/table.cpp +++ b/src/tex2lyx/table.cpp @@ -22,6 +22,7 @@ #include "support/convert.h" #include "support/lstrings.h" +#include #include #include #include @@ -31,15 +32,13 @@ using namespace std; namespace lyx { -// filled in preamble.cpp -map special_columns; - namespace { class ColInfo { public: - ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {} + ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0), + decimal_point('\0') {} /// column alignment char align; /// vertical alignment @@ -52,6 +51,8 @@ public: int rightlines; /// number of lines on the left int leftlines; + /// decimal separator + char decimal_point; }; @@ -103,14 +104,18 @@ public: /// the numeric values are part of the file format! -enum Multicolumn { +enum Multi { /// A normal cell CELL_NORMAL = 0, /// A multicolumn cell. The number of columns is 1 + number /// of CELL_PART_OF_MULTICOLUMN cells that follow directly - CELL_BEGIN_OF_MULTICOLUMN = 1, + CELL_BEGIN_OF_MULTICOLUMN, /// This is a dummy cell (part of a multicolumn cell) - CELL_PART_OF_MULTICOLUMN = 2 + CELL_PART_OF_MULTICOLUMN, + /// + CELL_BEGIN_OF_MULTIROW, + /// + CELL_PART_OF_MULTIROW }; @@ -118,11 +123,11 @@ class CellInfo { public: CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'), leftlines(0), rightlines(0), topline(false), - bottomline(false), rotate(false) {} + bottomline(false), rotate(0), mrxnum(0) {} /// cell content string content; /// multicolumn flag - Multicolumn multi; + Multi multi; /// cell alignment char align; /// vertical cell alignment @@ -135,19 +140,23 @@ public: bool topline; /// do we have a line below? bool bottomline; - /// is the cell rotated? - bool rotate; + /// how is the cell rotated? + int rotate; /// width for multicolumn cells string width; /// special formatting for multicolumn cells string special; + /// multirow offset + string mroffset; + /// number of further multirows + int mrxnum; }; class ltType { public: // constructor - ltType() : topDL(false), bottomDL(false), empty(false) {} + ltType() : set(false), topDL(false), bottomDL(false), empty(false) {} // we have this header type (is set in the getLT... functions) bool set; // double borders on top @@ -171,6 +180,8 @@ inline char const * verbose_align(char c) return "right"; case 'l': return "left"; + case 'd': + return "decimal"; default: return "none"; } @@ -209,6 +220,14 @@ string const write_attribute(string const & name, string const & s) } +string const write_attribute(string const & name, int const & i) +{ + // we write only true attribute values so we remove a bit of the + // file format bloat for tabulars. + return i ? write_attribute(name, convert(i)) : string(); +} + + /*! rather brutish way to code table structure in a string: \verbatim @@ -250,6 +269,17 @@ void ci2special(ColInfo & ci) // this case. return; + if (ci.decimal_point != '\0') { + // we only support decimal point natively + // with 'l' alignment in or 'n' alignment + // with width in second row + if (ci.align != 'l' && ci.align != 'n') { + ci.decimal_point = '\0'; + return; + } else + ci.special.clear(); + } + if (!ci.width.empty()) { switch (ci.align) { case 'l': @@ -316,13 +346,25 @@ void handle_colalign(Parser & p, vector & colinfo, } switch (t.character()) { + case ' ': + // whitespace, ignore. + break; case 'c': case 'l': case 'r': // new column, horizontal aligned next.align = t.character(); - if (!next.special.empty()) + if (!next.special.empty()) { ci2special(next); + // handle decimal separator + if (next.decimal_point != '\0') { + if (!colinfo.empty() && colinfo.back().align == 'r') { + colinfo.back().align = 'd'; + colinfo.back().decimal_point = next.decimal_point; + } else + next.decimal_point = '\0'; + } + } colinfo.push_back(next); next = ColInfo(); break; @@ -332,8 +374,17 @@ void handle_colalign(Parser & p, vector & colinfo, // new column, vertical aligned box next.valign = t.character(); next.width = p.verbatim_item(); - if (!next.special.empty()) + if (!next.special.empty()) { ci2special(next); + // handle decimal separator + if (next.decimal_point != '\0') { + if (!colinfo.empty() && colinfo.back().align == 'r') { + colinfo.back().align = 'd'; + colinfo.back().decimal_point = next.decimal_point; + } else + next.decimal_point = '\0'; + } + } colinfo.push_back(next); next = ColInfo(); break; @@ -386,6 +437,8 @@ void handle_colalign(Parser & p, vector & colinfo, break; } case '*': { + if (p.next_token().character() != '{') + continue; // *{n}{arg} means 'n' columns of type 'arg' string const num = p.verbatim_item(); string const arg = p.verbatim_item(); @@ -407,30 +460,32 @@ void handle_colalign(Parser & p, vector & colinfo, } case '@': // text instead of the column spacing - case '!': + case '!': { // text in addition to the column spacing + string const arg = p.verbatim_item(); next.special += t.character(); - next.special += '{' + p.verbatim_item() + '}'; + next.special += '{' + arg + '}'; + string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string(); + if (t.character() == '@' && sarg == "\\extracolsep{0pt}") + next.decimal_point = arg.back(); break; - default: + } + default: { // try user defined column types - if (special_columns.find(t.character()) != - special_columns.end()) { - ci2special(next); - next.special += t.character(); - int const nargs = - special_columns[t.character()]; - for (int i = 0; i < nargs; ++i) - next.special += '{' + - p.verbatim_item() + - '}'; - colinfo.push_back(next); - next = ColInfo(); - } else - cerr << "Ignoring column specification" - " '" << t << "'." << endl; + // unknown column types (nargs == -1) are + // assumed to consume no arguments + ci2special(next); + next.special += t.character(); + int const nargs = + preamble.getSpecialTableColumnArguments(t.character()); + for (int i = 0; i < nargs; ++i) + next.special += '{' + + p.verbatim_item() + '}'; + colinfo.push_back(next); + next = ColInfo(); break; } + } } // Maybe we have some column separators that need to be added to the @@ -726,13 +781,13 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, } } - else if (t.cat() == catSpace + else if (t.cat() == catSpace || t.cat() == catNewline - || t.cat() == catLetter - || t.cat() == catSuper - || t.cat() == catSub - || t.cat() == catOther - || t.cat() == catActive + || t.cat() == catLetter + || t.cat() == catSuper + || t.cat() == catSub + || t.cat() == catOther + || t.cat() == catActive || t.cat() == catParameter) os << t.cs(); @@ -778,7 +833,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, // treat the nested environment as a block, don't // parse &, \\ etc, because they don't belong to our // table if they appear. - os << p.verbatimEnvironment(name); + os << p.ertEnvironment(name); os << "\\end{" << name << '}'; active_environments.pop_back(); } @@ -831,7 +886,8 @@ void handle_hline_below(RowInfo & ri, vector & ci) void handle_tabular(Parser & p, ostream & os, string const & name, - string const & tabularwidth, Context & context) + string const & tabularwidth, string const & halign, + Context & context) { bool const is_long_tabular(name == "longtable"); bool booktabs = false; @@ -1114,10 +1170,65 @@ void handle_tabular(Parser & p, ostream & os, string const & name, << cells[cell] << "'." << endl; continue; } - Parser p(cells[cell]); + string cellcont = cells[cell]; + // For decimal cells, ass the content of the second one to the first one + // of a pair. + if (colinfo[col].decimal_point != '\0' && colinfo[col].align == 'd' && cell < cells.size() - 1) + cellcont += colinfo[col].decimal_point + cells[cell + 1]; + Parser p(cellcont); p.skip_spaces(); //cells[cell] << "'\n"; - if (p.next_token().cs() == "multicolumn") { + if (p.next_token().cs() == "multirow") { + // We do not support the vpos arg yet. + if (p.hasOpt()) { + string const vpos = p.getArg('[', ']'); + p.skip_spaces(true); + cerr << "Ignoring multirow's vpos arg '" + << vpos << "'!" << endl; + } + // how many cells? + p.get_token(); + size_t const ncells = + convert(p.verbatim_item()); + // We do not support the bigstrut arg yet. + if (p.hasOpt()) { + string const bs = p.getArg('[', ']'); + p.skip_spaces(true); + cerr << "Ignoring multirow's bigstrut arg '" + << bs << "'!" << endl; + } + // the width argument + string const width = p.getArg('{', '}'); + // the vmove arg + string vmove; + if (p.hasOpt()) { + vmove = p.getArg('[', ']'); + p.skip_spaces(true); + } + + if (width != "*") + colinfo[col].width = width; + if (!vmove.empty()) + cellinfo[row][col].mroffset = vmove; + cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW; + cellinfo[row][col].leftlines = colinfo[col].leftlines; + cellinfo[row][col].rightlines = colinfo[col].rightlines; + cellinfo[row][col].mrxnum = ncells - 1; + + ostringstream os2; + parse_text_in_inset(p, os2, FLAG_ITEM, false, context); + if (!cellinfo[row][col].content.empty()) { + // This may or may not work in LaTeX, + // but it does not work in LyX. + // FIXME: Handle it correctly! + cerr << "Moving cell content '" + << cells[cell] + << "' into a multirow cell. " + "This will probably not work." + << endl; + } + cellinfo[row][col].content += os2.str(); + } else if (p.next_token().cs() == "multicolumn") { // how many cells? p.get_token(); size_t const ncells = @@ -1156,8 +1267,20 @@ void handle_tabular(Parser & p, ostream & os, string const & name, cellinfo[row][col].content += os.str(); // add dummy cells for multicol - for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) { + for (size_t i = 0; i < ncells - 1; ++i) { ++col; + if (col >= colinfo.size()) { + cerr << "The cell '" + << cells[cell] + << "' specifies " + << convert(ncells) + << " columns while the table has only " + << convert(colinfo.size()) + << " columns!" + << " Therefore the surplus columns will be ignored." + << endl; + break; + } cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN; cellinfo[row][col].align = 'c'; } @@ -1171,51 +1294,66 @@ void handle_tabular(Parser & p, ostream & os, string const & name, // one multicolumn cell. The contents of that // cell must contain exactly one caption inset // and nothing else. - // LyX outputs all caption rows as first head, - // so we must not set the caption flag for - // captions not in the first head. // Fortunately, the caption flag is only needed // for tables with more than one column. - bool usecaption = (rowinfo[row].type == LT_NORMAL || - rowinfo[row].type == LT_FIRSTHEAD); - for (size_t r = 0; r < row && usecaption; ++r) - if (rowinfo[row].type != LT_NORMAL && - rowinfo[row].type != LT_FIRSTHEAD) - usecaption = false; - if (usecaption) { - rowinfo[row].caption = true; - for (size_t c = 1; c < cells.size(); ++c) { - if (!cells[c].empty()) { - cerr << "Moving cell content '" - << cells[c] - << "' into the caption cell. " - "This will probably not work." - << endl; - cells[0] += cells[c]; - } + rowinfo[row].caption = true; + for (size_t c = 1; c < cells.size(); ++c) { + if (!cells[c].empty()) { + cerr << "Moving cell content '" + << cells[c] + << "' into the caption cell. " + "This will probably not work." + << endl; + cells[0] += cells[c]; } - cells.resize(1); - cellinfo[row][col].align = colinfo[col].align; - cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN; - } else { - cellinfo[row][col].leftlines = colinfo[col].leftlines; - cellinfo[row][col].rightlines = colinfo[col].rightlines; - cellinfo[row][col].align = colinfo[col].align; } + cells.resize(1); + cellinfo[row][col].align = colinfo[col].align; + cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN; ostringstream os; parse_text_in_inset(p, os, FLAG_CELL, false, context); cellinfo[row][col].content += os.str(); - if (usecaption) { - // add dummy multicolumn cells - for (size_t c = 1; c < colinfo.size(); ++c) - cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN; - } + // add dummy multicolumn cells + for (size_t c = 1; c < colinfo.size(); ++c) + cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN; } else { + bool turn = false; + int rotate = 0; + if (p.next_token().cs() == "begin") { + p.pushPosition(); + p.get_token(); + string const env = p.getArg('{', '}'); + if (env == "sideways" || env == "turn") { + string angle = "90"; + if (env == "turn") { + turn = true; + angle = p.getArg('{', '}'); + } + active_environments.push_back(env); + p.ertEnvironment(env); + active_environments.pop_back(); + p.skip_spaces(); + if (!p.good() && support::isStrInt(angle)) + rotate = convert(angle); + } + p.popPosition(); + } cellinfo[row][col].leftlines = colinfo[col].leftlines; cellinfo[row][col].rightlines = colinfo[col].rightlines; cellinfo[row][col].align = colinfo[col].align; ostringstream os; - parse_text_in_inset(p, os, FLAG_CELL, false, context); + if (rotate != 0) { + cellinfo[row][col].rotate = rotate; + p.get_token(); + active_environments.push_back(p.getArg('{', '}')); + if (turn) + p.getArg('{', '}'); + parse_text_in_inset(p, os, FLAG_END, false, context); + active_environments.pop_back(); + preamble.registerAutomaticallyLoadedPackage("rotating"); + } else { + parse_text_in_inset(p, os, FLAG_CELL, false, context); + } cellinfo[row][col].content += os.str(); } } @@ -1239,13 +1377,23 @@ void handle_tabular(Parser & p, ostream & os, string const & name, // and cellinfo. // Unfortunately LyX has some limitations that we need to work around. - // Convert cells with special content to multicolumn cells - // (LyX ignores the special field for non-multicolumn cells). + // Some post work for (size_t row = 0; row < rowinfo.size(); ++row) { for (size_t col = 0; col < cellinfo[row].size(); ++col) { + // Convert cells with special content to multicolumn cells + // (LyX ignores the special field for non-multicolumn cells). if (cellinfo[row][col].multi == CELL_NORMAL && !cellinfo[row][col].special.empty()) cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN; + // Add multirow dummy cells + if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW + || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW) + && cellinfo[row - 1][col].mrxnum > 0) { + // add dummy cells for multirow + cellinfo[row][col].multi = CELL_PART_OF_MULTIROW; + cellinfo[row][col].align = 'c'; + cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1; + } } } @@ -1275,6 +1423,16 @@ void handle_tabular(Parser & p, ostream & os, string const & name, cellinfo[row][col].rightlines = colinfo[col].rightlines; if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL) cellinfo[row][col].leftlines = colinfo[col].leftlines; + } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) { + size_t s = row + 1; + while (s < rowinfo.size() && + cellinfo[s][col].multi == CELL_PART_OF_MULTIROW) + s++; + if (s < cellinfo[row].size() && + cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW) + cellinfo[row][col].bottomline = rowinfo[row].bottomline; + if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL) + cellinfo[row][col].topline = rowinfo[row].topline; } } } @@ -1286,10 +1444,16 @@ void handle_tabular(Parser & p, ostream & os, string const & name, //cerr << "// output what we have\n"; // output what we have + string const rotate = "0"; + size_type cols = colinfo.size(); + for (size_t col = 0; col < colinfo.size(); ++col) { + if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd') + --cols; + } os << "\n\n"; + << "\" columns=\"" << cols << "\">\n"; os << "\n"; for (size_t col = 0; col < colinfo.size(); ++col) { CellInfo const & cell = cellinfo[row][col]; + if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd') + // These are the second columns in a salign pair. Skip. + continue; os << " 0) << write_attribute("rightline", cell.rightlines > 0) - << write_attribute("rotate", cell.rotate); + << write_attribute("rotate", cell.rotate) + << write_attribute("mroffset", cell.mroffset); //cerr << "\nrow: " << row << " col: " << col; //if (cell.topline) // cerr << " topline=\"true\"";