4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * \author Abdelrazak Younes
11 * Full author contact details are available in file CREDITS.
14 #ifndef TEXT_METRICS_H
15 #define TEXT_METRICS_H
18 #include "ParagraphMetrics.h"
20 #include "support/types.h"
31 /// A map from a Text to the map of paragraphs metrics
35 /// Default constructor (only here for STL containers).
36 TextMetrics() : text_(0) {}
37 /// The only useful constructor.
38 TextMetrics(BufferView *, Text *);
41 bool contains(pit_type pit) const;
43 ParagraphMetrics const & parMetrics(pit_type) const;
45 std::pair<pit_type, ParagraphMetrics const *> first() const;
47 std::pair<pit_type, ParagraphMetrics const *> last() const;
50 int parPosition(pit_type pit) const;
53 Dimension const & dimension() const { return dim_; }
55 Point const & origin() const { return origin_; }
58 /// compute text metrics.
59 bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
62 void newParMetricsDown();
64 void newParMetricsUp();
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
71 Font displayFont(pit_type pit, pos_type pos) const;
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
80 void applyOuterFont(Font &) const;
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,
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;
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(); }
100 int ascent() const { return dim_.asc; }
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(); }
109 int maxWidth() const { return max_width_; }
111 int singleWidth(pit_type const pit, pos_type pos) const;
114 int rightMargin(ParagraphMetrics const & pm) const;
115 int rightMargin(pit_type const pit) const;
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;
122 void draw(PainterInfo & pi, int x, int y) const;
124 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
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.
131 pos_type const first,
133 bool topBottomSpace = true) const;
137 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
139 /// the minimum space a manual label needs on the screen in pixels
140 int labelFill(pit_type const pit, Row const & row) const;
143 int labelEnd(pit_type const pit) const;
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 pos_type rowBreakPoint(
153 /// returns the minimum space a row needs on the screen in pixel
157 pos_type const first,
161 /// draw selection for a single row
162 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
163 Cursor const & cur, pit_type const pit) const;
167 /// returns the column near the specified x-coordinate of the row.
168 /// x is an absolute screen coord, it is set to the real beginning
170 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
171 bool & boundary) const;
173 /// returns pos in given par at given x coord.
174 pos_type x2pos(pit_type pit, int row, int x) const;
176 // FIXME: is there a need for this?
177 //int pos2x(pit_type pit, pos_type pos) const;
179 /// returns the row near the specified y-coordinate in a given paragraph
180 /// (relative to the screen). If assert_in_view is true, it is made sure
181 /// that the row is on screen completely; this might change the given pit.
182 Row const & getPitAndRowNearY(int & y, pit_type & pit,
183 bool assert_in_view, bool up);
185 /// returns the paragraph number closest to screen y-coordinate.
186 /// This method uses the BufferView CoordCache to locate the
187 /// paragraph. The y-coodinate is allowed to be off-screen and
188 /// the CoordCache will be automatically updated if needed. This is
189 /// the reason why we need a non const BufferView.
190 pit_type getPitNearY(int y);
192 /// sets cursor recursively descending into nested editable insets
194 \return the inset pointer if x,y is covering that inset
195 \param x,y are absolute screen coordinates.
196 \param assert_in_view if true the cursor will be set on a row
197 that is completely visible
198 \param up whether we are going up or down (only used when
199 assert_in_view is true
200 \retval inset is non-null if the cursor is positionned inside
202 /// FIXME: cleanup to use BufferView::getCoveringInset() and
203 /// setCursorFromCoordinates() instead of checkInsetHit().
204 Inset * editXY(Cursor & cur, int x, int y,
205 bool assert_in_view = false, bool up = true);
207 /// sets cursor only within this Text.
208 /// x,y are screen coordinates
209 void setCursorFromCoordinates(Cursor & cur, int x, int y);
212 int cursorX(CursorSlice const & cursor,
213 bool boundary) const;
215 int cursorY(CursorSlice const & cursor,
216 bool boundary) const;
219 bool cursorHome(Cursor & cur);
221 bool cursorEnd(Cursor & cur);
223 void deleteLineForward(Cursor & cur);
225 /// is this row the last in the text?
226 bool isLastRow(pit_type pit, Row const & row) const;
227 /// is this row the first in the text?
228 bool isFirstRow(pit_type pit, Row const & row) const;
230 /// Returns an inset if inset was hit, or 0 if not.
231 /// \warning This method is not recursive! It will return the
232 /// outermost inset within this Text.
233 /// \sa BufferView::getCoveringInset() to get the innermost inset.
234 Inset * checkInsetHit(int x, int y);
237 * Returns the left beginning of the text.
238 * This information cannot be taken from the layout object, because
239 * in LaTeX the beginning of the text fits in some cases
240 * (for example sections) exactly the label-width.
242 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
243 int leftMargin(int max_width, pit_type pit) const;
245 /// calculates the position of a completion popup
246 void completionPosAndDim(Cursor const & cur, int & x, int & y,
247 Dimension & dim) const;
250 friend class BufferView;
252 /// The BufferView owner.
255 /// The text contents (the model).
256 /// \todo FIXME: this should be const.
260 /// A map from paragraph index number to paragraph metrics
261 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
263 mutable ParMetricsCache par_metrics_;
266 mutable Point origin_;
270 /// our 'outermost' font.
271 /// This is handed down from the surrounding
272 /// inset through the pi/mi parameter (pi.base.font)
273 /// It is used in applyOuterFont() and setCharFont() for reasons
274 /// that are not clear... to hand hand the outermost language and
275 /// also for char style apparently.
279 /// return the default height of a row in pixels, considering font zoom
280 int defaultRowHeight();
284 #endif // TEXT_METRICS_H