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