]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
Do not error out if -geometry is used with QPA_XCB defined.
[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), tight_(false) {}
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
77         /// The "nodraw" drawing stage for one single paragraph: set the
78         /// positions of the insets contained in 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         /// Gets the fully instantiated label font of a paragraph.
90         /// Basically the same routine as displayFont, but specialized for
91         /// a layout font.
92         Font labelDisplayFont(pit_type pit) const;
93
94
95         /// There are currently two font mechanisms in LyX:
96         /// 1. The font attributes in a lyxtext, and
97         /// 2. The inset-specific font properties, defined in an inset's
98         /// metrics() and draw() methods and handed down the inset chain through
99         /// the pi/mi parameters, and stored locally in a lyxtext in font_.
100         /// This is where the two are integrated in the final fully realized
101         /// font.
102         void applyOuterFont(Font &) const;
103
104         /// is this position in the paragraph right-to-left?
105         bool isRTL(CursorSlice const & sl, bool boundary) const;
106         /// is between pos-1 and pos an RTL<->LTR boundary?
107         bool isRTLBoundary(pit_type pit, pos_type pos) const;
108         /// would be a RTL<->LTR boundary between pos and the given font?
109         bool isRTLBoundary(pit_type pit, pos_type pos, Font const & font) const;
110
111
112         /// Rebreaks the given paragraph.
113         /// \retval true if a full screen redraw is needed.
114         /// \retval false if a single paragraph redraw is enough.
115         bool redoParagraph(pit_type const pit, bool align_rows = true);
116         /// Clear cache of paragraph metrics
117         void clear() { par_metrics_.clear(); }
118         /// Is cache of paragraph metrics empty ?
119         bool empty() const { return par_metrics_.empty(); }
120
121         ///
122         int ascent() const { return dim_.asc; }
123         ///
124         int descent() const { return dim_.des; }
125         /// current text width.
126         int width() const { return dim_.wid; }
127         /// current text height.
128         int height() const { return dim_.height(); }
129
130         /**
131          * Returns the left beginning of a row starting at \c pos.
132          * This information cannot be taken from the layout object, because
133          * in LaTeX the beginning of the text fits in some cases
134          * (for example sections) exactly the label-width.
135          */
136         int leftMargin(pit_type pit, pos_type pos) const;
137         /// Return the left beginning of a row which is not the first one.
138         /// This is the left margin when there is no indentation.
139         int leftMargin(pit_type pit) const;
140
141         ///
142         int rightMargin(ParagraphMetrics const & pm) const;
143         int rightMargin(pit_type const pit) const;
144
145         ///
146         void draw(PainterInfo & pi, int x, int y) const;
147
148         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
149
150 private:
151         ///
152         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
153
154         /// the minimum space a manual label needs on the screen in pixels
155         int labelFill(Row const & row) const;
156
157         // Turn paragraph oh index \c pit into a single row
158         Row tokenizeParagraph(pit_type pit) const;
159
160         // Break the row produced by tokenizeParagraph() into a list of rows.
161         RowList breakParagraph(Row const & row) const;
162
163         // Expands the alignment of row \param row in paragraph \param par
164         LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
165         /// Aligns properly the row contents (computes spaces and fills)
166         void setRowAlignment(Row & row, int width) const;
167
168         /// Set the height of the row (without space above/below paragraph)
169         void setRowHeight(Row & row) const;
170         // Compute the space on top of a paragraph
171         int parTopSpacing(pit_type pit) const;
172         // Compute the space below a a paragraph
173         int parBottomSpacing(pit_type pit) const;
174
175         // Helper function for the other checkInsetHit method.
176         InsetList::Element * checkInsetHit(pit_type pit, int x, int y);
177
178
179 // Temporary public:
180 public:
181         /// returns the position near the specified x-coordinate of the row.
182         /// x is an absolute screen coord, it is set to the real beginning
183         /// of this column. This takes in account horizontal cursor row scrolling.
184         pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
185
186         /// returns pos in given par at given x coord.
187         pos_type x2pos(pit_type pit, int row, int x) const;
188
189         // FIXME: is there a need for this?
190         //int pos2x(pit_type pit, pos_type pos) const;
191
192         /// returns the row near the specified y-coordinate in a given paragraph
193         /// (relative to the screen). If assert_in_view is true, it is made sure
194         /// that the row is on screen completely; this might change the given pit.
195         Row const & getPitAndRowNearY(int & y, pit_type & pit,
196                 bool assert_in_view, bool up);
197
198         /// returns the paragraph number closest to screen y-coordinate.
199         /// This method uses the BufferView CoordCache to locate the
200         /// paragraph. The y-coodinate is allowed to be off-screen and
201         /// the CoordCache will be automatically updated if needed. This is
202         /// the reason why we need a non const BufferView.
203         pit_type getPitNearY(int y);
204
205         /// sets cursor recursively descending into nested editable insets
206         /**
207         \return the inset pointer if x,y is covering that inset
208         \param x,y are absolute screen coordinates.
209         \param assert_in_view if true the cursor will be set on a row
210            that is completely visible
211         \param up whether we are going up or down (only used when
212            assert_in_view is true
213         \retval inset is null if the cursor is positioned over normal
214                text in the current Text object. Otherwise it is the inset
215                that the cursor points to, like for Inset::editXY.
216         */
217         /// FIXME: cleanup to use BufferView::getCoveringInset() and
218         /// setCursorFromCoordinates() instead of checkInsetHit().
219         Inset * editXY(Cursor & cur, int x, int y,
220                 bool assert_in_view = false, bool up = true);
221
222         /// sets cursor only within this Text.
223         /// x,y are screen coordinates
224         void setCursorFromCoordinates(Cursor & cur, int x, int y);
225
226         ///
227         int cursorX(CursorSlice const & sl, bool boundary) const;
228         ///
229         int cursorY(CursorSlice const & sl, bool boundary) const;
230
231         ///
232         bool cursorHome(Cursor & cur);
233         ///
234         bool cursorEnd(Cursor & cur);
235         ///
236         void deleteLineForward(Cursor & cur);
237
238         /// Returns an inset if inset was hit, or 0 if not.
239         /// \warning This method is not recursive! It will return the
240         /// outermost inset within this Text.
241         /// \sa BufferView::getCoveringInset() to get the innermost inset.
242         Inset * checkInsetHit(int x, int y);
243
244         /// calculates the position of a completion popup
245         void completionPosAndDim(Cursor const & cur, int & x, int & y,
246                 Dimension & dim) const;
247
248 private:
249
250         /// The BufferView owner.
251         BufferView * bv_;
252
253         /// The text contents (the model).
254         /// \todo FIXME: this should be const.
255         Text * text_;
256
257         /// A map from paragraph index number to paragraph metrics
258         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
259         ///
260         mutable ParMetricsCache par_metrics_;
261         Dimension dim_;
262         int max_width_;
263         /// if true, do not expand insets to max width artificially
264         bool tight_;
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