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 // FIXME: We only need Point class definition, not the full
20 #include "CoordCache.h"
21 #include "ParagraphMetrics.h"
23 #include "support/types.h"
25 #include <boost/utility.hpp>
26 #include <boost/tuple/tuple.hpp>
38 /// A map from a Text to the map of paragraphs metrics
42 /// Default constructor (only here for STL containers).
43 TextMetrics(): text_(0) {}
44 /// The only useful constructor.
45 TextMetrics(BufferView *, Text *);
48 bool has(pit_type pit) const;
50 ParagraphMetrics const & parMetrics(pit_type) const;
53 Dimension const & dimension() const;
55 Point const & origin() const { return origin_; }
58 /// compute text metrics.
59 bool metrics(MetricsInfo & mi, Dimension & dim);
61 /// Gets the fully instantiated font at a given position in a paragraph
62 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
63 /// The difference is that this one is used for displaying, and thus we
64 /// are allowed to make cosmetic improvements. For instance make footnotes
66 Font getDisplayFont(pit_type pit,
69 /// There are currently two font mechanisms in LyX:
70 /// 1. The font attributes in a lyxtext, and
71 /// 2. The inset-specific font properties, defined in an inset's
72 /// metrics() and draw() methods and handed down the inset chain through
73 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
74 /// This is where the two are integrated in the final fully realized
76 void applyOuterFont(Font &) const;
78 /// is this position in the paragraph right-to-left?
79 bool isRTL(CursorSlice const & sl, bool boundary) const;
80 /// is between pos-1 and pos an RTL<->LTR boundary?
81 bool isRTLBoundary(pit_type pit,
83 /// would be a RTL<->LTR boundary between pos and the given font?
84 bool isRTLBoundary(pit_type pit,
85 pos_type pos, Font const & font) const;
88 /// Rebreaks the given paragraph.
89 /// \retval true if a full screen redraw is needed.
90 /// \retval false if a single paragraph redraw is enough.
91 bool redoParagraph(pit_type const pit);
92 /// Clear cache of paragraph metrics
93 void clear() { par_metrics_.clear(); }
96 int ascent() const { return dim_.asc; }
98 int descent() const { return dim_.des; }
99 /// current text width.
100 int width() const { return dim_.wid; }
101 /// current text heigth.
102 int height() const { return dim_.height(); }
105 int maxWidth() const { return max_width_; }
107 int singleWidth(pit_type const pit, pos_type pos) const;
110 int rightMargin(ParagraphMetrics const & pm) const;
111 int rightMargin(pit_type const pit) const;
113 /** this calculates the specified parameters. needed when setting
114 * the cursor and when creating a visible row */
115 void computeRowMetrics(pit_type pit, Row & row) const;
118 void draw(PainterInfo & pi, int x, int y) const;
120 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
124 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
126 /// draw textselection.
127 /// FIXME: simplify to just to single row painting.
128 void drawSelection(PainterInfo & pi,
129 DocIterator const & beg, ///< selection begin.
130 DocIterator const & end, ///< selection end.
133 /// the minimum space a manual label needs on the screen in pixels
134 int labelFill(pit_type const pit, Row const & row) const;
137 int labelEnd(pit_type const pit) const;
139 /// sets row.end to the pos value *after* which a row should break.
140 /// for example, the pos after which isNewLine(pos) == true
141 pit_type rowBreakPoint(
147 /// sets row.width to the minimum space a row needs on the screen in pixel
151 pos_type const first,
155 /// Calculate and set the height of the row
156 boost::tuple<int, int> rowHeight(
158 pos_type const first,
162 /// draw selection for a single row
163 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
164 DocIterator const & beg, DocIterator const & end,
165 bool drawOnBegMargin, bool drawOnEndMargin) const;
169 /// returns the column near the specified x-coordinate of the row.
170 /// x is an absolute screen coord, it is set to the real beginning
172 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
173 bool & boundary) const;
175 /// returns pos in given par at given x coord.
176 pos_type x2pos(pit_type pit, int row, int x) const;
178 // FIXME: is there a need for this?
179 //int pos2x(pit_type pit, pos_type pos) const;
181 /** returns row near the specified
182 * y-coordinate in given paragraph (relative to the screen).
184 Row const & getRowNearY(int y,
187 /// returns the paragraph number closest to screen y-coordinate.
188 /// This method uses the BufferView CoordCache to locate the
189 /// paragraph. The y-coodinate is allowed to be off-screen and
190 /// the CoordCache will be automatically updated if needed. This is
191 /// the reason why we need a non const BufferView.
192 pit_type getPitNearY(int y);
194 /// sets cursor recursively descending into nested editable insets
196 \return the inset pointer if x,y is covering that inset
197 \param x,y are absolute screen coordinates.
198 \retval inset is non-null if the cursor is positionned inside
200 /// FIXME: cleanup to use BufferView::getCoveringInset() and
201 /// setCursorFromCoordinates() instead of checkInsetHit().
202 Inset * editXY(Cursor & cur, int x, int y);
204 /// sets cursor only within this Text.
205 /// x,y are screen coordinates
206 void setCursorFromCoordinates(Cursor & cur, int x, int y);
209 int cursorX(CursorSlice const & cursor,
210 bool boundary) const;
212 int cursorY(CursorSlice const & cursor,
213 bool boundary) const;
216 void cursorPrevious(Cursor & cur);
218 void cursorNext(Cursor & cur);
220 bool cursorHome(Cursor & cur);
222 bool cursorEnd(Cursor & cur);
224 void deleteLineForward(Cursor & cur);
226 /// is this row the last in the text?
227 bool isLastRow(pit_type pit, Row const & row) const;
228 /// is this row the first in the text?
229 bool isFirstRow(pit_type pit, Row const & row) const;
231 /// Returns an inset if inset was hit, or 0 if not.
232 /// \warning This method is not recursive! It will return the
233 /// outermost inset within this Text.
234 /// \sa BufferView::getCoveringInset() to get the innermost inset.
235 Inset * checkInsetHit(int x, int y);
238 * Returns the left beginning of the text.
239 * This information cannot be taken from the layout object, because
240 * in LaTeX the beginning of the text fits in some cases
241 * (for example sections) exactly the label-width.
243 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
244 int leftMargin(int max_width, pit_type pit) const;
248 /// The BufferView owner.
251 /// The text contents (the model).
252 /// \todo FIXME: this should be const.
256 /// A map from paragraph index number to paragraph metrics
257 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
259 mutable ParMetricsCache par_metrics_;
262 mutable Point origin_;
266 /// our 'outermost' font.
267 /// This is handed down from the surrounding
268 /// inset through the pi/mi parameter (pi.base.font)
269 /// It is used in applyOuterFont() and setCharFont() for reasons
270 /// that are not clear... to hand hand the outermost language and
271 /// also for char style apparently.
275 /// return the default height of a row in pixels, considering font zoom
276 int defaultRowHeight();
280 #endif // TEXT_METRICS_H