]> git.lyx.org Git - lyx.git/blob - src/Row.h
#9130 Text in main work area isn't rendered with high resolution
[lyx.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 width() const { return dim.wid + extra; };
80                 /** Return position in pixels (from the left) of position
81                  * \param i in the row element.
82                  */
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.
87                 */
88                 pos_type x2pos(double &x) const;
89                 /** Break the element if possible, so that its width is
90                  * less then \param w. Returns true on success.
91                  */
92                 bool breakAt(double w);
93
94                 // Returns the position on left side of the element.
95                 pos_type left_pos() const;
96                 // Returns the position on right side of the element.
97                 pos_type right_pos() const;
98
99                 // The kind of row element
100                 Type type;
101                 // position of the element in the paragraph
102                 pos_type pos;
103                 // first position after the element in the paragraph
104                 pos_type endpos;
105                 // The dimension of the chunk (does not contains the
106                 // separator correction)
107                 Dimension dim;
108
109                 // Non-zero only if element is an inset
110                 Inset const * inset;
111
112                 // Only non-null for separator elements
113                 double extra;
114
115                 // Non-empty if element is a string or separator
116                 docstring str;
117                 //
118                 Font font;
119                 //
120                 Change change;
121                 // is it possible to add contents to this element?
122                 bool final;
123
124                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
125         };
126
127
128         ///
129         Row();
130         ///
131         bool changed() const { return changed_; }
132         ///
133         void setChanged(bool c) { changed_ = c; }
134         ///
135         void setCrc(size_type crc) const;
136         /// Set the selection begin and end.
137         /**
138           * This is const because we update the selection status only at draw()
139           * time.
140           */
141         void setSelection(pos_type sel_beg, pos_type sel_end) const;
142         ///
143         bool selection() const;
144         /// Set the selection begin and end and whether the left and/or right
145         /// margins are selected.
146         void setSelectionAndMargins(DocIterator const & beg,
147                 DocIterator const & end) const;
148
149         ///
150         void pos(pos_type p) { pos_ = p; }
151         ///
152         pos_type pos() const { return pos_; }
153         ///
154         void endpos(pos_type p) { end_ = p; }
155         ///
156         pos_type endpos() const { return end_; }
157         ///
158         void right_boundary(bool b) { right_boundary_ = b; }
159         ///
160         bool right_boundary() const { return right_boundary_; }
161
162         ///
163         Dimension const & dimension() const { return dim_; }
164         ///
165         Dimension & dimension() { return dim_; }
166         ///
167         int height() const { return dim_.height(); }
168         ///
169         int width() const { return dim_.wid; }
170         ///
171         int ascent() const { return dim_.asc; }
172         ///
173         int descent() const { return dim_.des; }
174
175         ///
176         void add(pos_type pos, Inset const * ins, Dimension const & dim,
177                  Font const & f, Change const & ch);
178         ///
179         void add(pos_type pos, char_type const c,
180                  Font const & f, Change const & ch);
181         ///
182         void addVirtual(pos_type pos, docstring const & s,
183                         Font const & f, Change const & ch);
184         ///
185         void addSeparator(pos_type pos, char_type const c,
186                           Font const & f, Change const & ch);
187         ///
188         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
189
190         ///
191         typedef std::vector<Element> Elements;
192         ///
193         typedef Elements::iterator iterator;
194         ///
195         typedef Elements::const_iterator const_iterator;
196         ///
197         iterator begin() { return elements_.begin(); }
198         ///
199         iterator end() { return elements_.end(); }
200         ///
201         const_iterator begin() const { return elements_.begin(); }
202         ///
203         const_iterator end() const { return elements_.end(); }
204
205         ///
206         bool empty() const { return elements_.empty(); }
207         ///
208         Element & front() { return elements_.front(); }
209         ///
210         Element const & front() const { return elements_.front(); }
211         ///
212         Element & back() { return elements_.back(); }
213         ///
214         Element const & back() const { return elements_.back(); }
215         /// remove last element
216         void pop_back();
217         /// remove all row elements
218         void clear() { elements_.clear(); }
219         /**
220          * if row width is too large, remove all elements after last
221          * separator and update endpos if necessary. If all that
222          * remains is a large word, cut it to \param width.
223          * \param body_pos minimum amount of text to keep.
224          * \param width maximum width of the row
225          */
226         void shortenIfNeeded(pos_type const body_pos, int const width);
227
228         /**
229          * If last element of the row is a string, compute its width
230          * and mark it final.
231          */
232         void finalizeLast();
233
234         /**
235          * Find sequences of right-to-left elements and reverse them.
236          * This should be called once the row is completely built.
237          */
238         void reverseRTL(bool rtl_par);
239
240         friend std::ostream & operator<<(std::ostream & os, Row const & row);
241
242         /// width of a separator (i.e. space)
243         double separator;
244         /// width of hfills in the label
245         double label_hfill;
246         /// the x position of the row (left margin)
247         double x;
248         /// the right margin of the row
249         int right_margin;
250         ///
251         mutable pos_type sel_beg;
252         ///
253         mutable pos_type sel_end;
254         ///
255         mutable bool begin_margin_sel;
256         ///
257         mutable bool end_margin_sel;
258
259 private:
260         /// Decides whether the margin is selected.
261         /**
262           * \param margin_begin
263           * \param beg
264           * \param end
265           */
266         bool isMarginSelected(bool left_margin, DocIterator const & beg,
267                 DocIterator const & end) const;
268
269         /**
270          * Returns true if a char or string with font \c f and change
271          * type \c ch can be added to the current last element of the
272          * row.
273          */
274         bool sameString(Font const & f, Change const & ch) const;
275
276         ///
277         Elements elements_;
278
279         /// has the Row appearance changed since last drawing?
280         mutable bool changed_;
281         /// CRC of row contents.
282         mutable size_type crc_;
283         /// first pos covered by this row
284         pos_type pos_;
285         /// one behind last pos covered by this row
286         pos_type end_;
287         // Is there is a boundary at the end of the row (display inset...)
288         bool right_boundary_;
289         /// Row dimension.
290         Dimension dim_;
291 };
292
293
294 } // namespace lyx
295
296 #endif