]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
Avoid full metrics computation with Update:FitCursor
[lyx.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 "LayoutEnums.h"
20 #include "ParagraphMetrics.h"
21
22 #include "support/types.h"
23
24 #include <map>
25
26 namespace lyx {
27
28 class BufferView;
29 class Cursor;
30 class CursorSlice;
31 class MetricsInfo;
32 class Text;
33
34 /// A map from a Text to the map of paragraphs metrics
35 class TextMetrics
36 {
37         /// noncopyable
38         TextMetrics(TextMetrics const &);
39         void operator=(TextMetrics const &);
40 public:
41         /// Default constructor (only here for STL containers).
42         TextMetrics() : bv_(0), text_(0), max_width_(0), tight_(false) {}
43         /// The only useful constructor.
44         TextMetrics(BufferView *, Text *);
45
46         ///
47         bool contains(pit_type pit) const;
48         ///
49         void forget(pit_type pit);
50         ///
51         std::pair<pit_type, ParagraphMetrics const *> first() const;
52         ///
53         std::pair<pit_type, ParagraphMetrics const *> last() const;
54         /// is this row the last in the text?
55         bool isLastRow(Row const & row) const;
56         /// is this row the first in the text?
57         bool isFirstRow(Row const & row) const;
58         ///
59         void setRowChanged(pit_type pit, pos_type pos);
60
61         ///
62         Dimension const & dim() const { return dim_; }
63         ///
64         Point const & origin() const { return origin_; }
65
66         ///
67         ParagraphMetrics const & parMetrics(pit_type) const;
68         ///
69         ParagraphMetrics & parMetrics(pit_type);
70
71         ///
72         void newParMetricsDown();
73         ///
74         void newParMetricsUp();
75
76         /// Update metrics up to \c bv_height. Only usable for main text (for now).
77         void updateMetrics(pit_type const anchor_pit, int const anchor_ypos,
78                        int const bv_height);
79
80         /// compute text metrics.
81         bool metrics(MetricsInfo const & mi, Dimension & dim, int min_width = 0);
82
83         /// The "nodraw" drawing stage for one single paragraph: set the
84         /// positions of the insets contained in this paragraph in metrics
85         /// cache. Related to BufferView::updatePosCache.
86         void updatePosCache(pit_type pit) const;
87
88         /// Gets the fully instantiated font at a given position in a paragraph.
89         /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
90         /// The difference is that this one is used for displaying, and thus we
91         /// are allowed to make cosmetic improvements. For instance make footnotes
92         /// smaller. (Asger)
93         Font displayFont(pit_type pit, pos_type pos) const;
94
95         /// Gets the fully instantiated label font of a paragraph.
96         /// Basically the same routine as displayFont, but specialized for
97         /// a layout font.
98         Font labelDisplayFont(pit_type pit) const;
99
100
101         /// There are currently two font mechanisms in LyX:
102         /// 1. The font attributes in a lyxtext, and
103         /// 2. The inset-specific font properties, defined in an inset's
104         /// metrics() and draw() methods and handed down the inset chain through
105         /// the pi/mi parameters, and stored locally in a lyxtext in font_.
106         /// This is where the two are integrated in the final fully realized
107         /// font.
108         void applyOuterFont(Font &) const;
109
110         /// is this position in the paragraph right-to-left?
111         bool isRTL(CursorSlice const & sl, bool boundary) const;
112         /// is between pos-1 and pos an RTL<->LTR boundary?
113         bool isRTLBoundary(pit_type pit, pos_type pos) const;
114         /// would be a RTL<->LTR boundary between pos and the given font?
115         bool isRTLBoundary(pit_type pit, pos_type pos, Font const & font) const;
116
117
118         /// Rebreaks the given paragraph.
119         /// \retval true if a full screen redraw is needed.
120         /// \retval false if a single paragraph redraw is enough.
121         bool redoParagraph(pit_type const pit, bool align_rows = true);
122         /// Clear cache of paragraph metrics
123         void clear() { par_metrics_.clear(); }
124         /// Is cache of paragraph metrics empty ?
125         bool empty() const { return par_metrics_.empty(); }
126
127         ///
128         int ascent() const { return dim_.asc; }
129         ///
130         int descent() const { return dim_.des; }
131         /// current text width.
132         int width() const { return dim_.wid; }
133         /// current text height.
134         int height() const { return dim_.height(); }
135
136         /**
137          * Returns the left beginning of a row starting at \c pos.
138          * This information cannot be taken from the layout object, because
139          * in LaTeX the beginning of the text fits in some cases
140          * (for example sections) exactly the label-width.
141          */
142         int leftMargin(pit_type pit, pos_type pos) const;
143         /// Return the left beginning of a row which is not the first one.
144         /// This is the left margin when there is no indentation.
145         int leftMargin(pit_type pit) const;
146
147         ///
148         int rightMargin(ParagraphMetrics const & pm) const;
149         int rightMargin(pit_type const pit) const;
150
151         ///
152         void draw(PainterInfo & pi, int x, int y) const;
153
154         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
155
156 private:
157         ///
158         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
159
160         /// the minimum space a manual label needs on the screen in pixels
161         int labelFill(Row const & row) const;
162
163         // Turn paragraph oh index \c pit into a single row
164         Row tokenizeParagraph(pit_type pit) const;
165
166         // Break the row produced by tokenizeParagraph() into a list of rows.
167         RowList breakParagraph(Row const & row) const;
168
169         // Expands the alignment of row \param row in paragraph \param par
170         LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
171         /// Aligns properly the row contents (computes spaces and fills)
172         void setRowAlignment(Row & row, int width) const;
173
174         /// Set the height of the row (without space above/below paragraph)
175         void setRowHeight(Row & row) const;
176         // Compute the space on top of a paragraph
177         int parTopSpacing(pit_type pit) const;
178         // Compute the space below a a paragraph
179         int parBottomSpacing(pit_type pit) const;
180
181         // Helper function for the other checkInsetHit method.
182         InsetList::Element * checkInsetHit(pit_type pit, int x, int y);
183
184
185 // Temporary public:
186 public:
187         /// returns the position near the specified x-coordinate of the row.
188         /// x is an absolute screen coord, it is set to the real beginning
189         /// of this column. This takes in account horizontal cursor row scrolling.
190         pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
191
192         /// returns pos in given par at given x coord.
193         pos_type x2pos(pit_type pit, int row, int x) const;
194
195         // FIXME: is there a need for this?
196         //int pos2x(pit_type pit, pos_type pos) const;
197
198         /// returns the row near the specified y-coordinate in a given paragraph
199         /// (relative to the screen). If assert_in_view is true, it is made sure
200         /// that the row is on screen completely; this might change the given pit.
201         Row const & getPitAndRowNearY(int & y, pit_type & pit,
202                 bool assert_in_view, bool up);
203
204         /// returns the paragraph number closest to screen y-coordinate.
205         /// This method uses the BufferView CoordCache to locate the
206         /// paragraph. The y-coodinate is allowed to be off-screen and
207         /// the CoordCache will be automatically updated if needed. This is
208         /// the reason why we need a non const BufferView.
209         pit_type getPitNearY(int y);
210
211         /// sets cursor recursively descending into nested editable insets
212         /**
213         \return the inset pointer if x,y is covering that inset
214         \param x,y are absolute screen coordinates.
215         \param assert_in_view if true the cursor will be set on a row
216            that is completely visible
217         \param up whether we are going up or down (only used when
218            assert_in_view is true
219         \retval inset is null if the cursor is positioned over normal
220                text in the current Text object. Otherwise it is the inset
221                that the cursor points to, like for Inset::editXY.
222         */
223         /// FIXME: cleanup to use BufferView::getCoveringInset() and
224         /// setCursorFromCoordinates() instead of checkInsetHit().
225         Inset * editXY(Cursor & cur, int x, int y,
226                 bool assert_in_view = false, bool up = true);
227
228         /// sets cursor only within this Text.
229         /// x,y are screen coordinates
230         void setCursorFromCoordinates(Cursor & cur, int x, int y);
231
232         ///
233         int cursorX(CursorSlice const & sl, bool boundary) const;
234         ///
235         int cursorY(CursorSlice const & sl, bool boundary) const;
236
237         ///
238         bool cursorHome(Cursor & cur);
239         ///
240         bool cursorEnd(Cursor & cur);
241         ///
242         void deleteLineForward(Cursor & cur);
243
244         /// Returns an inset if inset was hit, or 0 if not.
245         /// \warning This method is not recursive! It will return the
246         /// outermost inset within this Text.
247         /// \sa BufferView::getCoveringInset() to get the innermost inset.
248         Inset * checkInsetHit(int x, int y);
249
250         /// calculates the position of a completion popup
251         void completionPosAndDim(Cursor const & cur, int & x, int & y,
252                 Dimension & dim) const;
253
254 private:
255
256         /// The BufferView owner.
257         BufferView * bv_;
258
259         /// The text contents (the model).
260         /// \todo FIXME: this should be const.
261         Text * text_;
262
263         /// A map from paragraph index number to paragraph metrics
264         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
265         ///
266         mutable ParMetricsCache par_metrics_;
267         Dimension dim_;
268         int max_width_;
269         /// if true, do not expand insets to max width artificially
270         bool tight_;
271         mutable Point origin_;
272
273 // temporary public:
274 public:
275         /// our 'outermost' font.
276         /// This is handed down from the surrounding
277         /// inset through the pi/mi parameter (pi.base.font)
278         /// It is used in applyOuterFont() and setCharFont() for reasons
279         /// that are not clear... to hand hand the outermost language and
280         /// also for char style apparently.
281         Font font_;
282 };
283
284 /// return the default height of a row in pixels, considering font zoom
285 int defaultRowHeight();
286
287 } // namespace lyx
288
289 #endif // TEXT_METRICS_H