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