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,
59 bool const expand_on_multipars = true);
62 void newParMetricsDown();
64 void newParMetricsUp();
66 /// The "nodraw" drawing stage for one single paragraph: set the
67 /// positions of the insets contained this paragraph in metrics
68 /// cache. Related to BufferView::updatePosCache.
69 void updatePosCache(pit_type pit) const;
71 /// Gets the fully instantiated font at a given position in a paragraph
72 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
73 /// The difference is that this one is used for displaying, and thus we
74 /// are allowed to make cosmetic improvements. For instance make footnotes
76 Font displayFont(pit_type pit, pos_type pos) const;
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(); }
103 /// Is cache of paragraph metrics empty ?
104 bool empty() const { return par_metrics_.empty(); }
107 int ascent() const { return dim_.asc; }
109 int descent() const { return dim_.des; }
110 /// current text width.
111 int width() const { return dim_.wid; }
112 /// current text height.
113 int height() const { return dim_.height(); }
116 int rightMargin(ParagraphMetrics const & pm) const;
117 int rightMargin(pit_type const pit) 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);
129 * Returns the left beginning of the text.
130 * This information cannot be taken from the layout object, because
131 * in LaTeX the beginning of the text fits in some cases
132 * (for example sections) exactly the label-width.
134 int leftMargin(pit_type pit, pos_type pos) const;
135 int leftMargin(pit_type pit) const;
137 /// the minimum space a manual label needs on the screen in pixels
138 int labelFill(Row const & row) const;
141 int labelEnd(pit_type const pit) const;
143 /// sets row.end to the pos value *after* which a row should break.
144 /// for example, the pos after which isNewLine(pos) == true
145 /// \return true when another row is required (after a newline)
146 bool breakRow(Row & row, int right_margin) const;
148 // Expand the alignment of row \param row in paragraph \param par
149 LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
150 /** this calculates the specified parameters. needed when setting
151 * the cursor and when creating a visible row */
152 void computeRowMetrics(Row & row, int width) const;
154 /// Set the height of the row (without space above/below paragraph)
155 void setRowHeight(Row & row) const;
156 // Compute the space on top of a paragraph
157 int parTopSpacing(pit_type pit) const;
158 // Compute the space below a a paragraph
159 int parBottomSpacing(pit_type pit) const;
161 // Helper function for the other checkInsetHit method.
162 InsetList::Element * checkInsetHit(pit_type pit, int x, int y);
167 /// returns the position near the specified x-coordinate of the row.
168 /// x is an absolute screen coord, it is set to the real beginning
169 /// of this column. This takes in account horizontal cursor row scrolling.
170 pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
172 /// returns pos in given par at given x coord.
173 pos_type x2pos(pit_type pit, int row, int x) const;
175 // FIXME: is there a need for this?
176 //int pos2x(pit_type pit, pos_type pos) const;
178 /// returns the row near the specified y-coordinate in a given paragraph
179 /// (relative to the screen). If assert_in_view is true, it is made sure
180 /// that the row is on screen completely; this might change the given pit.
181 Row const & getPitAndRowNearY(int & y, pit_type & pit,
182 bool assert_in_view, bool up);
184 /// returns the paragraph number closest to screen y-coordinate.
185 /// This method uses the BufferView CoordCache to locate the
186 /// paragraph. The y-coodinate is allowed to be off-screen and
187 /// the CoordCache will be automatically updated if needed. This is
188 /// the reason why we need a non const BufferView.
189 pit_type getPitNearY(int y);
191 /// sets cursor recursively descending into nested editable insets
193 \return the inset pointer if x,y is covering that inset
194 \param x,y are absolute screen coordinates.
195 \param assert_in_view if true the cursor will be set on a row
196 that is completely visible
197 \param up whether we are going up or down (only used when
198 assert_in_view is true
199 \retval inset is null if the cursor is positioned over normal
200 text in the current Text object. Otherwise it is the inset
201 that the cursor points to, like for Inset::editXY.
203 /// FIXME: cleanup to use BufferView::getCoveringInset() and
204 /// setCursorFromCoordinates() instead of checkInsetHit().
205 Inset * editXY(Cursor & cur, int x, int y,
206 bool assert_in_view = false, bool up = true);
208 /// sets cursor only within this Text.
209 /// x,y are screen coordinates
210 void setCursorFromCoordinates(Cursor & cur, int x, int y);
213 int cursorX(CursorSlice const & cursor, bool boundary) const;
215 int cursorY(CursorSlice const & cursor, bool boundary) const;
218 bool cursorHome(Cursor & cur);
220 bool cursorEnd(Cursor & cur);
222 void deleteLineForward(Cursor & cur);
224 /// is this row the last in the text?
225 bool isLastRow(Row const & row) const;
226 /// is this row the first in the text?
227 bool isFirstRow(Row const & row) const;
229 /// Returns an inset if inset was hit, or 0 if not.
230 /// \warning This method is not recursive! It will return the
231 /// outermost inset within this Text.
232 /// \sa BufferView::getCoveringInset() to get the innermost inset.
233 Inset * checkInsetHit(int x, int y);
235 /// calculates the position of a completion popup
236 void completionPosAndDim(Cursor const & cur, int & x, int & y,
237 Dimension & dim) const;
240 friend class BufferView;
242 /// The BufferView owner.
245 /// The text contents (the model).
246 /// \todo FIXME: this should be const.
249 /// A map from paragraph index number to paragraph metrics
250 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
252 mutable ParMetricsCache par_metrics_;
255 mutable Point origin_;
259 /// our 'outermost' font.
260 /// This is handed down from the surrounding
261 /// inset through the pi/mi parameter (pi.base.font)
262 /// It is used in applyOuterFont() and setCharFont() for reasons
263 /// that are not clear... to hand hand the outermost language and
264 /// also for char style apparently.
268 /// return the default height of a row in pixels, considering font zoom
269 int defaultRowHeight();
273 #endif // TEXT_METRICS_H