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"
22 #include <boost/utility.hpp>
23 #include <boost/tuple/tuple.hpp>
35 /// A map from a Text to the map of paragraphs metrics
39 /// Default constructor (only here for STL containers).
40 TextMetrics(): text_(0) {}
41 /// The only useful constructor.
42 TextMetrics(BufferView *, Text *);
44 ParagraphMetrics const & parMetrics(pit_type) const;
47 Dimension const & dimension() const;
48 Dimension const & dimension();
50 /// compute text metrics.
51 bool metrics(MetricsInfo & mi, Dimension & dim);
53 /// Gets the fully instantiated font at a given position in a paragraph
54 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
55 /// The difference is that this one is used for displaying, and thus we
56 /// are allowed to make cosmetic improvements. For instance make footnotes
58 Font getDisplayFont(pit_type pit,
61 /// There are currently two font mechanisms in LyX:
62 /// 1. The font attributes in a lyxtext, and
63 /// 2. The inset-specific font properties, defined in an inset's
64 /// metrics() and draw() methods and handed down the inset chain through
65 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
66 /// This is where the two are integrated in the final fully realized
68 void applyOuterFont(Font &) const;
70 /// is this position in the paragraph right-to-left?
71 bool isRTL(CursorSlice const & sl, bool boundary) const;
72 /// is between pos-1 and pos an RTL<->LTR boundary?
73 bool isRTLBoundary(pit_type pit,
75 /// would be a RTL<->LTR boundary between pos and the given font?
76 bool isRTLBoundary(pit_type pit,
77 pos_type pos, Font const & font) const;
80 /// Rebreaks the given paragraph.
81 /// \retval true if a full screen redraw is needed.
82 /// \retval false if a single paragraph redraw is enough.
83 bool redoParagraph(pit_type const pit);
84 /// Clear cache of paragraph metrics
85 void clear() { par_metrics_.clear(); }
88 int ascent() const { return dim_.asc; }
90 int descent() const { return dim_.des; }
91 /// current text width.
92 int width() const { return dim_.wid; }
93 /// current text heigth.
94 int height() const { return dim_.height(); }
97 int maxWidth() const { return max_width_; }
99 int singleWidth(pit_type const pit, pos_type pos) const;
102 int rightMargin(ParagraphMetrics const & pm) const;
103 int rightMargin(pit_type const pit) const;
105 /** this calculates the specified parameters. needed when setting
106 * the cursor and when creating a visible row */
107 void computeRowMetrics(pit_type pit, Row & row) const;
110 void draw(PainterInfo & pi, int x, int y) const;
112 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
116 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
118 /// draw textselection.
119 /// FIXME: simplify to just to single row painting.
120 void drawSelection(PainterInfo & pi,
121 DocIterator const & beg, ///< selection begin.
122 DocIterator const & end, ///< selection end.
125 /// the minimum space a manual label needs on the screen in pixels
126 int labelFill(pit_type const pit, Row const & row) const;
129 int labelEnd(pit_type const pit) const;
131 /// sets row.end to the pos value *after* which a row should break.
132 /// for example, the pos after which isNewLine(pos) == true
133 pit_type rowBreakPoint(
139 /// sets row.width to the minimum space a row needs on the screen in pixel
143 pos_type const first,
147 /// Calculate and set the height of the row
148 boost::tuple<int, int> rowHeight(
150 pos_type const first,
154 /// draw selection for a single row
155 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
156 DocIterator const & beg, DocIterator const & end,
157 bool drawOnBegMargin, bool drawOnEndMargin) const;
161 /// returns the column near the specified x-coordinate of the row.
162 /// x is an absolute screen coord, it is set to the real beginning
164 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
165 bool & boundary) const;
167 /// returns pos in given par at given x coord.
168 pos_type x2pos(pit_type pit, int row, int x) const;
170 // FIXME: is there a need for this?
171 //int pos2x(pit_type pit, pos_type pos) const;
173 /** returns row near the specified
174 * y-coordinate in given paragraph (relative to the screen).
176 Row const & getRowNearY(int y,
179 /// returns the paragraph number closest to screen y-coordinate.
180 /// This method uses the BufferView CoordCache to locate the
181 /// paragraph. The y-coodinate is allowed to be off-screen and
182 /// the CoordCache will be automatically updated if needed. This is
183 /// the reason why we need a non const BufferView.
184 pit_type getPitNearY(int y);
186 /// sets cursor recursively descending into nested editable insets
188 \return the inset pointer if x,y is covering that inset
189 \param x,y are absolute screen coordinates.
190 \retval inset is non-null if the cursor is positionned inside
192 /// FIXME: cleanup to use BufferView::getCoveringInset() and
193 /// setCursorFromCoordinates() instead of checkInsetHit().
194 Inset * editXY(Cursor & cur, int x, int y);
196 /// sets cursor only within this Text.
197 /// x,y are screen coordinates
198 void setCursorFromCoordinates(Cursor & cur, int x, int y);
201 int cursorX(CursorSlice const & cursor,
202 bool boundary) const;
204 int cursorY(CursorSlice const & cursor,
205 bool boundary) const;
208 void cursorPrevious(Cursor & cur);
210 void cursorNext(Cursor & cur);
212 bool cursorHome(Cursor & cur);
214 bool cursorEnd(Cursor & cur);
216 void deleteLineForward(Cursor & cur);
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;
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);
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.
235 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
236 int leftMargin(int max_width, pit_type pit) const;
240 /// The BufferView owner.
243 /// The text contents (the model).
244 /// \todo FIXME: this should be const.
248 /// A map from paragraph index number to paragraph metrics
249 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
251 mutable ParMetricsCache par_metrics_;
255 /// FIXME: transfer this code in CoordCache here.
257 /// A map from paragraph index number to screen point
258 typedef std::map<pit_type, Point> InnerParPosCache;
259 /// A map from a Text to the map of paragraphs to screen points
260 typedef std::map<Text const *, InnerParPosCache> ParPosCache;
261 /// Paragraph grouped by owning text
267 /// our 'outermost' font.
268 /// This is handed down from the surrounding
269 /// inset through the pi/mi parameter (pi.base.font)
270 /// It is used in applyOuterFont() and setCharFont() for reasons
271 /// that are not clear... to hand hand the outermost language and
272 /// also for char style apparently.
276 /// return the default height of a row in pixels, considering font zoom
277 int defaultRowHeight();
281 #endif // TEXT_METRICS_H