]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.h
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / math_data.h
1 // -*- C++ -*-
2 /**
3  *   You are free to use and modify this code under the terms of
4  *   the GNU General Public Licence version 2 or later.
5  */
6
7 /** \class MathArray
8     \brief Low level container for math insets
9  *  \author Alejandro Aguilar Sierra
10  *  \author André Pönitz
11  *  \author Lars Gullik Bjønnes
12  *
13  * Full author contact details are available in file CREDITS
14     \version February 2001
15   */
16
17 #ifndef MATH_DATA_H
18 #define MATH_DATA_H
19
20 #include <iosfwd>
21 #include <vector>
22
23 #include "math_atom.h"
24 #include "dimension.h"
25
26 class MathMacro;
27 class LaTeXFeatures;
28 class ReplaceData;
29 class MathMetricsInfo;
30 class MathPainterInfo;
31 class TextMetricsInfo;
32 class TextPainter;
33
34
35
36
37 class MathArray : private std::vector<MathAtom> {
38 public:
39         /// re-use inhertited stuff
40         typedef std::vector<MathAtom> base_type;
41         using base_type::const_iterator;
42         using base_type::iterator;
43         using base_type::size_type;
44         using base_type::difference_type;
45         using base_type::size;
46         using base_type::empty;
47         using base_type::clear;
48         using base_type::begin;
49         using base_type::end;
50         using base_type::push_back;
51         using base_type::pop_back;
52         using base_type::back;
53         using base_type::front;
54         using base_type::swap;
55         ///
56         typedef size_type idx_type;
57         typedef size_type pos_type;
58
59 public:
60         ///
61         MathArray();
62         ///
63         MathArray(const_iterator from, const_iterator to);
64         ///
65         void append(MathArray const & ar);
66
67         /// inserts single atom at position pos
68         void insert(size_type pos, MathAtom const & at);
69         /// inserts multiple atoms at position pos
70         void insert(size_type pos, MathArray const & ar);
71
72         /// erase range from pos1 to pos2
73         void erase(iterator pos1, iterator pos2);
74         /// erase single atom
75         void erase(iterator pos);
76         /// erase range from pos1 to pos2
77         void erase(size_type pos1, size_type pos2);
78         /// erase single atom
79         void erase(size_type pos);
80
81         ///
82         void dump() const;
83         ///
84         void dump2() const;
85         ///
86         void substitute(MathMacro const & macro);
87         ///
88         void replace(ReplaceData &);
89
90         /// looks for exact match
91         bool match(MathArray const & ar) const;
92         /// looks for inclusion match starting at pos
93         bool matchpart(MathArray const & ar, pos_type pos) const;
94         /// looks for containment, return == size mean not found
95         size_type find(MathArray const & ar) const;
96         /// looks for containment, return == size mean not found
97         size_type find_last(MathArray const & ar) const;
98         ///
99         bool contains(MathArray const & ar) const;
100         ///
101         void validate(LaTeXFeatures &) const;
102
103         /// checked write access
104         MathAtom & operator[](pos_type);
105         /// checked read access
106         MathAtom const & operator[](pos_type) const;
107         /// rebuild cached metrics information
108         Dimension const & metrics(MathMetricsInfo & mi) const;
109         /// redraw cell using cache metrics information
110         void draw(MathPainterInfo & pi, int x, int y) const;
111         /// rebuild cached metrics information
112         Dimension const & metricsT(TextMetricsInfo const & mi) const;
113         /// redraw cell using cache metrics information
114         void drawT(TextPainter & pi, int x, int y) const;
115         /// mark cell for re-drawing
116         void touch() const;
117
118         /// access to cached x coordinate of last drawing
119         int xo() const { return xo_; }
120         /// access to cached y coordinate of last drawing
121         int yo() const { return yo_; }
122         /// access to cached x coordinate of mid point of last drawing
123         int xm() const { return xo_ + dim_.w / 2; }
124         /// access to cached y coordinate of mid point of last drawing
125         int ym() const { return yo_ + (dim_.d - dim_.a) / 2; }
126         /// write access to coordinate;
127         void setXY(int x, int y) const;
128         /// returns x coordinate of given position in the array
129         int pos2x(size_type pos) const;
130         /// returns position of given x coordinate
131         int pos2x(size_type pos1, size_type pos2, int glue) const;
132         /// returns position of given x coordinate
133         size_type x2pos(int pos) const;
134         /// returns position of given x coordinate fstarting from a certain pos
135         size_type x2pos(size_type startpos, int targetx, int glue) const;
136         /// returns distance of this cell to the point given by x and y
137         // assumes valid position and size cache
138         int dist(int x, int y) const;
139
140         /// ascent of this cell above the baseline
141         int ascent() const { return dim_.a; }
142         /// descent of this cell below the baseline
143         int descent() const { return dim_.d; }
144         /// height of the cell
145         int height() const { return dim_.a + dim_.d; }
146         /// width of this cell
147         int width() const { return dim_.w; }
148         /// dimensions of cell
149         Dimension const & dim() const   { return dim_; }
150         /// dimensions of cell
151         void setDim(Dimension const & d) const { dim_ = d; }
152         /// bounding box of this cell
153         void boundingBox(int & xlow, int & xhigh, int & ylow, int & yhigh);
154         /// gives center coordinates
155         void center(int & x, int & y) const;
156         /// adjust (x,y) to point on boundary on a straight line from the center
157         void towards(int & x, int & y) const;
158         /// clean up if necessary
159         void notifyCursorLeaves();
160
161 private:
162         /// is this an exact match at this position?
163         bool find1(MathArray const & ar, size_type pos) const;
164
165         /// cached dimensions of cell
166         mutable Dimension dim_;
167         /// cached x coordinate of last drawing
168         mutable int xo_;
169         /// cached y coordinate of last drawing
170         mutable int yo_;
171         /// cached cleaness of cell
172         mutable bool clean_;
173         /// cached draw status of cell
174         mutable bool drawn_;
175 };
176
177 ///
178 std::ostream & operator<<(std::ostream & os, MathArray const & ar);
179
180
181 #endif