]> git.lyx.org Git - features.git/blob - src/mathed/math_hullinset.C
fonts as insets
[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 true;
269 /*
270         return
271                 objtype_ == LM_OT_ALIGN ||
272                 objtype_ == LM_OT_MULTLINE ||
273                 objtype_ == LM_OT_GATHER ||
274                 objtype_ == LM_OT_ALIGNAT ||
275                 objtype_ == LM_OT_XALIGNAT ||
276                 objtype_ == LM_OT_XXALIGNAT;
277 */
278 }
279
280
281 bool MathHullInset::display() const
282 {
283         return getType() != LM_OT_SIMPLE;
284 }
285
286
287 vector<string> const MathHullInset::getLabelList() const
288 {
289         vector<string> res;
290         for (row_type row = 0; row < nrows(); ++row)
291                 if (!label_[row].empty() && nonum_[row] != 1)
292                         res.push_back(label_[row]);
293         return res;
294 }
295
296
297 bool MathHullInset::numberedType() const
298 {
299         if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
300                 return false;
301         for (row_type row = 0; row < nrows(); ++row)
302                 if (!nonum_[row])
303                         return true;
304         return false;
305 }
306
307
308 void MathHullInset::validate(LaTeXFeatures & features) const
309 {
310         if (ams())
311                 features.require("amsmath");
312
313
314         // Validation is necessary only if not using AMS math.
315         // To be safe, we will always run mathedvalidate.
316         //if (features.amsstyle)
317         //  return;
318
319         features.require("boldsymbol");
320         //features.binom      = true;
321
322         MathNestInset::validate(features);
323 }
324
325
326 void MathHullInset::header_write(WriteStream & os) const
327 {
328         bool n = numberedType();
329
330         switch (getType()) {
331                 case LM_OT_SIMPLE:
332                         os << '$';
333                         if (cell(0).empty())
334                                 os << ' ';
335                         break;
336
337                 case LM_OT_EQUATION:
338                         if (n)
339                                 os << "\\begin{equation" << star(n) << "}\n";
340                         else
341                                 os << "\\[\n";
342                         break;
343
344                 case LM_OT_EQNARRAY:
345                         os << "\\begin{eqnarray" << star(n) << "}\n";
346                         break;
347
348                 case LM_OT_ALIGN:
349                         os << "\\begin{align" << star(n) << "}\n";
350                         break;
351
352                 case LM_OT_ALIGNAT:
353                         os << "\\begin{alignat" << star(n) << "}"
354                           << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
355                         break;
356
357                 case LM_OT_XALIGNAT:
358                         os << "\\begin{xalignat" << star(n) << "}"
359                            << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
360                         break;
361
362                 case LM_OT_XXALIGNAT:
363                         os << "\\begin{xxalignat}"
364                            << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
365                         break;
366
367                 case LM_OT_MULTLINE:
368                         os << "\\begin{multline}\n";
369                         break;
370
371                 case LM_OT_GATHER:
372                         os << "\\begin{gather}\n";
373                         break;
374
375                 case LM_OT_NONE:
376                         break;
377
378                 default:
379                         os << "\\begin{unknown" << star(n) << "}";
380         }
381 }
382
383
384 void MathHullInset::footer_write(WriteStream & os) const
385 {
386         bool n = numberedType();
387
388         switch (getType()) {
389                 case LM_OT_SIMPLE:
390                         os << '$';
391                         break;
392
393                 case LM_OT_EQUATION:
394                         if (n)
395                                 os << "\\end{equation" << star(n) << "}\n";
396                         else
397                                 os << "\\]\n";
398                         break;
399
400                 case LM_OT_EQNARRAY:
401                         os << "\n\\end{eqnarray" << star(n) << "}\n";
402                         break;
403
404                 case LM_OT_ALIGN:
405                         os << "\n\\end{align" << star(n) << "}\n";
406                         break;
407
408                 case LM_OT_ALIGNAT:
409                         os << "\n\\end{alignat" << star(n) << "}\n";
410                         break;
411
412                 case LM_OT_XALIGNAT:
413                         os << "\n\\end{xalignat" << star(n) << "}\n";
414                         break;
415
416                 case LM_OT_XXALIGNAT:
417                         os << "\n\\end{xxalignat}\n";
418                         break;
419
420                 case LM_OT_MULTLINE:
421                         os << "\n\\end{multline}\n";
422                         break;
423
424                 case LM_OT_GATHER:
425                         os << "\n\\end{gather}\n";
426                         break;
427
428                 case LM_OT_NONE:
429                         os << "\n";
430                         break;
431
432                 default:
433                         os << "\\end{unknown" << star(n) << "}";
434         }
435 }
436
437
438 void MathHullInset::addRow(row_type row)
439 {
440         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
441         label_.insert(label_.begin() + row + 1, string());
442         MathGridInset::addRow(row);
443 }
444
445
446 void MathHullInset::appendRow()
447 {
448         nonum_.push_back(!numberedType());
449         label_.push_back(string());
450         MathGridInset::appendRow();
451 }
452
453
454 void MathHullInset::delRow(row_type row)
455 {
456         MathGridInset::delRow(row);
457         nonum_.erase(nonum_.begin() + row);
458         label_.erase(label_.begin() + row);
459 }
460
461
462 void MathHullInset::addCol(col_type col)
463 {
464         switch (getType()) {
465                 case LM_OT_EQUATION:
466                         mutate(LM_OT_EQNARRAY);
467                         break;
468
469                 case LM_OT_EQNARRAY:
470                         mutate(LM_OT_ALIGN);
471                         addCol(col);
472                         break;
473
474                 case LM_OT_ALIGN:
475                         mutate(LM_OT_ALIGNAT);
476                         addCol(col);
477                         break;
478
479                 case LM_OT_ALIGNAT:
480                 case LM_OT_XALIGNAT:
481                 case LM_OT_XXALIGNAT:
482                         MathGridInset::addCol(col);
483                         MathGridInset::addCol(col + 1);
484                         break;
485
486                 default:
487                         break;
488         }
489 }
490
491
492 void MathHullInset::delCol(col_type col)
493 {
494         switch (getType()) {
495                 case LM_OT_ALIGNAT:
496                 case LM_OT_XALIGNAT:
497                 case LM_OT_XXALIGNAT:
498                         MathGridInset::delCol(col + 1);
499                         MathGridInset::delCol(col);
500                         break;
501                 default:
502                         break;
503         }
504 }
505
506
507 string MathHullInset::nicelabel(row_type row) const
508 {
509         if (nonum_[row])
510                 return string();
511         if (label_[row].empty())
512                 return string("(#)");
513         return "(" + label_[row] + ")";
514 }
515
516
517 void MathHullInset::mutate(string const & newtype)
518 {
519         if (newtype == "dump") {
520                 dump();
521                 return;
522         }
523         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
524         mutate(typecode(newtype));
525 }
526
527
528 void MathHullInset::glueall()
529 {
530         MathArray ar;
531         for (idx_type i = 0; i < nargs(); ++i)
532                 ar.push_back(cell(i));
533         *this = MathHullInset(LM_OT_SIMPLE);
534         cell(0) = ar;
535 }
536
537
538 MathInsetTypes MathHullInset::getType() const
539 {
540         return objtype_;
541 }
542
543
544 void MathHullInset::setType(MathInsetTypes t)
545 {
546         objtype_ = t;
547         setDefaults();
548 }
549
550
551
552 void MathHullInset::mutate(MathInsetTypes newtype)
553 {
554         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
555
556         if (newtype == getType())
557                 return;
558
559         switch (getType()) {
560                 case LM_OT_SIMPLE:
561                         setType(LM_OT_EQUATION);
562                         numbered(0, false);
563                         mutate(newtype);
564                         break;
565
566                 case LM_OT_EQUATION:
567                         switch (newtype) {
568                                 case LM_OT_SIMPLE:
569                                         setType(LM_OT_SIMPLE);
570                                         break;
571
572                                 case LM_OT_ALIGN:
573                                 case LM_OT_ALIGNAT:
574                                 case LM_OT_XALIGNAT:
575                                 case LM_OT_XXALIGNAT: {
576
577                                         MathGridInset::addCol(1);
578
579                                         // split it "nicely"
580                                         pos_type pos = firstRelOp(cell(0));
581                                         cell(1) = cell(0);
582                                         cell(0).erase(pos, cell(0).size());
583                                         cell(1).erase(0, pos);
584                                         setType(LM_OT_ALIGN);
585                                         mutate(newtype);
586                                         break;
587                                 }
588
589                                 case LM_OT_EQNARRAY:
590                                 default:
591                                         MathGridInset::addCol(1);
592                                         MathGridInset::addCol(1);
593
594                                         // split it "nicely" on the firest relop
595                                         pos_type pos = firstRelOp(cell(0));
596                                         cell(1) = MathArray(cell(0), pos, cell(0).size());
597                                         cell(0).erase(pos, cell(0).size());
598
599                                         if (cell(1).size()) {
600                                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
601                                                 cell(1).erase(1, cell(1).size());
602                                         }
603
604                                         setType(LM_OT_EQNARRAY);
605                                         mutate(newtype);
606                                         break;
607                                 }
608                         break;
609
610                 case LM_OT_EQNARRAY:
611                         switch (newtype) {
612                                 case LM_OT_SIMPLE:
613                                 case LM_OT_EQUATION: {
614                                         // set correct (no)numbering
615                                         bool allnonum = true;
616                                         for (row_type row = 0; row < nrows(); ++row) {
617                                                 if (!nonum_[row])
618                                                         allnonum = false;
619                                         }
620
621                                         // set first non-empty label
622                                         string label;
623                                         for (row_type row = 0; row < nrows(); ++row) {
624                                                 if (!label_[row].empty()) {
625                                                         label = label_[row];
626                                                         break;
627                                                 }
628                                         }
629
630                                         glueall();
631
632                                         nonum_[0] = allnonum;
633                                         label_[0] = label;
634                                         mutate(newtype);
635                                         break;
636                                 }
637
638                                 case LM_OT_ALIGN:
639                                 case LM_OT_ALIGNAT:
640                                 case LM_OT_XALIGNAT:
641                                 case LM_OT_XXALIGNAT:
642                                 default: {
643                                         for (row_type row = 0; row < nrows(); ++row) {
644                                                 idx_type c = 3 * row + 1;
645                                                 cell(c).push_back(cell(c + 1));
646                                         }
647                                         MathGridInset::delCol(2);
648                                         setType(LM_OT_ALIGN);
649                                         mutate(newtype);
650                                         break;
651                                 }
652                         }
653                         break;
654
655                 case LM_OT_ALIGN:
656                         switch (newtype) {
657                                 case LM_OT_SIMPLE:
658                                 case LM_OT_EQUATION:
659                                 case LM_OT_EQNARRAY:
660                                         MathGridInset::addCol(1);
661                                         setType(LM_OT_EQNARRAY);
662                                         mutate(newtype);
663                                         break;
664
665                                 case LM_OT_ALIGNAT:
666                                 case LM_OT_XALIGNAT:
667                                 case LM_OT_XXALIGNAT:
668                                         setType(newtype);
669                                         break;
670
671                                 default:
672                                         lyxerr << "mutation from '" << getType()
673                                                 << "' to '" << newtype << "' not implemented"
674                                                << endl;
675                                         break;
676                         }
677                         break;
678
679                 case LM_OT_MULTLINE:
680                         switch (newtype) {
681                                 case LM_OT_GATHER:
682                                         setType(LM_OT_GATHER);
683                                         break;
684                                 default:
685                                         lyxerr << "mutation from '" << getType()
686                                                 << "' to '" << newtype << "' not implemented"
687                                                << endl;
688                                         break;
689                         }
690
691                 case LM_OT_GATHER:
692                         switch (newtype) {
693                                 case LM_OT_MULTLINE:
694                                         setType(LM_OT_MULTLINE);
695                                         break;
696                                 default:
697                                         lyxerr << "mutation from '" << getType()
698                                                 << "' to '" << newtype << "' not implemented"
699                                                << endl;
700                                         break;
701                         }
702
703                 default:
704                         lyxerr << "mutation from '" << getType()
705                                << "' to '" << newtype << "' not implemented"
706                                << endl;
707                         break;
708         }
709 }
710
711
712 void MathHullInset::write(WriteStream & os) const
713 {
714         header_write(os);
715
716         bool n = numberedType();
717
718         for (row_type row = 0; row < nrows(); ++row) {
719                 for (col_type col = 0; col < ncols(); ++col)
720                         os << cell(index(row, col)) << eocString(col);
721                 if (n) {
722                         if (!label_[row].empty())
723                                 os << "\\label{" << label_[row] << "}";
724                         if (nonum_[row])
725                                 os << "\\nonumber ";
726                 }
727                 os << eolString(row);
728         }
729
730         footer_write(os);
731 }
732
733
734 void MathHullInset::normalize(NormalStream & os) const
735 {
736         os << "[formula " << normalName(getType()) << " ";
737         MathGridInset::normalize(os);
738         os << "] ";
739 }
740
741
742 void MathHullInset::mathmlize(MathMLStream & os) const
743 {
744         MathGridInset::mathmlize(os);
745 }
746
747
748 void MathHullInset::infoize(std::ostream & os) const
749 {
750         os << normalName(getType());
751 }
752
753 void MathHullInset::check() const
754 {
755         lyx::Assert(nonum_.size() == nrows());
756         lyx::Assert(label_.size() == nrows());
757 }