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