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 "ParagraphMetrics.h"
21 #include "support/types.h"
32 /// A map from a Text to the map of paragraphs metrics
36 /// Default constructor (only here for STL containers).
37 TextMetrics() : text_(0) {}
38 /// The only useful constructor.
39 TextMetrics(BufferView *, Text *);
42 bool contains(pit_type pit) const;
44 ParagraphMetrics const & parMetrics(pit_type) const;
46 std::pair<pit_type, ParagraphMetrics const *> first() const;
48 std::pair<pit_type, ParagraphMetrics const *> last() const;
51 Dimension const & dimension() const { return dim_; }
53 Point const & origin() const { return origin_; }
56 /// compute text metrics.
57 bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
60 void newParMetricsDown();
62 void newParMetricsUp();
64 /// Gets the fully instantiated font at a given position in a paragraph
65 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
66 /// The difference is that this one is used for displaying, and thus we
67 /// are allowed to make cosmetic improvements. For instance make footnotes
69 Font displayFont(pit_type pit, pos_type pos) const;
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 height.
104 int height() const { return dim_.height(); }
107 int maxWidth() const { return max_width_; }
110 int rightMargin(ParagraphMetrics const & pm) const;
111 int rightMargin(pit_type const pit) const;
114 void draw(PainterInfo & pi, int x, int y) const;
116 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
118 /// Returns the height of the row (width member is set to 0).
119 /// If \c topBottomSpace is true, extra space is added for the
120 /// top and bottom row.
121 void setRowHeight(Row & row, pit_type const pit,
122 bool topBottomSpace = true) const;
126 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
128 /// the minimum space a manual label needs on the screen in pixels
129 int labelFill(pit_type const pit, Row const & row) const;
132 int labelEnd(pit_type const pit) const;
134 /// sets row.end to the pos value *after* which a row should break.
135 /// for example, the pos after which isNewLine(pos) == true
136 void breakRow(Row & row, int right_margin, pit_type const pit) const;
138 // Expand the alignment of paragraph \param par at position \param pos
139 int getAlign(Paragraph const & par, pos_type pos) const;
140 /** this calculates the specified parameters. needed when setting
141 * the cursor and when creating a visible row */
142 void computeRowMetrics(pit_type pit, Row & row, int width) const;
144 // Helper function for the other checkInsetHit method.
145 InsetList::InsetTable * checkInsetHit(pit_type pit, int x, int y);
150 /// returns the position near the specified x-coordinate of the row.
151 /// x is an absolute screen coord, it is set to the real beginning
153 pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
155 /// returns pos in given par at given x coord.
156 pos_type x2pos(pit_type pit, int row, int x) const;
158 // FIXME: is there a need for this?
159 //int pos2x(pit_type pit, pos_type pos) const;
161 /// returns the row near the specified y-coordinate in a given paragraph
162 /// (relative to the screen). If assert_in_view is true, it is made sure
163 /// that the row is on screen completely; this might change the given pit.
164 Row const & getPitAndRowNearY(int & y, pit_type & pit,
165 bool assert_in_view, bool up);
167 /// returns the paragraph number closest to screen y-coordinate.
168 /// This method uses the BufferView CoordCache to locate the
169 /// paragraph. The y-coodinate is allowed to be off-screen and
170 /// the CoordCache will be automatically updated if needed. This is
171 /// the reason why we need a non const BufferView.
172 pit_type getPitNearY(int y);
174 /// sets cursor recursively descending into nested editable insets
176 \return the inset pointer if x,y is covering that inset
177 \param x,y are absolute screen coordinates.
178 \param assert_in_view if true the cursor will be set on a row
179 that is completely visible
180 \param up whether we are going up or down (only used when
181 assert_in_view is true
182 \retval inset is non-null if the cursor is positionned inside
184 /// FIXME: cleanup to use BufferView::getCoveringInset() and
185 /// setCursorFromCoordinates() instead of checkInsetHit().
186 Inset * editXY(Cursor & cur, int x, int y,
187 bool assert_in_view = false, bool up = true);
189 /// sets cursor only within this Text.
190 /// x,y are screen coordinates
191 void setCursorFromCoordinates(Cursor & cur, int x, int y);
194 int cursorX(CursorSlice const & cursor,
195 bool boundary) const;
197 int cursorY(CursorSlice const & cursor,
198 bool boundary) const;
201 bool cursorHome(Cursor & cur);
203 bool cursorEnd(Cursor & cur);
205 void deleteLineForward(Cursor & cur);
207 /// is this row the last in the text?
208 bool isLastRow(pit_type pit, Row const & row) const;
209 /// is this row the first in the text?
210 bool isFirstRow(pit_type pit, Row const & row) const;
212 /// Returns an inset if inset was hit, or 0 if not.
213 /// \warning This method is not recursive! It will return the
214 /// outermost inset within this Text.
215 /// \sa BufferView::getCoveringInset() to get the innermost inset.
216 Inset * checkInsetHit(int x, int y);
219 * Returns the left beginning of the text.
220 * This information cannot be taken from the layout object, because
221 * in LaTeX the beginning of the text fits in some cases
222 * (for example sections) exactly the label-width.
224 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
225 int leftMargin(int max_width, pit_type pit) const;
227 /// calculates the position of a completion popup
228 void completionPosAndDim(Cursor const & cur, int & x, int & y,
229 Dimension & dim) const;
232 friend class BufferView;
234 /// The BufferView owner.
237 /// The text contents (the model).
238 /// \todo FIXME: this should be const.
242 /// A map from paragraph index number to paragraph metrics
243 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
245 mutable ParMetricsCache par_metrics_;
248 mutable Point origin_;
252 /// our 'outermost' font.
253 /// This is handed down from the surrounding
254 /// inset through the pi/mi parameter (pi.base.font)
255 /// It is used in applyOuterFont() and setCharFont() for reasons
256 /// that are not clear... to hand hand the outermost language and
257 /// also for char style apparently.
261 /// return the default height of a row in pixels, considering font zoom
262 int defaultRowHeight();
266 #endif // TEXT_METRICS_H