]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
02eb591ff7ff66e763ba65209d2a1899ce4bbeb6
[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 "TexRow.h"
39 #include "VSpace.h"
40
41 #include "frontends/FontMetrics.h"
42
43 #include "insets/InsetBibitem.h"
44 #include "insets/InsetOptArg.h"
45
46 #include "support/lstrings.h"
47 #include "support/textutils.h"
48 #include "support/convert.h"
49 #include "support/unicode.h"
50
51 #include <boost/bind.hpp>
52 #include <boost/crc.hpp>
53
54 #include <algorithm>
55 #include <list>
56 #include <stack>
57 #include <sstream>
58
59
60 namespace lyx {
61
62 using lyx::support::contains;
63 using lyx::support::rsplit;
64 using support::subst;
65
66 using std::distance;
67 using std::endl;
68 using std::list;
69 using std::stack;
70 using std::string;
71 using std::ostream;
72 using std::ostringstream;
73
74
75 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): position_(-1), par_(&par)
76 {
77 }
78
79
80 ParagraphMetrics & ParagraphMetrics::operator=(
81         ParagraphMetrics const & pm)
82 {
83         rows_ = pm.rows_;
84         dim_ = pm.dim_;
85         par_ = pm.par_;
86         position_ = pm.position_;
87         return *this;
88 }
89
90
91 void ParagraphMetrics::reset(Paragraph const & par)
92 {
93         par_ = &par;
94         dim_ = Dimension();
95         //position_ = -1;
96 }
97
98
99 void ParagraphMetrics::computeRowSignature(Row & row,
100                 BufferParams const & bparams)
101 {
102         boost::crc_32_type crc;
103         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
104                 char_type const b[] = { par_->getChar(i) };
105                 crc.process_bytes(b, 1);
106                 if (bparams.trackChanges) {
107                         Change change = par_->lookupChange(i);
108                         char_type const b[] = { change.type };
109                         crc.process_bytes(b, 1);
110                 }                       
111         }
112         row.setCrc(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         BOOST_ASSERT(!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         BOOST_ASSERT(!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         BOOST_ASSERT(!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 << ":   ";
197                 rows_[i].dump();
198         }
199 }
200
201 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
202 {
203         BufferParams const & params = buffer.params();
204         TextClass const & tclass = params.getTextClass();
205         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
206         int const r_margin =
207                 lyx::rightMargin()
208                 + fm.signedWidth(tclass.rightmargin())
209                 + fm.signedWidth(par_->layout()->rightmargin)
210                 * 4 / (par_->getDepth() + 4);
211
212         return r_margin;
213 }
214
215
216 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
217 {
218         // The most special cases are handled first.
219         if (par_->isInset(pos);)
220                 return insetDimension(par_->getInset(pos)).wid;
221
222         char_type c = par_->getChar(pos);
223
224         if (!isPrintable(c))
225                 return theFontMetrics(font).width(c);
226
227         Language const * language = font.language();
228         if (language->rightToLeft()) {
229                 if (language->lang() == "arabic_arabtex" ||
230                         language->lang() == "arabic_arabi" ||
231                         language->lang() == "farsi") {
232                                 if (Encodings::isComposeChar_arabic(c))
233                                         return 0;
234                                 c = par_->transformChar(c, pos);
235                 } else if (language->lang() == "hebrew" &&
236                         Encodings::isComposeChar_hebrew(c))
237                         return 0;
238         }
239         return theFontMetrics(font).width(c);
240 }
241
242
243 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
244 {
245         if (!par_->isHfill(pos))
246                 return false;
247
248         BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
249
250         // expand at the end of a row only if there is another hfill on the same row
251         if (pos == row.endpos() - 1) {
252                 for (pos_type i = row.pos(); i < pos; i++) {
253                         if (par_->isHfill(i))
254                                 return true;
255                 }
256                 return false;
257         }
258
259         // expand at the beginning of a row only if it is the first row of a paragraph
260         if (pos == row.pos()) {
261                 return pos == 0;
262         }
263
264         // do not expand in some labels
265         if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
266                 return false;
267
268         // if there is anything between the first char of the row and
269         // the specified position that is neither a newline nor an hfill,
270         // the hfill will be expanded, otherwise it won't
271         for (pos_type i = row.pos(); i < pos; i++) {
272                 if (!par_->isNewline(i) && !par_->isHfill(i))
273                         return true;
274         }
275         return false;
276 }
277
278 } // namespace lyx