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