4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Matthias Ettrich
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
12 * Metrics for an on-screen text row.
19 #include "Dimension.h"
23 #include "support/docstring.h"
24 #include "support/types.h"
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
40 // Possible types of row elements
42 // a string of character
45 * Something (completion, end-of-par marker)
46 * that occupies space one screen but does not
47 * correspond to any paragraph contents
52 // Some spacing described by its width, not a string
54 // Spacing until the left margin, with a minimal value given
55 // by the initial width
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.
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) {}
69 // Return total width of element, including separator overhead
70 // FIXME: Cache this value or the number of expanders?
71 double full_width() const { return dim.wid + extra * countExpanders(); }
72 // Return the number of expanding characters in the element (only STRING
74 int countExpanders() const;
75 // Return the amount of expansion: the number of expanding characters
76 // that get stretched during justification, times the em of the font
77 // (only STRING type).
78 int expansionAmount() const;
79 // set extra proportionally to the font em value.
80 void setExtra(double extra_per_em);
82 /** Return position in pixels (from the left) of position
83 * \param i in the row element.
85 double pos2x(pos_type const i) const;
86 /** Return character position that is the closest to
87 * pixel position \param x. The value \param x is
88 * adjusted to the actual pixel position.
90 pos_type x2pos(int &x) const;
91 /** Break the element in two if possible, so that its width is less
93 * \return a vector of elements containing the remainder of
94 * the text (empty if nothing happened).
95 * \param width maximum width of the row.
96 * \param next_width available width on next row.
97 * \param force: if true, cut string at any place, even for
98 * languages that wrap at word delimiters; if false, do not
99 * break at all if first element would larger than \c width.
101 // FIXME: ideally last parameter should be Elements&, but it is not possible.
102 bool splitAt(int width, int next_width, bool force, std::vector<Element> & tail);
103 // remove trailing spaces (useful for end of row)
107 bool isRTL() const { return font.isVisibleRightToLeft(); }
108 // This is true for virtual elements.
109 bool isVirtual() const { return type == VIRTUAL; }
111 // Returns the position on left side of the element.
112 pos_type left_pos() const { return isRTL() ? endpos : pos; };
113 // Returns the position on right side of the element.
114 pos_type right_pos() const { return isRTL() ? pos : endpos; };
116 // The kind of row element
118 // position of the element in the paragraph
120 // first position after the element in the paragraph
122 // The dimension of the chunk (does not contain the
123 // separator correction)
125 // The width of the element without trailing spaces
128 // Non-zero only if element is an inset
129 Inset const * inset = nullptr;
131 // Only non-null for justified rows
134 // Non-empty if element is a string or is virtual
140 // is it possible to add contents to this element?
142 // properties with respect to row breaking (made of RowFlag enums)
143 int row_flags = Inline;
145 friend std::ostream & operator<<(std::ostream & os, Element const & row);
149 typedef Element value_type;
155 * Helper function: set variable \c var to value \c val, and mark
156 * row as changed is the values were different. This is intended
157 * for use when changing members of the row object.
159 template<class T1, class T2>
160 void change(T1 & var, T2 const val) {
166 * Helper function: set variable \c var to value \c val, and mark
167 * row as changed is the values were different. This is intended
168 * for use when changing members of the row object.
169 * This is the const version, useful for mutable members.
171 template<class T1, class T2>
172 void change(T1 & var, T2 const val) const {
178 bool changed() const { return changed_; }
180 void changed(bool c) const { changed_ = c; }
182 bool selection() const;
184 * Set the selection begin and end and whether the left and/or
185 * right margins are selected.
186 * This is const because we update the selection status only at
189 void setSelectionAndMargins(DocIterator const & beg,
190 DocIterator const & end) const;
191 /// no selection on this row.
192 void clearSelectionAndMargins() const;
195 void pit(pit_type p) { pit_ = p; }
197 pit_type pit() const { return pit_; }
199 void pos(pos_type p) { pos_ = p; }
201 pos_type pos() const { return pos_; }
203 void endpos(pos_type p) { end_ = p; }
205 pos_type endpos() const { return end_; }
207 void right_boundary(bool b) { right_boundary_ = b; }
209 bool right_boundary() const { return right_boundary_; }
211 void flushed(bool b) { flushed_ = b; }
213 bool flushed() const { return flushed_; }
216 Dimension const & dim() const { return dim_; }
218 Dimension & dim() { return dim_; }
220 int height() const { return dim_.height(); }
221 /// The width of the row, including the left margin, but not the right one.
222 int width() const { return dim_.wid; }
224 int ascent() const { return dim_.asc; }
226 int descent() const { return dim_.des; }
229 Dimension const & contents_dim() const { return contents_dim_; }
231 Dimension & contents_dim() { return contents_dim_; }
233 /// The offset of the left-most cursor position on the row
235 /// The offset of the right-most cursor position on the row
238 // Set the extra spacing for every expanding character in STRING-type
239 // elements. \param w is the total amount of extra width for the row to be
240 // distributed among expanders. \return false if the justification fails.
241 bool setExtraWidth(int w);
244 void add(pos_type pos, Inset const * ins, Dimension const & dim,
245 Font const & f, Change const & ch);
247 void add(pos_type pos, char_type const c,
248 Font const & f, Change const & ch);
250 void addVirtual(pos_type pos, docstring const & s,
251 Font const & f, Change const & ch);
253 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
255 void addMarginSpace(pos_type pos, int width, Font const & f, Change const & ch);
258 typedef std::vector<Element> Elements;
260 typedef Elements::iterator iterator;
262 typedef Elements::const_iterator const_iterator;
264 iterator begin() { return elements_.begin(); }
266 iterator end() { return elements_.end(); }
268 const_iterator begin() const { return elements_.begin(); }
270 const_iterator end() const { return elements_.end(); }
273 bool empty() const { return elements_.empty(); }
275 Element & front() { return elements_.front(); }
277 Element const & front() const { return elements_.front(); }
279 Element & back() { return elements_.back(); }
281 Element const & back() const { return elements_.back(); }
282 /// add element at the end and update width
283 void push_back(Element const &);
284 /// remove last element and update width
287 * if row width is too large, remove all elements after last
288 * separator and update endpos if necessary. If all that
289 * remains is a large word, cut it to \param width.
290 * \param width maximum width of the row.
291 * \param next_width available width on next row.
292 * \return list of elements remaining after breaking.
294 Elements shortenIfNeeded(int const width, int const next_width);
297 * If last element of the row is a string, compute its width
303 * Find sequences of right-to-left elements and reverse them.
304 * This should be called once the row is completely built.
308 bool isRTL() const { return rtl_; }
310 void setRTL(bool rtl) { rtl_ = rtl; }
312 bool needsChangeBar() const { return changebar_; }
314 void needsChangeBar(bool ncb) { changebar_ = ncb; }
316 /// Find row element that contains \c pos, and compute x offset.
317 const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
319 friend std::ostream & operator<<(std::ostream & os, Row const & row);
321 /// additional width for separators in justified rows (i.e. space)
322 double separator = 0;
323 /// width of hfills in the label
324 double label_hfill = 0;
325 /// the left margin position of the row
327 /// the right margin of the row
328 int right_margin = 0;
330 mutable pos_type sel_beg = -1;
332 mutable pos_type sel_end = -1;
334 mutable bool begin_margin_sel = false;
336 mutable bool end_margin_sel = false;
339 /// Decides whether the margin is selected.
341 * \param margin_begin
345 bool isMarginSelected(bool left, DocIterator const & beg,
346 DocIterator const & end) const;
347 /// Set the selection begin and end.
348 void setSelection(pos_type sel_beg, pos_type sel_end) const;
351 * Returns true if a char or string with font \c f and change
352 * type \c ch can be added to the current last element of the
355 bool sameString(Font const & f, Change const & ch) const;
360 /// has the Row appearance changed since last drawing?
361 mutable bool changed_ = true;
362 /// Index of the paragraph that contains this row
364 /// first pos covered by this row
366 /// one behind last pos covered by this row
368 // Is there a boundary at the end of the row (display inset...)
369 bool right_boundary_ = false;
370 // Shall the row be flushed when it is supposed to be justified?
371 bool flushed_ = false;
374 /// Row contents dimension. Does not contain the space above/below row.
375 Dimension contents_dim_;
376 /// true when this row lives in a right-to-left paragraph
378 /// true when a changebar should be drawn in the margin
379 bool changebar_ = false;
382 std::ostream & operator<<(std::ostream & os, Row::Elements const & elts);
386 * Each paragraph is broken up into a number of rows on the screen.
387 * This is a list of such on-screen rows, ordered from the top row
390 typedef std::vector<Row> RowList;