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() == "\\") {
224 // stuff before the line break
225 // and look ahead for stuff after the line break
226 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
230 else if (t.cs() == "hline")
233 else if (t.cs() == "cline")
234 hlines += "\\cline{" + p.verbatim_item() + '}';
236 else if (t.cat() == catComment)
239 else if (t.cs() == "(") {
241 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
245 else if (t.cs() == "[") {
247 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
251 else if (t.cs() == "begin") {
252 string const name = p.getArg('{', '}');
253 active_environments.push_back(name);
254 os << "\\begin{" << name << '}';
255 if (is_math_env(name)) {
256 parse_math(p, os, FLAG_END, MATH_MODE);
258 parse_table(p, os, FLAG_END);
260 os << "\\end{" << name << '}';
261 active_environments.pop_back();
264 else if (t.cs() == "end") {
265 if (flags & FLAG_END) {
266 // eat environment name
267 string const name = p.getArg('{', '}');
268 if (name != active_environment())
269 p.error("\\end{" + name + "} does not match \\begin{"
270 + active_environment() + "}");
273 p.error("found 'end' unexpectedly");
282 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
285 for (size_t col = 0; col < ci.size(); ++col)
286 ci[col].topline = true;
290 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
292 ri.bottomline = true;
293 for (size_t col = 0; col < ci.size(); ++col)
294 ci[col].bottomline = true;
298 void handle_tabular(Parser & p, ostream & os,
301 string posopts = p.getOpt();
302 if (!posopts.empty())
303 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
305 vector<ColInfo> colinfo;
307 // handle column formatting
308 handle_colalign(p, colinfo);
310 // handle initial hlines
312 // first scan of cells
313 // use table mode to keep it minimal-invasive
314 // not exactly what's TeX doing...
315 vector<string> lines;
317 ss << read_hlines(p) << HLINE; // handle initial hlines
318 parse_table(p, ss, FLAG_END);
319 split(ss.str(), lines, LINE);
321 vector< vector<CellInfo> > cellinfo(lines.size());
322 vector<RowInfo> rowinfo(lines.size());
325 //cerr << "// split into rows\n";
326 for (size_t row = 0; row < rowinfo.size(); ++row) {
329 cellinfo[row].resize(colinfo.size());
332 vector<string> dummy;
333 //cerr << "\n########### LINE: " << lines[row] << "########\n";
334 split(lines[row], dummy, HLINE);
336 // handle horizontal line fragments
337 if (dummy.size() != 3) {
338 if (dummy.size() != 1)
339 cerr << "unexpected dummy size: " << dummy.size()
340 << " content: " << lines[row] << "\n";
343 lines[row] = dummy[1];
345 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
346 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
347 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
349 for (int i = 0; i <= 2; i += 2) {
350 //cerr << " reading from line string '" << dummy[i] << "'\n";
353 Token t = p1.get_token();
354 //cerr << "read token: " << t << "\n";
355 if (t.cs() == "hline") {
357 if (rowinfo[row].topline) {
358 if (row > 0) // extra bottomline above
359 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
361 cerr << "dropping extra hline\n";
362 //cerr << "below row: " << row-1 << endl;
364 handle_hline_above(rowinfo[row], cellinfo[row]);
365 //cerr << "above row: " << row << endl;
368 //cerr << "below row: " << row << endl;
369 handle_hline_below(rowinfo[row], cellinfo[row]);
371 } else if (t.cs() == "cline") {
372 string arg = p1.verbatim_item();
373 //cerr << "read cline arg: '" << arg << "'\n";
377 size_t from = string2int(t[0]) - 1;
378 size_t to = string2int(t[1]);
379 for (size_t col = from; col < to; ++col) {
380 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
382 rowinfo[row].topline = true;
383 cellinfo[row][col].topline = true;
385 rowinfo[row].bottomline = true;
386 cellinfo[row][col].bottomline = true;
390 cerr << "unexpected line token: " << t << endl;
396 vector<string> cells;
397 split(lines[row], cells, TAB);
398 // Has the last multicolumn cell a rightline?
399 bool last_rightline = false;
400 for (size_t col = 0, cell = 0;
401 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
402 //cerr << "cell content: '" << cells[cell] << "'\n";
403 Parser p(cells[cell]);
405 //cells[cell] << "'\n";
406 if (p.next_token().cs() == "multicolumn") {
409 size_t const ncells = string2int(p.verbatim_item());
411 // special cell properties alignment
413 handle_colalign(p, t);
414 cellinfo[row][col].multi = 1;
415 cellinfo[row][col].align = t.front().align;
417 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
418 cellinfo[row][col].content = os.str();
420 // multicolumn cells are tricky: This
421 // \multicolumn{2}{|c|}{col1-2}&
422 // \multicolumn{2}{|c|}{col3-4} "\\"
423 // gives | col1-2 | col3-4 | and not
424 // | col1-2 || col3-4 |
426 if (last_rightline && t.front().leftline) {
427 t.front().leftline = false;
429 last_rightline = t.front().rightline;
431 // multicolumn lines override normal cell lines
432 cellinfo[row][col].leftline = t.front().leftline;
433 cellinfo[row][col].rightline = t.front().rightline;
435 // add dummy cells for multicol
436 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
438 cellinfo[row][col].multi = 2;
439 cellinfo[row][col].align = 'c';
442 // more than one line on the right?
443 if (t.front().rightline > 1)
444 cellinfo[row][col + 1].leftline = true;
447 // FLAG_END is a hack, we need to read all of it
448 cellinfo[row][col].leftline = colinfo[col].leftline;
449 cellinfo[row][col].rightline = colinfo[col].rightline;
450 cellinfo[row][col].align = colinfo[col].align;
452 parse_text_in_inset(p, os, FLAG_CELL, false, context);
453 cellinfo[row][col].content = os.str();
454 last_rightline = false;
458 //cerr << "// handle almost empty last row what we have\n";
459 // handle almost empty last row
460 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
461 //cerr << "remove empty last line\n";
462 if (rowinfo[row].topline)
463 rowinfo[row - 1].bottomline = true;
464 for (size_t col = 0; col < colinfo.size(); ++col)
465 if (cellinfo[row][col].topline)
466 cellinfo[row - 1][col].bottomline = true;
472 //cerr << "// output what we have\n";
473 // output what we have
474 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
475 << "\" columns=\"" << colinfo.size() << "\">\n"
478 //cerr << "// after header\n";
479 for (size_t col = 0; col < colinfo.size(); ++col) {
480 os << "<column alignment=\""
481 << verbose_align(colinfo[col].align) << "\"";
482 os << " valignment=\"top\"";
483 if (colinfo[col].leftline)
484 os << " leftline=\"true\"";
485 if (colinfo[col].rightline)
486 os << " rightline=\"true\"";
487 if (!colinfo[col].width.empty())
488 os << " width=\"" << colinfo[col].width << "\"";
489 if (!colinfo[col].special.empty())
490 os << " special=\"" << colinfo[col].special << "\"";
493 //cerr << "// after cols\n";
495 for (size_t row = 0; row < rowinfo.size(); ++row) {
497 if (rowinfo[row].topline)
498 os << " topline=\"true\"";
499 if (rowinfo[row].bottomline)
500 os << " bottomline=\"true\"";
502 for (size_t col = 0; col < colinfo.size(); ++col) {
503 CellInfo const & cell = cellinfo[row][col];
506 os << " multicolumn=\"" << cell.multi << "\"";
507 os << " alignment=\"" << verbose_align(cell.align)
509 << " valignment=\"top\"";
511 os << " topline=\"true\"";
513 os << " bottomline=\"true\"";
515 os << " leftline=\"true\"";
517 os << " rightline=\"true\"";
518 //cerr << "\nrow: " << row << " col: " << col;
520 // cerr << " topline=\"true\"";
521 //if (cell.bottomline)
522 // cerr << " bottomline=\"true\"";
523 os << " usebox=\"none\""
525 << "\n\\begin_inset Text\n"
527 << "\n\\end_inset \n"
533 os << "</lyxtabular>\n";