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;
52 std::pair<pit_type, ParagraphMetrics const *> first() const;
54 std::pair<pit_type, ParagraphMetrics const *> last() const;
57 int parPosition(pit_type pit) const;
60 Dimension const & dimension() const { return dim_; }
62 Point const & origin() const { return origin_; }
66 /// compute text metrics.
67 bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
70 void newParMetricsDown();
72 void newParMetricsUp();
74 /// Gets the fully instantiated font at a given position in a paragraph
75 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
76 /// The difference is that this one is used for displaying, and thus we
77 /// are allowed to make cosmetic improvements. For instance make footnotes
79 Font getDisplayFont(pit_type pit,
82 /// There are currently two font mechanisms in LyX:
83 /// 1. The font attributes in a lyxtext, and
84 /// 2. The inset-specific font properties, defined in an inset's
85 /// metrics() and draw() methods and handed down the inset chain through
86 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
87 /// This is where the two are integrated in the final fully realized
89 void applyOuterFont(Font &) const;
91 /// is this position in the paragraph right-to-left?
92 bool isRTL(CursorSlice const & sl, bool boundary) const;
93 /// is between pos-1 and pos an RTL<->LTR boundary?
94 bool isRTLBoundary(pit_type pit,
96 /// would be a RTL<->LTR boundary between pos and the given font?
97 bool isRTLBoundary(pit_type pit,
98 pos_type pos, Font const & font) const;
101 /// Rebreaks the given paragraph.
102 /// \retval true if a full screen redraw is needed.
103 /// \retval false if a single paragraph redraw is enough.
104 bool redoParagraph(pit_type const pit);
105 /// Clear cache of paragraph metrics
106 void clear() { par_metrics_.clear(); }
109 int ascent() const { return dim_.asc; }
111 int descent() const { return dim_.des; }
112 /// current text width.
113 int width() const { return dim_.wid; }
114 /// current text heigth.
115 int height() const { return dim_.height(); }
118 int maxWidth() const { return max_width_; }
120 int singleWidth(pit_type const pit, pos_type pos) const;
123 int rightMargin(ParagraphMetrics const & pm) const;
124 int rightMargin(pit_type const pit) const;
126 /** this calculates the specified parameters. needed when setting
127 * the cursor and when creating a visible row */
128 void computeRowMetrics(pit_type pit, Row & row, int width) const;
131 void draw(PainterInfo & pi, int x, int y) const;
133 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
137 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
139 /// draw textselection.
140 /// FIXME: simplify to just to single row painting.
141 void drawSelection(PainterInfo & pi,
142 DocIterator const & beg, ///< selection begin.
143 DocIterator const & end, ///< selection end.
146 /// the minimum space a manual label needs on the screen in pixels
147 int labelFill(pit_type const pit, Row const & row) const;
150 int labelEnd(pit_type const pit) const;
152 /// sets row.end to the pos value *after* which a row should break.
153 /// for example, the pos after which isNewLine(pos) == true
154 pit_type rowBreakPoint(
160 /// sets row.width to the minimum space a row needs on the screen in pixel
164 pos_type const first,
168 /// Calculate and set the height of the row
169 boost::tuple<int, int> rowHeight(
171 pos_type const first,
175 /// draw selection for a single row
176 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
177 DocIterator const & beg, DocIterator const & end,
178 bool drawOnBegMargin, bool drawOnEndMargin) const;
182 /// returns the column near the specified x-coordinate of the row.
183 /// x is an absolute screen coord, it is set to the real beginning
185 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
186 bool & boundary) const;
188 /// returns pos in given par at given x coord.
189 pos_type x2pos(pit_type pit, int row, int x) const;
191 // FIXME: is there a need for this?
192 //int pos2x(pit_type pit, pos_type pos) const;
194 /** returns row near the specified
195 * y-coordinate in given paragraph (relative to the screen).
197 Row const & getRowNearY(int y,
200 /// returns the paragraph number closest to screen y-coordinate.
201 /// This method uses the BufferView CoordCache to locate the
202 /// paragraph. The y-coodinate is allowed to be off-screen and
203 /// the CoordCache will be automatically updated if needed. This is
204 /// the reason why we need a non const BufferView.
205 pit_type getPitNearY(int y);
207 /// sets cursor recursively descending into nested editable insets
209 \return the inset pointer if x,y is covering that inset
210 \param x,y are absolute screen coordinates.
211 \retval inset is non-null if the cursor is positionned inside
213 /// FIXME: cleanup to use BufferView::getCoveringInset() and
214 /// setCursorFromCoordinates() instead of checkInsetHit().
215 Inset * editXY(Cursor & cur, int x, int y);
217 /// sets cursor only within this Text.
218 /// x,y are screen coordinates
219 void setCursorFromCoordinates(Cursor & cur, int x, int y);
222 int cursorX(CursorSlice const & cursor,
223 bool boundary) const;
225 int cursorY(CursorSlice const & cursor,
226 bool boundary) const;
229 void cursorPrevious(Cursor & cur);
231 void cursorNext(Cursor & cur);
233 bool cursorHome(Cursor & cur);
235 bool cursorEnd(Cursor & cur);
237 void deleteLineForward(Cursor & cur);
239 /// is this row the last in the text?
240 bool isLastRow(pit_type pit, Row const & row) const;
241 /// is this row the first in the text?
242 bool isFirstRow(pit_type pit, Row const & row) const;
244 /// Returns an inset if inset was hit, or 0 if not.
245 /// \warning This method is not recursive! It will return the
246 /// outermost inset within this Text.
247 /// \sa BufferView::getCoveringInset() to get the innermost inset.
248 Inset * checkInsetHit(int x, int y);
251 * Returns the left beginning of the text.
252 * This information cannot be taken from the layout object, because
253 * in LaTeX the beginning of the text fits in some cases
254 * (for example sections) exactly the label-width.
256 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
257 int leftMargin(int max_width, pit_type pit) const;
261 /// The BufferView owner.
264 /// The text contents (the model).
265 /// \todo FIXME: this should be const.
269 /// A map from paragraph index number to paragraph metrics
270 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
272 mutable ParMetricsCache par_metrics_;
275 mutable Point origin_;
279 /// our 'outermost' font.
280 /// This is handed down from the surrounding
281 /// inset through the pi/mi parameter (pi.base.font)
282 /// It is used in applyOuterFont() and setCharFont() for reasons
283 /// that are not clear... to hand hand the outermost language and
284 /// also for char style apparently.
288 /// return the default height of a row in pixels, considering font zoom
289 int defaultRowHeight();
293 #endif // TEXT_METRICS_H