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() == catLetter ||
196 t.cat() == catSpace ||
197 t.cat() == catSuper ||
199 t.cat() == catOther ||
200 t.cat() == catActive ||
201 t.cat() == catNewline ||
202 t.cat() == catParameter)
205 else if (t.cat() == catBegin) {
207 parse_table(p, os, FLAG_BRACE_LAST);
211 else if (t.cat() == catEnd) {
212 if (flags & FLAG_BRACE_LAST)
214 cerr << "unexpected '}'\n";
217 else if (t.cat() == catAlign) {
221 else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
222 // stuff before the line break
223 // and look ahead for stuff after the line break
224 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
228 else if (t.cs() == "hline")
231 else if (t.cs() == "cline")
232 hlines += "\\cline{" + p.verbatim_item() + '}';
234 else if (t.cat() == catComment)
237 else if (t.cs() == "(") {
239 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
243 else if (t.cs() == "[") {
245 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
249 else if (t.cs() == "begin") {
250 string const name = p.getArg('{', '}');
251 active_environments.push_back(name);
252 os << "\\begin{" << name << '}';
253 if (is_math_env(name)) {
254 parse_math(p, os, FLAG_END, MATH_MODE);
256 parse_table(p, os, FLAG_END);
258 os << "\\end{" << name << '}';
259 active_environments.pop_back();
262 else if (t.cs() == "end") {
263 if (flags & FLAG_END) {
264 // eat environment name
265 string const name = p.getArg('{', '}');
266 if (name != active_environment())
267 p.error("\\end{" + name + "} does not match \\begin{"
268 + active_environment() + "}");
271 p.error("found 'end' unexpectedly");
280 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
283 for (size_t col = 0; col < ci.size(); ++col)
284 ci[col].topline = true;
288 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
290 ri.bottomline = true;
291 for (size_t col = 0; col < ci.size(); ++col)
292 ci[col].bottomline = true;
296 void handle_tabular(Parser & p, ostream & os,
299 string posopts = p.getOpt();
301 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
303 vector<ColInfo> colinfo;
305 // handle column formatting
306 handle_colalign(p, colinfo);
308 // handle initial hlines
310 // first scan of cells
311 // use table mode to keep it minimal-invasive
312 // not exactly what's TeX doing...
313 vector<string> lines;
315 ss << read_hlines(p) << HLINE; // handle initial hlines
316 parse_table(p, ss, FLAG_END);
317 split(ss.str(), lines, LINE);
319 vector< vector<CellInfo> > cellinfo(lines.size());
320 vector<RowInfo> rowinfo(lines.size());
323 //cerr << "// split into rows\n";
324 for (size_t row = 0; row < rowinfo.size(); ++row) {
327 cellinfo[row].resize(colinfo.size());
330 vector<string> dummy;
331 //cerr << "\n########### LINE: " << lines[row] << "########\n";
332 split(lines[row], dummy, HLINE);
334 // handle horizontal line fragments
335 if (dummy.size() != 3) {
336 if (dummy.size() != 1)
337 cerr << "unexpected dummy size: " << dummy.size()
338 << " content: " << lines[row] << "\n";
341 lines[row] = dummy[1];
343 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
344 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
345 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
347 for (int i = 0; i <= 2; i += 2) {
348 //cerr << " reading from line string '" << dummy[i] << "'\n";
351 Token t = p1.get_token();
352 //cerr << "read token: " << t << "\n";
353 if (t.cs() == "hline") {
355 if (rowinfo[row].topline) {
356 if (row > 0) // extra bottomline above
357 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
359 cerr << "dropping extra hline\n";
360 //cerr << "below row: " << row-1 << endl;
362 handle_hline_above(rowinfo[row], cellinfo[row]);
363 //cerr << "above row: " << row << endl;
366 //cerr << "below row: " << row << endl;
367 handle_hline_below(rowinfo[row], cellinfo[row]);
369 } else if (t.cs() == "cline") {
370 string arg = p1.verbatim_item();
371 //cerr << "read cline arg: '" << arg << "'\n";
375 size_t from = string2int(t[0]) - 1;
376 size_t to = string2int(t[1]);
377 for (size_t col = from; col < to; ++col) {
378 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
380 rowinfo[row].topline = true;
381 cellinfo[row][col].topline = true;
383 rowinfo[row].bottomline = true;
384 cellinfo[row][col].bottomline = true;
388 cerr << "unexpected line token: " << t << endl;
394 vector<string> cells;
395 split(lines[row], cells, TAB);
396 // Has the last multicolumn cell a rightline?
397 bool last_rightline = false;
398 for (size_t col = 0, cell = 0;
399 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
400 //cerr << "cell content: '" << cells[cell] << "'\n";
401 Parser p(cells[cell]);
403 //cells[cell] << "'\n";
404 if (p.next_token().cs() == "multicolumn") {
407 size_t const ncells = string2int(p.verbatim_item());
409 // special cell properties alignment
411 handle_colalign(p, t);
412 cellinfo[row][col].multi = 1;
413 cellinfo[row][col].align = t.front().align;
415 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
416 cellinfo[row][col].content = os.str();
418 // multicolumn cells are tricky: This
419 // \multicolumn{2}{|c|}{col1-2}&
420 // \multicolumn{2}{|c|}{col3-4}\\
421 // gives | col1-2 | col3-4 | and not
422 // | col1-2 || col3-4 |
424 if (last_rightline && t.front().leftline) {
425 t.front().leftline = false;
427 last_rightline = t.front().rightline;
429 // multicolumn lines override normal cell lines
430 cellinfo[row][col].leftline = t.front().leftline;
431 cellinfo[row][col].rightline = t.front().rightline;
433 // add dummy cells for multicol
434 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
436 cellinfo[row][col].multi = 2;
437 cellinfo[row][col].align = 'c';
440 // more than one line on the right?
441 if (t.front().rightline > 1)
442 cellinfo[row][col + 1].leftline = true;
445 // FLAG_END is a hack, we need to read all of it
446 cellinfo[row][col].leftline = colinfo[col].leftline;
447 cellinfo[row][col].rightline = colinfo[col].rightline;
448 cellinfo[row][col].align = colinfo[col].align;
450 parse_text_in_inset(p, os, FLAG_CELL, false, context);
451 cellinfo[row][col].content = os.str();
452 last_rightline = false;
456 //cerr << "// handle almost empty last row what we have\n";
457 // handle almost empty last row
458 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
459 //cerr << "remove empty last line\n";
460 if (rowinfo[row].topline)
461 rowinfo[row - 1].bottomline = true;
462 for (size_t col = 0; col < colinfo.size(); ++col)
463 if (cellinfo[row][col].topline)
464 cellinfo[row - 1][col].bottomline = true;
470 //cerr << "// output what we have\n";
471 // output what we have
472 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
473 << "\" columns=\"" << colinfo.size() << "\">\n"
476 //cerr << "// after header\n";
477 for (size_t col = 0; col < colinfo.size(); ++col) {
478 os << "<column alignment=\""
479 << verbose_align(colinfo[col].align) << "\"";
480 os << " valignment=\"top\"";
481 if (colinfo[col].leftline)
482 os << " leftline=\"true\"";
483 if (colinfo[col].rightline)
484 os << " rightline=\"true\"";
485 if (colinfo[col].width.size())
486 os << " width=\"" << colinfo[col].width << "\"";
487 if (colinfo[col].special.size())
488 os << " special=\"" << colinfo[col].special << "\"";
491 //cerr << "// after cols\n";
493 for (size_t row = 0; row < rowinfo.size(); ++row) {
495 if (rowinfo[row].topline)
496 os << " topline=\"true\"";
497 if (rowinfo[row].bottomline)
498 os << " bottomline=\"true\"";
500 for (size_t col = 0; col < colinfo.size(); ++col) {
501 CellInfo const & cell = cellinfo[row][col];
504 os << " multicolumn=\"" << cell.multi << "\"";
505 os << " alignment=\"" << verbose_align(cell.align)
507 << " valignment=\"top\"";
509 os << " topline=\"true\"";
511 os << " bottomline=\"true\"";
513 os << " leftline=\"true\"";
515 os << " rightline=\"true\"";
516 //cerr << "\nrow: " << row << " col: " << col;
518 // cerr << " topline=\"true\"";
519 //if (cell.bottomline)
520 // cerr << " bottomline=\"true\"";
521 os << " usebox=\"none\""
523 << "\n\\begin_inset Text\n"
525 << "\n\\end_inset \n"
531 os << "</lyxtabular>\n";