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