]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / ParagraphMetrics.cpp
1 /**
2  * \file Paragraph.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "ParagraphMetrics.h"
21
22 #include "Buffer.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "Font.h"
26 #include "Layout.h"
27 #include "LayoutEnums.h"
28 #include "Paragraph.h"
29 #include "TextClass.h"
30
31 #include "frontends/FontMetrics.h"
32
33 #include "support/lassert.h"
34 #include "support/debug.h"
35
36 #include <sstream>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43
44 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
45         position_(-1), id_(par.id()), par_(&par)
46 {}
47
48
49 ParagraphMetrics & ParagraphMetrics::operator=(
50         ParagraphMetrics const & pm)
51 {
52         rows_ = pm.rows_;
53         dim_ = pm.dim_;
54         par_ = pm.par_;
55         position_ = pm.position_;
56         return *this;
57 }
58
59
60 void ParagraphMetrics::reset(Paragraph const & par)
61 {
62         par_ = &par;
63         dim_ = Dimension();
64         //position_ = -1;
65 }
66
67
68 void ParagraphMetrics::setPosition(int position)
69 {
70         position_ = position;
71 }
72
73
74 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
75 {
76         LBUFERR(!rows().empty());
77
78         // If boundary is set we should return the row on which
79         // the character before is inside.
80         if (pos > 0 && boundary)
81                 --pos;
82
83         RowList::const_iterator rit = rows_.end();
84         RowList::const_iterator const begin = rows_.begin();
85
86         for (--rit; rit != begin && rit->pos() > pos; --rit)
87                 ;
88
89         return *rit;
90 }
91
92
93 size_t ParagraphMetrics::pos2row(pos_type pos) const
94 {
95         LBUFERR(!rows().empty());
96
97         RowList::const_iterator rit = rows_.end();
98         RowList::const_iterator const begin = rows_.begin();
99
100         for (--rit; rit != begin && rit->pos() > pos; --rit)
101                 ;
102
103         return rit - begin;
104 }
105
106
107 void ParagraphMetrics::dump() const
108 {
109         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
110         for (size_t i = 0; i != rows_.size(); ++i) {
111                 lyxerr << "  row " << i << ":   " << rows_[i];
112         }
113 }
114
115 int ParagraphMetrics::rightMargin(BufferView const & bv) const
116 {
117         BufferParams const & params = bv.buffer().params();
118         DocumentClass const & tclass = params.documentClass();
119         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
120         int const r_margin =
121                 bv.rightMargin()
122                 + fm.signedWidth(tclass.rightmargin())
123                 + fm.signedWidth(par_->layout().rightmargin)
124                 * 4 / (par_->getDepth() + 4);
125
126         return r_margin;
127 }
128
129
130 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
131 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
132 {
133         if (!par_->isHfill(pos))
134                 return false;
135
136         LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
137
138         // expand at the end of a row only if there is another hfill on the same row
139         if (pos == row.endpos() - 1) {
140                 for (pos_type i = row.pos(); i < pos; i++) {
141                         if (par_->isHfill(i))
142                                 return true;
143                 }
144                 return false;
145         }
146
147         // expand at the beginning of a row only if it is the first row of a paragraph
148         if (pos == row.pos())
149                 return pos == 0;
150
151         // do not expand in some labels
152         if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
153                 return false;
154
155         // if there is anything between the first char of the row and
156         // the specified position that is neither a newline nor an hfill,
157         // the hfill will be expanded, otherwise it won't
158         for (pos_type i = row.pos(); i < pos; i++) {
159                 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
160                         return true;
161         }
162         return false;
163 }
164
165 } // namespace lyx