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