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)
96 void ParagraphMetrics::computeRowSignature(Row & row,
97 BufferParams const & bparams)
99 boost::crc_32_type crc;
100 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
101 char_type const b[] = { par_->getChar(i) };
102 crc.process_bytes(b, 1);
103 if (bparams.trackChanges) {
104 Change change = par_->lookupChange(i);
105 char_type const b[] = { change.type };
106 crc.process_bytes(b, 1);
109 row.setCrc(crc.checksum());
113 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
115 BOOST_ASSERT(!rows().empty());
117 // If boundary is set we should return the row on which
118 // the character before is inside.
119 if (pos > 0 && boundary)
122 RowList::iterator rit = rows_.end();
123 RowList::iterator const begin = rows_.begin();
125 for (--rit; rit != begin && rit->pos() > pos; --rit)
132 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
134 BOOST_ASSERT(!rows().empty());
136 // If boundary is set we should return the row on which
137 // the character before is inside.
138 if (pos > 0 && boundary)
141 RowList::const_iterator rit = rows_.end();
142 RowList::const_iterator const begin = rows_.begin();
144 for (--rit; rit != begin && rit->pos() > pos; --rit)
151 size_t ParagraphMetrics::pos2row(pos_type pos) const
153 BOOST_ASSERT(!rows().empty());
155 RowList::const_iterator rit = rows_.end();
156 RowList::const_iterator const begin = rows_.begin();
158 for (--rit; rit != begin && rit->pos() > pos; --rit)
165 void ParagraphMetrics::dump() const
167 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
168 for (size_t i = 0; i != rows_.size(); ++i) {
169 lyxerr << " row " << i << ": ";
174 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
176 BufferParams const & params = buffer.params();
177 TextClass const & tclass = params.getTextClass();
178 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
181 + fm.signedWidth(tclass.rightmargin())
182 + fm.signedWidth(par_->layout()->rightmargin)
183 * 4 / (par_->getDepth() + 4);
189 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
191 char_type c = par_->getChar(pos);
193 // The most special cases are handled first.
194 if (c == Paragraph::META_INSET)
195 return par_->getInset(pos)->width();
198 return theFontMetrics(font).width(c);
200 Language const * language = font.language();
201 if (language->rightToLeft()) {
202 if (language->lang() == "arabic_arabtex" ||
203 language->lang() == "arabic_arabi" ||
204 language->lang() == "farsi") {
205 if (Encodings::isComposeChar_arabic(c))
207 c = par_->transformChar(c, pos);
208 } else if (language->lang() == "hebrew" &&
209 Encodings::isComposeChar_hebrew(c))
212 return theFontMetrics(font).width(c);