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 switch (t.character()) {
108 ci.align = t.character();
109 if (colinfo.size() && colinfo.back().rightline > 1) {
111 --colinfo.back().rightline;
113 colinfo.push_back(ci);
117 colinfo.push_back(ColInfo());
118 colinfo.back().align = nextalign;
119 colinfo.back().width = p.verbatim_item();
126 ++colinfo.back().rightline;
129 string s = p.verbatim_item();
130 if (s == "\\raggedleft ")
132 else if (s == "\\raggedright ")
135 cerr << "unknown '>' column '" << s << "'\n";
139 if (special_columns.find(t.character()) != special_columns.end()) {
142 ci.special += t.character();
143 int const nargs = special_columns[t.character()];
144 for (int i = 0; i < nargs; ++i)
145 ci.special += "{" + p.verbatim_item() + "}";
146 //cerr << "handling special column '" << t << "' " << nargs
147 // << " '" << ci.special << "'\n";
148 colinfo.push_back(ci);
150 cerr << "ignoring special separator '" << t << "'\n";
155 if (colinfo.size() && leftline)
156 colinfo[0].leftline = true;
160 } // anonymous namespace
163 void parse_table(Parser & p, ostream & os, unsigned flags)
168 Token const & t = p.get_token();
171 cerr << "t: " << t << " flags: " << flags << "\n";
177 if (t.cat() == catMath) {
178 // we are inside some text mode thingy, so opening new math is allowed
179 Token const & n = p.get_token();
180 if (n.cat() == catMath) {
181 // TeX's $$...$$ syntax for displayed math
183 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
185 p.get_token(); // skip the second '$' token
187 // simple $...$ stuff
190 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
195 else if (t.cat() == catSpace || t.cat() == catNewline)
198 else if (t.cat() == catLetter ||
199 t.cat() == catSuper ||
201 t.cat() == catOther ||
202 t.cat() == catActive ||
203 t.cat() == catParameter)
206 else if (t.cat() == catBegin) {
208 parse_table(p, os, FLAG_BRACE_LAST);
212 else if (t.cat() == catEnd) {
213 if (flags & FLAG_BRACE_LAST)
215 cerr << "unexpected '}'\n";
218 else if (t.cat() == catAlign) {
223 else if (t.cs() == "tabularnewline" || t.cs() == "\\" ||
226 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
228 // stuff before the line break
229 // and look ahead for stuff after the line break
230 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
234 else if (t.cs() == "hline")
237 else if (t.cs() == "cline")
238 hlines += "\\cline{" + p.verbatim_item() + '}';
240 else if (t.cat() == catComment)
243 else if (t.cs() == "(") {
245 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
249 else if (t.cs() == "[") {
251 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
255 else if (t.cs() == "begin") {
256 string const name = p.getArg('{', '}');
257 active_environments.push_back(name);
258 os << "\\begin{" << name << '}';
259 if (is_math_env(name)) {
260 parse_math(p, os, FLAG_END, MATH_MODE);
262 parse_table(p, os, FLAG_END);
264 os << "\\end{" << name << '}';
265 active_environments.pop_back();
268 else if (t.cs() == "end") {
269 if (flags & FLAG_END) {
270 // eat environment name
271 string const name = p.getArg('{', '}');
272 if (name != active_environment())
273 p.error("\\end{" + name + "} does not match \\begin{"
274 + active_environment() + "}");
277 p.error("found 'end' unexpectedly");
286 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
289 for (size_t col = 0; col < ci.size(); ++col)
290 ci[col].topline = true;
294 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
296 ri.bottomline = true;
297 for (size_t col = 0; col < ci.size(); ++col)
298 ci[col].bottomline = true;
302 void handle_tabular(Parser & p, ostream & os,
305 string posopts = p.getOpt();
306 if (!posopts.empty())
307 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
309 vector<ColInfo> colinfo;
311 // handle column formatting
312 handle_colalign(p, colinfo);
314 // handle initial hlines
316 // first scan of cells
317 // use table mode to keep it minimal-invasive
318 // not exactly what's TeX doing...
319 vector<string> lines;
321 ss << read_hlines(p) << HLINE; // handle initial hlines
322 parse_table(p, ss, FLAG_END);
323 split(ss.str(), lines, LINE);
325 vector< vector<CellInfo> > cellinfo(lines.size());
326 vector<RowInfo> rowinfo(lines.size());
329 //cerr << "// split into rows\n";
330 for (size_t row = 0; row < rowinfo.size(); ++row) {
333 cellinfo[row].resize(colinfo.size());
336 vector<string> dummy;
337 //cerr << "\n########### LINE: " << lines[row] << "########\n";
338 split(lines[row], dummy, HLINE);
340 // handle horizontal line fragments
341 if (dummy.size() != 3) {
342 if (dummy.size() != 1)
343 cerr << "unexpected dummy size: " << dummy.size()
344 << " content: " << lines[row] << "\n";
347 lines[row] = dummy[1];
349 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
350 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
351 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
353 for (int i = 0; i <= 2; i += 2) {
354 //cerr << " reading from line string '" << dummy[i] << "'\n";
357 Token t = p1.get_token();
358 //cerr << "read token: " << t << "\n";
359 if (t.cs() == "hline") {
361 if (rowinfo[row].topline) {
362 if (row > 0) // extra bottomline above
363 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
365 cerr << "dropping extra hline\n";
366 //cerr << "below row: " << row-1 << endl;
368 handle_hline_above(rowinfo[row], cellinfo[row]);
369 //cerr << "above row: " << row << endl;
372 //cerr << "below row: " << row << endl;
373 handle_hline_below(rowinfo[row], cellinfo[row]);
375 } else if (t.cs() == "cline") {
376 string arg = p1.verbatim_item();
377 //cerr << "read cline arg: '" << arg << "'\n";
381 size_t from = string2int(t[0]) - 1;
382 size_t to = string2int(t[1]);
383 for (size_t col = from; col < to; ++col) {
384 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
386 rowinfo[row].topline = true;
387 cellinfo[row][col].topline = true;
389 rowinfo[row].bottomline = true;
390 cellinfo[row][col].bottomline = true;
394 cerr << "unexpected line token: " << t << endl;
400 vector<string> cells;
401 split(lines[row], cells, TAB);
402 // Has the last multicolumn cell a rightline?
403 bool last_rightline = false;
404 for (size_t col = 0, cell = 0;
405 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
406 //cerr << "cell content: '" << cells[cell] << "'\n";
407 Parser p(cells[cell]);
409 //cells[cell] << "'\n";
410 if (p.next_token().cs() == "multicolumn") {
413 size_t const ncells = string2int(p.verbatim_item());
415 // special cell properties alignment
417 handle_colalign(p, t);
418 cellinfo[row][col].multi = 1;
419 cellinfo[row][col].align = t.front().align;
421 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
422 cellinfo[row][col].content = os.str();
424 // multicolumn cells are tricky: This
425 // \multicolumn{2}{|c|}{col1-2}&
426 // \multicolumn{2}{|c|}{col3-4} "\\"
427 // gives | col1-2 | col3-4 | and not
428 // | col1-2 || col3-4 |
430 if (last_rightline && t.front().leftline) {
431 t.front().leftline = false;
433 last_rightline = t.front().rightline;
435 // multicolumn lines override normal cell lines
436 cellinfo[row][col].leftline = t.front().leftline;
437 cellinfo[row][col].rightline = t.front().rightline;
439 // add dummy cells for multicol
440 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
442 cellinfo[row][col].multi = 2;
443 cellinfo[row][col].align = 'c';
446 // more than one line on the right?
447 if (t.front().rightline > 1)
448 cellinfo[row][col + 1].leftline = true;
451 // FLAG_END is a hack, we need to read all of it
452 cellinfo[row][col].leftline = colinfo[col].leftline;
453 cellinfo[row][col].rightline = colinfo[col].rightline;
454 cellinfo[row][col].align = colinfo[col].align;
456 parse_text_in_inset(p, os, FLAG_CELL, false, context);
457 cellinfo[row][col].content = os.str();
458 last_rightline = false;
462 //cerr << "// handle almost empty last row what we have\n";
463 // handle almost empty last row
464 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
465 //cerr << "remove empty last line\n";
466 if (rowinfo[row].topline)
467 rowinfo[row - 1].bottomline = true;
468 for (size_t col = 0; col < colinfo.size(); ++col)
469 if (cellinfo[row][col].topline)
470 cellinfo[row - 1][col].bottomline = true;
476 //cerr << "// output what we have\n";
477 // output what we have
478 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
479 << "\" columns=\"" << colinfo.size() << "\">\n"
482 //cerr << "// after header\n";
483 for (size_t col = 0; col < colinfo.size(); ++col) {
484 os << "<column alignment=\""
485 << verbose_align(colinfo[col].align) << "\"";
486 os << " valignment=\"top\"";
487 if (colinfo[col].leftline)
488 os << " leftline=\"true\"";
489 if (colinfo[col].rightline)
490 os << " rightline=\"true\"";
491 if (!colinfo[col].width.empty())
492 os << " width=\"" << colinfo[col].width << "\"";
493 if (!colinfo[col].special.empty())
494 os << " special=\"" << colinfo[col].special << "\"";
497 //cerr << "// after cols\n";
499 for (size_t row = 0; row < rowinfo.size(); ++row) {
501 if (rowinfo[row].topline)
502 os << " topline=\"true\"";
503 if (rowinfo[row].bottomline)
504 os << " bottomline=\"true\"";
506 for (size_t col = 0; col < colinfo.size(); ++col) {
507 CellInfo const & cell = cellinfo[row][col];
510 os << " multicolumn=\"" << cell.multi << "\"";
511 os << " alignment=\"" << verbose_align(cell.align)
513 << " valignment=\"top\"";
515 os << " topline=\"true\"";
517 os << " bottomline=\"true\"";
519 os << " leftline=\"true\"";
521 os << " rightline=\"true\"";
522 //cerr << "\nrow: " << row << " col: " << col;
524 // cerr << " topline=\"true\"";
525 //if (cell.bottomline)
526 // cerr << " bottomline=\"true\"";
527 os << " usebox=\"none\""
529 << "\n\\begin_inset Text\n"
537 os << "</lyxtabular>\n";