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