]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.h
Merge branch 'master' of git.lyx.org:lyx
[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 "Dimension.h"
19 #include "MathAtom.h"
20
21 #include "OutputEnums.h"
22
23 #include "support/strfwd.h"
24
25 #include <cstddef>
26 #include <vector>
27
28
29 namespace lyx {
30
31 class Buffer;
32 class BufferView;
33 class Cursor;
34 class DocIterator;
35 class LaTeXFeatures;
36 class ReplaceData;
37 class MacroContext;
38 class MathMacro;
39 class MetricsInfo;
40 class PainterInfo;
41 class ParIterator;
42 class TextMetricsInfo;
43 class TextPainter;
44
45
46 class MathData : private std::vector<MathAtom> {
47 public:
48         /// re-use inhertited stuff
49         typedef std::vector<MathAtom> base_type;
50         using base_type::const_iterator;
51         using base_type::iterator;
52         using base_type::size_type;
53         using base_type::difference_type;
54         using base_type::size;
55         using base_type::empty;
56         using base_type::clear;
57         using base_type::begin;
58         using base_type::end;
59         using base_type::push_back;
60         using base_type::pop_back;
61         using base_type::back;
62         using base_type::front;
63         ///
64         typedef size_type idx_type;
65         typedef size_type pos_type;
66
67 public:
68         ///
69         MathData(Buffer * buf = 0) : buffer_(buf) {}
70         ///
71         MathData(Buffer * buf, const_iterator from, const_iterator to);
72         ///
73         Buffer * buffer() { return buffer_; }
74         ///
75         Buffer const * buffer() const { return buffer_; }
76         ///
77         void append(MathData const & ar);
78
79         /// inserts single atom at position pos
80         void insert(size_type pos, MathAtom const & at);
81         /// inserts multiple atoms at position pos
82         void insert(size_type pos, MathData const & ar);
83
84         /// erase range from pos1 to pos2
85         void erase(iterator pos1, iterator pos2);
86         /// erase single atom
87         void erase(iterator pos);
88         /// erase range from pos1 to pos2
89         void erase(size_type pos1, size_type pos2);
90         /// erase single atom
91         void erase(size_type pos);
92
93         ///
94         void dump() const;
95         ///
96         void dump2() const;
97         ///
98         void replace(ReplaceData &);
99         ///
100         void substitute(MathData const & m);
101
102         /// looks for exact match
103         bool match(MathData const & ar) const;
104         /// looks for inclusion match starting at pos
105         bool matchpart(MathData const & ar, pos_type pos) const;
106         /// looks for containment, return == size mean not found
107         size_type find(MathData const & ar) const;
108         /// looks for containment, return == size mean not found
109         size_type find_last(MathData const & ar) const;
110         ///
111         bool contains(MathData const & ar) const;
112         ///
113         void validate(LaTeXFeatures &) const;
114
115         /// checked write access
116         MathAtom & operator[](pos_type);
117         /// checked read access
118         MathAtom const & operator[](pos_type) const;
119         /// rebuild cached metrics information
120         void metrics(MetricsInfo & mi, Dimension & dim) const;
121         ///
122         Dimension const & dimension(BufferView const &) const;
123
124         /// redraw cell using cache metrics information
125         void draw(PainterInfo & pi, int x, int y) const;
126         /// rebuild cached metrics information
127         void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
128         /// redraw cell using cache metrics information
129         void drawT(TextPainter & pi, int x, int y) const;
130         /// mark cell for re-drawing
131         void touch() const;
132
133         /// access to cached x coordinate of last drawing
134         int xo(BufferView const & bv) const;
135         /// access to cached y coordinate of last drawing
136         int yo(BufferView const & bv) const;
137         /// access to cached x coordinate of mid point of last drawing
138         int xm(BufferView const & bv) const;
139         /// access to cached y coordinate of mid point of last drawing
140         int ym(BufferView const & bv) const;
141         /// write access to coordinate;
142         void setXY(BufferView & bv, int x, int y) const;
143         /// returns x coordinate of given position in the array
144         int pos2x(BufferView const * bv, size_type pos) const;
145         /// returns position of given x coordinate
146         int pos2x(BufferView const * bv, size_type pos, int glue) const;
147         /// returns position of given x coordinate
148         size_type x2pos(BufferView const * bv, int pos) const;
149         /// returns position of given x coordinate fstarting from a certain pos
150         size_type x2pos(BufferView const * bv, int targetx, int glue) const;
151         /// returns distance of this cell to the point given by x and y
152         // assumes valid position and size cache
153         int dist(BufferView const & bv, int x, int y) const;
154
155         /// minimum ascent offset for superscript
156         int minasc() const { return minasc_; }
157         /// minimum descent offset for subscript
158         int mindes() const { return mindes_; }
159         /// level above/below which super/subscript should extend
160         int slevel() const { return slevel_; }
161         /// additional super/subscript shift
162         int sshift() const { return sshift_; }
163         /// superscript kerning
164         int kerning(BufferView const *) const { return kerning_; }
165         ///
166         void swap(MathData & ar) { base_type::swap(ar); }
167
168         /// attach/detach arguments to macros, updating the cur to 
169         /// stay visually at the same position (cur==0 is allowed)
170         void updateMacros(Cursor * cur, MacroContext const & mc, UpdateType);
171         ///
172         void updateBuffer(ParIterator const &, UpdateType);
173
174 protected:
175         /// cached values for super/subscript placement
176         mutable int minasc_;
177         mutable int mindes_;
178         mutable int slevel_;
179         mutable int sshift_;
180         mutable int kerning_;
181         Buffer * buffer_;
182
183 private:
184         /// is this an exact match at this position?
185         bool find1(MathData const & ar, size_type pos) const;
186
187         ///
188         void detachMacroParameters(DocIterator * dit, const size_type macroPos);
189         ///
190         void attachMacroParameters(Cursor * cur, const size_type macroPos, 
191                 const size_type macroNumArgs, const int macroOptionals,
192                 const bool fromInitToNormalMode, const bool interactiveInit,
193                 const size_t appetite);
194         ///
195         void collectOptionalParameters(Cursor * cur, 
196                 const size_type numOptionalParams, std::vector<MathData> & params, 
197                 size_t & pos, MathAtom & scriptToPutAround,
198                 const pos_type macroPos, const int thisPos, const int thisSlice);
199         ///
200         void collectParameters(Cursor * cur, 
201                 const size_type numParams, std::vector<MathData> & params, 
202                 size_t & pos, MathAtom & scriptToPutAround,
203                 const pos_type macroPos, const int thisPos, const int thisSlice,
204                 const size_t appetite);
205 };
206
207 ///
208 std::ostream & operator<<(std::ostream & os, MathData const & ar);
209 ///
210 odocstream & operator<<(odocstream & os, MathData const & ar);
211
212
213 } // namespace lyx
214
215 #endif