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
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
* \author Angus Leeming
|
|
|
|
|
* \author John Levon
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
* \author Dekel Tsur
|
|
|
|
|
* \author J<EFBFBD>rgen Vigna
|
|
|
|
|
*
|
|
|
|
|
* 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 "paragraph_funcs.h"
|
|
|
|
|
#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"
|
|
|
|
|
#include "VSpace.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"
|
|
|
|
|
#include "insets/InsetOptArg.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
|
#include "support/convert.h"
|
|
|
|
|
#include "support/debug.h"
|
|
|
|
|
#include "support/gettext.h"
|
2006-12-29 23:54:48 +00:00
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/textutils.h"
|
|
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
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));
|
2007-08-28 22:44:09 +00:00
|
|
|
|
if (bparams.trackChanges) {
|
|
|
|
|
Change change = par_->lookupChange(i);
|
|
|
|
|
char_type const b[] = { 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();
|
|
|
|
|
char_type const b[] = { row.sel_beg, row.sel_end, d.wid, d.asc, d.des};
|
2007-11-18 21:47:46 +00:00
|
|
|
|
// Each of the variable to process is 4 bytes: 4x5 = 20
|
|
|
|
|
crc.process_bytes(b, 20);
|
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)
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!rows().empty());
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!rows().empty());
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!rows().empty());
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
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)) {
|
2008-02-09 10:41:49 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
|
|
BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
if (pos == row.pos()) {
|
|
|
|
|
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
|