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 /// Gets the fully instantiated font at a given position in a paragraph
66 /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
67 /// The difference is that this one is used for displaying, and thus we
68 /// are allowed to make cosmetic improvements. For instance make footnotes
70 Font displayFont(pit_type pit, pos_type pos) const;
72 /// There are currently two font mechanisms in LyX:
73 /// 1. The font attributes in a lyxtext, and
74 /// 2. The inset-specific font properties, defined in an inset's
75 /// metrics() and draw() methods and handed down the inset chain through
76 /// the pi/mi parameters, and stored locally in a lyxtext in font_.
77 /// This is where the two are integrated in the final fully realized
79 void applyOuterFont(Font &) const;
81 /// is this position in the paragraph right-to-left?
82 bool isRTL(CursorSlice const & sl, bool boundary) const;
83 /// is between pos-1 and pos an RTL<->LTR boundary?
84 bool isRTLBoundary(pit_type pit,
86 /// would be a RTL<->LTR boundary between pos and the given font?
87 bool isRTLBoundary(pit_type pit,
88 pos_type pos, Font const & font) const;
91 /// Rebreaks the given paragraph.
92 /// \retval true if a full screen redraw is needed.
93 /// \retval false if a single paragraph redraw is enough.
94 bool redoParagraph(pit_type const pit);
95 /// Clear cache of paragraph metrics
96 void clear() { par_metrics_.clear(); }
99 int ascent() const { return dim_.asc; }
101 int descent() const { return dim_.des; }
102 /// current text width.
103 int width() const { return dim_.wid; }
104 /// current text height.
105 int height() const { return dim_.height(); }
108 int rightMargin(ParagraphMetrics const & pm) const;
109 int rightMargin(pit_type const pit) const;
112 void draw(PainterInfo & pi, int x, int y) const;
114 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
116 /// Returns the height of the row (width member is set to 0).
117 /// If \c topBottomSpace is true, extra space is added for the
118 /// top and bottom row.
119 void setRowHeight(Row & row, pit_type const pit,
120 bool topBottomSpace = true) const;
124 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
126 /// the minimum space a manual label needs on the screen in pixels
127 int labelFill(pit_type const pit, Row const & row) const;
130 int labelEnd(pit_type const pit) const;
132 /// sets row.end to the pos value *after* which a row should break.
133 /// for example, the pos after which isNewLine(pos) == true
134 void breakRow(Row & row, int right_margin, pit_type const pit) const;
136 // Expand the alignment of row \param row in paragraph \param par
137 LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
138 /** this calculates the specified parameters. needed when setting
139 * the cursor and when creating a visible row */
140 void computeRowMetrics(pit_type pit, Row & row, int width) const;
142 // Helper function for the other checkInsetHit method.
143 InsetList::InsetTable * checkInsetHit(pit_type pit, int x, int y);
148 /// returns the position near the specified x-coordinate of the row.
149 /// x is an absolute screen coord, it is set to the real beginning
150 /// of this column. This takes in account horizontal cursor row scrolling.
151 pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
153 /// returns pos in given par at given x coord.
154 pos_type x2pos(pit_type pit, int row, int x) const;
156 // FIXME: is there a need for this?
157 //int pos2x(pit_type pit, pos_type pos) const;
159 /// returns the row near the specified y-coordinate in a given paragraph
160 /// (relative to the screen). If assert_in_view is true, it is made sure
161 /// that the row is on screen completely; this might change the given pit.
162 Row const & getPitAndRowNearY(int & y, pit_type & pit,
163 bool assert_in_view, bool up);
165 /// returns the paragraph number closest to screen y-coordinate.
166 /// This method uses the BufferView CoordCache to locate the
167 /// paragraph. The y-coodinate is allowed to be off-screen and
168 /// the CoordCache will be automatically updated if needed. This is
169 /// the reason why we need a non const BufferView.
170 pit_type getPitNearY(int y);
172 /// sets cursor recursively descending into nested editable insets
174 \return the inset pointer if x,y is covering that inset
175 \param x,y are absolute screen coordinates.
176 \param assert_in_view if true the cursor will be set on a row
177 that is completely visible
178 \param up whether we are going up or down (only used when
179 assert_in_view is true
180 \retval inset is non-null if the cursor is positionned inside
182 /// FIXME: cleanup to use BufferView::getCoveringInset() and
183 /// setCursorFromCoordinates() instead of checkInsetHit().
184 Inset * editXY(Cursor & cur, int x, int y,
185 bool assert_in_view = false, bool up = true);
187 /// sets cursor only within this Text.
188 /// x,y are screen coordinates
189 void setCursorFromCoordinates(Cursor & cur, int x, int y);
191 /// Helper function: find row element that contains pos, and
192 /// compute x offset.
193 Row::const_iterator const
194 findRowElement(Row const & row, pos_type const pos,
195 bool const boundary, double & x) const;
198 int cursorX(CursorSlice const & cursor, bool boundary) const;
200 int cursorY(CursorSlice const & cursor, bool boundary) const;
203 bool cursorHome(Cursor & cur);
205 bool cursorEnd(Cursor & cur);
207 void deleteLineForward(Cursor & cur);
209 /// is this row the last in the text?
210 bool isLastRow(pit_type pit, Row const & row) const;
211 /// is this row the first in the text?
212 bool isFirstRow(pit_type pit, Row const & row) const;
214 /// Returns an inset if inset was hit, or 0 if not.
215 /// \warning This method is not recursive! It will return the
216 /// outermost inset within this Text.
217 /// \sa BufferView::getCoveringInset() to get the innermost inset.
218 Inset * checkInsetHit(int x, int y);
221 * Returns the left beginning of the text.
222 * This information cannot be taken from the layout object, because
223 * in LaTeX the beginning of the text fits in some cases
224 * (for example sections) exactly the label-width.
226 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
227 int leftMargin(int max_width, pit_type pit) const;
229 /// calculates the position of a completion popup
230 void completionPosAndDim(Cursor const & cur, int & x, int & y,
231 Dimension & dim) const;
234 friend class BufferView;
236 /// The BufferView owner.
239 /// The text contents (the model).
240 /// \todo FIXME: this should be const.
243 /// A map from paragraph index number to paragraph metrics
244 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
246 mutable ParMetricsCache par_metrics_;
249 mutable Point origin_;
253 /// our 'outermost' font.
254 /// This is handed down from the surrounding
255 /// inset through the pi/mi parameter (pi.base.font)
256 /// It is used in applyOuterFont() and setCharFont() for reasons
257 /// that are not clear... to hand hand the outermost language and
258 /// also for char style apparently.
262 /// return the default height of a row in pixels, considering font zoom
263 int defaultRowHeight();
267 #endif // TEXT_METRICS_H