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