]> git.lyx.org Git - features.git/blob - src/mathed/math_hullinset.C
re-enable ams "autodetection" (I still think it is wrong...)
[features.git] / src / mathed / math_hullinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
11 #include "debug.h"
12 #include "frontends/Painter.h"
13 #include "textpainter.h"
14 #include "Lsstream.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
17
18 #include <vector>
19
20 using std::vector;
21 using std::max;
22 using std::endl;
23
24 namespace {
25
26         int getCols(MathInsetTypes type)
27         {
28                 switch (type) {
29                         case LM_OT_EQNARRAY:
30                                 return 3;
31                         case LM_OT_ALIGN:
32                         case LM_OT_ALIGNAT:
33                         case LM_OT_XALIGNAT:
34                         case LM_OT_XXALIGNAT:
35                                 return 2;
36                         default:;
37                 }
38                 return 1;
39         }
40
41
42         // returns position of first relation operator in the array
43         // used for "intelligent splitting"
44         MathArray::size_type firstRelOp(MathArray const & ar)
45         {
46                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
47                         if ((*it)->isRelOp())
48                                 return it - ar.begin();
49                 return ar.size();
50         }
51
52
53         char const * star(bool numbered)
54         {
55                 return numbered ? "" : "*";
56         }
57
58         MathInsetTypes typecode(string const & s)
59         {
60                 if (s == "none")      return LM_OT_NONE;
61                 if (s == "equation")  return LM_OT_EQUATION;
62                 if (s == "display")   return LM_OT_EQUATION;
63                 if (s == "eqnarray")  return LM_OT_EQNARRAY;
64                 if (s == "align")     return LM_OT_ALIGN;
65                 if (s == "alignat")   return LM_OT_ALIGNAT;
66                 if (s == "xalignat")  return LM_OT_XALIGNAT;
67                 if (s == "xxalignat") return LM_OT_XXALIGNAT;
68                 if (s == "multline")  return LM_OT_MULTLINE;
69                 if (s == "gather")    return LM_OT_GATHER;
70                 return LM_OT_SIMPLE;
71         }
72
73
74         string normalName(MathInsetTypes t)
75         {
76                 switch (t) {
77                         case LM_OT_EQUATION:  return "equation";
78                         case LM_OT_EQNARRAY:  return "eqnarray";
79                         case LM_OT_ALIGN:     return "align";
80                         case LM_OT_ALIGNAT:   return "alignat";
81                         case LM_OT_XALIGNAT:  return "xalignat";
82                         case LM_OT_XXALIGNAT: return "xxalignat";
83                         case LM_OT_MULTLINE:  return "multline";
84                         case LM_OT_GATHER:    return "gather";
85                         case LM_OT_SIMPLE:    return "simple";
86                         default: break;
87                 }
88                 return "unknown";
89         }
90
91 } // end anon namespace
92
93
94 MathHullInset::MathHullInset()
95         : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
96 {
97         setDefaults();
98 }
99
100
101 MathHullInset::MathHullInset(MathInsetTypes t)
102         : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
103 {
104         setDefaults();
105 }
106
107
108 MathHullInset::MathHullInset(MathInsetTypes t, MathGridInset const & grid)
109         : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
110 {
111         setDefaults();
112 }
113
114
115 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
116         : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
117 {
118         setDefaults();
119 }
120
121
122 MathInset * MathHullInset::clone() const
123 {
124         return new MathHullInset(*this);
125 }
126
127
128 char MathHullInset::defaultColAlign(col_type col)
129 {
130         switch (getType()) {
131                 case LM_OT_ALIGN:
132                 case LM_OT_ALIGNAT:
133                 case LM_OT_XALIGNAT:
134                 case LM_OT_XXALIGNAT:
135                         return "rl"[col & 1];
136                 case LM_OT_EQNARRAY:
137                         return "rcl"[col];
138                 default:;
139         }
140         return 'c';
141 }
142
143
144 int MathHullInset::defaultColSpace(col_type col)
145 {
146         switch (getType()) {
147                 case LM_OT_ALIGN:
148                 case LM_OT_ALIGNAT:
149                         return 0;
150                 case LM_OT_XALIGNAT:
151                         return (col & 1) ? 20 : 0;
152                 case LM_OT_XXALIGNAT:
153                         return (col & 1) ? 40 : 0;
154                 default:;
155         }
156         return 0;
157 }
158
159
160 void MathHullInset::metrics(MathMetricsInfo & mi) const
161 {
162         // let the cells adjust themselves
163         MathGridInset::metrics(mi);
164
165         if (display()) {
166                 ascent_  += 12;
167                 descent_ += 12;
168         }
169
170         mi_ = mi;
171         mi_.base.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
172
173         if (numberedType()) {
174                 //augmentFont(mi_.base.font, "mathbf");
175                 int l = 0;
176                 for (row_type row = 0; row < nrows(); ++row)
177                         l = max(l, mathed_string_width(mi_.base.font, nicelabel(row)));
178
179                 if (l)
180                         width_ += 30 + l;
181         }
182
183         // make it at least as high as the current font
184         int asc = 0;
185         int des = 0;
186         math_font_max_dim(mi_.base.font, asc, des);
187         ascent_  = max(ascent_,  asc);
188         descent_ = max(descent_, des);
189 }
190
191
192 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
193 {
194         MathGridInset::draw(pi, x, y);
195
196         if (numberedType()) {
197                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
198                 for (row_type row = 0; row < nrows(); ++row) {
199                         int const yy = y + rowinfo_[row].offset_;
200                         drawStrBlack(pi, xx, yy, nicelabel(row));
201                 }
202         }
203 }
204
205
206 void MathHullInset::metricsT(TextMetricsInfo const &) const
207 {
208 #if 0
209         if (display()) {
210                 MathGridInset::metricsT(mi);
211         } else
212 #endif
213         {
214                 ostringstream os;
215                 WriteStream wi(os, false, true);
216                 write(wi);
217                 width_   = os.str().size();
218                 ascent_  = 1;
219                 descent_ = 0;
220         }
221 }
222
223
224 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
225 {
226 #if 0
227         if (display()) {
228                 MathGridInset::drawT(pain, x, y);
229         } else
230 #endif
231         {
232                 ostringstream os;
233                 WriteStream wi(os, false, true);
234                 write(wi);
235                 pain.draw(x, y, os.str().c_str());
236         }
237 }
238
239
240 string MathHullInset::label(row_type row) const
241 {
242         row_type n = nrows();
243         lyx::Assert(row < n);
244         return label_[row];
245 }
246
247
248 void MathHullInset::label(row_type row, string const & label)
249 {
250         label_[row] = label;
251 }
252
253
254 void MathHullInset::numbered(row_type row, bool num)
255 {
256         nonum_[row] = !num;
257 }
258
259
260 bool MathHullInset::numbered(row_type row) const
261 {
262         return !nonum_[row];
263 }
264
265
266 bool MathHullInset::ams() const
267 {
268         return
269                 objtype_ == LM_OT_ALIGN ||
270                 objtype_ == LM_OT_MULTLINE ||
271                 objtype_ == LM_OT_GATHER ||
272                 objtype_ == LM_OT_ALIGNAT ||
273                 objtype_ == LM_OT_XALIGNAT ||
274                 objtype_ == LM_OT_XXALIGNAT;
275 }
276
277
278 bool MathHullInset::display() const
279 {
280         return getType() != LM_OT_SIMPLE;
281 }
282
283
284 vector<string> const MathHullInset::getLabelList() const
285 {
286         vector<string> res;
287         for (row_type row = 0; row < nrows(); ++row)
288                 if (!label_[row].empty() && nonum_[row] != 1)
289                         res.push_back(label_[row]);
290         return res;
291 }
292
293
294 bool MathHullInset::numberedType() const
295 {
296         if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
297                 return false;
298         for (row_type row = 0; row < nrows(); ++row)
299                 if (!nonum_[row])
300                         return true;
301         return false;
302 }
303
304
305 void MathHullInset::validate(LaTeXFeatures & features) const
306 {
307         if (ams())
308                 features.require("amsmath");
309
310
311         // Validation is necessary only if not using AMS math.
312         // To be safe, we will always run mathedvalidate.
313         //if (features.amsstyle)
314         //  return;
315
316         features.require("boldsymbol");
317         //features.binom      = true;
318
319         MathNestInset::validate(features);
320 }
321
322
323 void MathHullInset::header_write(WriteStream & os) const
324 {
325         bool n = numberedType();
326
327         switch (getType()) {
328                 case LM_OT_SIMPLE:
329                         os << '$';
330                         if (cell(0).empty())
331                                 os << ' ';
332                         break;
333
334                 case LM_OT_EQUATION:
335                         if (n)
336                                 os << "\\begin{equation" << star(n) << "}\n";
337                         else
338                                 os << "\\[\n";
339                         break;
340
341                 case LM_OT_EQNARRAY:
342                         os << "\\begin{eqnarray" << star(n) << "}\n";
343                         break;
344
345                 case LM_OT_ALIGN:
346                         os << "\\begin{align" << star(n) << "}\n";
347                         break;
348
349                 case LM_OT_ALIGNAT:
350                         os << "\\begin{alignat" << star(n) << "}"
351                           << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
352                         break;
353
354                 case LM_OT_XALIGNAT:
355                         os << "\\begin{xalignat" << star(n) << "}"
356                            << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
357                         break;
358
359                 case LM_OT_XXALIGNAT:
360                         os << "\\begin{xxalignat}"
361                            << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
362                         break;
363
364                 case LM_OT_MULTLINE:
365                         os << "\\begin{multline}\n";
366                         break;
367
368                 case LM_OT_GATHER:
369                         os << "\\begin{gather}\n";
370                         break;
371
372                 case LM_OT_NONE:
373                         break;
374
375                 default:
376                         os << "\\begin{unknown" << star(n) << "}";
377         }
378 }
379
380
381 void MathHullInset::footer_write(WriteStream & os) const
382 {
383         bool n = numberedType();
384
385         switch (getType()) {
386                 case LM_OT_SIMPLE:
387                         os << '$';
388                         break;
389
390                 case LM_OT_EQUATION:
391                         if (n)
392                                 os << "\\end{equation" << star(n) << "}\n";
393                         else
394                                 os << "\\]\n";
395                         break;
396
397                 case LM_OT_EQNARRAY:
398                         os << "\n\\end{eqnarray" << star(n) << "}\n";
399                         break;
400
401                 case LM_OT_ALIGN:
402                         os << "\n\\end{align" << star(n) << "}\n";
403                         break;
404
405                 case LM_OT_ALIGNAT:
406                         os << "\n\\end{alignat" << star(n) << "}\n";
407                         break;
408
409                 case LM_OT_XALIGNAT:
410                         os << "\n\\end{xalignat" << star(n) << "}\n";
411                         break;
412
413                 case LM_OT_XXALIGNAT:
414                         os << "\n\\end{xxalignat}\n";
415                         break;
416
417                 case LM_OT_MULTLINE:
418                         os << "\n\\end{multline}\n";
419                         break;
420
421                 case LM_OT_GATHER:
422                         os << "\n\\end{gather}\n";
423                         break;
424
425                 case LM_OT_NONE:
426                         os << "\n";
427                         break;
428
429                 default:
430                         os << "\\end{unknown" << star(n) << "}";
431         }
432 }
433
434
435 void MathHullInset::addRow(row_type row)
436 {
437         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
438         label_.insert(label_.begin() + row + 1, string());
439         MathGridInset::addRow(row);
440 }
441
442
443 void MathHullInset::appendRow()
444 {
445         nonum_.push_back(!numberedType());
446         label_.push_back(string());
447         MathGridInset::appendRow();
448 }
449
450
451 void MathHullInset::delRow(row_type row)
452 {
453         MathGridInset::delRow(row);
454         nonum_.erase(nonum_.begin() + row);
455         label_.erase(label_.begin() + row);
456 }
457
458
459 void MathHullInset::addCol(col_type col)
460 {
461         switch (getType()) {
462                 case LM_OT_EQUATION:
463                         mutate(LM_OT_EQNARRAY);
464                         break;
465
466                 case LM_OT_EQNARRAY:
467                         mutate(LM_OT_ALIGN);
468                         addCol(col);
469                         break;
470
471                 case LM_OT_ALIGN:
472                         mutate(LM_OT_ALIGNAT);
473                         addCol(col);
474                         break;
475
476                 case LM_OT_ALIGNAT:
477                 case LM_OT_XALIGNAT:
478                 case LM_OT_XXALIGNAT:
479                         MathGridInset::addCol(col);
480                         MathGridInset::addCol(col + 1);
481                         break;
482
483                 default:
484                         break;
485         }
486 }
487
488
489 void MathHullInset::delCol(col_type col)
490 {
491         switch (getType()) {
492                 case LM_OT_ALIGNAT:
493                 case LM_OT_XALIGNAT:
494                 case LM_OT_XXALIGNAT:
495                         MathGridInset::delCol(col + 1);
496                         MathGridInset::delCol(col);
497                         break;
498                 default:
499                         break;
500         }
501 }
502
503
504 string MathHullInset::nicelabel(row_type row) const
505 {
506         if (nonum_[row])
507                 return string();
508         if (label_[row].empty())
509                 return string("(#)");
510         return "(" + label_[row] + ")";
511 }
512
513
514 void MathHullInset::mutate(string const & newtype)
515 {
516         if (newtype == "dump") {
517                 dump();
518                 return;
519         }
520         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
521         mutate(typecode(newtype));
522 }
523
524
525 void MathHullInset::glueall()
526 {
527         MathArray ar;
528         for (idx_type i = 0; i < nargs(); ++i)
529                 ar.push_back(cell(i));
530         *this = MathHullInset(LM_OT_SIMPLE);
531         cell(0) = ar;
532 }
533
534
535 MathInsetTypes MathHullInset::getType() const
536 {
537         return objtype_;
538 }
539
540
541 void MathHullInset::setType(MathInsetTypes t)
542 {
543         objtype_ = t;
544         setDefaults();
545 }
546
547
548
549 void MathHullInset::mutate(MathInsetTypes newtype)
550 {
551         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
552
553         if (newtype == getType())
554                 return;
555
556         switch (getType()) {
557                 case LM_OT_SIMPLE:
558                         setType(LM_OT_EQUATION);
559                         numbered(0, false);
560                         mutate(newtype);
561                         break;
562
563                 case LM_OT_EQUATION:
564                         switch (newtype) {
565                                 case LM_OT_SIMPLE:
566                                         setType(LM_OT_SIMPLE);
567                                         break;
568
569                                 case LM_OT_ALIGN:
570                                 case LM_OT_ALIGNAT:
571                                 case LM_OT_XALIGNAT:
572                                 case LM_OT_XXALIGNAT: {
573
574                                         MathGridInset::addCol(1);
575
576                                         // split it "nicely"
577                                         pos_type pos = firstRelOp(cell(0));
578                                         cell(1) = cell(0);
579                                         cell(0).erase(pos, cell(0).size());
580                                         cell(1).erase(0, pos);
581                                         setType(LM_OT_ALIGN);
582                                         mutate(newtype);
583                                         break;
584                                 }
585
586                                 case LM_OT_EQNARRAY:
587                                 default:
588                                         MathGridInset::addCol(1);
589                                         MathGridInset::addCol(1);
590
591                                         // split it "nicely" on the firest relop
592                                         pos_type pos = firstRelOp(cell(0));
593                                         cell(1) = MathArray(cell(0), pos, cell(0).size());
594                                         cell(0).erase(pos, cell(0).size());
595
596                                         if (cell(1).size()) {
597                                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
598                                                 cell(1).erase(1, cell(1).size());
599                                         }
600
601                                         setType(LM_OT_EQNARRAY);
602                                         mutate(newtype);
603                                         break;
604                                 }
605                         break;
606
607                 case LM_OT_EQNARRAY:
608                         switch (newtype) {
609                                 case LM_OT_SIMPLE:
610                                 case LM_OT_EQUATION: {
611                                         // set correct (no)numbering
612                                         bool allnonum = true;
613                                         for (row_type row = 0; row < nrows(); ++row) {
614                                                 if (!nonum_[row])
615                                                         allnonum = false;
616                                         }
617
618                                         // set first non-empty label
619                                         string label;
620                                         for (row_type row = 0; row < nrows(); ++row) {
621                                                 if (!label_[row].empty()) {
622                                                         label = label_[row];
623                                                         break;
624                                                 }
625                                         }
626
627                                         glueall();
628
629                                         nonum_[0] = allnonum;
630                                         label_[0] = label;
631                                         mutate(newtype);
632                                         break;
633                                 }
634
635                                 case LM_OT_ALIGN:
636                                 case LM_OT_ALIGNAT:
637                                 case LM_OT_XALIGNAT:
638                                 case LM_OT_XXALIGNAT:
639                                 default: {
640                                         for (row_type row = 0; row < nrows(); ++row) {
641                                                 idx_type c = 3 * row + 1;
642                                                 cell(c).push_back(cell(c + 1));
643                                         }
644                                         MathGridInset::delCol(2);
645                                         setType(LM_OT_ALIGN);
646                                         mutate(newtype);
647                                         break;
648                                 }
649                         }
650                         break;
651
652                 case LM_OT_ALIGN:
653                         switch (newtype) {
654                                 case LM_OT_SIMPLE:
655                                 case LM_OT_EQUATION:
656                                 case LM_OT_EQNARRAY:
657                                         MathGridInset::addCol(1);
658                                         setType(LM_OT_EQNARRAY);
659                                         mutate(newtype);
660                                         break;
661
662                                 case LM_OT_ALIGNAT:
663                                 case LM_OT_XALIGNAT:
664                                 case LM_OT_XXALIGNAT:
665                                         setType(newtype);
666                                         break;
667
668                                 default:
669                                         lyxerr << "mutation from '" << getType()
670                                                 << "' to '" << newtype << "' not implemented"
671                                                << endl;
672                                         break;
673                         }
674                         break;
675
676                 case LM_OT_MULTLINE:
677                         switch (newtype) {
678                                 case LM_OT_GATHER:
679                                         setType(LM_OT_GATHER);
680                                         break;
681                                 default:
682                                         lyxerr << "mutation from '" << getType()
683                                                 << "' to '" << newtype << "' not implemented"
684                                                << endl;
685                                         break;
686                         }
687
688                 case LM_OT_GATHER:
689                         switch (newtype) {
690                                 case LM_OT_MULTLINE:
691                                         setType(LM_OT_MULTLINE);
692                                         break;
693                                 default:
694                                         lyxerr << "mutation from '" << getType()
695                                                 << "' to '" << newtype << "' not implemented"
696                                                << endl;
697                                         break;
698                         }
699
700                 default:
701                         lyxerr << "mutation from '" << getType()
702                                << "' to '" << newtype << "' not implemented"
703                                << endl;
704                         break;
705         }
706 }
707
708
709 void MathHullInset::write(WriteStream & os) const
710 {
711         header_write(os);
712
713         bool n = numberedType();
714
715         for (row_type row = 0; row < nrows(); ++row) {
716                 for (col_type col = 0; col < ncols(); ++col)
717                         os << cell(index(row, col)) << eocString(col);
718                 if (n) {
719                         if (!label_[row].empty())
720                                 os << "\\label{" << label_[row] << "}";
721                         if (nonum_[row])
722                                 os << "\\nonumber ";
723                 }
724                 os << eolString(row);
725         }
726
727         footer_write(os);
728 }
729
730
731 void MathHullInset::normalize(NormalStream & os) const
732 {
733         os << "[formula " << normalName(getType()) << " ";
734         MathGridInset::normalize(os);
735         os << "] ";
736 }
737
738
739 void MathHullInset::mathmlize(MathMLStream & os) const
740 {
741         MathGridInset::mathmlize(os);
742 }
743
744
745 void MathHullInset::infoize(std::ostream & os) const
746 {
747         os << normalName(getType());
748 }
749
750 void MathHullInset::check() const
751 {
752         lyx::Assert(nonum_.size() == nrows());
753         lyx::Assert(label_.size() == nrows());
754 }