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