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