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