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