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"
|
2006-12-29 23:54:48 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "gettext.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/textutils.h"
|
|
|
|
|
#include "support/convert.h"
|
|
|
|
|
#include "support/unicode.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>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using lyx::support::contains;
|
|
|
|
|
using lyx::support::rsplit;
|
|
|
|
|
using support::subst;
|
|
|
|
|
|
|
|
|
|
using std::distance;
|
|
|
|
|
using std::endl;
|
|
|
|
|
using std::list;
|
|
|
|
|
using std::stack;
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::ostringstream;
|
|
|
|
|
|
|
|
|
|
|
2007-09-16 10:36:57 +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-08-29 21:03:41 +00:00
|
|
|
|
void ParagraphMetrics::computeRowSignature(Row & row,
|
2007-08-28 22:44:09 +00:00
|
|
|
|
BufferParams const & bparams)
|
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) };
|
|
|
|
|
crc.process_bytes(b, 1);
|
2007-08-28 22:44:09 +00:00
|
|
|
|
if (bparams.trackChanges) {
|
|
|
|
|
Change change = par_->lookupChange(i);
|
|
|
|
|
char_type const b[] = { change.type };
|
|
|
|
|
crc.process_bytes(b, 1);
|
|
|
|
|
}
|
2007-01-06 09:15:59 +00:00
|
|
|
|
}
|
2007-08-29 21:03:41 +00:00
|
|
|
|
row.setCrc(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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ParagraphMetrics::rightMargin(Buffer const & buffer) const
|
|
|
|
|
{
|
|
|
|
|
BufferParams const & params = buffer.params();
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClass const & tclass = params.getTextClass();
|
2006-12-29 23:54:48 +00:00
|
|
|
|
frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
|
|
|
|
|
int const r_margin =
|
|
|
|
|
lyx::rightMargin()
|
2007-08-18 13:21:12 +00:00
|
|
|
|
+ fm.signedWidth(tclass.rightmargin())
|
|
|
|
|
+ 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.
|
2007-10-24 07:10:26 +00:00
|
|
|
|
if (par_->isInset(pos))
|
2007-09-21 20:39:47 +00:00
|
|
|
|
return insetDimension(par_->getInset(pos)).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") {
|
|
|
|
|
if (Encodings::isComposeChar_arabic(c))
|
|
|
|
|
return 0;
|
|
|
|
|
c = par_->transformChar(c, pos);
|
|
|
|
|
} else if (language->lang() == "hebrew" &&
|
|
|
|
|
Encodings::isComposeChar_hebrew(c))
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
|
|
|
|
|
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
|