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
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "ParagraphMetrics.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
30 #include "LaTeXFeatures.h"
34 #include "OutputParams.h"
35 #include "paragraph_funcs.h"
40 #include "frontends/FontMetrics.h"
42 #include "insets/InsetBibitem.h"
43 #include "insets/InsetOptArg.h"
45 #include "support/lstrings.h"
46 #include "support/textutils.h"
47 #include "support/convert.h"
48 #include "support/unicode.h"
50 #include <boost/bind.hpp>
51 #include <boost/crc.hpp>
61 using lyx::support::contains;
62 using lyx::support::rsplit;
71 using std::ostringstream;
74 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): par_(&par)
79 ParagraphMetrics & ParagraphMetrics::operator=(
80 ParagraphMetrics const & pm)
89 void ParagraphMetrics::reset(Paragraph const & par)
97 void ParagraphMetrics::computeRowSignature(Row & row,
98 BufferParams const & bparams)
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);
110 row.setCrc(crc.checksum());
114 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
116 BOOST_ASSERT(!rows().empty());
118 // If boundary is set we should return the row on which
119 // the character before is inside.
120 if (pos > 0 && boundary)
123 RowList::iterator rit = rows_.end();
124 RowList::iterator const begin = rows_.begin();
126 for (--rit; rit != begin && rit->pos() > pos; --rit)
133 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
135 BOOST_ASSERT(!rows().empty());
137 // If boundary is set we should return the row on which
138 // the character before is inside.
139 if (pos > 0 && boundary)
142 RowList::const_iterator rit = rows_.end();
143 RowList::const_iterator const begin = rows_.begin();
145 for (--rit; rit != begin && rit->pos() > pos; --rit)
152 size_t ParagraphMetrics::pos2row(pos_type pos) const
154 BOOST_ASSERT(!rows().empty());
156 RowList::const_iterator rit = rows_.end();
157 RowList::const_iterator const begin = rows_.begin();
159 for (--rit; rit != begin && rit->pos() > pos; --rit)
166 void ParagraphMetrics::dump() const
168 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
169 for (size_t i = 0; i != rows_.size(); ++i) {
170 lyxerr << " row " << i << ": ";
175 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
177 BufferParams const & params = buffer.params();
178 TextClass const & tclass = params.getTextClass();
179 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
182 + fm.signedWidth(tclass.rightmargin())
183 + fm.signedWidth(par_->layout()->rightmargin)
184 * 4 / (par_->getDepth() + 4);
190 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
192 char_type c = par_->getChar(pos);
194 // The most special cases are handled first.
195 if (c == Paragraph::META_INSET)
196 return par_->getInset(pos)->width();
199 return theFontMetrics(font).width(c);
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))
208 c = par_->transformChar(c, pos);
209 } else if (language->lang() == "hebrew" &&
210 Encodings::isComposeChar_hebrew(c))
213 return theFontMetrics(font).width(c);