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