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 "InsetList.h"
19 #include "LayoutEnums.h"
20 #include "ParagraphMetrics.h"
22 #include "support/types.h"
33 /// A map from a Text to the map of paragraphs metrics
37 /// Default constructor (only here for STL containers).
38 TextMetrics() : bv_(0), text_(0), max_width_(0) {}
39 /// The only useful constructor.
40 TextMetrics(BufferView *, Text *);
43 bool contains(pit_type pit) const;
45 ParagraphMetrics const & parMetrics(pit_type) const;
47 std::pair<pit_type, ParagraphMetrics const *> first() const;
49 std::pair<pit_type, ParagraphMetrics const *> last() const;
52 Dimension const & dimension() const { return dim_; }
54 Point const & origin() const { return origin_; }
57 /// compute text metrics.
58 bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
61 void newParMetricsDown();
63 void newParMetricsUp();
65 /// The "nodraw" drawing stage for one single paragraph: set the
66 /// positions of the insets contained this paragraph in metrics
67 /// cache. Related to BufferView::updatePosCache.
68 void updatePosCache(pit_type pit) const;
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 displayFont(pit_type pit, pos_type pos) const;
77 /// There are currently two font mechanisms in LyX:
78 /// 1. The font attributes in a lyxtext, and
79 /// 2. The inset-specific font properties, defined in an inset's
80 /// metrics() and draw() methods and handed down the inset chain through
81 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
82 /// This is where the two are integrated in the final fully realized
84 void applyOuterFont(Font &) const;
86 /// is this position in the paragraph right-to-left?
87 bool isRTL(CursorSlice const & sl, bool boundary) const;
88 /// is between pos-1 and pos an RTL<->LTR boundary?
89 bool isRTLBoundary(pit_type pit,
91 /// would be a RTL<->LTR boundary between pos and the given font?
92 bool isRTLBoundary(pit_type pit,
93 pos_type pos, Font const & font) const;
96 /// Rebreaks the given paragraph.
97 /// \retval true if a full screen redraw is needed.
98 /// \retval false if a single paragraph redraw is enough.
99 bool redoParagraph(pit_type const pit);
100 /// Clear cache of paragraph metrics
101 void clear() { par_metrics_.clear(); }
102 /// Is cache of paragraph metrics empty ?
103 bool empty() const { return par_metrics_.empty(); }
106 int ascent() const { return dim_.asc; }
108 int descent() const { return dim_.des; }
109 /// current text width.
110 int width() const { return dim_.wid; }
111 /// current text height.
112 int height() const { return dim_.height(); }
115 int rightMargin(ParagraphMetrics const & pm) const;
116 int rightMargin(pit_type const pit) const;
119 void draw(PainterInfo & pi, int x, int y) const;
121 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
125 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
128 * Returns the left beginning of the text.
129 * This information cannot be taken from the layout object, because
130 * in LaTeX the beginning of the text fits in some cases
131 * (for example sections) exactly the label-width.
133 int leftMargin(pit_type pit, pos_type pos) const;
134 int leftMargin(pit_type pit) const;
136 /// the minimum space a manual label needs on the screen in pixels
137 int labelFill(Row const & row) const;
140 int labelEnd(pit_type const pit) const;
142 /// sets row.end to the pos value *after* which a row should break.
143 /// for example, the pos after which isNewLine(pos) == true
144 /// \return true when another row is required (after a newline)
145 bool breakRow(Row & row, int right_margin) const;
147 // Expand the alignment of row \param row in paragraph \param par
148 LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
149 /** this calculates the specified parameters. needed when setting
150 * the cursor and when creating a visible row */
151 void computeRowMetrics(Row & row, int width) const;
153 /// Set the height of the row (without space above/below paragraph)
154 void setRowHeight(Row & row) const;
155 // Compute the space on top of a paragraph
156 int parTopSpacing(pit_type pit) const;
157 // Compute the space below a a paragraph
158 int parBottomSpacing(pit_type pit) const;
160 // Helper function for the other checkInsetHit method.
161 InsetList::InsetTable * checkInsetHit(pit_type pit, int x, int y);
166 /// returns the position near the specified x-coordinate of the row.
167 /// x is an absolute screen coord, it is set to the real beginning
168 /// of this column. This takes in account horizontal cursor row scrolling.
169 pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
171 /// returns pos in given par at given x coord.
172 pos_type x2pos(pit_type pit, int row, int x) const;
174 // FIXME: is there a need for this?
175 //int pos2x(pit_type pit, pos_type pos) const;
177 /// returns the row near the specified y-coordinate in a given paragraph
178 /// (relative to the screen). If assert_in_view is true, it is made sure
179 /// that the row is on screen completely; this might change the given pit.
180 Row const & getPitAndRowNearY(int & y, pit_type & pit,
181 bool assert_in_view, bool up);
183 /// returns the paragraph number closest to screen y-coordinate.
184 /// This method uses the BufferView CoordCache to locate the
185 /// paragraph. The y-coodinate is allowed to be off-screen and
186 /// the CoordCache will be automatically updated if needed. This is
187 /// the reason why we need a non const BufferView.
188 pit_type getPitNearY(int y);
190 /// sets cursor recursively descending into nested editable insets
192 \return the inset pointer if x,y is covering that inset
193 \param x,y are absolute screen coordinates.
194 \param assert_in_view if true the cursor will be set on a row
195 that is completely visible
196 \param up whether we are going up or down (only used when
197 assert_in_view is true
198 \retval inset is null if the cursor is positioned over normal
199 text in the current Text object. Otherwise it is the inset
200 that the cursor points to, like for Inset::editXY.
202 /// FIXME: cleanup to use BufferView::getCoveringInset() and
203 /// setCursorFromCoordinates() instead of checkInsetHit().
204 Inset * editXY(Cursor & cur, int x, int y,
205 bool assert_in_view = false, bool up = true);
207 /// sets cursor only within this Text.
208 /// x,y are screen coordinates
209 void setCursorFromCoordinates(Cursor & cur, int x, int y);
212 int cursorX(CursorSlice const & cursor, bool boundary) const;
214 int cursorY(CursorSlice const & cursor, bool boundary) const;
217 bool cursorHome(Cursor & cur);
219 bool cursorEnd(Cursor & cur);
221 void deleteLineForward(Cursor & cur);
223 /// is this row the last in the text?
224 bool isLastRow(Row const & row) const;
225 /// is this row the first in the text?
226 bool isFirstRow(Row const & row) const;
228 /// Returns an inset if inset was hit, or 0 if not.
229 /// \warning This method is not recursive! It will return the
230 /// outermost inset within this Text.
231 /// \sa BufferView::getCoveringInset() to get the innermost inset.
232 Inset * checkInsetHit(int x, int y);
234 /// calculates the position of a completion popup
235 void completionPosAndDim(Cursor const & cur, int & x, int & y,
236 Dimension & dim) const;
239 friend class BufferView;
241 /// The BufferView owner.
244 /// The text contents (the model).
245 /// \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_;
254 mutable Point origin_;
258 /// our 'outermost' font.
259 /// This is handed down from the surrounding
260 /// inset through the pi/mi parameter (pi.base.font)
261 /// It is used in applyOuterFont() and setCharFont() for reasons
262 /// that are not clear... to hand hand the outermost language and
263 /// also for char style apparently.
267 /// return the default height of a row in pixels, considering font zoom
268 int defaultRowHeight();
272 #endif // TEXT_METRICS_H