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