]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
0fd4838d2563d8d7fbc3c12b87436985627e2398
[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): 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         return *this;
86 }
87
88
89 void ParagraphMetrics::reset(Paragraph const & par)
90 {
91         par_ = &par;
92         dim_ = Dimension();
93         rows_.clear();
94 }
95
96
97 size_type ParagraphMetrics::calculateRowSignature(Row const & row,
98                 BufferParams const & bparams)
99 {
100         boost::crc_32_type crc;
101         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
102                 char_type const b[] = { par_->getChar(i) };
103                 crc.process_bytes(b, 1);
104                 if (bparams.trackChanges) {
105                         Change change = par_->lookupChange(i);
106                         char_type const b[] = { change.type };
107                         crc.process_bytes(b, 1);
108                 }                       
109         }
110         return crc.checksum();
111 }
112
113
114 void ParagraphMetrics::updateRowChangeStatus(BufferParams const & bparams)
115 {
116         size_t const size = rows_.size();
117         row_change_status_.resize(size);
118         row_signature_.resize(size);
119
120         for (size_t i = 0; i != size; ++i) {
121                 // Row signature; has row changed since last update?
122                 size_type const row_sig = calculateRowSignature(rows_[i], bparams);
123                 row_change_status_[i] = row_signature_[i] != row_sig;
124                 row_signature_[i] = row_sig;
125         }
126 }
127
128
129 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
130 {
131         BOOST_ASSERT(!rows().empty());
132
133         // If boundary is set we should return the row on which
134         // the character before is inside.
135         if (pos > 0 && boundary)
136                 --pos;
137
138         RowList::iterator rit = rows_.end();
139         RowList::iterator const begin = rows_.begin();
140
141         for (--rit; rit != begin && rit->pos() > pos; --rit)
142                 ;
143
144         return *rit;
145 }
146
147
148 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
149 {
150         BOOST_ASSERT(!rows().empty());
151
152         // If boundary is set we should return the row on which
153         // the character before is inside.
154         if (pos > 0 && boundary)
155                 --pos;
156
157         RowList::const_iterator rit = rows_.end();
158         RowList::const_iterator const begin = rows_.begin();
159
160         for (--rit; rit != begin && rit->pos() > pos; --rit)
161                 ;
162
163         return *rit;
164 }
165
166
167 size_t ParagraphMetrics::pos2row(pos_type pos) const
168 {
169         BOOST_ASSERT(!rows().empty());
170
171         RowList::const_iterator rit = rows_.end();
172         RowList::const_iterator const begin = rows_.begin();
173
174         for (--rit; rit != begin && rit->pos() > pos; --rit)
175                 ;
176
177         return rit - begin;
178 }
179
180
181 void ParagraphMetrics::dump() const
182 {
183         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
184         for (size_t i = 0; i != rows_.size(); ++i) {
185                 lyxerr << "  row " << i << ":   ";
186                 rows_[i].dump();
187         }
188 }
189
190 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
191 {
192         BufferParams const & params = buffer.params();
193         TextClass const & tclass = params.getTextClass();
194         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
195         int const r_margin =
196                 lyx::rightMargin()
197                 + fm.signedWidth(tclass.rightmargin())
198                 + fm.signedWidth(par_->layout()->rightmargin)
199                 * 4 / (par_->getDepth() + 4);
200
201         return r_margin;
202 }
203
204
205 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
206 {
207         char_type c = par_->getChar(pos);
208
209         // The most special cases are handled first.
210         if (c == Paragraph::META_INSET)
211                 return par_->getInset(pos)->width();
212
213         if (!isPrintable(c))
214                 return theFontMetrics(font).width(c);
215
216         Language const * language = font.language();
217         if (language->rightToLeft()) {
218                 if (language->lang() == "arabic_arabtex" ||
219                         language->lang() == "arabic_arabi" ||
220                         language->lang() == "farsi") {
221                                 if (Encodings::isComposeChar_arabic(c))
222                                         return 0;
223                                 c = par_->transformChar(c, pos);
224                 } else if (language->lang() == "hebrew" &&
225                         Encodings::isComposeChar_hebrew(c))
226                         return 0;
227         }
228         return theFontMetrics(font).width(c);
229 }
230
231
232 } // namespace lyx