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