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 CoordCache.
19 #include "CoordCache.h"
20 #include "ParagraphMetrics.h"
22 #include "support/types.h"
24 #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 *);
45 bool has(pit_type pit) const;
47 ParagraphMetrics const & parMetrics(pit_type) const;
49 std::pair<pit_type, ParagraphMetrics const *> first() const;
51 std::pair<pit_type, ParagraphMetrics const *> last() const;
54 int parPosition(pit_type pit) const;
57 Dimension const & dimension() const { return dim_; }
59 Point const & origin() const { return origin_; }
62 /// compute text metrics.
63 bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
66 void newParMetricsDown();
68 void newParMetricsUp();
70 /// Gets the fully instantiated font at a given position in a paragraph
71 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
72 /// The difference is that this one is used for displaying, and thus we
73 /// are allowed to make cosmetic improvements. For instance make footnotes
75 Font getDisplayFont(pit_type pit,
78 /// There are currently two font mechanisms in LyX:
79 /// 1. The font attributes in a lyxtext, and
80 /// 2. The inset-specific font properties, defined in an inset's
81 /// metrics() and draw() methods and handed down the inset chain through
82 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
83 /// This is where the two are integrated in the final fully realized
85 void applyOuterFont(Font &) const;
87 /// is this position in the paragraph right-to-left?
88 bool isRTL(CursorSlice const & sl, bool boundary) const;
89 /// is between pos-1 and pos an RTL<->LTR boundary?
90 bool isRTLBoundary(pit_type pit,
92 /// would be a RTL<->LTR boundary between pos and the given font?
93 bool isRTLBoundary(pit_type pit,
94 pos_type pos, Font const & font) const;
97 /// Rebreaks the given paragraph.
98 /// \retval true if a full screen redraw is needed.
99 /// \retval false if a single paragraph redraw is enough.
100 bool redoParagraph(pit_type const pit);
101 /// Clear cache of paragraph metrics
102 void clear() { par_metrics_.clear(); }
105 int ascent() const { return dim_.asc; }
107 int descent() const { return dim_.des; }
108 /// current text width.
109 int width() const { return dim_.wid; }
110 /// current text heigth.
111 int height() const { return dim_.height(); }
114 int maxWidth() const { return max_width_; }
116 int singleWidth(pit_type const pit, pos_type pos) const;
119 int rightMargin(ParagraphMetrics const & pm) const;
120 int rightMargin(pit_type const pit) const;
122 /** this calculates the specified parameters. needed when setting
123 * the cursor and when creating a visible row */
124 void computeRowMetrics(pit_type pit, Row & row, int width) const;
127 void draw(PainterInfo & pi, int x, int y) const;
129 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
133 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
135 /// draw textselection.
136 /// FIXME: simplify to just to single row painting.
137 void drawSelection(PainterInfo & pi,
138 DocIterator const & beg, ///< selection begin.
139 DocIterator const & end, ///< selection end.
142 /// the minimum space a manual label needs on the screen in pixels
143 int labelFill(pit_type const pit, Row const & row) const;
146 int labelEnd(pit_type const pit) const;
148 /// sets row.end to the pos value *after* which a row should break.
149 /// for example, the pos after which isNewLine(pos) == true
150 pit_type rowBreakPoint(
156 /// sets row.width to the minimum space a row needs on the screen in pixel
160 pos_type const first,
164 /// Calculate and set the height of the row
165 boost::tuple<int, int> rowHeight(
167 pos_type const first,
171 /// draw selection for a single row
172 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
173 DocIterator const & beg, DocIterator const & end,
174 bool drawOnBegMargin, bool drawOnEndMargin) const;
178 /// returns the column near the specified x-coordinate of the row.
179 /// x is an absolute screen coord, it is set to the real beginning
181 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
182 bool & boundary) const;
184 /// returns pos in given par at given x coord.
185 pos_type x2pos(pit_type pit, int row, int x) const;
187 // FIXME: is there a need for this?
188 //int pos2x(pit_type pit, pos_type pos) const;
190 /** returns row near the specified
191 * y-coordinate in given paragraph (relative to the screen).
193 Row const & getRowNearY(int y,
196 /// returns the paragraph number closest to screen y-coordinate.
197 /// This method uses the BufferView CoordCache to locate the
198 /// paragraph. The y-coodinate is allowed to be off-screen and
199 /// the CoordCache will be automatically updated if needed. This is
200 /// the reason why we need a non const BufferView.
201 pit_type getPitNearY(int y);
203 /// sets cursor recursively descending into nested editable insets
205 \return the inset pointer if x,y is covering that inset
206 \param x,y are absolute screen coordinates.
207 \retval inset is non-null if the cursor is positionned inside
209 /// FIXME: cleanup to use BufferView::getCoveringInset() and
210 /// setCursorFromCoordinates() instead of checkInsetHit().
211 Inset * editXY(Cursor & cur, int x, int y);
213 /// sets cursor only within this Text.
214 /// x,y are screen coordinates
215 void setCursorFromCoordinates(Cursor & cur, int x, int y);
218 int cursorX(CursorSlice const & cursor,
219 bool boundary) const;
221 int cursorY(CursorSlice const & cursor,
222 bool boundary) const;
225 void cursorPrevious(Cursor & cur);
227 void cursorNext(Cursor & cur);
229 bool cursorHome(Cursor & cur);
231 bool cursorEnd(Cursor & cur);
233 void deleteLineForward(Cursor & cur);
235 /// is this row the last in the text?
236 bool isLastRow(pit_type pit, Row const & row) const;
237 /// is this row the first in the text?
238 bool isFirstRow(pit_type pit, Row const & row) const;
240 /// Returns an inset if inset was hit, or 0 if not.
241 /// \warning This method is not recursive! It will return the
242 /// outermost inset within this Text.
243 /// \sa BufferView::getCoveringInset() to get the innermost inset.
244 Inset * checkInsetHit(int x, int y);
247 * Returns the left beginning of the text.
248 * This information cannot be taken from the layout object, because
249 * in LaTeX the beginning of the text fits in some cases
250 * (for example sections) exactly the label-width.
252 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
253 int leftMargin(int max_width, pit_type pit) const;
257 /// The BufferView owner.
260 /// The text contents (the model).
261 /// \todo FIXME: this should be const.
265 /// A map from paragraph index number to paragraph metrics
266 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
268 mutable ParMetricsCache par_metrics_;
271 mutable Point origin_;
275 /// our 'outermost' font.
276 /// This is handed down from the surrounding
277 /// inset through the pi/mi parameter (pi.base.font)
278 /// It is used in applyOuterFont() and setCharFont() for reasons
279 /// that are not clear... to hand hand the outermost language and
280 /// also for char style apparently.
284 /// return the default height of a row in pixels, considering font zoom
285 int defaultRowHeight();
289 #endif // TEXT_METRICS_H