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