]> git.lyx.org Git - features.git/blob - src/TextMetrics.h
Change TextMetrics::rowHeight to setRowHeight
[features.git] / src / TextMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file TextMetrics.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TEXT_METRICS_H
15 #define TEXT_METRICS_H
16
17 #include "Font.h"
18 #include "InsetList.h"
19 #include "ParagraphMetrics.h"
20
21 #include "support/types.h"
22
23 namespace lyx {
24
25 class BufferView;
26 class Cursor;
27 class CursorSlice;
28 class DocIterator;
29 class MetricsInfo;
30 class Text;
31
32 /// A map from a Text to the map of paragraphs metrics
33 class TextMetrics
34 {
35 public:
36         /// Default constructor (only here for STL containers).
37         TextMetrics() : text_(0) {}
38         /// The only useful constructor.
39         TextMetrics(BufferView *, Text *);
40
41         ///
42         bool contains(pit_type pit) const;
43         ///
44         ParagraphMetrics const & parMetrics(pit_type) const;
45         ///
46         std::pair<pit_type, ParagraphMetrics const *> first() const;
47         ///
48         std::pair<pit_type, ParagraphMetrics const *> last() const;
49
50         ///
51         Dimension const & dimension() const { return dim_; }
52         ///
53         Point const & origin() const { return origin_; }
54
55
56         /// compute text metrics.
57         bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
58
59         ///
60         void newParMetricsDown();
61         ///
62         void newParMetricsUp();
63
64         /// Gets the fully instantiated font at a given position in a paragraph
65         /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
66         /// The difference is that this one is used for displaying, and thus we
67         /// are allowed to make cosmetic improvements. For instance make footnotes
68         /// smaller. (Asger)
69         Font displayFont(pit_type pit, pos_type pos) const;
70
71         /// There are currently two font mechanisms in LyX:
72         /// 1. The font attributes in a lyxtext, and
73         /// 2. The inset-specific font properties, defined in an inset's
74         /// metrics() and draw() methods and handed down the inset chain through
75         /// the pi/mi parameters, and stored locally in a lyxtext in font_.
76         /// This is where the two are integrated in the final fully realized
77         /// font.
78         void applyOuterFont(Font &) const;
79
80         /// is this position in the paragraph right-to-left?
81         bool isRTL(CursorSlice const & sl, bool boundary) const;
82         /// is between pos-1 and pos an RTL<->LTR boundary?
83         bool isRTLBoundary(pit_type pit,
84           pos_type pos) const;
85         /// would be a RTL<->LTR boundary between pos and the given font?
86         bool isRTLBoundary(pit_type pit,
87           pos_type pos, Font const & font) const;
88
89
90         /// Rebreaks the given paragraph.
91         /// \retval true if a full screen redraw is needed.
92         /// \retval false if a single paragraph redraw is enough.
93         bool redoParagraph(pit_type const pit);
94         /// Clear cache of paragraph metrics
95         void clear() { par_metrics_.clear(); }
96
97         ///
98         int ascent() const { return dim_.asc; }
99         ///
100         int descent() const { return dim_.des; }
101         /// current text width.
102         int width() const { return dim_.wid; }
103         /// current text height.
104         int height() const { return dim_.height(); }
105
106         ///
107         int maxWidth() const { return max_width_; }
108         ///
109         int singleWidth(pit_type const pit,     pos_type pos) const;
110
111         ///
112         int rightMargin(ParagraphMetrics const & pm) const;
113         int rightMargin(pit_type const pit) const;
114
115         /** this calculates the specified parameters. needed when setting
116          * the cursor and when creating a visible row */
117         void computeRowMetrics(pit_type pit, Row & row, int width) const;
118
119         ///
120         void draw(PainterInfo & pi, int x, int y) const;
121         
122         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
123
124         /// Returns the height of the row (width member is set to 0).
125         /// If \c topBottomSpace is true, extra space is added for the
126         /// top and bottom row.
127         void setRowHeight(Row & row, pit_type const pit, 
128                           bool topBottomSpace = true) const;
129
130 private:
131         ///
132         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
133
134         /// the minimum space a manual label needs on the screen in pixels
135         int labelFill(pit_type const pit, Row const & row) const;
136
137         /// FIXME??
138         int labelEnd(pit_type const pit) const;
139
140         /// sets row.end to the pos value *after* which a row should break.
141         /// for example, the pos after which isNewLine(pos) == true
142         void breakRow(Row & row, int right_margin, pit_type const pit) const;
143
144         /// returns the minimum space a row needs on the screen in pixel
145         int rowWidth(
146                 int right_margin,
147                 pit_type const pit,
148                 pos_type const first,
149                 pos_type const end
150                 ) const;
151
152         // Helper function for the other checkInsetHit method.
153         InsetList::InsetTable * checkInsetHit(pit_type pit, int x, int y);
154
155
156 // Temporary public:
157 public:
158         /// returns the column near the specified x-coordinate of the row.
159         /// x is an absolute screen coord, it is set to the real beginning
160         /// of this column.
161         pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
162                 bool & boundary) const;
163
164         /// returns pos in given par at given x coord.
165         pos_type x2pos(pit_type pit, int row, int x) const;
166
167         // FIXME: is there a need for this?
168         //int pos2x(pit_type pit, pos_type pos) const;
169
170         /// returns the row near the specified y-coordinate in a given paragraph
171         /// (relative to the screen). If assert_in_view is true, it is made sure
172         /// that the row is on screen completely; this might change the given pit.
173         Row const & getPitAndRowNearY(int & y, pit_type & pit,
174                 bool assert_in_view, bool up);
175
176         /// returns the paragraph number closest to screen y-coordinate.
177         /// This method uses the BufferView CoordCache to locate the
178         /// paragraph. The y-coodinate is allowed to be off-screen and
179         /// the CoordCache will be automatically updated if needed. This is
180         /// the reason why we need a non const BufferView.
181         pit_type getPitNearY(int y);
182
183         /// sets cursor recursively descending into nested editable insets
184         /**
185         \return the inset pointer if x,y is covering that inset
186         \param x,y are absolute screen coordinates.
187         \param assert_in_view if true the cursor will be set on a row
188            that is completely visible
189     \param up whether we are going up or down (only used when
190            assert_in_view is true
191         \retval inset is non-null if the cursor is positionned inside
192         */
193         /// FIXME: cleanup to use BufferView::getCoveringInset() and
194         /// setCursorFromCoordinates() instead of checkInsetHit().
195         Inset * editXY(Cursor & cur, int x, int y,
196                 bool assert_in_view = false, bool up = true);
197
198         /// sets cursor only within this Text.
199         /// x,y are screen coordinates
200         void setCursorFromCoordinates(Cursor & cur, int x, int y);
201
202         ///
203         int cursorX(CursorSlice const & cursor,
204                 bool boundary) const;
205         ///
206         int cursorY(CursorSlice const & cursor,
207                 bool boundary) const;
208
209         ///
210         bool cursorHome(Cursor & cur);
211         ///
212         bool cursorEnd(Cursor & cur);
213         ///
214         void deleteLineForward(Cursor & cur);
215
216         /// is this row the last in the text?
217         bool isLastRow(pit_type pit, Row const & row) const;
218         /// is this row the first in the text?
219         bool isFirstRow(pit_type pit, Row const & row) const;
220
221         /// Returns an inset if inset was hit, or 0 if not.
222         /// \warning This method is not recursive! It will return the
223         /// outermost inset within this Text.
224         /// \sa BufferView::getCoveringInset() to get the innermost inset.
225         Inset * checkInsetHit(int x, int y);
226
227         /**
228          * Returns the left beginning of the text.
229          * This information cannot be taken from the layout object, because
230          * in LaTeX the beginning of the text fits in some cases
231          * (for example sections) exactly the label-width.
232          */
233         int leftMargin(int max_width, pit_type pit, pos_type pos) const;
234         int leftMargin(int max_width, pit_type pit) const;
235
236         /// calculates the position of a completion popup
237         void completionPosAndDim(Cursor const & cur, int & x, int & y, 
238                 Dimension & dim) const;
239
240 private:
241         friend class BufferView;
242
243         /// The BufferView owner.
244         BufferView * bv_;
245
246         /// The text contents (the model).
247         /// \todo FIXME: this should be const.
248         Text * text_;
249
250         bool main_text_;
251         /// A map from paragraph index number to paragraph metrics
252         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
253         ///
254         mutable ParMetricsCache par_metrics_;
255         Dimension dim_;
256         int max_width_;
257         mutable Point origin_;
258
259 // temporary public:
260 public:
261         /// our 'outermost' font.
262         /// This is handed down from the surrounding
263         /// inset through the pi/mi parameter (pi.base.font)
264         /// It is used in applyOuterFont() and setCharFont() for reasons 
265         /// that are not clear... to hand hand the outermost language and
266         /// also for char style apparently.
267         Font font_;
268 };
269
270 /// return the default height of a row in pixels, considering font zoom
271 int defaultRowHeight();
272
273 } // namespace lyx
274
275 #endif // TEXT_METRICS_H