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 TextMetrics(TextMetrics const &);
38 void operator=(TextMetrics const &);
40 /// Default constructor (only here for STL containers).
41 TextMetrics() : bv_(0), text_(0), max_width_(0) {}
42 /// The only useful constructor.
43 TextMetrics(BufferView *, Text *);
46 bool contains(pit_type pit) const;
48 std::pair<pit_type, ParagraphMetrics const *> first() const;
50 std::pair<pit_type, ParagraphMetrics const *> last() const;
51 /// is this row the last in the text?
52 bool isLastRow(Row const & row) const;
53 /// is this row the first in the text?
54 bool isFirstRow(Row const & row) const;
56 void setRowChanged(pit_type pit, pos_type pos);
59 Dimension const & dim() const { return dim_; }
61 Point const & origin() const { return origin_; }
65 ParagraphMetrics const & parMetrics(pit_type) const;
67 void newParMetricsDown();
69 void newParMetricsUp();
71 /// compute text metrics.
72 bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0,
73 bool const expand_on_multipars = true);
75 /// The "nodraw" drawing stage for one single paragraph: set the
76 /// positions of the insets contained this paragraph in metrics
77 /// cache. Related to BufferView::updatePosCache.
78 void updatePosCache(pit_type pit) const;
80 /// Gets the fully instantiated font at a given position in a paragraph
81 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
82 /// The difference is that this one is used for displaying, and thus we
83 /// are allowed to make cosmetic improvements. For instance make footnotes
85 Font displayFont(pit_type pit, pos_type pos) const;
87 /// There are currently two font mechanisms in LyX:
88 /// 1. The font attributes in a lyxtext, and
89 /// 2. The inset-specific font properties, defined in an inset's
90 /// metrics() and draw() methods and handed down the inset chain through
91 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
92 /// This is where the two are integrated in the final fully realized
94 void applyOuterFont(Font &) const;
96 /// is this position in the paragraph right-to-left?
97 bool isRTL(CursorSlice const & sl, bool boundary) const;
98 /// is between pos-1 and pos an RTL<->LTR boundary?
99 bool isRTLBoundary(pit_type pit,
101 /// would be a RTL<->LTR boundary between pos and the given font?
102 bool isRTLBoundary(pit_type pit,
103 pos_type pos, Font const & font) const;
106 /// Rebreaks the given paragraph.
107 /// \retval true if a full screen redraw is needed.
108 /// \retval false if a single paragraph redraw is enough.
109 bool redoParagraph(pit_type const pit, bool align_rows = true);
110 /// Clear cache of paragraph metrics
111 void clear() { par_metrics_.clear(); }
112 /// Is cache of paragraph metrics empty ?
113 bool empty() const { return par_metrics_.empty(); }
116 int ascent() const { return dim_.asc; }
118 int descent() const { return dim_.des; }
119 /// current text width.
120 int width() const { return dim_.wid; }
121 /// current text height.
122 int height() const { return dim_.height(); }
125 int rightMargin(ParagraphMetrics const & pm) const;
126 int rightMargin(pit_type const pit) const;
129 void draw(PainterInfo & pi, int x, int y) const;
131 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
135 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
138 * Returns the left beginning of the text.
139 * This information cannot be taken from the layout object, because
140 * in LaTeX the beginning of the text fits in some cases
141 * (for example sections) exactly the label-width.
143 int leftMargin(pit_type pit, pos_type pos) const;
144 int leftMargin(pit_type pit) const;
146 /// the minimum space a manual label needs on the screen in pixels
147 int labelFill(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 /// \return true when another row is required (after a newline)
155 bool breakRow(Row & row, int right_margin) const;
157 // Expands the alignment of row \param row in paragraph \param par
158 LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
159 /// Aligns properly the row contents (computes spaces and fills)
160 void setRowAlignment(Row & row, int width) const;
162 /// Set the height of the row (without space above/below paragraph)
163 void setRowHeight(Row & row) const;
164 // Compute the space on top of a paragraph
165 int parTopSpacing(pit_type pit) const;
166 // Compute the space below a a paragraph
167 int parBottomSpacing(pit_type pit) const;
169 // Helper function for the other checkInsetHit method.
170 InsetList::Element * checkInsetHit(pit_type pit, int x, int y);
175 /// returns the position near the specified x-coordinate of the row.
176 /// x is an absolute screen coord, it is set to the real beginning
177 /// of this column. This takes in account horizontal cursor row scrolling.
178 pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
180 /// returns pos in given par at given x coord.
181 pos_type x2pos(pit_type pit, int row, int x) const;
183 // FIXME: is there a need for this?
184 //int pos2x(pit_type pit, pos_type pos) const;
186 /// returns the row near the specified y-coordinate in a given paragraph
187 /// (relative to the screen). If assert_in_view is true, it is made sure
188 /// that the row is on screen completely; this might change the given pit.
189 Row const & getPitAndRowNearY(int & y, pit_type & pit,
190 bool assert_in_view, bool up);
192 /// returns the paragraph number closest to screen y-coordinate.
193 /// This method uses the BufferView CoordCache to locate the
194 /// paragraph. The y-coodinate is allowed to be off-screen and
195 /// the CoordCache will be automatically updated if needed. This is
196 /// the reason why we need a non const BufferView.
197 pit_type getPitNearY(int y);
199 /// sets cursor recursively descending into nested editable insets
201 \return the inset pointer if x,y is covering that inset
202 \param x,y are absolute screen coordinates.
203 \param assert_in_view if true the cursor will be set on a row
204 that is completely visible
205 \param up whether we are going up or down (only used when
206 assert_in_view is true
207 \retval inset is null if the cursor is positioned over normal
208 text in the current Text object. Otherwise it is the inset
209 that the cursor points to, like for Inset::editXY.
211 /// FIXME: cleanup to use BufferView::getCoveringInset() and
212 /// setCursorFromCoordinates() instead of checkInsetHit().
213 Inset * editXY(Cursor & cur, int x, int y,
214 bool assert_in_view = false, bool up = true);
216 /// sets cursor only within this Text.
217 /// x,y are screen coordinates
218 void setCursorFromCoordinates(Cursor & cur, int x, int y);
221 int cursorX(CursorSlice const & cursor, bool boundary) const;
223 int cursorY(CursorSlice const & cursor, bool boundary) const;
226 bool cursorHome(Cursor & cur);
228 bool cursorEnd(Cursor & cur);
230 void deleteLineForward(Cursor & cur);
232 /// Returns an inset if inset was hit, or 0 if not.
233 /// \warning This method is not recursive! It will return the
234 /// outermost inset within this Text.
235 /// \sa BufferView::getCoveringInset() to get the innermost inset.
236 Inset * checkInsetHit(int x, int y);
238 /// calculates the position of a completion popup
239 void completionPosAndDim(Cursor const & cur, int & x, int & y,
240 Dimension & dim) const;
243 friend class BufferView;
245 /// The BufferView owner.
248 /// The text contents (the model).
249 /// \todo FIXME: this should be const.
252 /// A map from paragraph index number to paragraph metrics
253 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
255 mutable ParMetricsCache par_metrics_;
258 mutable Point origin_;
262 /// our 'outermost' font.
263 /// This is handed down from the surrounding
264 /// inset through the pi/mi parameter (pi.base.font)
265 /// It is used in applyOuterFont() and setCharFont() for reasons
266 /// that are not clear... to hand hand the outermost language and
267 /// also for char style apparently.
271 /// return the default height of a row in pixels, considering font zoom
272 int defaultRowHeight();
276 #endif // TEXT_METRICS_H