]> git.lyx.org Git - features.git/blob - src/mathed/math_hullinset.C
support for wasy symbols
[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(string const & type)
27         {
28                 if (type == "eqnarray")
29                         return 3;
30                 if (type == "align")
31                         return 2;
32                 if (type == "alignat")
33                         return 2;
34                 if (type == "xalignat")
35                         return 2;
36                 if (type == "xxalignat")
37                         return 2;
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
59         int typecode(string const & s)
60         {
61                 if (s == "none")      return 0;
62                 if (s == "simple")    return 1;
63                 if (s == "equation")  return 2;
64                 if (s == "eqnarray")  return 3;
65                 if (s == "align")     return 4;
66                 if (s == "alignat")   return 5;
67                 if (s == "xalignat")  return 6;
68                 if (s == "xxalignat") return 7;
69                 if (s == "multline")  return 8;
70                 if (s == "gather")    return 9;
71                 lyxerr << "unknown hull type '" << s << "'\n";
72                 return 0;
73         }
74
75         bool smaller(string const & s, string const & t)
76         {
77                 return typecode(s) < typecode(t);
78         }
79
80
81 } // end anon namespace
82
83
84 MathHullInset::MathHullInset()
85         : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
86 {
87         setDefaults();
88 }
89
90
91 MathHullInset::MathHullInset(string const & type)
92         : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
93 {
94         setDefaults();
95 }
96
97
98 MathHullInset::MathHullInset(string const & type, MathGridInset const & grid)
99         : MathGridInset(grid), type_(type), nonum_(1), label_(1)
100 {
101         setDefaults();
102 }
103
104
105 MathHullInset::MathHullInset(string const & type, col_type cols)
106         : MathGridInset(cols, 1), type_(type), nonum_(1), label_(1)
107 {
108         setDefaults();
109 }
110
111
112 MathInset * MathHullInset::clone() const
113 {
114         return new MathHullInset(*this);
115 }
116
117
118 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
119 {
120         idx = 0;
121         pos = 0;
122         return true;
123 }
124
125
126 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
127 {
128         idx = nargs() - 1;
129         pos = cell(idx).size();
130         return true;
131 }
132
133
134 char MathHullInset::defaultColAlign(col_type col)
135 {
136         if (type_ == "eqnarray")
137                 return "rcl"[col];
138         if (typecode(type_) >= typecode("align"))
139                 return "rl"[col & 1];
140         return 'c';
141 }
142
143
144 int MathHullInset::defaultColSpace(col_type col)
145 {
146         if (type_ == "align" || type_ == "alignat")
147                 return 0;
148         if (type_ == "xalignat")
149                 return (col & 1) ? 20 : 0;
150         if (type_ == "xxalignat")
151                 return (col & 1) ? 40 : 0;
152         return 0;
153 }
154
155
156 char const * MathHullInset::standardFont() const
157 {
158         if (type_ == "none")
159                 return "lyxnochange";
160         return "mathnormal";
161 }
162
163
164 void MathHullInset::metrics(MathMetricsInfo & mi) const
165 {
166         MathFontSetChanger dummy(mi.base, standardFont());
167
168         // let the cells adjust themselves
169         MathGridInset::metrics(mi);
170
171         if (display()) {
172                 ascent_  += 12;
173                 descent_ += 12;
174         }
175
176         if (numberedType()) {
177                 MathFontSetChanger dummy(mi.base, "mathbf");
178                 int l = 0;
179                 for (row_type row = 0; row < nrows(); ++row)
180                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
181
182                 if (l)
183                         width_ += 30 + l;
184         }
185
186         // make it at least as high as the current font
187         int asc = 0;
188         int des = 0;
189         math_font_max_dim(mi.base.font, asc, des);
190         ascent_  = max(ascent_,  asc);
191         descent_ = max(descent_, des);
192
193         // for markers
194         width_   += 2;
195         descent_ += 1;
196         ascent_  += 1;
197 }
198
199
200 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
201 {
202         MathFontSetChanger dummy(pi.base, standardFont());
203         MathGridInset::draw(pi, x + 1, y);
204
205         if (numberedType()) {
206                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
207                 for (row_type row = 0; row < nrows(); ++row) {
208                         int const yy = y + rowinfo_[row].offset_;
209                         MathFontSetChanger dummy(pi.base, "mathrm");
210                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
211                 }
212         }
213
214         drawMarkers2(pi, x, y);
215 }
216
217
218 void MathHullInset::metricsT(TextMetricsInfo const &) const
219 {
220 #if 0
221         if (display()) {
222                 MathGridInset::metricsT(mi);
223         } else
224 #endif
225         {
226                 ostringstream os;
227                 WriteStream wi(os, false, true);
228                 write(wi);
229                 width_   = os.str().size();
230                 ascent_  = 1;
231                 descent_ = 0;
232         }
233 }
234
235
236 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
237 {
238 #if 0
239         if (display()) {
240                 MathGridInset::drawT(pain, x, y);
241         } else
242 #endif
243         {
244                 ostringstream os;
245                 WriteStream wi(os, false, true);
246                 write(wi);
247                 pain.draw(x, y, os.str().c_str());
248         }
249 }
250
251
252 string MathHullInset::label(row_type row) const
253 {
254         row_type n = nrows();
255         lyx::Assert(row < n);
256         return label_[row];
257 }
258
259
260 void MathHullInset::label(row_type row, string const & label)
261 {
262         label_[row] = label;
263 }
264
265
266 void MathHullInset::numbered(row_type row, bool num)
267 {
268         nonum_[row] = !num;
269 }
270
271
272 bool MathHullInset::numbered(row_type row) const
273 {
274         return !nonum_[row];
275 }
276
277
278 bool MathHullInset::ams() const
279 {
280         return
281                 type_ == "align" ||
282                 type_ == "multline" ||
283                 type_ == "gather" ||
284                 type_ == "alignat" ||
285                 type_ == "xalignat" ||
286                 type_ == "xxalignat";
287 }
288
289
290 bool MathHullInset::display() const
291 {
292         return type_ != "simple" && type_ != "none";
293 }
294
295
296 vector<string> MathHullInset::getLabelList() const
297 {
298         vector<string> res;
299         for (row_type row = 0; row < nrows(); ++row)
300                 if (!label_[row].empty() && nonum_[row] != 1)
301                         res.push_back(label_[row]);
302         return res;
303 }
304
305
306 bool MathHullInset::numberedType() const
307 {
308         if (type_ == "none")
309                 return false;
310         if (type_ == "simple")
311                 return false;
312         if (type_ == "xxalignat")
313                 return false;
314         for (row_type row = 0; row < nrows(); ++row)
315                 if (!nonum_[row])
316                         return true;
317         return false;
318 }
319
320
321 void MathHullInset::validate(LaTeXFeatures & features) const
322 {
323         if (ams())
324                 features.require("amsmath");
325
326
327         // Validation is necessary only if not using AMS math.
328         // To be safe, we will always run mathedvalidate.
329         //if (features.amsstyle)
330         //  return;
331
332         features.require("boldsymbol");
333         //features.binom      = true;
334
335         MathNestInset::validate(features);
336 }
337
338
339 void MathHullInset::header_write(WriteStream & os) const
340 {
341         bool n = numberedType();
342
343         if (type_ == "none")
344                 ;
345
346         else if (type_ == "simple") {
347                 os << '$';
348                 if (cell(0).empty())
349                         os << ' ';
350         }
351
352         else if (type_ == "equation") {
353                 if (n)
354                         os << "\\begin{equation" << star(n) << "}\n";
355                 else
356                         os << "\\[\n";
357         }
358
359         else if (type_ == "eqnarray" || type_ == "align")
360                         os << "\\begin{" << type_ << star(n) << "}\n";
361
362         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") 
363                 os << "\\begin{" << type_ << star(n) << "}"
364                   << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
365
366         else if (type_ == "multline" || type_ == "gather") 
367                 os << "\\begin{" << type_ << "}\n";
368
369         else 
370                 os << "\\begin{unknown" << star(n) << "}";
371 }
372
373
374 void MathHullInset::footer_write(WriteStream & os) const
375 {
376         bool n = numberedType();
377
378         if (type_ == "none")
379                 os << "\n";
380
381         else if (type_ == "simple")
382                 os << '$';
383
384         else if (type_ == "equation")
385                 if (n)
386                         os << "\\end{equation" << star(n) << "}\n";
387                 else
388                         os << "\\]\n";
389
390         else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
391               || type_ == "xalignat")
392                 os << "\n\\end{" << type_ << star(n) << "}\n";
393
394         else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
395                 os << "\n\\end{" << type_ << "}\n";
396
397         else
398                 os << "\\end{unknown" << star(n) << "}";
399 }
400
401
402 void MathHullInset::addRow(row_type row)
403 {
404         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
405         label_.insert(label_.begin() + row + 1, string());
406         MathGridInset::addRow(row);
407 }
408
409
410 void MathHullInset::delRow(row_type row)
411 {
412         MathGridInset::delRow(row);
413         nonum_.erase(nonum_.begin() + row);
414         label_.erase(label_.begin() + row);
415 }
416
417
418 void MathHullInset::addFancyCol(col_type col)
419 {
420         if (type_ == "equation")
421                 mutate("eqnarray");
422         
423         else if (type_ == "eqnarray") {
424                 mutate("align");
425                 addFancyCol(col);
426         }
427
428         else if (type_ == "align") {
429                 mutate("alignat");
430                 addFancyCol(col);
431         }
432
433         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
434                 MathGridInset::addCol(col);
435                 MathGridInset::addCol(col + 1);
436         }
437 }
438
439
440 void MathHullInset::delFancyCol(col_type col)
441 {
442         if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
443                 MathGridInset::delCol(col + 1);
444                 MathGridInset::delCol(col);
445         }
446 }
447
448
449 string MathHullInset::nicelabel(row_type row) const
450 {
451         if (nonum_[row])
452                 return string();
453         if (label_[row].empty())
454                 return string("(#)");
455         return "(" + label_[row] + ")";
456 }
457
458
459 void MathHullInset::glueall()
460 {
461         MathArray ar;
462         for (idx_type i = 0; i < nargs(); ++i)
463                 ar.push_back(cell(i));
464         *this = MathHullInset("simple");
465         cell(0) = ar;
466         setDefaults();
467 }
468
469
470 string const & MathHullInset::getType() const
471 {
472         return type_;
473 }
474
475
476 void MathHullInset::setType(string const & type)
477 {
478         type_ = type;
479         setDefaults();
480 }
481
482
483
484 void MathHullInset::mutate(string const & newtype)
485 {
486         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
487
488         // we try to move along the chain
489         // none <-> simple <-> equation <-> eqnarray 
490
491         if (newtype == "dump") {
492                 dump();
493         }
494
495         else if (newtype == type_) {
496                 // done
497         }
498
499         else if (type_ == "none") {
500                 setType("simple");
501                 numbered(0, false);
502                 mutate(newtype);
503         }
504
505         else if (type_ == "simple") {
506                 if (newtype == "none") {
507                         setType("none");
508                 } else {
509                         setType("equation");
510                         numbered(0, false);
511                         mutate(newtype);
512                 }
513         }
514
515         else if (type_ == "equation") {
516                 if (smaller(newtype, type_)) {
517                         setType("simple");
518                         mutate(newtype);
519                 } else if (newtype == "eqnarray") {
520                         MathGridInset::addCol(1);
521                         MathGridInset::addCol(1);
522
523                         // split it "nicely" on the firest relop
524                         pos_type pos = firstRelOp(cell(0));
525                         cell(1) = MathArray(cell(0), pos, cell(0).size());
526                         cell(0).erase(pos, cell(0).size());
527
528                         if (cell(1).size()) {
529                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
530                                 cell(1).erase(1, cell(1).size());
531                         }
532                         setType("eqnarray");
533                         mutate(newtype);
534                 } else {
535                         MathGridInset::addCol(1);
536                         // split it "nicely"
537                         pos_type pos = firstRelOp(cell(0));
538                         cell(1) = cell(0);
539                         cell(0).erase(pos, cell(0).size());
540                         cell(1).erase(0, pos);
541                         setType("align");
542                         mutate(newtype);
543                 }
544         }
545
546         else if (type_ == "eqnarray") {
547                 if (smaller(newtype, type_)) {
548                         // set correct (no)numbering
549                         bool allnonum = true;
550                         for (row_type row = 0; row < nrows(); ++row)
551                                 if (!nonum_[row])
552                                         allnonum = false;
553
554                         // set first non-empty label
555                         string label;
556                         for (row_type row = 0; row < nrows(); ++row) {
557                                 if (!label_[row].empty()) {
558                                         label = label_[row];
559                                         break;
560                                 }
561                         }
562
563                         glueall();
564                         nonum_[0] = allnonum;
565                         label_[0] = label;
566                         mutate(newtype);
567                 } else { // align & Co.
568                         for (row_type row = 0; row < nrows(); ++row) {
569                                 idx_type c = 3 * row + 1;
570                                 cell(c).push_back(cell(c + 1));
571                         }
572                         MathGridInset::delCol(2);
573                         setType("align");
574                         mutate(newtype);
575                 }
576         }
577
578         else if (type_ == "align") {
579                 if (smaller(newtype, type_)) {
580                         MathGridInset::addCol(1);
581                         setType("eqnarray");
582                         mutate(newtype);
583                 } else {
584                         setType(newtype);
585                 }
586         }
587
588         else if (type_ == "multline") {
589                 if (newtype == "gather") {
590                         setType("gather");
591                 } else {
592                         lyxerr << "mutation from '" << type_
593                                 << "' to '" << newtype << "' not implemented"
594                                                  << endl;
595                 }
596         }
597
598         else if (type_ == "gather") {
599                 if (newtype == "multline") {
600                         setType("multline");
601                 } else {
602                         lyxerr << "mutation from '" << type_
603                                 << "' to '" << newtype << "' not implemented" << endl;
604                 }
605         }
606
607         else {
608                 lyxerr << "mutation from '" << type_
609                                          << "' to '" << newtype << "' not implemented" << endl;
610         }
611 }
612
613
614 void MathHullInset::write(WriteStream & os) const
615 {
616         header_write(os);
617
618         bool n = numberedType();
619
620         for (row_type row = 0; row < nrows(); ++row) {
621                 for (col_type col = 0; col < ncols(); ++col)
622                         os << cell(index(row, col)) << eocString(col);
623                 if (n) {
624                         if (!label_[row].empty())
625                                 os << "\\label{" << label_[row] << "}";
626                         if (nonum_[row])
627                                 os << "\\nonumber ";
628                 }
629                 os << eolString(row);
630         }
631
632         footer_write(os);
633 }
634
635
636 void MathHullInset::normalize(NormalStream & os) const
637 {
638         os << "[formula " << type_ << " ";
639         MathGridInset::normalize(os);
640         os << "] ";
641 }
642
643
644 void MathHullInset::mathmlize(MathMLStream & os) const
645 {
646         MathGridInset::mathmlize(os);
647 }
648
649
650 void MathHullInset::infoize(std::ostream & os) const
651 {
652         os << "Type: " << type_;
653 }
654
655
656 void MathHullInset::check() const
657 {
658         lyx::Assert(nonum_.size() == nrows());
659         lyx::Assert(label_.size() == nrows());
660 }