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 int parPosition(pit_type pit) const;
55 Dimension const & dimension() const;
57 Point const & origin() const { return origin_; }
60 /// compute text metrics.
61 bool metrics(MetricsInfo & mi, Dimension & dim);
63 /// Gets the fully instantiated font at a given position in a paragraph
64 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
65 /// The difference is that this one is used for displaying, and thus we
66 /// are allowed to make cosmetic improvements. For instance make footnotes
68 Font getDisplayFont(pit_type pit,
71 /// There are currently two font mechanisms in LyX:
72 /// 1. The font attributes in a lyxtext, and
73 /// 2. The inset-specific font properties, defined in an inset's
74 /// metrics() and draw() methods and handed down the inset chain through
75 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
76 /// This is where the two are integrated in the final fully realized
78 void applyOuterFont(Font &) const;
80 /// is this position in the paragraph right-to-left?
81 bool isRTL(CursorSlice const & sl, bool boundary) const;
82 /// is between pos-1 and pos an RTL<->LTR boundary?
83 bool isRTLBoundary(pit_type pit,
85 /// would be a RTL<->LTR boundary between pos and the given font?
86 bool isRTLBoundary(pit_type pit,
87 pos_type pos, Font const & font) const;
90 /// Rebreaks the given paragraph.
91 /// \retval true if a full screen redraw is needed.
92 /// \retval false if a single paragraph redraw is enough.
93 bool redoParagraph(pit_type const pit);
94 /// Clear cache of paragraph metrics
95 void clear() { par_metrics_.clear(); }
98 int ascent() const { return dim_.asc; }
100 int descent() const { return dim_.des; }
101 /// current text width.
102 int width() const { return dim_.wid; }
103 /// current text heigth.
104 int height() const { return dim_.height(); }
107 int maxWidth() const { return max_width_; }
109 int singleWidth(pit_type const pit, pos_type pos) const;
112 int rightMargin(ParagraphMetrics const & pm) const;
113 int rightMargin(pit_type const pit) const;
115 /** this calculates the specified parameters. needed when setting
116 * the cursor and when creating a visible row */
117 void computeRowMetrics(pit_type pit, Row & row) const;
120 void draw(PainterInfo & pi, int x, int y) const;
122 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
126 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
128 /// draw textselection.
129 /// FIXME: simplify to just to single row painting.
130 void drawSelection(PainterInfo & pi,
131 DocIterator const & beg, ///< selection begin.
132 DocIterator const & end, ///< selection end.
135 /// the minimum space a manual label needs on the screen in pixels
136 int labelFill(pit_type const pit, Row const & row) const;
139 int labelEnd(pit_type const pit) const;
141 /// sets row.end to the pos value *after* which a row should break.
142 /// for example, the pos after which isNewLine(pos) == true
143 pit_type rowBreakPoint(
149 /// sets row.width to the minimum space a row needs on the screen in pixel
153 pos_type const first,
157 /// Calculate and set the height of the row
158 boost::tuple<int, int> rowHeight(
160 pos_type const first,
164 /// draw selection for a single row
165 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
166 DocIterator const & beg, DocIterator const & end,
167 bool drawOnBegMargin, bool drawOnEndMargin) const;
171 /// returns the column near the specified x-coordinate of the row.
172 /// x is an absolute screen coord, it is set to the real beginning
174 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
175 bool & boundary) const;
177 /// returns pos in given par at given x coord.
178 pos_type x2pos(pit_type pit, int row, int x) const;
180 // FIXME: is there a need for this?
181 //int pos2x(pit_type pit, pos_type pos) const;
183 /** returns row near the specified
184 * y-coordinate in given paragraph (relative to the screen).
186 Row const & getRowNearY(int y,
189 /// returns the paragraph number closest to screen y-coordinate.
190 /// This method uses the BufferView CoordCache to locate the
191 /// paragraph. The y-coodinate is allowed to be off-screen and
192 /// the CoordCache will be automatically updated if needed. This is
193 /// the reason why we need a non const BufferView.
194 pit_type getPitNearY(int y);
196 /// sets cursor recursively descending into nested editable insets
198 \return the inset pointer if x,y is covering that inset
199 \param x,y are absolute screen coordinates.
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);
206 /// sets cursor only within this Text.
207 /// x,y are screen coordinates
208 void setCursorFromCoordinates(Cursor & cur, int x, int y);
211 int cursorX(CursorSlice const & cursor,
212 bool boundary) const;
214 int cursorY(CursorSlice const & cursor,
215 bool boundary) const;
218 void cursorPrevious(Cursor & cur);
220 void cursorNext(Cursor & cur);
222 bool cursorHome(Cursor & cur);
224 bool cursorEnd(Cursor & cur);
226 void deleteLineForward(Cursor & cur);
228 /// is this row the last in the text?
229 bool isLastRow(pit_type pit, Row const & row) const;
230 /// is this row the first in the text?
231 bool isFirstRow(pit_type pit, Row const & row) const;
233 /// Returns an inset if inset was hit, or 0 if not.
234 /// \warning This method is not recursive! It will return the
235 /// outermost inset within this Text.
236 /// \sa BufferView::getCoveringInset() to get the innermost inset.
237 Inset * checkInsetHit(int x, int y);
240 * Returns the left beginning of the text.
241 * This information cannot be taken from the layout object, because
242 * in LaTeX the beginning of the text fits in some cases
243 * (for example sections) exactly the label-width.
245 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
246 int leftMargin(int max_width, pit_type pit) const;
250 /// The BufferView owner.
253 /// The text contents (the model).
254 /// \todo FIXME: this should be const.
258 /// A map from paragraph index number to paragraph metrics
259 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
261 mutable ParMetricsCache par_metrics_;
264 mutable Point origin_;
268 /// our 'outermost' font.
269 /// This is handed down from the surrounding
270 /// inset through the pi/mi parameter (pi.base.font)
271 /// It is used in applyOuterFont() and setCharFont() for reasons
272 /// that are not clear... to hand hand the outermost language and
273 /// also for char style apparently.
277 /// return the default height of a row in pixels, considering font zoom
278 int defaultRowHeight();
282 #endif // TEXT_METRICS_H