]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
Account for old versions of Pygments
[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 "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Layout.h"
30 #include "Font.h"
31 #include "LyXRC.h"
32 #include "Row.h"
33 #include "OutputParams.h"
34 #include "sgml.h"
35 #include "TextClass.h"
36 #include "TexRow.h"
37
38 #include "frontends/FontMetrics.h"
39
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetArgument.h"
42
43 #include "support/lassert.h"
44 #include "support/debug.h"
45 #include "support/ExceptionMessage.h"
46 #include "support/gettext.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
49
50 #include <boost/crc.hpp>
51
52 #include <algorithm>
53 #include <list>
54 #include <stack>
55 #include <sstream>
56
57 using namespace std;
58 using namespace lyx::support;
59
60 namespace lyx {
61
62
63 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
64         position_(-1), par_(&par)
65 {}
66
67
68 ParagraphMetrics & ParagraphMetrics::operator=(
69         ParagraphMetrics const & pm)
70 {
71         rows_ = pm.rows_;
72         dim_ = pm.dim_;
73         par_ = pm.par_;
74         position_ = pm.position_;
75         return *this;
76 }
77
78
79 void ParagraphMetrics::reset(Paragraph const & par)
80 {
81         par_ = &par;
82         dim_ = Dimension();
83         //position_ = -1;
84 }
85
86
87 size_t ParagraphMetrics::computeRowSignature(Row const & row,
88                 BufferView const & bv) const
89 {
90         boost::crc_32_type crc;
91         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
92                 if (par_->isInset(i)) {
93                         Inset const * in = par_->getInset(i);
94                         Dimension const d = in->dimension(bv);
95                         int const b[] = { d.wid, d.asc, d.des };
96                         crc.process_bytes(b, sizeof(b));
97                 } else {
98                         char_type const b[] = { par_->getChar(i) };
99                         crc.process_bytes(b, sizeof(char_type));
100                 }
101                 if (bv.buffer().params().track_changes) {
102                         Change change = par_->lookupChange(i);
103                         char_type const b[] = { static_cast<char_type>(change.type) };
104                         // 1 byte is enough to encode Change::Type
105                         crc.process_bytes(b, 1);
106                 }
107         }
108
109         pos_type const b1[] = { row.sel_beg, row.sel_end };
110         crc.process_bytes(b1, sizeof(b1));
111
112         Dimension const & d = row.dimension();
113         int const b2[] = { row.begin_margin_sel,
114                            row.end_margin_sel,
115                            d.wid, d.asc, d.des };
116         crc.process_bytes(b2, sizeof(b2));
117         crc.process_bytes(&row.separator, sizeof(row.separator));
118
119         return crc.checksum();
120 }
121
122
123 void ParagraphMetrics::setPosition(int position)
124 {
125         position_ = position;
126 }
127
128
129 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
130 {
131         LBUFERR(!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         LBUFERR(!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         LBUFERR(!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 << ":   " << rows_[i];
186         }
187 }
188
189 int ParagraphMetrics::rightMargin(BufferView const & bv) const
190 {
191         BufferParams const & params = bv.buffer().params();
192         DocumentClass const & tclass = params.documentClass();
193         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
194         int const r_margin =
195                 bv.rightMargin()
196                 + fm.signedWidth(tclass.rightmargin())
197                 + fm.signedWidth(par_->layout().rightmargin)
198                 * 4 / (par_->getDepth() + 4);
199
200         return r_margin;
201 }
202
203
204 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
205 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
206 {
207         if (!par_->isHfill(pos))
208                 return false;
209
210         LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
211
212         // expand at the end of a row only if there is another hfill on the same row
213         if (pos == row.endpos() - 1) {
214                 for (pos_type i = row.pos(); i < pos; i++) {
215                         if (par_->isHfill(i))
216                                 return true;
217                 }
218                 return false;
219         }
220
221         // expand at the beginning of a row only if it is the first row of a paragraph
222         if (pos == row.pos())
223                 return pos == 0;
224
225         // do not expand in some labels
226         if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
227                 return false;
228
229         // if there is anything between the first char of the row and
230         // the specified position that is neither a newline nor an hfill,
231         // the hfill will be expanded, otherwise it won't
232         for (pos_type i = row.pos(); i < pos; i++) {
233                 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
234                         return true;
235         }
236         return false;
237 }
238
239 } // namespace lyx