3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
27 using std::istringstream;
29 using std::ostringstream;
33 #include "mathed/math_gridinfo.h"
35 // filled in preamble.C
36 std::map<char, int> special_columns;
41 int string2int(string const & s, int deflt = 0)
50 string read_hlines(Parser & p)
55 if (p.next_token().cs() == "hline") {
58 } else if (p.next_token().cs() == "cline") {
60 os << "\\cline{" << p.verbatim_item() << "}";
65 //cerr << "read_hlines(), read: '" << os.str() << "'\n";
66 //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
71 /* rather brutish way to code table structure in a string:
75 \multicolumn{2}{c}{4} & 5 //
82 \hline HLINE TAB 5 LINE
86 char const TAB = '\001';
87 char const LINE = '\002';
88 char const HLINE = '\004';
91 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
93 if (p.get_token().cat() != catBegin)
94 cerr << "wrong syntax for table column alignment. '{' expected\n";
97 bool leftline = false;
98 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
100 cerr << "t: " << t << " c: '" << t.character() << "'\n";
103 // We cannot handle comments here
104 if (t.cat() == catComment) {
105 if (t.cs().empty()) {
109 cerr << "Ignoring comment: " << t.asInput();
113 switch (t.character()) {
118 ci.align = t.character();
119 if (colinfo.size() && colinfo.back().rightline > 1) {
121 --colinfo.back().rightline;
123 colinfo.push_back(ci);
127 colinfo.push_back(ColInfo());
128 colinfo.back().align = nextalign;
129 colinfo.back().width = p.verbatim_item();
136 ++colinfo.back().rightline;
139 string s = p.verbatim_item();
140 if (s == "\\raggedleft ")
142 else if (s == "\\raggedright ")
145 cerr << "unknown '>' column '" << s << "'\n";
149 if (special_columns.find(t.character()) != special_columns.end()) {
152 ci.special += t.character();
153 int const nargs = special_columns[t.character()];
154 for (int i = 0; i < nargs; ++i)
155 ci.special += "{" + p.verbatim_item() + "}";
156 //cerr << "handling special column '" << t << "' " << nargs
157 // << " '" << ci.special << "'\n";
158 colinfo.push_back(ci);
160 cerr << "ignoring special separator '" << t << "'\n";
165 if (colinfo.size() && leftline)
166 colinfo[0].leftline = true;
170 } // anonymous namespace
173 void parse_table(Parser & p, ostream & os, unsigned flags)
178 Token const & t = p.get_token();
181 cerr << "t: " << t << " flags: " << flags << "\n";
187 if (t.cat() == catMath) {
188 // we are inside some text mode thingy, so opening new math is allowed
189 Token const & n = p.get_token();
190 if (n.cat() == catMath) {
191 // TeX's $$...$$ syntax for displayed math
193 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
195 p.get_token(); // skip the second '$' token
197 // simple $...$ stuff
200 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
205 else if (t.cat() == catSpace || t.cat() == catNewline)
208 else if (t.cat() == catLetter ||
209 t.cat() == catSuper ||
211 t.cat() == catOther ||
212 t.cat() == catActive ||
213 t.cat() == catParameter)
216 else if (t.cat() == catBegin) {
218 parse_table(p, os, FLAG_BRACE_LAST);
222 else if (t.cat() == catEnd) {
223 if (flags & FLAG_BRACE_LAST)
225 cerr << "unexpected '}'\n";
228 else if (t.cat() == catAlign) {
233 else if (t.cs() == "tabularnewline" || t.cs() == "\\" ||
236 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
238 // stuff before the line break
239 // and look ahead for stuff after the line break
240 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
244 else if (t.cs() == "hline")
247 else if (t.cs() == "cline")
248 hlines += "\\cline{" + p.verbatim_item() + '}';
250 else if (t.cat() == catComment)
253 else if (t.cs() == "(") {
255 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
259 else if (t.cs() == "[") {
261 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
265 else if (t.cs() == "begin") {
266 string const name = p.getArg('{', '}');
267 active_environments.push_back(name);
268 os << "\\begin{" << name << '}';
269 if (is_math_env(name)) {
270 parse_math(p, os, FLAG_END, MATH_MODE);
272 parse_table(p, os, FLAG_END);
274 os << "\\end{" << name << '}';
275 active_environments.pop_back();
278 else if (t.cs() == "end") {
279 if (flags & FLAG_END) {
280 // eat environment name
281 string const name = p.getArg('{', '}');
282 if (name != active_environment())
283 p.error("\\end{" + name + "} does not match \\begin{"
284 + active_environment() + "}");
287 p.error("found 'end' unexpectedly");
296 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
299 for (size_t col = 0; col < ci.size(); ++col)
300 ci[col].topline = true;
304 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
306 ri.bottomline = true;
307 for (size_t col = 0; col < ci.size(); ++col)
308 ci[col].bottomline = true;
312 void handle_tabular(Parser & p, ostream & os,
315 string posopts = p.getOpt();
316 if (!posopts.empty())
317 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
319 vector<ColInfo> colinfo;
321 // handle column formatting
322 handle_colalign(p, colinfo);
324 // handle initial hlines
326 // first scan of cells
327 // use table mode to keep it minimal-invasive
328 // not exactly what's TeX doing...
329 vector<string> lines;
331 ss << read_hlines(p) << HLINE; // handle initial hlines
332 parse_table(p, ss, FLAG_END);
333 split(ss.str(), lines, LINE);
335 vector< vector<CellInfo> > cellinfo(lines.size());
336 vector<RowInfo> rowinfo(lines.size());
339 //cerr << "// split into rows\n";
340 for (size_t row = 0; row < rowinfo.size(); ++row) {
343 cellinfo[row].resize(colinfo.size());
346 vector<string> dummy;
347 //cerr << "\n########### LINE: " << lines[row] << "########\n";
348 split(lines[row], dummy, HLINE);
350 // handle horizontal line fragments
351 if (dummy.size() != 3) {
352 if (dummy.size() != 1)
353 cerr << "unexpected dummy size: " << dummy.size()
354 << " content: " << lines[row] << "\n";
357 lines[row] = dummy[1];
359 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
360 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
361 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
363 for (int i = 0; i <= 2; i += 2) {
364 //cerr << " reading from line string '" << dummy[i] << "'\n";
367 Token t = p1.get_token();
368 //cerr << "read token: " << t << "\n";
369 if (t.cs() == "hline") {
371 if (rowinfo[row].topline) {
372 if (row > 0) // extra bottomline above
373 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
375 cerr << "dropping extra hline\n";
376 //cerr << "below row: " << row-1 << endl;
378 handle_hline_above(rowinfo[row], cellinfo[row]);
379 //cerr << "above row: " << row << endl;
382 //cerr << "below row: " << row << endl;
383 handle_hline_below(rowinfo[row], cellinfo[row]);
385 } else if (t.cs() == "cline") {
386 string arg = p1.verbatim_item();
387 //cerr << "read cline arg: '" << arg << "'\n";
391 size_t from = string2int(t[0]) - 1;
392 if (from >= colinfo.size()) {
393 cerr << "cline starts at non "
396 from = colinfo.size() - 1;
398 size_t to = string2int(t[1]);
399 if (to >= colinfo.size()) {
400 cerr << "cline ends at non "
403 to = colinfo.size() - 1;
405 for (size_t col = from; col < to; ++col) {
406 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
408 rowinfo[row].topline = true;
409 cellinfo[row][col].topline = true;
411 rowinfo[row].bottomline = true;
412 cellinfo[row][col].bottomline = true;
416 cerr << "unexpected line token: " << t << endl;
422 vector<string> cells;
423 split(lines[row], cells, TAB);
424 // Has the last multicolumn cell a rightline?
425 bool last_rightline = false;
426 for (size_t col = 0, cell = 0;
427 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
428 //cerr << "cell content: '" << cells[cell] << "'\n";
429 Parser p(cells[cell]);
431 //cells[cell] << "'\n";
432 if (p.next_token().cs() == "multicolumn") {
435 size_t const ncells = string2int(p.verbatim_item());
437 // special cell properties alignment
439 handle_colalign(p, t);
440 cellinfo[row][col].multi = 1;
441 cellinfo[row][col].align = t.front().align;
443 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
444 cellinfo[row][col].content = os.str();
446 // multicolumn cells are tricky: This
447 // \multicolumn{2}{|c|}{col1-2}&
448 // \multicolumn{2}{|c|}{col3-4} "\\"
449 // gives | col1-2 | col3-4 | and not
450 // | col1-2 || col3-4 |
452 if (last_rightline && t.front().leftline) {
453 t.front().leftline = false;
455 last_rightline = t.front().rightline;
457 // multicolumn lines override normal cell lines
458 cellinfo[row][col].leftline = t.front().leftline;
459 cellinfo[row][col].rightline = t.front().rightline;
461 // add dummy cells for multicol
462 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
464 cellinfo[row][col].multi = 2;
465 cellinfo[row][col].align = 'c';
468 // more than one line on the right?
469 if (t.front().rightline > 1)
470 cellinfo[row][col + 1].leftline = true;
473 // FLAG_END is a hack, we need to read all of it
474 cellinfo[row][col].leftline = colinfo[col].leftline;
475 cellinfo[row][col].rightline = colinfo[col].rightline;
476 cellinfo[row][col].align = colinfo[col].align;
478 parse_text_in_inset(p, os, FLAG_CELL, false, context);
479 cellinfo[row][col].content = os.str();
480 last_rightline = false;
484 //cerr << "// handle almost empty last row what we have\n";
485 // handle almost empty last row
486 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
487 //cerr << "remove empty last line\n";
488 if (rowinfo[row].topline)
489 rowinfo[row - 1].bottomline = true;
490 for (size_t col = 0; col < colinfo.size(); ++col)
491 if (cellinfo[row][col].topline)
492 cellinfo[row - 1][col].bottomline = true;
498 //cerr << "// output what we have\n";
499 // output what we have
500 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
501 << "\" columns=\"" << colinfo.size() << "\">\n"
504 //cerr << "// after header\n";
505 for (size_t col = 0; col < colinfo.size(); ++col) {
506 os << "<column alignment=\""
507 << verbose_align(colinfo[col].align) << "\"";
508 os << " valignment=\"top\"";
509 if (colinfo[col].leftline)
510 os << " leftline=\"true\"";
511 if (colinfo[col].rightline)
512 os << " rightline=\"true\"";
513 if (!colinfo[col].width.empty())
514 os << " width=\"" << colinfo[col].width << "\"";
515 if (!colinfo[col].special.empty())
516 os << " special=\"" << colinfo[col].special << "\"";
519 //cerr << "// after cols\n";
521 for (size_t row = 0; row < rowinfo.size(); ++row) {
523 if (rowinfo[row].topline)
524 os << " topline=\"true\"";
525 if (rowinfo[row].bottomline)
526 os << " bottomline=\"true\"";
528 for (size_t col = 0; col < colinfo.size(); ++col) {
529 CellInfo const & cell = cellinfo[row][col];
532 os << " multicolumn=\"" << cell.multi << "\"";
533 os << " alignment=\"" << verbose_align(cell.align)
535 << " valignment=\"top\"";
537 os << " topline=\"true\"";
539 os << " bottomline=\"true\"";
541 os << " leftline=\"true\"";
543 os << " rightline=\"true\"";
544 //cerr << "\nrow: " << row << " col: " << col;
546 // cerr << " topline=\"true\"";
547 //if (cell.bottomline)
548 // cerr << " bottomline=\"true\"";
549 os << " usebox=\"none\""
551 << "\n\\begin_inset Text\n"
559 os << "</lyxtabular>\n";