]> git.lyx.org Git - lyx.git/blob - src/mathed/math_defs.h
fix the smallcaps drawing, move xfont metrics functions out from LyXFont, move non...
[lyx.git] / src / mathed / math_defs.h
1 // -*- C++ -*-
2 /*
3  *  File:        math_defs.h
4  *  Purpose:     Math editor definitions 
5  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
6  *  Created:     January 1996
7  *  Description: Math paragraph and objects for a WYSIWYG math editor.
8  *
9  *  Dependencies: Xlib
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.8beta, Mathed & Lyx project.
14  *
15  *   You are free to use and modify this code under the terms of
16  *   the GNU General Public Licence version 2 or later.
17  * 
18  */
19
20 #ifndef MATH_DEFS
21 #define MATH_DEFS
22
23 #ifdef __GNUG__
24 #pragma interface
25 #endif
26
27 #include <iosfwd>
28
29 #include "LString.h"
30 #include "debug.h"
31
32 #include "array.h"
33
34 class Painter;
35
36 ///
37 enum math_align {
38         ///
39         MATH_ALIGN_LEFT = 1,
40         ///
41         MATH_ALIGN_RIGHT = 2,
42         ///
43         MATH_ALIGN_BOTTOM = 4,
44         ///
45         MATH_ALIGN_TOP = 8
46 };
47 ///
48 #define MATH_COLSEP 8
49 ///
50 #define MATH_ROWSEP 8
51
52
53 /// Standard Math Sizes (Math mode styles)
54 enum MathedStyles {
55         ///
56         LM_ST_DISPLAY = 0,
57         ///
58         LM_ST_TEXT,
59         ///
60         LM_ST_SCRIPT,
61         ///
62         LM_ST_SCRIPTSCRIPT
63 };
64
65
66 /// Standard LaTeX Math Environments
67 enum MathedEnvironment {
68         ///
69         LM_EN_INTEXT = 0,
70         ///
71         LM_EN_DISPLAY,
72         ///
73         LM_EN_EQUATION,
74         ///
75         LM_EN_EQNARRAYAST,
76         ///
77         LM_EN_EQNARRAY,
78         ///
79         LM_EN_ARRAY
80 };
81
82
83 /** The restrictions of a standard LaTeX math paragraph
84   allows to get a small number of text codes (<30) */
85 enum MathedTextCodes  {
86         /// This must be >= 0
87         LM_TC_MIN = 0,
88         /// Open and Close group
89         LM_TC_OPEN,
90         ///
91         LM_TC_CLOSE,
92         /// Tabulator
93         LM_TC_TAB,
94         /// New line
95         LM_TC_CR,
96         /// Math Inset
97         LM_TC_INSET,
98         /// Super and sub scripts
99         LM_TC_UP,
100         ///
101         LM_TC_DOWN,
102         /// Editable Math Inset
103         LM_TC_ACTIVE_INSET,
104         /// Editable Text Inset
105         LM_TC_TEXT_INSET,
106         ///
107         LM_FONT_BEGIN,
108         /// Internal code for constants
109         LM_TC_CONST,
110         /// Internal code for variables
111         LM_TC_VAR,
112         ///
113         LM_TC_RM,
114         ///
115         LM_TC_CAL,
116         ///
117         LM_TC_BF,
118         ///
119         LM_TC_SF,
120         ///
121         LM_TC_TT,
122         ///
123         LM_TC_IT,
124         ///
125         LM_TC_TEXTRM,
126         /// Math mode TeX characters ",;:{}"
127         LM_TC_TEX,
128         /// Special characters "{}&#_%"
129         LM_TC_SPECIAL,
130         /// Internal code for operators
131         LM_TC_BOP,
132         /// Internal code for symbols
133         LM_TC_SYMB,
134         ///
135         LM_TC_BOPS,
136         ///
137         LM_TC_BSYM,
138         ///
139         LM_FONT_END,
140         
141         /// This must be < 32 
142         LM_TC_MAX
143 };
144
145 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
146
147 ///
148 #define LM_TC_NORMAL LM_TC_VAR
149  
150        
151 /// Types of lyx-math insets 
152 enum MathedInsetTypes  {
153         ///
154         LM_OT_MIN = 0,
155         /// A simple paragraph
156         LM_OT_PAR,
157         /// A simple numbered paragraph
158         LM_OT_PARN,
159         /// A multiline paragraph
160         LM_OT_MPAR,
161         /// A multiline numbered paragraph
162         LM_OT_MPARN,
163         /// An array
164         LM_OT_MATRIX,
165         /// A big operator
166         LM_OT_BIGOP,
167         /// A LaTeX macro
168         LM_OT_UNDEF,
169         ///
170         LM_OT_FUNC,
171         ///
172         LM_OT_FUNCLIM,
173         ///
174         LM_OT_SCRIPT,
175         ///
176         LM_OT_SPACE,
177         ///
178         LM_OT_DOTS,
179         /// A fraction
180         LM_OT_FRAC,
181         ///
182         LM_OT_ATOP,
183         ///
184         LM_OT_STACKREL,
185         /// A radical
186         LM_OT_SQRT,
187         /// A delimiter
188         LM_OT_DELIM,
189         /// A decoration
190         LM_OT_DECO,
191         /// An accent
192         LM_OT_ACCENT,
193         ///
194         LM_OT_MACRO,
195         ///
196         LM_OT_MACRO_ARG,
197         ///
198         LM_OT_MAX
199 };
200
201 ///
202 enum MathedBinaryTypes {
203         ///
204         LMB_NONE = 0,
205         ///
206         LMB_RELATION,
207         ///
208         LMB_OPERATOR,
209         ///
210         LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
211 };
212
213 class MathParInset;
214
215 /** Abstract base class for all math objects.
216     A math insets is for use of the math editor only, it isn't a
217     general LyX inset. It's used to represent all the math objects.
218     The formulaInset (a LyX inset) encapsulates a math inset.
219  */
220 class MathedInset  {
221  public: 
222     /// A math inset has a name (usually its LaTeX name), type and font-size
223     MathedInset(char const * nm, short ot, short st);
224     ///
225     MathedInset(MathedInset *);
226     ///
227     virtual ~MathedInset() {}
228
229     /// Draw the object
230     virtual void draw(Painter &, int x, int baseline) = 0;      
231
232     /// Write LaTeX and Lyx code
233     virtual void Write(std::ostream &) = 0;
234
235     /// Reproduces itself
236     virtual MathedInset * Clone() = 0;
237    
238     /// Compute the size of the object
239     virtual void Metrics() = 0; 
240     /// 
241     virtual int Ascent() const { return ascent; }
242     ///
243     virtual int Descent() const { return descent; }
244     ///
245     virtual int Width() const { return width; }
246     ///
247     virtual int Height() const { return ascent + descent; }
248     
249     ///
250     virtual bool GetLimits() const { return false; }
251     ///
252     virtual void SetLimits(bool) {}   
253    
254     ///
255     char const * GetName() const { return name; }
256     ///
257     short GetType() const { return objtype; }
258     ///
259     short GetStyle() const { return size; }
260           
261     //Man:  Avoid to use these functions if it's not strictly necessary 
262     ///
263     virtual void  SetType(short t) { objtype = t; }
264     ///
265     virtual void  SetStyle(short st) { size = st; } // Metrics();
266     ///
267     virtual void  SetName(char const * n) { name = n; }
268  protected:
269     ///
270     char const * name;
271     ///
272     short objtype;
273     ///
274     int width;
275     ///
276     int ascent;
277     ///
278     int descent;
279     ///
280     short size;
281     /// Default metrics
282     static int df_asc, df_des, df_width;
283
284     /// In a near future maybe we use a better fonts renderer than X
285     void drawStr(Painter &, short, int, int, int, byte const *, int);
286     ///
287     friend class MathedCursor;
288     ///
289     friend void mathed_init_fonts();
290 };
291
292 struct MathedRowSt;
293
294
295 /// Paragraph permissions
296 enum MathedParFlag {
297         LMPF_BASIC = 0,
298         /// If false can use a non-standard size
299         LMPF_FIXED_SIZE = 1,
300         /// If true can insert newlines 
301         LMPF_ALLOW_CR  = 2,
302         /// If true can use tabs
303         LMPF_ALLOW_TAB = 4,
304         /// If true can insert new columns
305         LMPF_ALLOW_NEW_COL = 8,
306         /// Smaller than current size (frac)
307         LMPF_SMALLER = 16,
308         /// Script size (subscript, stackrel)
309         LMPF_SCRIPT = 32
310 };
311
312
313 /** The math paragraph base class, base to all editable math objects */
314 class MathParInset: public MathedInset  {
315  public: 
316     ///
317     MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
318                  short ot = LM_OT_MIN);
319     ///
320     MathParInset(MathParInset *);
321     ///
322     virtual ~MathParInset();
323     ///
324     virtual MathedInset * Clone();
325
326     /// Draw the object on a drawable
327     virtual void draw(Painter &, int x, int baseline);
328
329     /// Write LaTeX code
330     virtual void Write(std::ostream &);
331
332     ///
333     virtual void Metrics();
334     ///
335     virtual void UserSetSize(short);
336  
337     /// Data is stored in a LyXArray
338     virtual void SetData(LyxArrayBase *);
339     ///
340     virtual LyxArrayBase * GetData() { return array; }
341
342     /// Paragraph position
343     virtual void GetXY(int &, int &) const;
344     ///
345     virtual void setXY(int x, int y) { xo = x;  yo = y; }
346     ///
347     virtual void SetFocus(int, int) {}
348     ///
349     virtual bool Inside(int, int);   
350    
351     // Tab stuff used by Matrix.
352     ///
353     virtual void SetAlign(char, char const *) {}
354 //    ///
355 //    virtual int GetTabPos() { return 0; }
356 //    ///
357 //    virtual int GetTab(int) { return 0; }
358     ///
359     virtual int GetColumns() { return 1; }
360     ///
361     virtual int GetRows() { return 1; }
362     ///
363     virtual bool isMatrix() { return false; }
364 //    /// These functions should report an error
365 //    virtual char const* GetLabel() { return 0; }
366 //    virtual char const* GetLabel(int) { return 0; }
367
368     // Vertical switching
369     ///
370     virtual bool setArgumentIdx(int i) { return (i == 0); }
371     ///
372     virtual bool setNextArgIdx() { return false; }
373     ///
374     virtual int  getArgumentIdx() { return 0; }
375     ///
376     virtual int  getMaxArgumentIdx() { return 0; }
377 //    ///
378 //    virtual void SetLabel(char const *) {}
379     ///
380     virtual void SetStyle(short);
381         ///
382     virtual MathedRowSt * getRowSt() const { return 0; }
383         ///
384     virtual void setRowSt(MathedRowSt *) {}
385         ///
386     virtual bool Permit(short f) { return bool(f & flag); }
387     
388  protected:
389     /// Paragraph data is stored here
390     LyxArrayBase * array;
391     /// Cursor start position
392     int xo, yo;
393     /// 
394     short flag;
395
396  private:
397     ///
398     virtual void setFlag(MathedParFlag f) { flag |= f; }
399         ///
400     friend class InsetFormula;
401         ///
402     friend class MathedXIter;
403         ///
404     friend class MathedCursor;
405         ///
406     friend LyxArrayBase * mathed_parse(unsigned flags = 0,
407                                        LyxArrayBase * a = 0,
408                                        MathParInset ** p = 0);
409 };
410
411
412 /* The physical structure of a row and aditional information is stored here.
413     It allows to manage the extra info independently of the paragraph data.  
414     Only used for multiline paragraphs.
415  */
416 struct MathedRowSt {    
417     /// 
418     MathedRowSt(int n) {
419             w = new int[n + 1]; // this leaks
420             asc = desc = y = 0;
421             for (int i = 0 ; i < n + 1 ; ++i)
422               w[i] = 0;
423             next = 0;
424             label = 0;
425             numbered = true;
426     }
427     ///
428     ~MathedRowSt() {
429             delete[] w;
430             delete[] label;
431     }
432     /// Should be const but...
433     MathedRowSt * getNext() const  { return next; }
434     /// ...we couldn't use this.
435     void setNext(MathedRowSt * n) { next = n; }
436     ///
437     char const * getLabel() const { return label; }
438     ///
439     bool isNumbered() const { return numbered; }
440     ///
441     int  getBaseline() const { return y; }    
442     ///
443     int  getTab(int i) { return w[i]; }
444     /// 
445     void setLabel(char * l) { label = l; }
446     ///
447     void setNumbered(bool nf) { numbered = nf; }
448     ///
449     void setTab(int i, int t) { w[i] = t; }
450     
451  private:
452     /// Vericals 
453     int asc, desc, y;
454     /// widths 
455     int * w;
456     /// 
457     char * label;
458     ///
459     bool numbered;
460     ///
461     MathedRowSt * next;
462         ///
463     friend class MathMatrixInset;
464         ///
465     friend class MathedXIter;
466 };
467
468
469 /** Multiline math paragraph base class.
470     This is the base to all multiline editable math objects
471     like array and eqnarray. 
472  */
473 class MathMatrixInset: public MathParInset {
474  public: 
475     ///
476     MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
477     ///
478     MathMatrixInset(MathMatrixInset *);
479     ///
480     MathedInset * Clone();
481     ///
482     virtual ~MathMatrixInset();
483     ///
484     void draw(Painter &, int, int);
485     ///
486     void Write(std::ostream &);
487     ///
488     void Metrics();
489     ///
490     void SetData(LyxArrayBase *);
491     ///
492     void SetAlign(char, char const *);
493     ///
494     char * GetAlign(char * vv) {
495         *vv = v_align;
496         return h_align;
497     }
498 //    ///
499 //    int GetTab(int);
500     ///
501     int GetColumns() { return nc; }
502     ///
503     int GetRows() { return nr; }
504     ///
505     virtual bool isMatrix() { return true; }
506
507     /// Use this to manage the extra information independently of paragraph
508     MathedRowSt * getRowSt() const { return row; }
509         ///
510     void setRowSt(MathedRowSt * r) { row = r; }
511     
512  protected:
513     ///  Number of columns & rows
514     int nc, nr;
515     /// tab sizes
516     int * ws;   
517     /// 
518     char v_align; // add approp. signedness
519     ///
520     char * h_align;
521     /// Vertical structure
522     MathedRowSt * row;
523
524 };
525
526
527
528 /*************************  Prototypes  **********************************/
529 /// 
530 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
531                             MathParInset ** mt);
532 ///
533 void mathed_write(MathParInset *, std::ostream &, int *, char fragile,
534                   char const * label = 0);
535
536 ///
537 void mathed_parser_file(std::istream &, int);
538 ///
539 int mathed_parser_lineno();
540 ///
541 int MathedLookupBOP(short);
542
543 /************************ Inline functions ********************************/
544 ///
545 inline
546 bool MathIsInset(short x)
547 {
548         return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
549 }
550
551 ///
552 inline
553 bool MathIsFont(short x)
554 {
555         return LM_TC_CONST <= x && x <= LM_TC_BSYM;
556 }
557
558 ///
559 inline
560 bool MathIsAlphaFont(short x)
561 {
562         return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
563 }
564
565 ///
566 inline
567 bool MathIsActive(short x)
568 {
569         return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
570 }
571
572 ///
573 inline
574 bool MathIsUp(short x)
575 {
576         return x == LM_TC_UP;
577 }
578
579 ///
580 inline
581 bool MathIsDown(short x)
582 {
583         return x == LM_TC_DOWN;
584 }
585
586 ///
587 inline
588 bool MathIsScript(short x)
589 {
590         return x == LM_TC_DOWN || x == LM_TC_UP;
591 }
592
593 ///
594 inline
595 bool MathIsBOPS(short x)
596 {
597         return MathedLookupBOP(x) > LMB_NONE;
598 }
599
600
601 ///
602 inline
603 bool MathIsBinary(short x)
604 {
605     return x == LM_TC_BOP || x == LM_TC_BOPS;
606 }
607
608 ///
609 inline
610 bool MathIsSymbol(short x) {
611     return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
612 }
613      
614
615 inline
616 MathedInset::MathedInset(char const * nm, short ot, short st):
617   name(nm), objtype(ot), size(st) 
618 {
619    width = ascent = descent = 0;
620 }
621
622 inline
623 bool MathParInset::Inside(int x, int y) 
624 {
625   return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
626 }
627
628
629 inline
630 void MathParInset::GetXY(int & x, int & y) const
631 {
632    x = xo; y = yo;
633 }
634
635
636 inline
637 void MathParInset::UserSetSize(short sz)
638 {
639    if (sz >= 0) {
640        size = sz;      
641        flag = flag & ~LMPF_FIXED_SIZE;
642    }
643 }
644
645
646 inline
647 void MathParInset::SetStyle(short sz) 
648 {
649     if (Permit(LMPF_FIXED_SIZE)) {
650         if (Permit(LMPF_SCRIPT)) 
651           sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
652         if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
653             ++sz;
654         } 
655         MathedInset::SetStyle(sz);
656     }
657 }
658
659 #endif