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