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