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