* de.po: sync with branch.
[lyx.git] / src / ParagraphMetrics.cpp
blobd0822312fb2b5421d357e38c66c6e9e8e6bf6039
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.
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
15 * Full author contact details are available in file CREDITS.
18 #include <config.h>
20 #include "ParagraphMetrics.h"
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 #include "VSpace.h"
39 #include "frontends/FontMetrics.h"
41 #include "insets/InsetBibitem.h"
42 #include "insets/InsetOptArg.h"
44 #include "support/lassert.h"
45 #include "support/debug.h"
46 #include "support/gettext.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
50 #include <boost/bind.hpp>
51 #include <boost/crc.hpp>
53 #include <algorithm>
54 #include <list>
55 #include <stack>
56 #include <sstream>
58 using namespace std;
59 using namespace lyx::support;
61 namespace lyx {
64 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
65 position_(-1), par_(&par)
69 ParagraphMetrics & ParagraphMetrics::operator=(
70 ParagraphMetrics const & pm)
72 rows_ = pm.rows_;
73 dim_ = pm.dim_;
74 par_ = pm.par_;
75 position_ = pm.position_;
76 return *this;
80 void ParagraphMetrics::reset(Paragraph const & par)
82 par_ = &par;
83 dim_ = Dimension();
84 //position_ = -1;
88 size_t ParagraphMetrics::computeRowSignature(Row const & row,
89 BufferParams const & bparams) const
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.trackChanges) {
96 Change change = par_->lookupChange(i);
97 char_type const b[] = { change.type };
98 // 1 byte is enough to encode Change::Type
99 crc.process_bytes(b, 1);
103 Dimension const & d = row.dimension();
104 char_type const b[] = { row.sel_beg, row.sel_end,
105 row.begin_margin_sel, row.end_margin_sel, d.wid, d.asc, d.des};
106 // Each of the variable to process is 4 bytes: 4x7 = 28
107 crc.process_bytes(b, 28);
109 return crc.checksum();
113 void ParagraphMetrics::setPosition(int position)
115 position_ = position;
119 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
121 InsetDims::const_iterator it = inset_dims_.find(inset);
122 if (it != inset_dims_.end())
123 return it->second;
125 static Dimension dummy;
126 return dummy;
130 void ParagraphMetrics::setInsetDimension(Inset const * inset,
131 Dimension const & dim)
133 inset_dims_[inset] = dim;
137 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
139 LASSERT(!rows().empty(), /**/);
141 // If boundary is set we should return the row on which
142 // the character before is inside.
143 if (pos > 0 && boundary)
144 --pos;
146 RowList::iterator rit = rows_.end();
147 RowList::iterator const begin = rows_.begin();
149 for (--rit; rit != begin && rit->pos() > pos; --rit)
152 return *rit;
156 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
158 LASSERT(!rows().empty(), /**/);
160 // If boundary is set we should return the row on which
161 // the character before is inside.
162 if (pos > 0 && boundary)
163 --pos;
165 RowList::const_iterator rit = rows_.end();
166 RowList::const_iterator const begin = rows_.begin();
168 for (--rit; rit != begin && rit->pos() > pos; --rit)
171 return *rit;
175 size_t ParagraphMetrics::pos2row(pos_type pos) const
177 LASSERT(!rows().empty(), /**/);
179 RowList::const_iterator rit = rows_.end();
180 RowList::const_iterator const begin = rows_.begin();
182 for (--rit; rit != begin && rit->pos() > pos; --rit)
185 return rit - begin;
189 void ParagraphMetrics::dump() const
191 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
192 for (size_t i = 0; i != rows_.size(); ++i) {
193 lyxerr << " row " << i << ": ";
194 rows_[i].dump();
198 int ParagraphMetrics::rightMargin(BufferView const & bv) const
200 BufferParams const & params = bv.buffer().params();
201 DocumentClass const & tclass = params.documentClass();
202 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
203 int const r_margin =
204 bv.rightMargin()
205 + fm.signedWidth(tclass.rightmargin())
206 + fm.signedWidth(par_->layout().rightmargin)
207 * 4 / (par_->getDepth() + 4);
209 return r_margin;
213 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
215 // The most special cases are handled first.
216 if (Inset const * inset = par_->getInset(pos))
217 return insetDimension(inset).wid;
219 char_type c = par_->getChar(pos);
221 if (c == '\t')
222 return 4 * theFontMetrics(font).width(' ');
224 if (!isPrintable(c))
225 return theFontMetrics(font).width(c);
227 Language const * language = font.language();
228 if (language->rightToLeft()) {
229 if (language->lang() == "arabic_arabtex" ||
230 language->lang() == "arabic_arabi" ||
231 language->lang() == "farsi") {
232 if (Encodings::isArabicComposeChar(c))
233 return 0;
234 c = par_->transformChar(c, pos);
235 } else if (language->lang() == "hebrew" &&
236 Encodings::isHebrewComposeChar(c)) {
237 return 0;
240 return theFontMetrics(font).width(c);
244 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
246 if (!par_->isHfill(pos))
247 return false;
249 LASSERT(pos >= row.pos() && pos < row.endpos(), /**/);
251 // expand at the end of a row only if there is another hfill on the same row
252 if (pos == row.endpos() - 1) {
253 for (pos_type i = row.pos(); i < pos; i++) {
254 if (par_->isHfill(i))
255 return true;
257 return false;
260 // expand at the beginning of a row only if it is the first row of a paragraph
261 if (pos == row.pos()) {
262 return pos == 0;
265 // do not expand in some labels
266 if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
267 return false;
269 // if there is anything between the first char of the row and
270 // the specified position that is neither a newline nor an hfill,
271 // the hfill will be expanded, otherwise it won't
272 for (pos_type i = row.pos(); i < pos; i++) {
273 if (!par_->isNewline(i) && !par_->isHfill(i))
274 return true;
276 return false;
279 } // namespace lyx