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