]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
Revert 82c7669381
[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 "Counters.h"
26 #include "Encoding.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Layout.h"
30 #include "Font.h"
31 #include "LyXRC.h"
32 #include "Row.h"
33 #include "OutputParams.h"
34 #include "sgml.h"
35 #include "TextClass.h"
36 #include "TexRow.h"
37
38 #include "frontends/FontMetrics.h"
39
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetArgument.h"
42
43 #include "support/lassert.h"
44 #include "support/debug.h"
45 #include "support/ExceptionMessage.h"
46 #include "support/gettext.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
49
50 #include "support/bind.h"
51 #include <boost/crc.hpp>
52
53 #include <algorithm>
54 #include <list>
55 #include <stack>
56 #include <sstream>
57
58 using namespace std;
59 using namespace lyx::support;
60
61 namespace lyx {
62
63
64 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
65         position_(-1), par_(&par)
66 {}
67
68
69 ParagraphMetrics & ParagraphMetrics::operator=(
70         ParagraphMetrics const & pm)
71 {
72         rows_ = pm.rows_;
73         dim_ = pm.dim_;
74         par_ = pm.par_;
75         position_ = pm.position_;
76         return *this;
77 }
78
79
80 void ParagraphMetrics::reset(Paragraph const & par)
81 {
82         par_ = &par;
83         dim_ = Dimension();
84         //position_ = -1;
85 }
86
87
88 size_t ParagraphMetrics::computeRowSignature(Row const & row,
89                 BufferParams const & bparams) const
90 {
91         boost::crc_32_type crc;
92         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
93                 char_type const b[] = { par_->getChar(i) };
94                 crc.process_bytes(b, sizeof(char_type));
95                 if (bparams.track_changes) {
96                         Change change = par_->lookupChange(i);
97                         char_type const b[] = { static_cast<char_type>(change.type) };
98                         // 1 byte is enough to encode Change::Type
99                         crc.process_bytes(b, 1);
100                 }
101         }
102
103         Dimension const & d = row.dimension();
104         char_type const b[] = { static_cast<char_type>(row.sel_beg),
105                                 static_cast<char_type>(row.sel_end),
106                                 row.begin_margin_sel,
107                                 row.end_margin_sel,
108                                 d.wid, d.asc, d.des };
109         crc.process_bytes(b, sizeof(b));
110         crc.process_bytes(&row.separator, sizeof(row.separator));
111
112         return crc.checksum();
113 }
114
115
116 void ParagraphMetrics::setPosition(int position)
117 {
118         position_ = position;
119 }
120
121
122 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
123 {
124         InsetDims::const_iterator it = inset_dims_.find(inset);
125         if (it != inset_dims_.end())
126                 return it->second;
127
128         static Dimension dummy;
129         return dummy;
130 }
131
132
133 void ParagraphMetrics::setInsetDimension(Inset const * inset,
134                 Dimension const & dim)
135 {
136         inset_dims_[inset] = dim;
137 }
138
139
140 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
141 {
142         LBUFERR(!rows().empty());
143
144         // If boundary is set we should return the row on which
145         // the character before is inside.
146         if (pos > 0 && boundary)
147                 --pos;
148
149         RowList::iterator rit = rows_.end();
150         RowList::iterator const begin = rows_.begin();
151
152         for (--rit; rit != begin && rit->pos() > pos; --rit)
153                 ;
154
155         return *rit;
156 }
157
158
159 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
160 {
161         LBUFERR(!rows().empty());
162
163         // If boundary is set we should return the row on which
164         // the character before is inside.
165         if (pos > 0 && boundary)
166                 --pos;
167
168         RowList::const_iterator rit = rows_.end();
169         RowList::const_iterator const begin = rows_.begin();
170
171         for (--rit; rit != begin && rit->pos() > pos; --rit)
172                 ;
173
174         return *rit;
175 }
176
177
178 size_t ParagraphMetrics::pos2row(pos_type pos) const
179 {
180         LBUFERR(!rows().empty());
181
182         RowList::const_iterator rit = rows_.end();
183         RowList::const_iterator const begin = rows_.begin();
184
185         for (--rit; rit != begin && rit->pos() > pos; --rit)
186                 ;
187
188         return rit - begin;
189 }
190
191
192 void ParagraphMetrics::dump() const
193 {
194         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
195         for (size_t i = 0; i != rows_.size(); ++i) {
196                 lyxerr << "  row " << i << ":   " << rows_[i];
197         }
198 }
199
200 int ParagraphMetrics::rightMargin(BufferView const & bv) const
201 {
202         BufferParams const & params = bv.buffer().params();
203         DocumentClass const & tclass = params.documentClass();
204         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
205         int const r_margin =
206                 bv.rightMargin()
207                 + fm.signedWidth(tclass.rightmargin())
208                 + fm.signedWidth(par_->layout().rightmargin)
209                 * 4 / (par_->getDepth() + 4);
210
211         return r_margin;
212 }
213
214
215 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
216 {
217         // The most special cases are handled first.
218         if (Inset const * inset = par_->getInset(pos))
219                 return insetDimension(inset).wid;
220
221         char_type const c = par_->getChar(pos);
222
223         if (c == '\t')
224                 return 4 * theFontMetrics(font).width(' ');
225
226         // Note that this function is only called in
227         // RowPainter::paintText, and only used for characters that do
228         // not require handling of compose chars or ligatures. It can
229         // therefore be kept simple.
230         return theFontMetrics(font).width(c);
231 }
232
233
234 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
235 {
236         if (!par_->isHfill(pos))
237                 return false;
238
239         LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
240
241         // expand at the end of a row only if there is another hfill on the same row
242         if (pos == row.endpos() - 1) {
243                 for (pos_type i = row.pos(); i < pos; i++) {
244                         if (par_->isHfill(i))
245                                 return true;
246                 }
247                 return false;
248         }
249
250         // expand at the beginning of a row only if it is the first row of a paragraph
251         if (pos == row.pos())
252                 return pos == 0;
253
254         // do not expand in some labels
255         if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
256                 return false;
257
258         // if there is anything between the first char of the row and
259         // the specified position that is neither a newline nor an hfill,
260         // the hfill will be expanded, otherwise it won't
261         for (pos_type i = row.pos(); i < pos; i++) {
262                 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
263                         return true;
264         }
265         return false;
266 }
267
268 } // namespace lyx