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