]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
Limit the nopassthurchars case in beamer to URL
[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 "InsetList.h"
19 #include "LayoutEnums.h"
20 #include "ParagraphMetrics.h"
21
22 #include "support/types.h"
23
24 #include <map>
25
26 namespace lyx {
27
28 class BufferView;
29 class Cursor;
30 class CursorSlice;
31 class MetricsInfo;
32 class Text;
33
34 /// A map from a Text to the map of paragraphs metrics
35 class TextMetrics
36 {
37         /// noncopyable
38         TextMetrics(TextMetrics const &);
39         void operator=(TextMetrics const &);
40 public:
41         /// Default constructor (only here for STL containers).
42         TextMetrics() : bv_(0), text_(0), max_width_(0), tight_(false) {}
43         /// The only useful constructor.
44         TextMetrics(BufferView *, Text *);
45
46         ///
47         bool contains(pit_type pit) const;
48         ///
49         void forget(pit_type pit);
50         ///
51         std::pair<pit_type, ParagraphMetrics const *> first() const;
52         ///
53         std::pair<pit_type, ParagraphMetrics const *> firstVisible() const;
54         ///
55         std::pair<pit_type, ParagraphMetrics const *> last() const;
56         /// is this row the last in the text?
57         bool isLastRow(Row const & row) const;
58         /// is this row the first in the text?
59         bool isFirstRow(Row const & row) const;
60         ///
61         void setRowChanged(pit_type pit, pos_type pos);
62
63         ///
64         Dimension const & dim() const { return dim_; }
65         ///
66         Point const & origin() const { return origin_; }
67
68         ///
69         ParagraphMetrics const & parMetrics(pit_type) const;
70         ///
71         ParagraphMetrics & parMetrics(pit_type);
72
73         ///
74         void newParMetricsDown();
75         ///
76         void newParMetricsUp();
77
78         /// Update metrics up to \c bv_height. Only usable for main text (for now).
79         void updateMetrics(pit_type const anchor_pit, int const anchor_ypos,
80                        int const bv_height);
81
82         /// compute text metrics.
83         bool metrics(MetricsInfo const & mi, Dimension & dim, int min_width = 0);
84
85         /// The "nodraw" drawing stage for one single paragraph: set the
86         /// positions of the insets contained in this paragraph in metrics
87         /// cache. Related to BufferView::updatePosCache.
88         void updatePosCache(pit_type pit) const;
89
90         /// Gets the fully instantiated font at a given position in a paragraph.
91         /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
92         /// The difference is that this one is used for displaying, and thus we
93         /// are allowed to make cosmetic improvements. For instance make footnotes
94         /// smaller. (Asger)
95         Font displayFont(pit_type pit, pos_type pos) const;
96
97         /// Gets the fully instantiated label font of a paragraph.
98         /// Basically the same routine as displayFont, but specialized for
99         /// a layout font.
100         Font labelDisplayFont(pit_type pit) const;
101
102
103         /// There are currently two font mechanisms in LyX:
104         /// 1. The font attributes in a lyxtext, and
105         /// 2. The inset-specific font properties, defined in an inset's
106         /// metrics() and draw() methods and handed down the inset chain through
107         /// the pi/mi parameters, and stored locally in a lyxtext in font_.
108         /// This is where the two are integrated in the final fully realized
109         /// font.
110         void applyOuterFont(Font &) const;
111
112         /// is this position in the paragraph right-to-left?
113         bool isRTL(CursorSlice const & sl, bool boundary) const;
114         /// is between pos-1 and pos an RTL<->LTR boundary?
115         bool isRTLBoundary(pit_type pit, pos_type pos) const;
116         /// would be a RTL<->LTR boundary between pos and the given font?
117         bool isRTLBoundary(pit_type pit, pos_type pos, Font const & font) const;
118
119
120         /// Rebreaks the given paragraph.
121         /// \retval true if a full screen redraw is needed.
122         /// \retval false if a single paragraph redraw is enough.
123         bool redoParagraph(pit_type const pit, bool align_rows = true);
124         /// Clear cache of paragraph metrics
125         void clear() { par_metrics_.clear(); }
126         /// Is cache of paragraph metrics empty ?
127         bool empty() const { return par_metrics_.empty(); }
128
129         ///
130         int ascent() const { return dim_.asc; }
131         ///
132         int descent() const { return dim_.des; }
133         /// current text width.
134         int width() const { return dim_.wid; }
135         /// current text height.
136         int height() const { return dim_.height(); }
137
138         /**
139          * Returns the left beginning of a row starting at \c pos.
140          * This information cannot be taken from the layout object, because
141          * in LaTeX the beginning of the text fits in some cases
142          * (for example sections) exactly the label-width.
143          */
144         int leftMargin(pit_type pit, pos_type pos) const;
145         /// Return the left beginning of a row which is not the first one.
146         /// This is the left margin when there is no indentation.
147         int leftMargin(pit_type pit) const;
148
149         ///
150         int rightMargin(ParagraphMetrics const & pm) const;
151         int rightMargin(pit_type const pit) const;
152
153         ///
154         void draw(PainterInfo & pi, int x, int y) const;
155
156         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
157
158 private:
159         ///
160         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
161
162         /// the minimum space a manual label needs on the screen in pixels
163         int labelFill(Row const & row) const;
164
165         // Turn paragraph oh index \c pit into a single row
166         Row tokenizeParagraph(pit_type pit) const;
167
168         // Break the row produced by tokenizeParagraph() into a list of rows.
169         RowList breakParagraph(Row const & row) const;
170
171         // Expands the alignment of row \param row in paragraph \param par
172         LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
173         /// Aligns properly the row contents (computes spaces and fills)
174         void setRowAlignment(Row & row, int width) const;
175
176         /// Set the height of the row (without space above/below paragraph)
177         void setRowHeight(Row & row) const;
178         // Compute the space on top of a paragraph
179         int parTopSpacing(pit_type pit) const;
180         // Compute the space below a a paragraph
181         int parBottomSpacing(pit_type pit) const;
182
183         // Helper function for the other checkInsetHit method.
184         InsetList::Element * checkInsetHit(pit_type pit, int x, int y);
185
186
187 // Temporary public:
188 public:
189         /// returns the position near the specified x-coordinate of the row.
190         /// x is an absolute screen coord, it is set to the real beginning
191         /// of this column. This takes in account horizontal cursor row scrolling.
192         pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
193
194         /// returns pos in given par at given x coord.
195         pos_type x2pos(pit_type pit, int row, int x) const;
196
197         // FIXME: is there a need for this?
198         //int pos2x(pit_type pit, pos_type pos) const;
199
200         /// returns the row near the specified y-coordinate in a given paragraph
201         /// (relative to the screen). If assert_in_view is true, it is made sure
202         /// that the row is on screen completely; this might change the given pit.
203         Row const & getPitAndRowNearY(int & y, pit_type & pit,
204                 bool assert_in_view, bool up);
205
206         /// returns the paragraph number closest to screen y-coordinate.
207         /// This method uses the BufferView CoordCache to locate the
208         /// paragraph. The y-coodinate is allowed to be off-screen and
209         /// the CoordCache will be automatically updated if needed. This is
210         /// the reason why we need a non const BufferView.
211         pit_type getPitNearY(int y);
212
213         /// sets cursor recursively descending into nested editable insets
214         /**
215         \return the inset pointer if x,y is covering that inset
216         \param x,y are absolute screen coordinates.
217         \param assert_in_view if true the cursor will be set on a row
218            that is completely visible
219         \param up whether we are going up or down (only used when
220            assert_in_view is true
221         \retval inset is null if the cursor is positioned over normal
222                text in the current Text object. Otherwise it is the inset
223                that the cursor points to, like for Inset::editXY.
224         */
225         /// FIXME: cleanup to use BufferView::getCoveringInset() and
226         /// setCursorFromCoordinates() instead of checkInsetHit().
227         Inset * editXY(Cursor & cur, int x, int y,
228                 bool assert_in_view = false, bool up = true);
229
230         /// sets cursor only within this Text.
231         /// x,y are screen coordinates
232         void setCursorFromCoordinates(Cursor & cur, int x, int y);
233
234         ///
235         int cursorX(CursorSlice const & sl, bool boundary) const;
236         ///
237         int cursorY(CursorSlice const & sl, bool boundary) const;
238
239         ///
240         bool cursorHome(Cursor & cur);
241         ///
242         bool cursorEnd(Cursor & cur);
243         ///
244         void deleteLineForward(Cursor & cur);
245
246         /// Returns an inset if inset was hit, or 0 if not.
247         /// \warning This method is not recursive! It will return the
248         /// outermost inset within this Text.
249         /// \sa BufferView::getCoveringInset() to get the innermost inset.
250         Inset * checkInsetHit(int x, int y);
251
252         /// calculates the position of a completion popup
253         void completionPosAndDim(Cursor const & cur, int & x, int & y,
254                 Dimension & dim) const;
255
256 private:
257
258         /// The BufferView owner.
259         BufferView * bv_;
260
261         /// The text contents (the model).
262         /// \todo FIXME: this should be const.
263         Text * text_;
264
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         /// if true, do not expand insets to max width artificially
272         bool tight_;
273         mutable Point origin_;
274
275 // temporary public:
276 public:
277         /// our 'outermost' font.
278         /// This is handed down from the surrounding
279         /// inset through the pi/mi parameter (pi.base.font)
280         /// It is used in applyOuterFont() and setCharFont() for reasons
281         /// that are not clear... to hand hand the outermost language and
282         /// also for char style apparently.
283         Font font_;
284 };
285
286 /// return the default height of a row in pixels, considering font zoom
287 int defaultRowHeight();
288
289 } // namespace lyx
290
291 #endif // TEXT_METRICS_H