]> git.lyx.org Git - features.git/blob - src/Row.h
Re-implement text justification
[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
22 #include "support/docstring.h"
23 #include "support/types.h"
24
25 #include <vector>
26
27 namespace lyx {
28
29 class DocIterator;
30 class Inset;
31
32 /**
33  * FIXME: Change Row object to operate only on integers and not doubles.
34  *
35  * This use of double is only useful to distribute the extra
36  * horizontal space between separators in justified text. If we do
37  * integer arithmetic, then it is possible to have two groups of
38  * separators, with size s or s+1. Then strings can be drawn without
39  * cutting at separators in justfied text, as it is done in
40  * non-justified text. This will improve performance.
41  */
42
43
44 /**
45  * An on-screen row of text. A paragraph is broken into a RowList for
46  * display. Each Row contains a tokenized description of the contents
47  * of the line.
48  */
49 class Row {
50 public:
51         // Possible types of row elements
52         enum Type {
53                 // a string of character
54                 STRING,
55                 /**
56                  * Something (completion, end-of-par marker)
57                  * that occupies space one screen but does not
58                  * correspond to any paragraph contents
59                  */
60                 VIRTUAL,
61                 // A stretchable space, basically
62                 SEPARATOR,
63                 // An inset
64                 INSET,
65                 // Some spacing described by its width, not a string
66                 SPACE
67         };
68
69 /**
70  * One element of a Row. It has a set of attributes that can be used
71  * by other methods that need to parse the Row contents.
72  */
73         struct Element {
74                 Element(Type const t, pos_type p, Font const & f, Change const & ch)
75                         : type(t), pos(p), endpos(p + 1), inset(0),
76                           extra(0), font(f), change(ch), final(false) {}
77
78                 // Return total width of element, including separator overhead
79                 double full_width() const { return dim.wid + extra * countSeparators(); };
80                 // Return the number of separator in the element (only STRING type)
81                 int countSeparators() const;
82
83                 /** Return position in pixels (from the left) of position
84                  * \param i in the row element.
85                  */
86                 double pos2x(pos_type const i) const;
87                 /** Return character position that is the closest to
88                  *  pixel position \param x. The value \param x is
89                  *  adjusted to the actual pixel position.
90                 */
91                 pos_type x2pos(int &x) const;
92                 /** Break the element if possible, so that its width is less
93                  * than \param w. Returns true on success. When \param force
94                  * is true, the string is cut at any place, other wise it
95                  * respects the row breaking rules of characters.
96                  */
97                 bool breakAt(int w, bool force);
98
99                 // Returns the position on left side of the element.
100                 pos_type left_pos() const;
101                 // Returns the position on right side of the element.
102                 pos_type right_pos() const;
103
104                 // The kind of row element
105                 Type type;
106                 // position of the element in the paragraph
107                 pos_type pos;
108                 // first position after the element in the paragraph
109                 pos_type endpos;
110                 // The dimension of the chunk (does not contains the
111                 // separator correction)
112                 Dimension dim;
113
114                 // Non-zero only if element is an inset
115                 Inset const * inset;
116
117                 // Only non-null for separator elements
118                 double extra;
119
120                 // Non-empty if element is a string or separator
121                 docstring str;
122                 //
123                 Font font;
124                 //
125                 Change change;
126                 // is it possible to add contents to this element?
127                 bool final;
128
129                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
130         };
131
132
133         ///
134         Row();
135         ///
136         bool changed() const { return changed_; }
137         ///
138         void setChanged(bool c) { changed_ = c; }
139         ///
140         void setCrc(size_type crc) const;
141         /// Set the selection begin and end.
142         /**
143           * This is const because we update the selection status only at draw()
144           * time.
145           */
146         void setSelection(pos_type sel_beg, pos_type sel_end) const;
147         ///
148         bool selection() const;
149         /// Set the selection begin and end and whether the left and/or right
150         /// margins are selected.
151         void setSelectionAndMargins(DocIterator const & beg,
152                 DocIterator const & end) const;
153
154         ///
155         void pos(pos_type p) { pos_ = p; }
156         ///
157         pos_type pos() const { return pos_; }
158         ///
159         void endpos(pos_type p) { end_ = p; }
160         ///
161         pos_type endpos() const { return end_; }
162         ///
163         void right_boundary(bool b) { right_boundary_ = b; }
164         ///
165         bool right_boundary() const { return right_boundary_; }
166
167         ///
168         Dimension const & dimension() const { return dim_; }
169         ///
170         Dimension & dimension() { return dim_; }
171         ///
172         int height() const { return dim_.height(); }
173         /// The width of the row, including the left margin, but not the right one.
174         int width() const { return dim_.wid; }
175         ///
176         int ascent() const { return dim_.asc; }
177         ///
178         int descent() const { return dim_.des; }
179
180         // Return the number of separators in the row
181         int countSeparators() const;
182         // Set the extra spacing for every separator in STRING elements
183         void setSeparatorExtraWidth(double w);
184
185         ///
186         void add(pos_type pos, Inset const * ins, Dimension const & dim,
187                  Font const & f, Change const & ch);
188         ///
189         void add(pos_type pos, char_type const c,
190                  Font const & f, Change const & ch);
191         ///
192         void addVirtual(pos_type pos, docstring const & s,
193                         Font const & f, Change const & ch);
194         ///
195         void addSeparator(pos_type pos, char_type const c,
196                           Font const & f, Change const & ch);
197         ///
198         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
199
200         ///
201         typedef std::vector<Element> Elements;
202         ///
203         typedef Elements::iterator iterator;
204         ///
205         typedef Elements::const_iterator const_iterator;
206         ///
207         iterator begin() { return elements_.begin(); }
208         ///
209         iterator end() { return elements_.end(); }
210         ///
211         const_iterator begin() const { return elements_.begin(); }
212         ///
213         const_iterator end() const { return elements_.end(); }
214
215         ///
216         bool empty() const { return elements_.empty(); }
217         ///
218         Element & front() { return elements_.front(); }
219         ///
220         Element const & front() const { return elements_.front(); }
221         ///
222         Element & back() { return elements_.back(); }
223         ///
224         Element const & back() const { return elements_.back(); }
225         /// remove last element
226         void pop_back();
227         /// remove all row elements
228         void clear() { elements_.clear(); }
229         /**
230          * if row width is too large, remove all elements after last
231          * separator and update endpos if necessary. If all that
232          * remains is a large word, cut it to \param width.
233          * \param body_pos minimum amount of text to keep.
234          * \param width maximum width of the row
235          */
236         void shortenIfNeeded(pos_type const body_pos, int const width);
237
238         /**
239          * If last element of the row is a string, compute its width
240          * and mark it final.
241          */
242         void finalizeLast();
243
244         /**
245          * Find sequences of right-to-left elements and reverse them.
246          * This should be called once the row is completely built.
247          */
248         void reverseRTL(bool rtl_par);
249
250         friend std::ostream & operator<<(std::ostream & os, Row const & row);
251
252         /// width of a separator (i.e. space)
253         double separator;
254         /// width of hfills in the label
255         double label_hfill;
256         /// the left margin position of the row
257         int left_margin;
258         /// the right margin of the row
259         int right_margin;
260         ///
261         mutable pos_type sel_beg;
262         ///
263         mutable pos_type sel_end;
264         ///
265         mutable bool begin_margin_sel;
266         ///
267         mutable bool end_margin_sel;
268
269 private:
270         /// Decides whether the margin is selected.
271         /**
272           * \param margin_begin
273           * \param beg
274           * \param end
275           */
276         bool isMarginSelected(bool left_margin, DocIterator const & beg,
277                 DocIterator const & end) const;
278
279         /**
280          * Returns true if a char or string with font \c f and change
281          * type \c ch can be added to the current last element of the
282          * row.
283          */
284         bool sameString(Font const & f, Change const & ch) const;
285
286         ///
287         Elements elements_;
288
289         /// has the Row appearance changed since last drawing?
290         mutable bool changed_;
291         /// CRC of row contents.
292         mutable size_type crc_;
293         /// first pos covered by this row
294         pos_type pos_;
295         /// one behind last pos covered by this row
296         pos_type end_;
297         // Is there is a boundary at the end of the row (display inset...)
298         bool right_boundary_;
299         /// Row dimension.
300         Dimension dim_;
301 };
302
303
304 } // namespace lyx
305
306 #endif