]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
Amend 6c3447c8: FindAdv: sometimes a space is added on some math symbols
[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 const int pm_npos = -10000;
44
45 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
46         position_(pm_npos), id_(par.id()), par_(&par)
47 {}
48
49
50 ParagraphMetrics & ParagraphMetrics::operator=(
51         ParagraphMetrics const & pm)
52 {
53         rows_ = pm.rows_;
54         dim_ = pm.dim_;
55         par_ = pm.par_;
56         position_ = pm.position_;
57         return *this;
58 }
59
60
61 void ParagraphMetrics::reset(Paragraph const & par)
62 {
63         par_ = &par;
64         dim_ = Dimension();
65         //position_ = pm_npos;
66 }
67
68
69 int ParagraphMetrics::position() const
70 {
71         LASSERT(hasPosition(), return pm_npos);
72         return position_;
73 }
74
75
76 void ParagraphMetrics::setPosition(int position)
77 {
78         position_ = position;
79 }
80
81
82 void ParagraphMetrics::resetPosition()
83 {
84         position_ = pm_npos;
85 }
86
87
88 bool ParagraphMetrics::hasPosition() const
89 {
90         return position_ != pm_npos;
91 }
92
93
94 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
95 {
96         LBUFERR(!rows().empty());
97
98         // If boundary is set we should return the row on which
99         // the character before is inside.
100         if (pos > 0 && boundary)
101                 --pos;
102
103         RowList::const_iterator rit = rows_.end();
104         RowList::const_iterator const begin = rows_.begin();
105
106         for (--rit; rit != begin && rit->pos() > pos; --rit)
107                 ;
108
109         return *rit;
110 }
111
112
113 size_t ParagraphMetrics::pos2row(pos_type pos) const
114 {
115         LBUFERR(!rows().empty());
116
117         RowList::const_iterator rit = rows_.end();
118         RowList::const_iterator const begin = rows_.begin();
119
120         for (--rit; rit != begin && rit->pos() > pos; --rit)
121                 ;
122
123         return rit - begin;
124 }
125
126
127 void ParagraphMetrics::dump() const
128 {
129         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
130         for (size_t i = 0; i != rows_.size(); ++i) {
131                 lyxerr << "  row " << i << ":   " << rows_[i];
132         }
133 }
134
135 int ParagraphMetrics::rightMargin(BufferView const & bv) const
136 {
137         BufferParams const & params = bv.buffer().params();
138         DocumentClass const & tclass = params.documentClass();
139         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
140         int const r_margin =
141                 bv.rightMargin()
142                 + fm.signedWidth(tclass.rightmargin())
143                 + fm.signedWidth(par_->layout().rightmargin)
144                 * 4 / (par_->getDepth() + 4);
145
146         return r_margin;
147 }
148
149
150 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
151 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
152 {
153         if (!par_->isHfill(pos))
154                 return false;
155
156         LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
157
158         // expand at the end of a row only if there is another hfill on the same row
159         if (pos == row.endpos() - 1) {
160                 for (pos_type i = row.pos(); i < pos; i++) {
161                         if (par_->isHfill(i))
162                                 return true;
163                 }
164                 return false;
165         }
166
167         // expand at the beginning of a row only if it is the first row of a paragraph
168         if (pos == row.pos())
169                 return pos == 0;
170
171         // do not expand in some labels
172         if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
173                 return false;
174
175         // if there is anything between the first char of the row and
176         // the specified position that is neither a newline nor an hfill,
177         // the hfill will be expanded, otherwise it won't
178         for (pos_type i = row.pos(); i < pos; i++) {
179                 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
180                         return true;
181         }
182         return false;
183 }
184
185 } // namespace lyx