]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
Cosmetics.
[lyx.git] / src / TextMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file TextMetrics.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TEXT_METRICS_H
15 #define TEXT_METRICS_H
16
17 #include "Font.h"
18 // FIXME: We only need Point class definition, not the full CoordCache.
19 #include "CoordCache.h"
20 #include "ParagraphMetrics.h"
21
22 #include "support/types.h"
23
24 #include <boost/tuple/tuple.hpp>
25
26 namespace lyx {
27
28 class BufferView;
29 class Cursor;
30 class CursorSlice;
31 class DocIterator;
32 class MetricsInfo;
33 class Text;
34
35 /// A map from a Text to the map of paragraphs metrics
36 class TextMetrics
37 {
38 public:
39         /// Default constructor (only here for STL containers).
40         TextMetrics() : text_(0) {}
41         /// The only useful constructor.
42         TextMetrics(BufferView *, Text *);
43         
44         ///
45         bool has(pit_type pit) const;
46         ///
47         ParagraphMetrics const & parMetrics(pit_type) const;
48         ///
49         std::pair<pit_type, ParagraphMetrics const *> first() const;
50         ///
51         std::pair<pit_type, ParagraphMetrics const *> last() const;
52
53         ///
54         int parPosition(pit_type pit) const;
55
56         ///
57         Dimension const & dimension() const { return dim_; }
58         ///
59         Point const & origin() const { return origin_; }
60
61
62         /// compute text metrics.
63         bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
64
65         ///
66         void newParMetricsDown();
67         ///
68         void newParMetricsUp();
69
70         /// Gets the fully instantiated font at a given position in a paragraph
71         /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
72         /// The difference is that this one is used for displaying, and thus we
73         /// are allowed to make cosmetic improvements. For instance make footnotes
74         /// smaller. (Asger)
75         Font getDisplayFont(pit_type pit,
76                 pos_type pos) const;
77
78         /// There are currently two font mechanisms in LyX:
79         /// 1. The font attributes in a lyxtext, and
80         /// 2. The inset-specific font properties, defined in an inset's
81         /// metrics() and draw() methods and handed down the inset chain through
82         /// the pi/mi parameters, and stored locally in a lyxtext in font_.
83         /// This is where the two are integrated in the final fully realized
84         /// font.
85         void applyOuterFont(Font &) const;
86
87         /// is this position in the paragraph right-to-left?
88         bool isRTL(CursorSlice const & sl, bool boundary) const;
89         /// is between pos-1 and pos an RTL<->LTR boundary?
90         bool isRTLBoundary(pit_type pit,
91           pos_type pos) const;
92         /// would be a RTL<->LTR boundary between pos and the given font?
93         bool isRTLBoundary(pit_type pit,
94           pos_type pos, Font const & font) const;
95
96
97         /// Rebreaks the given paragraph.
98         /// \retval true if a full screen redraw is needed.
99         /// \retval false if a single paragraph redraw is enough.
100         bool redoParagraph(pit_type const pit);
101         /// Clear cache of paragraph metrics
102         void clear() { par_metrics_.clear(); }
103
104         ///
105         int ascent() const { return dim_.asc; }
106         ///
107         int descent() const { return dim_.des; }
108         /// current text width.
109         int width() const { return dim_.wid; }
110         /// current text heigth.
111         int height() const { return dim_.height(); }
112
113         ///
114         int maxWidth() const { return max_width_; }
115         ///
116         int singleWidth(pit_type const pit,     pos_type pos) const;
117
118         ///
119         int rightMargin(ParagraphMetrics const & pm) const;
120         int rightMargin(pit_type const pit) const;
121
122         /** this calculates the specified parameters. needed when setting
123          * the cursor and when creating a visible row */
124         void computeRowMetrics(pit_type pit, Row & row, int width) const;
125
126         ///
127         void draw(PainterInfo & pi, int x, int y) const;
128         
129         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
130
131 private:
132         ///
133         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
134
135         /// draw textselection.
136         /// FIXME: simplify to just to single row painting.
137         void drawSelection(PainterInfo & pi,
138                 DocIterator const & beg, ///< selection begin.
139                 DocIterator const & end, ///< selection end.
140                 int x) const;
141
142         /// the minimum space a manual label needs on the screen in pixels
143         int labelFill(pit_type const pit, Row const & row) const;
144
145         /// FIXME??
146         int labelEnd(pit_type const pit) const;
147
148         /// sets row.end to the pos value *after* which a row should break.
149         /// for example, the pos after which isNewLine(pos) == true
150         pit_type rowBreakPoint(
151                 int width,
152                 pit_type const pit,
153                 pit_type first
154                 ) const;
155
156         /// sets row.width to the minimum space a row needs on the screen in pixel
157         int rowWidth(
158                 int right_margin,
159                 pit_type const pit,
160                 pos_type const first,
161                 pos_type const end
162                 ) const;
163
164         /// Calculate and set the height of the row
165         boost::tuple<int, int> rowHeight(
166                 pit_type const pit,
167                 pos_type const first,
168                 pos_type const end
169                 ) const;
170
171         /// draw selection for a single row
172         void drawRowSelection(PainterInfo & pi, int x, Row const & row,
173                 DocIterator const & beg, DocIterator const & end, 
174                 bool drawOnBegMargin, bool drawOnEndMargin) const;
175
176 // Temporary public:
177 public:
178         /// returns the column near the specified x-coordinate of the row.
179         /// x is an absolute screen coord, it is set to the real beginning
180         /// of this column.
181         pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
182                 bool & boundary) const;
183
184         /// returns pos in given par at given x coord.
185         pos_type x2pos(pit_type pit, int row, int x) const;
186
187         // FIXME: is there a need for this?
188         //int pos2x(pit_type pit, pos_type pos) const;
189
190         /** returns row near the specified
191           * y-coordinate in given paragraph (relative to the screen).
192           */
193         Row const & getRowNearY(int y,
194                 pit_type pit) const;
195
196         /// returns the paragraph number closest to screen y-coordinate.
197         /// This method uses the BufferView CoordCache to locate the
198         /// paragraph. The y-coodinate is allowed to be off-screen and
199         /// the CoordCache will be automatically updated if needed. This is
200         /// the reason why we need a non const BufferView.
201         pit_type getPitNearY(int y);
202
203         /// sets cursor recursively descending into nested editable insets
204         /**
205         \return the inset pointer if x,y is covering that inset
206         \param x,y are absolute screen coordinates.
207         \retval inset is non-null if the cursor is positionned inside
208         */
209         /// FIXME: cleanup to use BufferView::getCoveringInset() and
210         /// setCursorFromCoordinates() instead of checkInsetHit().
211         Inset * editXY(Cursor & cur, int x, int y);
212
213         /// sets cursor only within this Text.
214         /// x,y are screen coordinates
215         void setCursorFromCoordinates(Cursor & cur, int x, int y);
216
217         ///
218         int cursorX(CursorSlice const & cursor,
219                 bool boundary) const;
220         ///
221         int cursorY(CursorSlice const & cursor,
222                 bool boundary) const;
223
224         ///
225         void cursorPrevious(Cursor & cur);
226         ///
227         void cursorNext(Cursor & cur);
228         ///
229         bool cursorHome(Cursor & cur);
230         ///
231         bool cursorEnd(Cursor & cur);
232         ///
233         void deleteLineForward(Cursor & cur);
234
235         /// is this row the last in the text?
236         bool isLastRow(pit_type pit, Row const & row) const;
237         /// is this row the first in the text?
238         bool isFirstRow(pit_type pit, Row const & row) const;
239
240         /// Returns an inset if inset was hit, or 0 if not.
241         /// \warning This method is not recursive! It will return the
242         /// outermost inset within this Text.
243         /// \sa BufferView::getCoveringInset() to get the innermost inset.
244         Inset * checkInsetHit(int x, int y);
245
246         /**
247          * Returns the left beginning of the text.
248          * This information cannot be taken from the layout object, because
249          * in LaTeX the beginning of the text fits in some cases
250          * (for example sections) exactly the label-width.
251          */
252         int leftMargin(int max_width, pit_type pit, pos_type pos) const;
253         int leftMargin(int max_width, pit_type pit) const;
254
255 private:
256
257         /// The BufferView owner.
258         BufferView * bv_;
259
260         /// The text contents (the model).
261         /// \todo FIXME: this should be const.
262         Text * text_;
263
264         bool main_text_;
265         /// A map from paragraph index number to paragraph metrics
266         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
267         ///
268         mutable ParMetricsCache par_metrics_;
269         Dimension dim_;
270         int max_width_;
271         mutable Point origin_;
272
273 // temporary public:
274 public:
275         /// our 'outermost' font.
276         /// This is handed down from the surrounding
277         /// inset through the pi/mi parameter (pi.base.font)
278         /// It is used in applyOuterFont() and setCharFont() for reasons 
279         /// that are not clear... to hand hand the outermost language and
280         /// also for char style apparently.
281         Font font_;
282 };
283
284 /// return the default height of a row in pixels, considering font zoom
285 int defaultRowHeight();
286
287 } // namespace lyx
288
289 #endif // TEXT_METRICS_H