]> git.lyx.org Git - features.git/blob - src/Row.h
Next runtime messages to be ommitted if compiled with clang using flag '-fsanitize'.
[features.git] / src / Row.h
1 // -*- C++ -*-
2 /**
3  * \file Row.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Matthias Ettrich
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Metrics for an on-screen text row.
13  */
14
15 #ifndef ROW_H
16 #define ROW_H
17
18 #include "Changes.h"
19 #include "Dimension.h"
20 #include "Font.h"
21 #include "RowFlags.h"
22
23 #include "support/docstring.h"
24 #include "support/types.h"
25
26 #include <vector>
27
28 namespace lyx {
29
30 class DocIterator;
31 class Inset;
32
33 /**
34  * An on-screen row of text. A paragraph is broken into a RowList for
35  * display. Each Row contains a tokenized description of the contents
36  * of the line.
37  */
38 class Row {
39 public:
40         // Possible types of row elements
41         enum Type {
42                 // a string of character
43                 STRING,
44                 /**
45                  * Something (completion, end-of-par marker)
46                  * that occupies space one screen but does not
47                  * correspond to any paragraph contents
48                  */
49                 VIRTUAL,
50                 // An inset
51                 INSET,
52                 // Some spacing described by its width, not a string
53                 SPACE,
54                 // Spacing until the left margin, with a minimal value given
55                 // by the initial width
56                 MARGINSPACE
57         };
58
59 /**
60  * One element of a Row. It has a set of attributes that can be used
61  * by other methods that need to parse the Row contents.
62  */
63         struct Element {
64                 //
65                 Element(Type const t, pos_type p, Font const & f, Change const & ch)
66                         : type(t), pos(p), endpos(p + 1), font(f), change(ch) {}
67
68
69                 // Return the number of separator in the element (only STRING type)
70                 int countSeparators() const;
71
72                 // Return total width of element, including separator overhead
73                 // FIXME: Cache this value or the number of expanders?
74                 double full_width() const { return dim.wid + extra * countExpanders(); }
75                 // Return the number of expanding characters in the element (only STRING
76                 // type).
77                 int countExpanders() const;
78                 // Return the amount of expansion: the number of expanding characters
79                 // that get stretched during justification, times the em of the font
80                 // (only STRING type).
81                 int expansionAmount() const;
82                 // set extra proportionally to the font em value.
83                 void setExtra(double extra_per_em);
84
85                 /** Return position in pixels (from the left) of position
86                  * \param i in the row element.
87                  */
88                 double pos2x(pos_type const i) const;
89                 /** Return character position that is the closest to
90                  *  pixel position \param x. The value \param x is
91                  *  adjusted to the actual pixel position.
92                 */
93                 pos_type x2pos(int &x) const;
94                 /** Break the element in two if possible, so that its width is less
95                  * than \param w.
96                  * \return a vector of elements containing the remainder of
97                  *   the text (empty if nothing happened).
98                  * \param width maximum width of the row.
99                  * \param next_width available width on next row.
100                  * \param force: if true, cut string at any place, even for
101                  *   languages that wrap at word delimiters; if false, do not
102                  *   break at all if first element would larger than \c width.
103                  */
104                 // FIXME: ideally last parameter should be Elements&, but it is not possible.
105                 bool splitAt(int width, int next_width, bool force, std::vector<Element> & tail);
106                 // remove trailing spaces (useful for end of row)
107                 void rtrim();
108
109                 //
110                 bool isRTL() const { return font.isVisibleRightToLeft(); }
111                 // This is true for virtual elements.
112                 bool isVirtual() const { return type == VIRTUAL; }
113
114                 // Returns the position on left side of the element.
115                 pos_type left_pos() const { return isRTL() ? endpos : pos; };
116                 // Returns the position on right side of the element.
117                 pos_type right_pos() const { return isRTL() ? pos : endpos; };
118
119                 // The kind of row element
120                 Type type;
121                 // position of the element in the paragraph
122                 pos_type pos;
123                 // first position after the element in the paragraph
124                 pos_type endpos;
125                 // The dimension of the chunk (does not contain the
126                 // separator correction)
127                 Dimension dim;
128                 // The width of the element without trailing spaces
129                 int nspc_wid = 0;
130
131                 // Non-zero only if element is an inset
132                 Inset const * inset = nullptr;
133
134                 // Only non-null for justified rows
135                 double extra = 0;
136
137                 // Non-empty if element is a string or is virtual
138                 docstring str;
139                 //
140                 Font font;
141                 //
142                 Change change;
143                 // is it possible to add contents to this element?
144                 bool final = false;
145                 // properties with respect to row breaking (made of RowFlag enums)
146                 int row_flags = Inline;
147
148                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
149         };
150
151         ///
152         typedef Element value_type;
153
154         ///
155         Row() {}
156
157         /**
158          * Helper function: set variable \c var to value \c val, and mark
159          * row as changed is the values were different. This is intended
160          * for use when changing members of the row object.
161          */
162         template<class T1, class T2>
163         void change(T1 & var, T2 const val) {
164                 if (var != val)
165                         changed(true);
166                 var = val;
167         }
168         /**
169          * Helper function: set variable \c var to value \c val, and mark
170          * row as changed is the values were different. This is intended
171          * for use when changing members of the row object.
172          * This is the const version, useful for mutable members.
173          */
174         template<class T1, class T2>
175         void change(T1 & var, T2 const val) const {
176                 if (var != val)
177                         changed(true);
178                 var = val;
179         }
180         ///
181         bool changed() const { return changed_; }
182         ///
183         void changed(bool c) const { changed_ = c; }
184         ///
185         bool selection() const;
186         /**
187          * Set the selection begin and end and whether the left and/or
188          * right margins are selected.
189          * This is const because we update the selection status only at
190          * draw() time.
191          */
192         void setSelectionAndMargins(DocIterator const & beg,
193                 DocIterator const & end) const;
194         /// no selection on this row.
195         void clearSelectionAndMargins() const;
196
197         ///
198         void pit(pit_type p) { pit_ = p; }
199         ///
200         pit_type pit() const { return pit_; }
201         ///
202         void pos(pos_type p) { pos_ = p; }
203         ///
204         pos_type pos() const { return pos_; }
205         ///
206         void endpos(pos_type p) { end_ = p; }
207         ///
208         pos_type endpos() const { return end_; }
209         ///
210         void right_boundary(bool b) { right_boundary_ = b; }
211         ///
212         bool right_boundary() const { return right_boundary_; }
213         ///
214         void flushed(bool b) { flushed_ = b; }
215         ///
216         bool flushed() const { return flushed_; }
217
218         ///
219         Dimension const & dim() const { return dim_; }
220         ///
221         Dimension & dim() { return dim_; }
222         ///
223         int height() const { return dim_.height(); }
224         /// The width of the row, including the left margin, but not the right one.
225         int width() const { return dim_.wid; }
226         ///
227         int ascent() const { return dim_.asc; }
228         ///
229         int descent() const { return dim_.des; }
230
231         ///
232         Dimension const & contents_dim() const { return contents_dim_; }
233         ///
234         Dimension & contents_dim() { return contents_dim_; }
235
236         /// The offset of the left-most cursor position on the row
237         int left_x() const;
238         /// The offset of the right-most cursor position on the row
239         int right_x() const;
240
241         // Return the number of separators in the row
242         int countSeparators() const;
243         // Set the extra spacing for every expanding character in STRING-type
244         // elements.  \param w is the total amount of extra width for the row to be
245         // distributed among expanders.  \return false if the justification fails.
246         bool setExtraWidth(int w);
247
248         ///
249         void add(pos_type pos, Inset const * ins, Dimension const & dim,
250                  Font const & f, Change const & ch);
251         ///
252         void add(pos_type pos, char_type const c,
253                  Font const & f, Change const & ch);
254         ///
255         void addVirtual(pos_type pos, docstring const & s,
256                         Font const & f, Change const & ch);
257         ///
258         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
259         ///
260         void addMarginSpace(pos_type pos, int width, Font const & f, Change const & ch);
261
262         ///
263         typedef std::vector<Element> Elements;
264         ///
265         typedef Elements::iterator iterator;
266         ///
267         typedef Elements::const_iterator const_iterator;
268         ///
269         iterator begin() { return elements_.begin(); }
270         ///
271         iterator end() { return elements_.end(); }
272         ///
273         const_iterator begin() const { return elements_.begin(); }
274         ///
275         const_iterator end() const { return elements_.end(); }
276
277         ///
278         bool empty() const { return elements_.empty(); }
279         ///
280         Element & front() { return elements_.front(); }
281         ///
282         Element const & front() const { return elements_.front(); }
283         ///
284         Element & back() { return elements_.back(); }
285         ///
286         Element const & back() const { return elements_.back(); }
287         /// add element at the end and update width
288         void push_back(Element const &);
289         /// remove last element and update width
290         void pop_back();
291         /**
292          * if row width is too large, remove all elements after last
293          * separator and update endpos if necessary. If all that
294          * remains is a large word, cut it to \param width.
295          * \param width maximum width of the row.
296          * \param next_width available width on next row.
297          * \return list of elements remaining after breaking.
298          */
299         Elements shortenIfNeeded(int const width, int const next_width);
300
301         /**
302          * If last element of the row is a string, compute its width
303          * and mark it final.
304          */
305         void finalizeLast();
306
307         /**
308          * Find sequences of right-to-left elements and reverse them.
309          * This should be called once the row is completely built.
310          */
311         void reverseRTL();
312         ///
313         bool isRTL() const { return rtl_; }
314         ///
315         void setRTL(bool rtl) { rtl_ = rtl; }
316         ///
317         bool needsChangeBar() const { return changebar_; }
318         ///
319         void needsChangeBar(bool ncb) { changebar_ = ncb; }
320
321         /// Find row element that contains \c pos, and compute x offset.
322         const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
323
324         friend std::ostream & operator<<(std::ostream & os, Row const & row);
325
326         /// additional width for separators in justified rows (i.e. space)
327         double separator = 0;
328         /// width of hfills in the label
329         double label_hfill = 0;
330         /// the left margin position of the row
331         int left_margin = 0;
332         /// the right margin of the row
333         int right_margin = 0;
334         ///
335         mutable pos_type sel_beg = -1;
336         ///
337         mutable pos_type sel_end = -1;
338         ///
339         mutable bool begin_margin_sel = false;
340         ///
341         mutable bool end_margin_sel = false;
342
343 private:
344         /// Decides whether the margin is selected.
345         /**
346           * \param margin_begin
347           * \param beg
348           * \param end
349           */
350         bool isMarginSelected(bool left, DocIterator const & beg,
351                 DocIterator const & end) const;
352         /// Set the selection begin and end.
353         void setSelection(pos_type sel_beg, pos_type sel_end) const;
354
355         /**
356          * Returns true if a char or string with font \c f and change
357          * type \c ch can be added to the current last element of the
358          * row.
359          */
360         bool sameString(Font const & f, Change const & ch) const;
361
362         ///
363         Elements elements_;
364
365         /// has the Row appearance changed since last drawing?
366         mutable bool changed_ = true;
367         /// Index of the paragraph that contains this row
368         pit_type pit_ = 0;
369         /// first pos covered by this row
370         pos_type pos_ = 0;
371         /// one behind last pos covered by this row
372         pos_type end_ = 0;
373         // Is there a boundary at the end of the row (display inset...)
374         bool right_boundary_ = false;
375         // Shall the row be flushed when it is supposed to be justified?
376         bool flushed_ = false;
377         /// Row dimension.
378         Dimension dim_;
379         /// Row contents dimension. Does not contain the space above/below row.
380         Dimension contents_dim_;
381         /// true when this row lives in a right-to-left paragraph
382         bool rtl_ = false;
383         /// true when a changebar should be drawn in the margin
384         bool changebar_ = false;
385 };
386
387 std::ostream & operator<<(std::ostream & os, Row::Elements const & elts);
388
389
390 /**
391  * Each paragraph is broken up into a number of rows on the screen.
392  * This is a list of such on-screen rows, ordered from the top row
393  * downwards.
394  */
395 typedef std::vector<Row> RowList;
396
397 } // namespace lyx
398
399 #endif