]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
fix text background painting.
[lyx.git] / src / TextMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file TextMetrics.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TEXT_METRICS_H
15 #define TEXT_METRICS_H
16
17 #include "ParagraphMetrics.h"
18
19 #include "support/types.h"
20
21 #include <boost/utility.hpp>
22 #include <boost/tuple/tuple.hpp>
23
24 #include <map>
25
26
27 namespace lyx {
28
29 class BufferView;
30 class Text;
31 class MetricsInfo;
32
33 /// A map from a Text to the map of paragraphs metrics
34 class TextMetrics
35 {
36 public:
37         /// Default constructor (only here for STL containers).
38         TextMetrics(): text_(0) {}
39         /// The only useful constructor.
40         TextMetrics(BufferView *, Text *);
41         ///
42         ParagraphMetrics const & parMetrics(pit_type) const;
43         ///
44         Dimension const & dimension() const;
45         Dimension const & dimension();
46
47         /// compute text metrics.
48         bool metrics(MetricsInfo & mi, Dimension & dim);
49
50         /// Rebreaks the given paragraph.
51         /// \retval true if a full screen redraw is needed.
52         /// \retval false if a single paragraph redraw is enough.
53         bool redoParagraph(pit_type const pit);
54         /// Clear cache of paragraph metrics
55         void clear() { par_metrics_.clear(); }
56
57         ///
58         int ascent() const { return dim_.asc; }
59         ///
60         int descent() const { return dim_.des; }
61         /// current text width.
62         int width() const { return dim_.wid; }
63         /// current text heigth.
64         int height() const { return dim_.height(); }
65
66         ///
67         int maxWidth() const { return max_width_; }
68         ///
69         int singleWidth(pit_type const pit,     pos_type pos) const;
70
71         ///
72         int rightMargin(ParagraphMetrics const & pm) const;
73         int rightMargin(pit_type const pit) const;
74
75         /** this calculates the specified parameters. needed when setting
76          * the cursor and when creating a visible row */
77         void computeRowMetrics(pit_type pit, Row & row) const;
78
79         ///
80         void draw(PainterInfo & pi, int x, int y) const;
81         
82         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
83
84 private:
85         ///
86         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
87
88         /// the minimum space a manual label needs on the screen in pixels
89         int labelFill(pit_type const pit, Row const & row) const;
90
91         /// FIXME??
92         int labelEnd(pit_type const pit) const;
93
94         /// sets row.end to the pos value *after* which a row should break.
95         /// for example, the pos after which isNewLine(pos) == true
96         pit_type rowBreakPoint(
97                 int width,
98                 pit_type const pit,
99                 pit_type first
100                 ) const;
101
102         /// sets row.width to the minimum space a row needs on the screen in pixel
103         int rowWidth(
104                 int right_margin,
105                 pit_type const pit,
106                 pos_type const first,
107                 pos_type const end
108                 ) const;
109
110         /// Calculate and set the height of the row
111         boost::tuple<int, int> rowHeight(
112                 pit_type const pit,
113                 pos_type const first,
114                 pos_type const end
115                 ) const;
116
117 // Temporary public:
118 public:
119         /// returns the column near the specified x-coordinate of the row.
120         /// x is an absolute screen coord, it is set to the real beginning
121         /// of this column.
122         pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
123                 bool & boundary) const;
124
125         /// returns pos in given par at given x coord.
126         pos_type x2pos(pit_type pit, int row, int x) const;
127
128         // FIXME: is there a need for this?
129         //int pos2x(pit_type pit, pos_type pos) const;
130
131 private:
132
133         /// The BufferView owner.
134         BufferView * bv_;
135
136         /// The text contents (the model).
137         /// \todo FIXME: this should be const.
138         Text * text_;
139
140         bool main_text_;
141         /// A map from paragraph index number to paragraph metrics
142         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
143         ///
144         mutable ParMetricsCache par_metrics_;
145         Dimension dim_;
146         int max_width_;
147
148         /// FIXME: transfer this code in CoordCache here.
149         /*
150         /// A map from paragraph index number to screen point
151         typedef std::map<pit_type, Point> InnerParPosCache;
152         /// A map from a Text to the map of paragraphs to screen points
153         typedef std::map<Text const *, InnerParPosCache> ParPosCache;
154         /// Paragraph grouped by owning text
155         ParPosCache pars_;
156         */
157 };
158
159 /// return the default height of a row in pixels, considering font zoom
160 int defaultRowHeight();
161
162 } // namespace lyx
163
164 #endif // TEXT_METRICS_H