]> git.lyx.org Git - features.git/blob - src/mathed/MathRow.h
Set correctly the spacing between atoms in MathData
[features.git] / src / mathed / MathRow.h
1 // -*- C++ -*-
2 /**
3  * \file MathRow.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MATH_ROW_H
13 #define MATH_ROW_H
14
15 #include "MathClass.h"
16
17 #include "support/docstring.h"
18
19 #include <vector>
20
21 namespace lyx {
22
23 class BufferView;
24 class Dimension;
25 class MetricsInfo;
26 class PainterInfo;
27
28 class InsetMath;
29 class MathData;
30 class MathMacro;
31
32 /*
33  * While for editing purpose it is important that macros are counted
34  * as a single element, this is not the case for display. To get the
35  * spacing correct, it is necessary to dissolve all the macros that
36  * can be, along with their arguments. Then one obtains a
37  * representation of the MathData contents as a string of insets and
38  * then spacing can be done properly.
39  *
40  * This is the purpose of the MathRow class.
41  */
42 class MathRow
43 {
44 public:
45         // What row elements can be
46         enum Type {
47                 INSET, // this element is a plain inset
48                 BEG_MACRO, // a macro begins here
49                 END_MACRO, // a macro ends here
50                 BEG_ARG, // a macro argument begins here
51                 END_ARG, // a macro argument ends here
52                 BEGIN, // dummy element before row
53                 END, // dummy element after row
54         };
55
56         // An elements, together with its spacing
57         struct Element
58         {
59                 ///
60                 Element(Type t = INSET, MathClass const mc = MC_ORD);
61
62                 /// Classifies the contents of the object
63                 Type type;
64
65                 /// When type is INSET
66                 /// the math inset
67                 InsetMath const * inset;
68                 /// the class of the inset
69                 MathClass mclass;
70                 /// the spacing around the inset
71                 int before, after;
72                 // Non empty when there is a completion to draw
73                 docstring compl_text;
74                 // the number of characters forming the unique part.
75                 size_t compl_unique_to;
76
77                 /// When type is BEG_MACRO, END_MACRO, BEG_ARG, END_ARG
78                 /// the math macro
79                 MathMacro const * macro;
80
81                 // type is BEG_ARG, END_ARG
82                 MathData const * ar;
83         };
84
85         ///
86         MathRow() {};
87         ///
88         typedef std::vector<Element> Elements;
89         ///
90         typedef Elements::iterator iterator;
91         ///
92         typedef Elements::const_iterator const_iterator;
93         ///
94         iterator begin() { return elements_.begin(); }
95         ///
96         iterator end() { return elements_.end(); }
97         ///
98         const_iterator begin() const { return elements_.begin(); }
99         ///
100         const_iterator end() const { return elements_.end(); }
101         //
102         void push_back(Element const & e) { elements_.push_back(e); }
103         //
104         Element & back() { return elements_.back(); }
105
106         // create the math row by unwinding all macros in the MathData and
107         // compute the spacings.
108         MathRow(MetricsInfo const & mi, MathData const * ar);
109
110         //
111         void metrics(MetricsInfo & mi, Dimension & dim) const;
112         //
113         void draw(PainterInfo & pi, int const x, int const y) const;
114
115         /// superscript kerning
116         int kerning(BufferView const *) const;
117
118 private:
119         // Index of the first inset element before position i
120         int before(int i) const;
121         // Index of the first inset element after position i
122         int after(int i) const;
123
124         ///
125         Elements elements_;
126 };
127
128 ///
129 std::ostream & operator<<(std::ostream & os, MathRow::Element const & elt);
130
131 ///
132 std::ostream & operator<<(std::ostream & os, MathRow const & mrow);
133
134
135 } // namespace lyx
136
137 #endif