2006-12-29 23:54:48 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Paragraph.cpp
|
2006-12-29 23:54:48 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Asger Alstrup
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2006-12-29 23:54:48 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author Angus Leeming
|
|
|
|
* \author John Levon
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2006-12-29 23:54:48 +00:00
|
|
|
* \author Dekel Tsur
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Jürgen Vigna
|
2006-12-29 23:54:48 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "ParagraphMetrics.h"
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
2007-08-28 15:33:15 +00:00
|
|
|
#include "BufferView.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Counters.h"
|
|
|
|
#include "Encoding.h"
|
|
|
|
#include "Language.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2007-09-29 20:02:32 +00:00
|
|
|
#include "Layout.h"
|
2007-04-29 18:17:15 +00:00
|
|
|
#include "Font.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "LyXRC.h"
|
|
|
|
#include "Row.h"
|
|
|
|
#include "OutputParams.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
#include "sgml.h"
|
2007-11-07 23:25:08 +00:00
|
|
|
#include "TextClass.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "TexRow.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
#include "frontends/FontMetrics.h"
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "insets/InsetBibitem.h"
|
2010-06-04 22:44:58 +00:00
|
|
|
#include "insets/InsetArgument.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2013-04-25 21:27:10 +00:00
|
|
|
#include "support/ExceptionMessage.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/gettext.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/textutils.h"
|
|
|
|
|
2010-04-22 11:16:58 +00:00
|
|
|
#include "support/bind.h"
|
2007-01-06 11:12:24 +00:00
|
|
|
#include <boost/crc.hpp>
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <list>
|
|
|
|
#include <stack>
|
|
|
|
#include <sstream>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2008-02-24 15:44:11 +00:00
|
|
|
ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
|
|
|
|
position_(-1), par_(&par)
|
|
|
|
{}
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
|
2007-01-06 09:15:59 +00:00
|
|
|
ParagraphMetrics & ParagraphMetrics::operator=(
|
|
|
|
ParagraphMetrics const & pm)
|
|
|
|
{
|
|
|
|
rows_ = pm.rows_;
|
|
|
|
dim_ = pm.dim_;
|
|
|
|
par_ = pm.par_;
|
2007-09-11 16:04:10 +00:00
|
|
|
position_ = pm.position_;
|
2007-01-06 09:15:59 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-27 14:04:46 +00:00
|
|
|
void ParagraphMetrics::reset(Paragraph const & par)
|
|
|
|
{
|
|
|
|
par_ = ∥
|
|
|
|
dim_ = Dimension();
|
2007-09-11 16:04:10 +00:00
|
|
|
//position_ = -1;
|
2007-08-27 14:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-17 11:27:03 +00:00
|
|
|
size_t ParagraphMetrics::computeRowSignature(Row const & row,
|
|
|
|
BufferParams const & bparams) const
|
2007-01-06 09:15:59 +00:00
|
|
|
{
|
|
|
|
boost::crc_32_type crc;
|
|
|
|
for (pos_type i = row.pos(); i < row.endpos(); ++i) {
|
|
|
|
char_type const b[] = { par_->getChar(i) };
|
2007-11-18 23:46:15 +00:00
|
|
|
crc.process_bytes(b, sizeof(char_type));
|
2014-03-29 22:52:36 +00:00
|
|
|
if (bparams.track_changes) {
|
2007-08-28 22:44:09 +00:00
|
|
|
Change change = par_->lookupChange(i);
|
2012-01-07 18:29:07 +00:00
|
|
|
char_type const b[] = { static_cast<char_type>(change.type) };
|
2007-11-18 21:47:46 +00:00
|
|
|
// 1 byte is enough to encode Change::Type
|
2007-08-28 22:44:09 +00:00
|
|
|
crc.process_bytes(b, 1);
|
|
|
|
}
|
2007-01-06 09:15:59 +00:00
|
|
|
}
|
2007-11-17 11:27:03 +00:00
|
|
|
|
|
|
|
Dimension const & d = row.dimension();
|
2012-01-07 18:29:07 +00:00
|
|
|
char_type const b[] = { static_cast<char_type>(row.sel_beg),
|
|
|
|
static_cast<char_type>(row.sel_end),
|
2008-10-13 23:19:06 +00:00
|
|
|
row.begin_margin_sel, row.end_margin_sel, d.wid, d.asc, d.des};
|
2011-05-06 06:49:55 +00:00
|
|
|
crc.process_bytes(b, sizeof(b));
|
2007-11-17 11:27:03 +00:00
|
|
|
|
|
|
|
return crc.checksum();
|
2007-01-06 09:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-11 16:04:10 +00:00
|
|
|
void ParagraphMetrics::setPosition(int position)
|
|
|
|
{
|
|
|
|
position_ = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
|
|
|
|
{
|
|
|
|
InsetDims::const_iterator it = inset_dims_.find(inset);
|
|
|
|
if (it != inset_dims_.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
static Dimension dummy;
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ParagraphMetrics::setInsetDimension(Inset const * inset,
|
|
|
|
Dimension const & dim)
|
|
|
|
{
|
|
|
|
inset_dims_[inset] = dim;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-29 23:54:48 +00:00
|
|
|
Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
|
|
|
|
{
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(!rows().empty());
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
// If boundary is set we should return the row on which
|
|
|
|
// the character before is inside.
|
|
|
|
if (pos > 0 && boundary)
|
|
|
|
--pos;
|
|
|
|
|
|
|
|
RowList::iterator rit = rows_.end();
|
|
|
|
RowList::iterator const begin = rows_.begin();
|
|
|
|
|
|
|
|
for (--rit; rit != begin && rit->pos() > pos; --rit)
|
|
|
|
;
|
|
|
|
|
|
|
|
return *rit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
|
|
|
|
{
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(!rows().empty());
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
// If boundary is set we should return the row on which
|
|
|
|
// the character before is inside.
|
|
|
|
if (pos > 0 && boundary)
|
|
|
|
--pos;
|
|
|
|
|
|
|
|
RowList::const_iterator rit = rows_.end();
|
|
|
|
RowList::const_iterator const begin = rows_.begin();
|
|
|
|
|
|
|
|
for (--rit; rit != begin && rit->pos() > pos; --rit)
|
|
|
|
;
|
|
|
|
|
|
|
|
return *rit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ParagraphMetrics::pos2row(pos_type pos) const
|
|
|
|
{
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(!rows().empty());
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
|
|
RowList::const_iterator rit = rows_.end();
|
|
|
|
RowList::const_iterator const begin = rows_.begin();
|
|
|
|
|
|
|
|
for (--rit; rit != begin && rit->pos() > pos; --rit)
|
|
|
|
;
|
|
|
|
|
|
|
|
return rit - begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ParagraphMetrics::dump() const
|
|
|
|
{
|
|
|
|
lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
|
|
|
|
for (size_t i = 0; i != rows_.size(); ++i) {
|
|
|
|
lyxerr << " row " << i << ": ";
|
|
|
|
rows_[i].dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-09 17:20:23 +00:00
|
|
|
int ParagraphMetrics::rightMargin(BufferView const & bv) const
|
2006-12-29 23:54:48 +00:00
|
|
|
{
|
2008-02-09 17:20:23 +00:00
|
|
|
BufferParams const & params = bv.buffer().params();
|
2008-02-28 01:42:02 +00:00
|
|
|
DocumentClass const & tclass = params.documentClass();
|
2006-12-29 23:54:48 +00:00
|
|
|
frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
|
|
|
|
int const r_margin =
|
2008-02-09 17:20:23 +00:00
|
|
|
bv.rightMargin()
|
2007-08-18 13:21:12 +00:00
|
|
|
+ fm.signedWidth(tclass.rightmargin())
|
2008-03-06 21:31:27 +00:00
|
|
|
+ fm.signedWidth(par_->layout().rightmargin)
|
2006-12-29 23:54:48 +00:00
|
|
|
* 4 / (par_->getDepth() + 4);
|
|
|
|
|
|
|
|
return r_margin;
|
|
|
|
}
|
|
|
|
|
2007-08-18 21:17:10 +00:00
|
|
|
|
|
|
|
int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
|
|
|
|
{
|
|
|
|
// The most special cases are handled first.
|
2008-02-09 10:41:49 +00:00
|
|
|
if (Inset const * inset = par_->getInset(pos))
|
|
|
|
return insetDimension(inset).wid;
|
2007-08-18 21:17:10 +00:00
|
|
|
|
2007-10-24 07:08:55 +00:00
|
|
|
char_type c = par_->getChar(pos);
|
|
|
|
|
2008-09-30 18:00:02 +00:00
|
|
|
if (c == '\t')
|
|
|
|
return 4 * theFontMetrics(font).width(' ');
|
|
|
|
|
2007-08-18 21:17:10 +00:00
|
|
|
if (!isPrintable(c))
|
|
|
|
return theFontMetrics(font).width(c);
|
|
|
|
|
|
|
|
Language const * language = font.language();
|
|
|
|
if (language->rightToLeft()) {
|
|
|
|
if (language->lang() == "arabic_arabtex" ||
|
|
|
|
language->lang() == "arabic_arabi" ||
|
|
|
|
language->lang() == "farsi") {
|
2008-03-21 21:47:37 +00:00
|
|
|
if (Encodings::isArabicComposeChar(c))
|
2007-08-18 21:17:10 +00:00
|
|
|
return 0;
|
|
|
|
c = par_->transformChar(c, pos);
|
|
|
|
} else if (language->lang() == "hebrew" &&
|
2008-03-21 21:47:37 +00:00
|
|
|
Encodings::isHebrewComposeChar(c)) {
|
2010-11-22 12:10:16 +00:00
|
|
|
return 0;
|
2008-02-09 10:41:49 +00:00
|
|
|
}
|
2007-08-18 21:17:10 +00:00
|
|
|
}
|
|
|
|
return theFontMetrics(font).width(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 14:46:13 +00:00
|
|
|
bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
|
|
|
|
{
|
|
|
|
if (!par_->isHfill(pos))
|
|
|
|
return false;
|
|
|
|
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
|
2007-08-31 14:46:13 +00:00
|
|
|
|
|
|
|
// expand at the end of a row only if there is another hfill on the same row
|
|
|
|
if (pos == row.endpos() - 1) {
|
|
|
|
for (pos_type i = row.pos(); i < pos; i++) {
|
|
|
|
if (par_->isHfill(i))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// expand at the beginning of a row only if it is the first row of a paragraph
|
2012-10-27 13:45:27 +00:00
|
|
|
if (pos == row.pos())
|
2007-08-31 14:46:13 +00:00
|
|
|
return pos == 0;
|
|
|
|
|
|
|
|
// do not expand in some labels
|
2008-03-06 21:31:27 +00:00
|
|
|
if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
|
2007-08-31 14:46:13 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// if there is anything between the first char of the row and
|
|
|
|
// the specified position that is neither a newline nor an hfill,
|
|
|
|
// the hfill will be expanded, otherwise it won't
|
|
|
|
for (pos_type i = row.pos(); i < pos; i++) {
|
|
|
|
if (!par_->isNewline(i) && !par_->isHfill(i))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-12-29 23:54:48 +00:00
|
|
|
} // namespace lyx
|