]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.h
typo
[lyx.git] / src / mathed / MathData.h
1 // -*- C++ -*-
2 /**
3  * \file MathData.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author André Pönitz
9  * \author Lars Gullik Bjønnes
10  * \author Stefan Schimanski
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef MATH_DATA_H
16 #define MATH_DATA_H
17
18 #include "MathAtom.h"
19 #include "MathClass.h"
20
21 #include "Dimension.h"
22 #include "OutputEnums.h"
23
24 #include "support/strfwd.h"
25
26 #include <cstddef>
27 #include <vector>
28
29
30 namespace lyx {
31
32 class Buffer;
33 class BufferView;
34 class Cursor;
35 class Dimension;
36 class DocIterator;
37 class InsetMathMacro;
38 class LaTeXFeatures;
39 class MacroContext;
40 class MathRow;
41 class MetricsInfo;
42 class PainterInfo;
43 class ParIterator;
44 class ReplaceData;
45 class TextMetricsInfo;
46 class TextPainter;
47
48
49 class MathData : private std::vector<MathAtom> {
50 public:
51         /// re-use inhertited stuff
52         typedef std::vector<MathAtom> base_type;
53         using base_type::const_iterator;
54         using base_type::iterator;
55         using base_type::size_type;
56         using base_type::difference_type;
57         using base_type::size;
58         using base_type::empty;
59         using base_type::clear;
60         using base_type::begin;
61         using base_type::end;
62         using base_type::push_back;
63         using base_type::pop_back;
64         using base_type::back;
65         using base_type::front;
66         ///
67         typedef size_type idx_type;
68         typedef size_type pos_type;
69
70 public:
71         ///
72         MathData() = default;
73         ///
74         explicit MathData(Buffer * buf) : buffer_(buf) {}
75         ///
76         MathData(Buffer * buf, const_iterator from, const_iterator to);
77         ///
78         Buffer * buffer() { return buffer_; }
79         ///
80         Buffer const * buffer() const { return buffer_; }
81         ///
82         void append(MathData const & ar);
83
84         /// inserts single atom at position pos
85         void insert(size_type pos, MathAtom const & at);
86         /// inserts multiple atoms at position pos
87         void insert(size_type pos, MathData const & ar);
88
89         /// erase range from pos1 to pos2
90         void erase(iterator pos1, iterator pos2);
91         /// erase single atom
92         void erase(iterator pos);
93         /// erase range from pos1 to pos2
94         void erase(size_type pos1, size_type pos2);
95         /// erase single atom
96         void erase(size_type pos);
97
98         ///
99         void dump() const;
100         ///
101         void dump2() const;
102         ///
103         void replace(ReplaceData &);
104         ///
105         void substitute(MathData const & m);
106
107         /// looks for exact match
108         bool match(MathData const & ar) const;
109         /// looks for inclusion match starting at pos
110         bool matchpart(MathData const & ar, pos_type pos) const;
111         /// looks for containment, return == size mean not found
112         size_type find(MathData const & ar) const;
113         /// looks for containment, return == size mean not found
114         size_type find_last(MathData const & ar) const;
115         ///
116         bool contains(MathData const & ar) const;
117         ///
118         void validate(LaTeXFeatures &) const;
119
120         /// checked write access
121         MathAtom & operator[](pos_type);
122         /// checked read access
123         MathAtom const & operator[](pos_type) const;
124
125         /// Add this array to a math row. Return true if contents got added
126         bool addToMathRow(MathRow &, MetricsInfo & mi) const;
127
128         /// rebuild cached metrics information
129         /** When \c tight is true, the height of the cell will be at least
130          *  the x height of the font. Otherwise, it will be the max height
131          *  of the font.
132          */
133         void metrics(MetricsInfo & mi, Dimension & dim, bool tight = true) const;
134         ///
135         Dimension const & dimension(BufferView const &) const;
136
137         /// draw the selection over the cell
138         void drawSelection(PainterInfo & pi, int x, int y) const;
139         /// redraw cell using cache metrics information
140         void draw(PainterInfo & pi, int x, int y) const;
141         /// rebuild cached metrics information
142         void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
143         /// redraw cell using cache metrics information
144         void drawT(TextPainter & pi, int x, int y) const;
145         /// approximate mathclass of the data
146         MathClass mathClass() const;
147         /// math class of first interesting element
148         MathClass firstMathClass() const;
149         /// math class of last interesting element
150         MathClass lastMathClass() const;
151         /// is the cell in display style
152         bool displayStyle() const { return display_style_; }
153
154         /// access to cached x coordinate of last drawing
155         int xo(BufferView const & bv) const;
156         /// access to cached y coordinate of last drawing
157         int yo(BufferView const & bv) const;
158         /// access to cached x coordinate of mid point of last drawing
159         int xm(BufferView const & bv) const;
160         /// access to cached y coordinate of mid point of last drawing
161         int ym(BufferView const & bv) const;
162         /// write access to coordinate;
163         void setXY(BufferView & bv, int x, int y) const;
164         /// returns x coordinate of given position in the array
165         int pos2x(BufferView const * bv, size_type pos) const;
166         /// returns position of given x coordinate
167         size_type x2pos(BufferView const * bv, int targetx) const;
168         /// returns distance of this cell to the point given by x and y
169         // assumes valid position and size cache
170         int dist(BufferView const & bv, int x, int y) const;
171
172         /// minimum ascent offset for superscript
173         int minasc() const { return minasc_; }
174         /// minimum descent offset for subscript
175         int mindes() const { return mindes_; }
176         /// level above/below which super/subscript should extend
177         int slevel() const { return slevel_; }
178         /// additional super/subscript shift
179         int sshift() const { return sshift_; }
180         /// Italic correction as described in InsetMathScript.h
181         int kerning(BufferView const *) const;
182         ///
183         void swap(MathData & ar) { base_type::swap(ar); }
184
185         /// attach/detach arguments to macros, updating the cur to
186         /// stay visually at the same position (cur==0 is allowed)
187         void updateMacros(Cursor * cur, MacroContext const & mc, UpdateType, int nesting);
188         ///
189         void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false);
190         ///
191         void setBuffer(Buffer & b);
192
193 protected:
194         /// cached values for super/subscript placement
195         mutable int minasc_ = 0;
196         mutable int mindes_ = 0;
197         mutable int slevel_ = 0;
198         mutable int sshift_ = 0;
199         /// cached value for display style
200         mutable bool display_style_ = false;
201         Buffer * buffer_ = nullptr;
202
203 private:
204         /// is this an exact match at this position?
205         bool find1(MathData const & ar, size_type pos) const;
206
207         ///
208         void detachMacroParameters(DocIterator * dit, const size_type macroPos);
209         ///
210         void attachMacroParameters(Cursor * cur, const size_type macroPos,
211                 const size_type macroNumArgs, const int macroOptionals,
212                 const bool fromInitToNormalMode, const bool interactiveInit,
213                 const size_t appetite);
214         ///
215         void collectOptionalParameters(Cursor * cur,
216                 const size_type numOptionalParams, std::vector<MathData> & params,
217                 size_t & pos, MathAtom & scriptToPutAround,
218                 const pos_type macroPos, const int thisPos, const int thisSlice);
219         ///
220         void collectParameters(Cursor * cur,
221                 const size_type numParams, std::vector<MathData> & params,
222                 size_t & pos, MathAtom & scriptToPutAround,
223                 const pos_type macroPos, const int thisPos, const int thisSlice,
224                 const size_t appetite);
225 };
226
227 ///
228 std::ostream & operator<<(std::ostream & os, MathData const & ar);
229 ///
230 odocstream & operator<<(odocstream & os, MathData const & ar);
231
232
233 } // namespace lyx
234
235 #endif