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