]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
simplification
[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                 crc.process_bytes(b, sizeof(char_type));
107                 if (bparams.trackChanges) {
108                         Change change = par_->lookupChange(i);
109                         char_type const b[] = { change.type };
110                         // 1 byte is enough to encode Change::Type
111                         crc.process_bytes(b, 1);
112                 }                       
113         }
114
115         Dimension const & d = row.dimension();
116         char_type const b[] = { row.sel_beg, row.sel_end, d.wid, d.asc, d.des};
117         // Each of the variable to process is 4 bytes: 4x5 = 20
118         crc.process_bytes(b, 20);
119
120         return crc.checksum();
121 }
122
123
124 void ParagraphMetrics::setPosition(int position)
125 {
126         position_ = position;
127 }
128
129
130 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
131 {
132         InsetDims::const_iterator it = inset_dims_.find(inset);
133         if (it != inset_dims_.end())
134                 return it->second;
135
136         static Dimension dummy;
137         return dummy;
138 }
139
140
141 void ParagraphMetrics::setInsetDimension(Inset const * inset,
142                 Dimension const & dim)
143 {
144         inset_dims_[inset] = dim;
145 }
146
147
148 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
149 {
150         BOOST_ASSERT(!rows().empty());
151
152         // If boundary is set we should return the row on which
153         // the character before is inside.
154         if (pos > 0 && boundary)
155                 --pos;
156
157         RowList::iterator rit = rows_.end();
158         RowList::iterator const begin = rows_.begin();
159
160         for (--rit; rit != begin && rit->pos() > pos; --rit)
161                 ;
162
163         return *rit;
164 }
165
166
167 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
168 {
169         BOOST_ASSERT(!rows().empty());
170
171         // If boundary is set we should return the row on which
172         // the character before is inside.
173         if (pos > 0 && boundary)
174                 --pos;
175
176         RowList::const_iterator rit = rows_.end();
177         RowList::const_iterator const begin = rows_.begin();
178
179         for (--rit; rit != begin && rit->pos() > pos; --rit)
180                 ;
181
182         return *rit;
183 }
184
185
186 size_t ParagraphMetrics::pos2row(pos_type pos) const
187 {
188         BOOST_ASSERT(!rows().empty());
189
190         RowList::const_iterator rit = rows_.end();
191         RowList::const_iterator const begin = rows_.begin();
192
193         for (--rit; rit != begin && rit->pos() > pos; --rit)
194                 ;
195
196         return rit - begin;
197 }
198
199
200 void ParagraphMetrics::dump() const
201 {
202         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
203         for (size_t i = 0; i != rows_.size(); ++i) {
204                 lyxerr << "  row " << i << ":   ";
205                 rows_[i].dump();
206         }
207 }
208
209 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
210 {
211         BufferParams const & params = buffer.params();
212         TextClass const & tclass = params.getTextClass();
213         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
214         int const r_margin =
215                 lyx::rightMargin()
216                 + fm.signedWidth(tclass.rightmargin())
217                 + fm.signedWidth(par_->layout()->rightmargin)
218                 * 4 / (par_->getDepth() + 4);
219
220         return r_margin;
221 }
222
223
224 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
225 {
226         // The most special cases are handled first.
227         if (par_->isInset(pos))
228                 return insetDimension(par_->getInset(pos)).wid;
229
230         char_type c = par_->getChar(pos);
231
232         if (!isPrintable(c))
233                 return theFontMetrics(font).width(c);
234
235         Language const * language = font.language();
236         if (language->rightToLeft()) {
237                 if (language->lang() == "arabic_arabtex" ||
238                         language->lang() == "arabic_arabi" ||
239                         language->lang() == "farsi") {
240                                 if (Encodings::isComposeChar_arabic(c))
241                                         return 0;
242                                 c = par_->transformChar(c, pos);
243                 } else if (language->lang() == "hebrew" &&
244                         Encodings::isComposeChar_hebrew(c))
245                         return 0;
246         }
247         return theFontMetrics(font).width(c);
248 }
249
250
251 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
252 {
253         if (!par_->isHfill(pos))
254                 return false;
255
256         BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
257
258         // expand at the end of a row only if there is another hfill on the same row
259         if (pos == row.endpos() - 1) {
260                 for (pos_type i = row.pos(); i < pos; i++) {
261                         if (par_->isHfill(i))
262                                 return true;
263                 }
264                 return false;
265         }
266
267         // expand at the beginning of a row only if it is the first row of a paragraph
268         if (pos == row.pos()) {
269                 return pos == 0;
270         }
271
272         // do not expand in some labels
273         if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
274                 return false;
275
276         // if there is anything between the first char of the row and
277         // the specified position that is neither a newline nor an hfill,
278         // the hfill will be expanded, otherwise it won't
279         for (pos_type i = row.pos(); i < pos; i++) {
280                 if (!par_->isNewline(i) && !par_->isHfill(i))
281                         return true;
282         }
283         return false;
284 }
285
286 } // namespace lyx