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"
22 #include "support/docstring.h"
23 #include "support/types.h"
33 * An on-screen row of text. A paragraph is broken into a RowList for
34 * display. Each Row contains a tokenized description of the contents
39 // Possible types of row elements
41 // a string of character
44 * Something (completion, end-of-par marker)
45 * that occupies space one screen but does not
46 * correspond to any paragraph contents
51 // Some spacing described by its width, not a string
56 * One element of a Row. It has a set of attributes that can be used
57 * by other methods that need to parse the Row contents.
60 Element(Type const t, pos_type p, Font const & f, Change const & ch)
61 : type(t), pos(p), endpos(p + 1), inset(0),
62 extra(0), font(f), change(ch), final(false) {}
64 // Return the number of separator in the element (only STRING type)
65 int countSeparators() const;
67 // Return total width of element, including separator overhead
68 // FIXME: Cache this value or the number of expanders?
69 double full_width() const { return dim.wid + extra * countExpanders(); }
70 // Return the number of expanding characters in the element (only STRING
72 int countExpanders() const;
73 // Return the amount of expansion: the number of expanding characters
74 // that get stretched during justification, times the em of the font
75 // (only STRING type).
76 int expansionAmount() const;
77 // set extra proportionally to the font em value.
78 void setExtra(double extra_per_em);
80 /** Return position in pixels (from the left) of position
81 * \param i in the row element.
83 double pos2x(pos_type const i) const;
84 /** Return character position that is the closest to
85 * pixel position \param x. The value \param x is
86 * adjusted to the actual pixel position.
88 pos_type x2pos(int &x) const;
89 /** Break the element if possible, so that its width is less
90 * than \param w. Returns true on success. When \param force
91 * is true, the string is cut at any place, other wise it
92 * respects the row breaking rules of characters.
94 bool breakAt(int w, bool force);
96 // Returns the position on left side of the element.
97 pos_type left_pos() const;
98 // Returns the position on right side of the element.
99 pos_type right_pos() const;
102 bool isRTL() const { return font.isVisibleRightToLeft(); }
103 // This is true for virtual elements.
104 // Note that we do not use the type here. The two definitions
105 // should be equivalent
106 bool isVirtual() const { return pos == endpos; }
108 // The kind of row element
110 // position of the element in the paragraph
112 // first position after the element in the paragraph
114 // The dimension of the chunk (does not contains the
115 // separator correction)
118 // Non-zero only if element is an inset
121 // Only non-null for justified rows
124 // Non-empty if element is a string or is virtual
130 // is it possible to add contents to this element?
133 friend std::ostream & operator<<(std::ostream & os, Element const & row);
140 * Helper function: set variable \c var to value \c val, and mark
141 * row as changed is the values were different. This is intended
142 * for use when changing members of the row object.
144 template<class T1, class T2>
145 void change(T1 & var, T2 const val) {
151 * Helper function: set variable \c var to value \c val, and mark
152 * row as changed is the values were different. This is intended
153 * for use when changing members of the row object.
154 * This is the const version, useful for mutable members.
156 template<class T1, class T2>
157 void change(T1 & var, T2 const val) const {
163 bool changed() const { return changed_; }
165 void changed(bool c) const { changed_ = c; }
167 bool selection() const;
169 * Set the selection begin and end and whether the left and/or
170 * right margins are selected.
171 * This is const because we update the selection status only at
174 void setSelectionAndMargins(DocIterator const & beg,
175 DocIterator const & end) const;
176 /// no selection on this row.
177 void clearSelectionAndMargins() const;
180 void pit(pit_type p) { pit_ = p; }
182 pit_type pit() const { return pit_; }
184 void pos(pos_type p) { pos_ = p; }
186 pos_type pos() const { return pos_; }
188 void endpos(pos_type p) { end_ = p; }
190 pos_type endpos() const { return end_; }
192 void right_boundary(bool b) { right_boundary_ = b; }
194 bool right_boundary() const { return right_boundary_; }
196 void flushed(bool b) { flushed_ = b; }
198 bool flushed() const { return flushed_; }
201 Dimension const & dim() const { return dim_; }
203 Dimension & dim() { return dim_; }
205 int height() const { return dim_.height(); }
206 /// The width of the row, including the left margin, but not the right one.
207 int width() const { return dim_.wid; }
209 int ascent() const { return dim_.asc; }
211 int descent() const { return dim_.des; }
213 /// The offset of the left-most cursor position on the row
215 /// The offset of the right-most cursor position on the row
218 // Return the number of separators in the row
219 int countSeparators() const;
220 // Set the extra spacing for every expanding character in STRING-type
221 // elements. \param w is the total amount of extra width for the row to be
222 // distributed among expanders. \return false if the justification fails.
223 bool setExtraWidth(int w);
226 void add(pos_type pos, Inset const * ins, Dimension const & dim,
227 Font const & f, Change const & ch);
229 void add(pos_type pos, char_type const c,
230 Font const & f, Change const & ch);
232 void addVirtual(pos_type pos, docstring const & s,
233 Font const & f, Change const & ch);
235 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
238 typedef std::vector<Element> Elements;
240 typedef Elements::iterator iterator;
242 typedef Elements::const_iterator const_iterator;
244 iterator begin() { return elements_.begin(); }
246 iterator end() { return elements_.end(); }
248 const_iterator begin() const { return elements_.begin(); }
250 const_iterator end() const { return elements_.end(); }
253 bool empty() const { return elements_.empty(); }
255 Element & front() { return elements_.front(); }
257 Element const & front() const { return elements_.front(); }
259 Element & back() { return elements_.back(); }
261 Element const & back() const { return elements_.back(); }
262 /// remove last element
264 /// remove all row elements
265 void clear() { *this = Row(); }
267 * if row width is too large, remove all elements after last
268 * separator and update endpos if necessary. If all that
269 * remains is a large word, cut it to \param width.
270 * \param body_pos minimum amount of text to keep.
271 * \param width maximum width of the row.
272 * \param available width on next row.
273 * \return true if the row has been shortened.
275 bool shortenIfNeeded(pos_type const body_pos, int const width, int const next_width);
278 * If last element of the row is a string, compute its width
284 * Find sequences of right-to-left elements and reverse them.
285 * This should be called once the row is completely built.
287 void reverseRTL(bool rtl_par);
289 bool isRTL() const { return rtl_; }
291 bool needsChangeBar() const { return changebar_; }
293 void needsChangeBar(bool ncb) { changebar_ = ncb; }
295 /// Find row element that contains \c pos, and compute x offset.
296 const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
298 friend std::ostream & operator<<(std::ostream & os, Row const & row);
300 /// additional width for separators in justified rows (i.e. space)
302 /// width of hfills in the label
304 /// the left margin position of the row
306 /// the right margin of the row
309 mutable pos_type sel_beg;
311 mutable pos_type sel_end;
313 mutable bool begin_margin_sel;
315 mutable bool end_margin_sel;
318 /// Decides whether the margin is selected.
320 * \param margin_begin
324 bool isMarginSelected(bool left, DocIterator const & beg,
325 DocIterator const & end) const;
326 /// Set the selection begin and end.
327 void setSelection(pos_type sel_beg, pos_type sel_end) const;
330 * Returns true if a char or string with font \c f and change
331 * type \c ch can be added to the current last element of the
334 bool sameString(Font const & f, Change const & ch) const;
339 /// has the Row appearance changed since last drawing?
340 mutable bool changed_;
341 /// Index of the paragraph that contains this row
343 /// first pos covered by this row
345 /// one behind last pos covered by this row
347 // Is there a boundary at the end of the row (display inset...)
348 bool right_boundary_;
349 // Shall the row be flushed when it is supposed to be justified?
353 /// true when this row lives in a right-to-left paragraph
355 /// true when a changebar should be drawn in the margin