1 /**
2  * \file Paragraph.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17 
18 #include <config.h>
19 
20 #include "ParagraphMetrics.h"
21 
22 #include "Buffer.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "Counters.h"
26 #include "Encoding.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Layout.h"
30 #include "Font.h"
31 #include "LyXRC.h"
32 #include "Row.h"
33 #include "OutputParams.h"
34 #include "sgml.h"
35 #include "TextClass.h"
36 #include "TexRow.h"
37 
38 #include "frontends/FontMetrics.h"
39 
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetArgument.h"
42 
43 #include "support/lassert.h"
44 #include "support/debug.h"
45 #include "support/ExceptionMessage.h"
46 #include "support/gettext.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
49 
50 #include <algorithm>
51 #include <list>
52 #include <stack>
53 #include <sstream>
54 
55 using namespace std;
56 using namespace lyx::support;
57 
58 namespace lyx {
59 
60 
ParagraphMetrics(Paragraph const & par)61 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
62 	position_(-1), par_(&par)
63 {}
64 
65 
operator =(ParagraphMetrics const & pm)66 ParagraphMetrics & ParagraphMetrics::operator=(
67 	ParagraphMetrics const & pm)
68 {
69 	rows_ = pm.rows_;
70 	dim_ = pm.dim_;
71 	par_ = pm.par_;
72 	position_ = pm.position_;
73 	return *this;
74 }
75 
76 
reset(Paragraph const & par)77 void ParagraphMetrics::reset(Paragraph const & par)
78 {
79 	par_ = &par;
80 	dim_ = Dimension();
81 	//position_ = -1;
82 }
83 
84 
setPosition(int position)85 void ParagraphMetrics::setPosition(int position)
86 {
87 	position_ = position;
88 }
89 
90 
getRow(pos_type pos,bool boundary)91 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
92 {
93 	LBUFERR(!rows().empty());
94 
95 	// If boundary is set we should return the row on which
96 	// the character before is inside.
97 	if (pos > 0 && boundary)
98 		--pos;
99 
100 	RowList::iterator rit = rows_.end();
101 	RowList::iterator const begin = rows_.begin();
102 
103 	for (--rit; rit != begin && rit->pos() > pos; --rit)
104 		;
105 
106 	return *rit;
107 }
108 
109 
getRow(pos_type pos,bool boundary) const110 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
111 {
112 	LBUFERR(!rows().empty());
113 
114 	// If boundary is set we should return the row on which
115 	// the character before is inside.
116 	if (pos > 0 && boundary)
117 		--pos;
118 
119 	RowList::const_iterator rit = rows_.end();
120 	RowList::const_iterator const begin = rows_.begin();
121 
122 	for (--rit; rit != begin && rit->pos() > pos; --rit)
123 		;
124 
125 	return *rit;
126 }
127 
128 
pos2row(pos_type pos) const129 size_t ParagraphMetrics::pos2row(pos_type pos) const
130 {
131 	LBUFERR(!rows().empty());
132 
133 	RowList::const_iterator rit = rows_.end();
134 	RowList::const_iterator const begin = rows_.begin();
135 
136 	for (--rit; rit != begin && rit->pos() > pos; --rit)
137 		;
138 
139 	return rit - begin;
140 }
141 
142 
dump() const143 void ParagraphMetrics::dump() const
144 {
145 	lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
146 	for (size_t i = 0; i != rows_.size(); ++i) {
147 		lyxerr << "  row " << i << ":   " << rows_[i];
148 	}
149 }
150 
rightMargin(BufferView const & bv) const151 int ParagraphMetrics::rightMargin(BufferView const & bv) const
152 {
153 	BufferParams const & params = bv.buffer().params();
154 	DocumentClass const & tclass = params.documentClass();
155 	frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
156 	int const r_margin =
157 		bv.rightMargin()
158 		+ fm.signedWidth(tclass.rightmargin())
159 		+ fm.signedWidth(par_->layout().rightmargin)
160 		* 4 / (par_->getDepth() + 4);
161 
162 	return r_margin;
163 }
164 
165 
166 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
hfillExpansion(Row const & row,pos_type pos) const167 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
168 {
169 	if (!par_->isHfill(pos))
170 		return false;
171 
172 	LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
173 
174 	// expand at the end of a row only if there is another hfill on the same row
175 	if (pos == row.endpos() - 1) {
176 		for (pos_type i = row.pos(); i < pos; i++) {
177 			if (par_->isHfill(i))
178 				return true;
179 		}
180 		return false;
181 	}
182 
183 	// expand at the beginning of a row only if it is the first row of a paragraph
184 	if (pos == row.pos())
185 		return pos == 0;
186 
187 	// do not expand in some labels
188 	if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
189 		return false;
190 
191 	// if there is anything between the first char of the row and
192 	// the specified position that is neither a newline nor an hfill,
193 	// the hfill will be expanded, otherwise it won't
194 	for (pos_type i = row.pos(); i < pos; i++) {
195 		if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
196 			return true;
197 	}
198 	return false;
199 }
200 
201 } // namespace lyx
202