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