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 "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-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
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();
|
Patch by Vincent that solves a number of problems related to the painting of a selection:
1. When a listing is inserted in a bit of text, the line above the listing is not drawn over the full width like it is done for lines above other insets. This is because InsetListing has a AlignLeft alignment. Now, if you start selecting downwards with the mouse in this empty area, strange selection drawings appear (see attachment).
This is caused by the fact that starting your selection at such a place, causes beg.boundary() to be true in TextMetrics::drawRowSelection(..). This is correct, but this value is true for _all_ selected lines. Now, the selection acts as if it is RTL text. Therefore, just like for end.boundary, this value needs to be reset for every line.
2. Starting your selection in an end margin often causes the selection in this end margin to be painted later. This is because when starting your selection in an end margin, you may have set a (possible empty) selection before really selecting the end margin. The problem is that the checksum (computed later) is the same for this empty selection and for the end margin selection. Therfore, we need a call to cur.setSelection() before evaluating cur.selection().
3. In the following two lines, it is assumed that there is only an end margin to be painted if the selection extends to the next paragraph. This is not true for the above described case of an AlignLeft Inset. Then, the margin has also be drawn within a paragraph
4. The end and begin margins are only painted when the selection extends into the following or previous paragraph. This difference is not resembled in the checksum if you first select a row completely and then procede to the next or previous paragraph as the selection remains at the end of a row. This also holds for the AlignLeft case. Therefore I added a term to the checksum to monitor whether the end and begin margins need to be drawn.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26399 a592a061-630c-0410-9148-cb99ea01b6c8
2008-09-14 14:32:40 +00:00
|
|
|
char_type const b[] = { row.sel_beg, 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};
|
Patch by Vincent that solves a number of problems related to the painting of a selection:
1. When a listing is inserted in a bit of text, the line above the listing is not drawn over the full width like it is done for lines above other insets. This is because InsetListing has a AlignLeft alignment. Now, if you start selecting downwards with the mouse in this empty area, strange selection drawings appear (see attachment).
This is caused by the fact that starting your selection at such a place, causes beg.boundary() to be true in TextMetrics::drawRowSelection(..). This is correct, but this value is true for _all_ selected lines. Now, the selection acts as if it is RTL text. Therefore, just like for end.boundary, this value needs to be reset for every line.
2. Starting your selection in an end margin often causes the selection in this end margin to be painted later. This is because when starting your selection in an end margin, you may have set a (possible empty) selection before really selecting the end margin. The problem is that the checksum (computed later) is the same for this empty selection and for the end margin selection. Therfore, we need a call to cur.setSelection() before evaluating cur.selection().
3. In the following two lines, it is assumed that there is only an end margin to be painted if the selection extends to the next paragraph. This is not true for the above described case of an AlignLeft Inset. Then, the margin has also be drawn within a paragraph
4. The end and begin margins are only painted when the selection extends into the following or previous paragraph. This difference is not resembled in the checksum if you first select a row completely and then procede to the next or previous paragraph as the selection remains at the end of a row. This also holds for the AlignLeft case. Therefore I added a term to the checksum to monitor whether the end and begin margins need to be drawn.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26399 a592a061-630c-0410-9148-cb99ea01b6c8
2008-09-14 14:32:40 +00:00
|
|
|
// Each of the variable to process is 4 bytes: 4x7 = 28
|
|
|
|
crc.process_bytes(b, 28);
|
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)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!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
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!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
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!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)) {
|
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;
|
|
|
|
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(pos >= row.pos() && pos < row.endpos(), /**/);
|
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
|
|
|
|
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
|