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