]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
Reduce caption hardcoding
[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 "support/bind.h"
51 #include <boost/crc.hpp>
52
53 #include <algorithm>
54 #include <list>
55 #include <stack>
56 #include <sstream>
57
58 using namespace std;
59 using namespace lyx::support;
60
61 namespace lyx {
62
63
64 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
65         position_(-1), par_(&par)
66 {}
67
68
69 ParagraphMetrics & ParagraphMetrics::operator=(
70         ParagraphMetrics const & pm)
71 {
72         rows_ = pm.rows_;
73         dim_ = pm.dim_;
74         par_ = pm.par_;
75         position_ = pm.position_;
76         return *this;
77 }
78
79
80 void ParagraphMetrics::reset(Paragraph const & par)
81 {
82         par_ = &par;
83         dim_ = Dimension();
84         //position_ = -1;
85 }
86
87
88 size_t ParagraphMetrics::computeRowSignature(Row const & row,
89                 BufferParams const & bparams) const
90 {
91         boost::crc_32_type crc;
92         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
93                 char_type const b[] = { par_->getChar(i) };
94                 crc.process_bytes(b, sizeof(char_type));
95                 if (bparams.track_changes) {
96                         Change change = par_->lookupChange(i);
97                         char_type const b[] = { static_cast<char_type>(change.type) };
98                         // 1 byte is enough to encode Change::Type
99                         crc.process_bytes(b, 1);
100                 }
101         }
102
103         Dimension const & d = row.dimension();
104         char_type const b[] = { static_cast<char_type>(row.sel_beg),
105                                 static_cast<char_type>(row.sel_end),
106                                 row.begin_margin_sel,
107                                 row.end_margin_sel,
108                                 reinterpret_cast<char_type const *>(&row.separator)[0],
109                                 reinterpret_cast<char_type const *>(&row.separator)[1],
110                                 d.wid, d.asc, d.des };
111         crc.process_bytes(b, sizeof(b));
112
113         return crc.checksum();
114 }
115
116
117 void ParagraphMetrics::setPosition(int position)
118 {
119         position_ = position;
120 }
121
122
123 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
124 {
125         InsetDims::const_iterator it = inset_dims_.find(inset);
126         if (it != inset_dims_.end())
127                 return it->second;
128
129         static Dimension dummy;
130         return dummy;
131 }
132
133
134 void ParagraphMetrics::setInsetDimension(Inset const * inset,
135                 Dimension const & dim)
136 {
137         inset_dims_[inset] = dim;
138 }
139
140
141 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
142 {
143         LBUFERR(!rows().empty());
144
145         // If boundary is set we should return the row on which
146         // the character before is inside.
147         if (pos > 0 && boundary)
148                 --pos;
149
150         RowList::iterator rit = rows_.end();
151         RowList::iterator const begin = rows_.begin();
152
153         for (--rit; rit != begin && rit->pos() > pos; --rit)
154                 ;
155
156         return *rit;
157 }
158
159
160 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
161 {
162         LBUFERR(!rows().empty());
163
164         // If boundary is set we should return the row on which
165         // the character before is inside.
166         if (pos > 0 && boundary)
167                 --pos;
168
169         RowList::const_iterator rit = rows_.end();
170         RowList::const_iterator const begin = rows_.begin();
171
172         for (--rit; rit != begin && rit->pos() > pos; --rit)
173                 ;
174
175         return *rit;
176 }
177
178
179 size_t ParagraphMetrics::pos2row(pos_type pos) const
180 {
181         LBUFERR(!rows().empty());
182
183         RowList::const_iterator rit = rows_.end();
184         RowList::const_iterator const begin = rows_.begin();
185
186         for (--rit; rit != begin && rit->pos() > pos; --rit)
187                 ;
188
189         return rit - begin;
190 }
191
192
193 void ParagraphMetrics::dump() const
194 {
195         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
196         for (size_t i = 0; i != rows_.size(); ++i) {
197                 lyxerr << "  row " << i << ":   " << rows_[i];
198         }
199 }
200
201 int ParagraphMetrics::rightMargin(BufferView const & bv) const
202 {
203         BufferParams const & params = bv.buffer().params();
204         DocumentClass const & tclass = params.documentClass();
205         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
206         int const r_margin =
207                 bv.rightMargin()
208                 + fm.signedWidth(tclass.rightmargin())
209                 + fm.signedWidth(par_->layout().rightmargin)
210                 * 4 / (par_->getDepth() + 4);
211
212         return r_margin;
213 }
214
215
216 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
217 {
218         // The most special cases are handled first.
219         if (Inset const * inset = par_->getInset(pos))
220                 return insetDimension(inset).wid;
221
222         char_type const c = par_->getChar(pos);
223
224         if (c == '\t')
225                 return 4 * theFontMetrics(font).width(' ');
226
227         // Note that this function is only called in
228         // RowPainter::paintText, and only used for characters that do
229         // not require handling of compose chars or ligatures. It can
230         // therefore be kept simple.
231         return theFontMetrics(font).width(c);
232 }
233
234
235 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
236 {
237         if (!par_->isHfill(pos))
238                 return false;
239
240         LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
241
242         // expand at the end of a row only if there is another hfill on the same row
243         if (pos == row.endpos() - 1) {
244                 for (pos_type i = row.pos(); i < pos; i++) {
245                         if (par_->isHfill(i))
246                                 return true;
247                 }
248                 return false;
249         }
250
251         // expand at the beginning of a row only if it is the first row of a paragraph
252         if (pos == row.pos())
253                 return pos == 0;
254
255         // do not expand in some labels
256         if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
257                 return false;
258
259         // if there is anything between the first char of the row and
260         // the specified position that is neither a newline nor an hfill,
261         // the hfill will be expanded, otherwise it won't
262         for (pos_type i = row.pos(); i < pos; i++) {
263                 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
264                         return true;
265         }
266         return false;
267 }
268
269 } // namespace lyx