]> git.lyx.org Git - lyx.git/blob - src/ParagraphMetrics.cpp
Transfer current_font and real_current_font from Text to Cursor.
[lyx.git] / src / ParagraphMetrics.cpp
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 "debug.h"
28 #include "gettext.h"
29 #include "Language.h"
30 #include "LaTeXFeatures.h"
31 #include "Font.h"
32 #include "LyXRC.h"
33 #include "Row.h"
34 #include "OutputParams.h"
35 #include "paragraph_funcs.h"
36 #include "sgml.h"
37 #include "TexRow.h"
38 #include "VSpace.h"
39
40 #include "frontends/FontMetrics.h"
41
42 #include "insets/InsetBibitem.h"
43 #include "insets/InsetOptArg.h"
44
45 #include "support/lstrings.h"
46 #include "support/textutils.h"
47 #include "support/convert.h"
48 #include "support/unicode.h"
49
50 #include <boost/bind.hpp>
51 #include <boost/crc.hpp>
52
53 #include <algorithm>
54 #include <list>
55 #include <stack>
56 #include <sstream>
57
58
59 namespace lyx {
60
61 using lyx::support::contains;
62 using lyx::support::rsplit;
63 using support::subst;
64
65 using std::distance;
66 using std::endl;
67 using std::list;
68 using std::stack;
69 using std::string;
70 using std::ostream;
71 using std::ostringstream;
72
73
74 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): par_(&par)
75 {
76 }
77
78
79 ParagraphMetrics & ParagraphMetrics::operator=(
80         ParagraphMetrics const & pm)
81 {
82         rows_ = pm.rows_;
83         dim_ = pm.dim_;
84         par_ = pm.par_;
85         return *this;
86 }
87
88
89 void ParagraphMetrics::reset(Paragraph const & par)
90 {
91         par_ = &par;
92         dim_ = Dimension();
93 }
94
95
96 void ParagraphMetrics::computeRowSignature(Row & row,
97                 BufferParams const & bparams)
98 {
99         boost::crc_32_type crc;
100         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
101                 char_type const b[] = { par_->getChar(i) };
102                 crc.process_bytes(b, 1);
103                 if (bparams.trackChanges) {
104                         Change change = par_->lookupChange(i);
105                         char_type const b[] = { change.type };
106                         crc.process_bytes(b, 1);
107                 }                       
108         }
109         row.setCrc(crc.checksum());
110 }
111
112
113 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
114 {
115         BOOST_ASSERT(!rows().empty());
116
117         // If boundary is set we should return the row on which
118         // the character before is inside.
119         if (pos > 0 && boundary)
120                 --pos;
121
122         RowList::iterator rit = rows_.end();
123         RowList::iterator const begin = rows_.begin();
124
125         for (--rit; rit != begin && rit->pos() > pos; --rit)
126                 ;
127
128         return *rit;
129 }
130
131
132 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
133 {
134         BOOST_ASSERT(!rows().empty());
135
136         // If boundary is set we should return the row on which
137         // the character before is inside.
138         if (pos > 0 && boundary)
139                 --pos;
140
141         RowList::const_iterator rit = rows_.end();
142         RowList::const_iterator const begin = rows_.begin();
143
144         for (--rit; rit != begin && rit->pos() > pos; --rit)
145                 ;
146
147         return *rit;
148 }
149
150
151 size_t ParagraphMetrics::pos2row(pos_type pos) const
152 {
153         BOOST_ASSERT(!rows().empty());
154
155         RowList::const_iterator rit = rows_.end();
156         RowList::const_iterator const begin = rows_.begin();
157
158         for (--rit; rit != begin && rit->pos() > pos; --rit)
159                 ;
160
161         return rit - begin;
162 }
163
164
165 void ParagraphMetrics::dump() const
166 {
167         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
168         for (size_t i = 0; i != rows_.size(); ++i) {
169                 lyxerr << "  row " << i << ":   ";
170                 rows_[i].dump();
171         }
172 }
173
174 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
175 {
176         BufferParams const & params = buffer.params();
177         TextClass const & tclass = params.getTextClass();
178         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
179         int const r_margin =
180                 lyx::rightMargin()
181                 + fm.signedWidth(tclass.rightmargin())
182                 + fm.signedWidth(par_->layout()->rightmargin)
183                 * 4 / (par_->getDepth() + 4);
184
185         return r_margin;
186 }
187
188
189 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
190 {
191         char_type c = par_->getChar(pos);
192
193         // The most special cases are handled first.
194         if (c == Paragraph::META_INSET)
195                 return par_->getInset(pos)->width();
196
197         if (!isPrintable(c))
198                 return theFontMetrics(font).width(c);
199
200         Language const * language = font.language();
201         if (language->rightToLeft()) {
202                 if (language->lang() == "arabic_arabtex" ||
203                         language->lang() == "arabic_arabi" ||
204                         language->lang() == "farsi") {
205                                 if (Encodings::isComposeChar_arabic(c))
206                                         return 0;
207                                 c = par_->transformChar(c, pos);
208                 } else if (language->lang() == "hebrew" &&
209                         Encodings::isComposeChar_hebrew(c))
210                         return 0;
211         }
212         return theFontMetrics(font).width(c);
213 }
214
215
216 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
217 {
218         if (!par_->isHfill(pos))
219                 return false;
220
221         BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
222
223         // expand at the end of a row only if there is another hfill on the same row
224         if (pos == row.endpos() - 1) {
225                 for (pos_type i = row.pos(); i < pos; i++) {
226                         if (par_->isHfill(i))
227                                 return true;
228                 }
229                 return false;
230         }
231
232         // expand at the beginning of a row only if it is the first row of a paragraph
233         if (pos == row.pos()) {
234                 return pos == 0;
235         }
236
237         // do not expand in some labels
238         if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
239                 return false;
240
241         // if there is anything between the first char of the row and
242         // the specified position that is neither a newline nor an hfill,
243         // the hfill will be expanded, otherwise it won't
244         for (pos_type i = row.pos(); i < pos; i++) {
245                 if (!par_->isNewline(i) && !par_->isHfill(i))
246                         return true;
247         }
248         return false;
249 }
250
251 } // namespace lyx