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 // Something that should not happen (for error handling)
59 * One element of a Row. It has a set of attributes that can be used
60 * by other methods that need to parse the Row contents.
66 Element(Type const t, pos_type p, Font const & f, Change const & ch)
67 : type(t), pos(p), endpos(p + 1), font(f), change(ch) {}
70 // Return the number of separator in the element (only STRING type)
71 int countSeparators() const;
73 // Return total width of element, including separator overhead
74 // FIXME: Cache this value or the number of expanders?
75 double full_width() const { return dim.wid + extra * countExpanders(); }
76 // Return the number of expanding characters in the element (only STRING
78 int countExpanders() const;
79 // Return the amount of expansion: the number of expanding characters
80 // that get stretched during justification, times the em of the font
81 // (only STRING type).
82 int expansionAmount() const;
83 // set extra proportionally to the font em value.
84 void setExtra(double extra_per_em);
86 /** Return position in pixels (from the left) of position
87 * \param i in the row element.
89 double pos2x(pos_type const i) const;
90 /** Return character position that is the closest to
91 * pixel position \param x. The value \param x is
92 * adjusted to the actual pixel position.
94 pos_type x2pos(int &x) const;
95 /** Break the element in two if possible, so that its width is less
97 * \return an element containing the remainder of the text, or
98 * an invalid element if nothing happened.
99 * \param w: the desired maximum width
100 * \param force: if true, the string is cut at any place, otherwise it
101 * respects the row breaking rules of characters.
103 Element splitAt(int w, bool force);
104 // remove trailing spaces (useful for end of row)
108 bool isRTL() const { return font.isVisibleRightToLeft(); }
109 // This is true for virtual elements.
110 bool isVirtual() const { return type == VIRTUAL; }
111 // Invalid element, for error handling
112 bool isValid() const { return type !=INVALID; }
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; };
119 // The kind of row element
121 // position of the element in the paragraph
123 // first position after the element in the paragraph
125 // The dimension of the chunk (does not contains the
126 // separator correction)
129 // Non-zero only if element is an inset
130 Inset const * inset = nullptr;
132 // Only non-null for justified rows
135 // Non-empty if element is a string or is virtual
141 // is it possible to add contents to this element?
143 // properties with respect to row breaking (made of RowFlag enums)
144 int row_flags = Inline;
146 friend std::ostream & operator<<(std::ostream & os, Element const & row);
150 typedef Element value_type;
156 * Helper function: set variable \c var to value \c val, and mark
157 * row as changed is the values were different. This is intended
158 * for use when changing members of the row object.
160 template<class T1, class T2>
161 void change(T1 & var, T2 const val) {
167 * Helper function: set variable \c var to value \c val, and mark
168 * row as changed is the values were different. This is intended
169 * for use when changing members of the row object.
170 * This is the const version, useful for mutable members.
172 template<class T1, class T2>
173 void change(T1 & var, T2 const val) const {
179 bool changed() const { return changed_; }
181 void changed(bool c) const { changed_ = c; }
183 bool selection() const;
185 * Set the selection begin and end and whether the left and/or
186 * right margins are selected.
187 * This is const because we update the selection status only at
190 void setSelectionAndMargins(DocIterator const & beg,
191 DocIterator const & end) const;
192 /// no selection on this row.
193 void clearSelectionAndMargins() const;
196 void pit(pit_type p) { pit_ = p; }
198 pit_type pit() const { return pit_; }
200 void pos(pos_type p) { pos_ = p; }
202 pos_type pos() const { return pos_; }
204 void endpos(pos_type p) { end_ = p; }
206 pos_type endpos() const { return end_; }
208 void right_boundary(bool b) { right_boundary_ = b; }
210 bool right_boundary() const { return right_boundary_; }
212 void flushed(bool b) { flushed_ = b; }
214 bool flushed() const { return flushed_; }
217 Dimension const & dim() const { return dim_; }
219 Dimension & dim() { return dim_; }
221 int height() const { return dim_.height(); }
222 /// The width of the row, including the left margin, but not the right one.
223 int width() const { return dim_.wid; }
225 int ascent() const { return dim_.asc; }
227 int descent() const { return dim_.des; }
230 Dimension const & contents_dim() const { return contents_dim_; }
232 Dimension & contents_dim() { return contents_dim_; }
234 /// The offset of the left-most cursor position on the row
236 /// The offset of the right-most cursor position on the row
239 // Return the number of separators in the row
240 int countSeparators() const;
241 // Set the extra spacing for every expanding character in STRING-type
242 // elements. \param w is the total amount of extra width for the row to be
243 // distributed among expanders. \return false if the justification fails.
244 bool setExtraWidth(int w);
247 void add(pos_type pos, Inset const * ins, Dimension const & dim,
248 Font const & f, Change const & ch);
250 void add(pos_type pos, char_type const c,
251 Font const & f, Change const & ch, bool can_break);
253 void addVirtual(pos_type pos, docstring const & s,
254 Font const & f, Change const & ch);
256 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
259 typedef std::vector<Element> Elements;
261 typedef Elements::iterator iterator;
263 typedef Elements::const_iterator const_iterator;
265 iterator begin() { return elements_.begin(); }
267 iterator end() { return elements_.end(); }
269 const_iterator begin() const { return elements_.begin(); }
271 const_iterator end() const { return elements_.end(); }
274 bool empty() const { return elements_.empty(); }
276 Element & front() { return elements_.front(); }
278 Element const & front() const { return elements_.front(); }
280 Element & back() { return elements_.back(); }
282 Element const & back() const { return elements_.back(); }
283 /// add element at the end and update width
284 void push_back(Element const &);
285 /// remove last element and update width
288 * if row width is too large, remove all elements after last
289 * separator and update endpos if necessary. If all that
290 * remains is a large word, cut it to \param width.
291 * \param width maximum width of the row.
292 * \param available width on next row.
293 * \return true if the row has been shortened.
295 Elements shortenIfNeeded(int const width, int const next_width);
298 * If last element of the row is a string, compute its width
304 * Find sequences of right-to-left elements and reverse them.
305 * This should be called once the row is completely built.
307 void reverseRTL(bool rtl_par);
309 bool isRTL() const { return rtl_; }
311 bool needsChangeBar() const { return changebar_; }
313 void needsChangeBar(bool ncb) { changebar_ = ncb; }
315 /// Find row element that contains \c pos, and compute x offset.
316 const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
318 friend std::ostream & operator<<(std::ostream & os, Row const & row);
320 /// additional width for separators in justified rows (i.e. space)
321 double separator = 0;
322 /// width of hfills in the label
323 double label_hfill = 0;
324 /// the left margin position of the row
326 /// the right margin of the row
327 int right_margin = 0;
329 mutable pos_type sel_beg = -1;
331 mutable pos_type sel_end = -1;
333 mutable bool begin_margin_sel = false;
335 mutable bool end_margin_sel = false;
338 /// Decides whether the margin is selected.
340 * \param margin_begin
344 bool isMarginSelected(bool left, DocIterator const & beg,
345 DocIterator const & end) const;
346 /// Set the selection begin and end.
347 void setSelection(pos_type sel_beg, pos_type sel_end) const;
350 * Returns true if a char or string with font \c f and change
351 * type \c ch can be added to the current last element of the
354 bool sameString(Font const & f, Change const & ch) const;
359 /// has the Row appearance changed since last drawing?
360 mutable bool changed_ = true;
361 /// Index of the paragraph that contains this row
363 /// first pos covered by this row
365 /// one behind last pos covered by this row
367 // Is there a boundary at the end of the row (display inset...)
368 bool right_boundary_ = false;
369 // Shall the row be flushed when it is supposed to be justified?
370 bool flushed_ = false;
373 /// Row contents dimension. Does not contain the space above/below row.
374 Dimension contents_dim_;
375 /// true when this row lives in a right-to-left paragraph
377 /// true when a changebar should be drawn in the margin
378 bool changebar_ = false;
381 std::ostream & operator<<(std::ostream & os, Row::Elements const & elts);
385 * Each paragraph is broken up into a number of rows on the screen.
386 * This is a list of such on-screen rows, ordered from the top row
389 typedef std::vector<Row> RowList;