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