]> git.lyx.org Git - features.git/blob - src/ParagraphMetrics.cpp
Remove non-const version of ParagraphMetrics::getRow
[features.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 "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 "xml.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
61 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
62         position_(-1), par_(&par)
63 {}
64
65
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
77 void ParagraphMetrics::reset(Paragraph const & par)
78 {
79         par_ = &par;
80         dim_ = Dimension();
81         //position_ = -1;
82 }
83
84
85 void ParagraphMetrics::setPosition(int position)
86 {
87         position_ = position;
88 }
89
90
91 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
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::const_iterator rit = rows_.end();
101         RowList::const_iterator const begin = rows_.begin();
102
103         for (--rit; rit != begin && rit->pos() > pos; --rit)
104                 ;
105
106         return *rit;
107 }
108
109
110 size_t ParagraphMetrics::pos2row(pos_type pos) const
111 {
112         LBUFERR(!rows().empty());
113
114         RowList::const_iterator rit = rows_.end();
115         RowList::const_iterator const begin = rows_.begin();
116
117         for (--rit; rit != begin && rit->pos() > pos; --rit)
118                 ;
119
120         return rit - begin;
121 }
122
123
124 void ParagraphMetrics::dump() const
125 {
126         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
127         for (size_t i = 0; i != rows_.size(); ++i) {
128                 lyxerr << "  row " << i << ":   " << rows_[i];
129         }
130 }
131
132 int ParagraphMetrics::rightMargin(BufferView const & bv) const
133 {
134         BufferParams const & params = bv.buffer().params();
135         DocumentClass const & tclass = params.documentClass();
136         frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
137         int const r_margin =
138                 bv.rightMargin()
139                 + fm.signedWidth(tclass.rightmargin())
140                 + fm.signedWidth(par_->layout().rightmargin)
141                 * 4 / (par_->getDepth() + 4);
142
143         return r_margin;
144 }
145
146
147 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
148 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
149 {
150         if (!par_->isHfill(pos))
151                 return false;
152
153         LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
154
155         // expand at the end of a row only if there is another hfill on the same row
156         if (pos == row.endpos() - 1) {
157                 for (pos_type i = row.pos(); i < pos; i++) {
158                         if (par_->isHfill(i))
159                                 return true;
160                 }
161                 return false;
162         }
163
164         // expand at the beginning of a row only if it is the first row of a paragraph
165         if (pos == row.pos())
166                 return pos == 0;
167
168         // do not expand in some labels
169         if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
170                 return false;
171
172         // if there is anything between the first char of the row and
173         // the specified position that is neither a newline nor an hfill,
174         // the hfill will be expanded, otherwise it won't
175         for (pos_type i = row.pos(); i < pos; i++) {
176                 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))
177                         return true;
178         }
179         return false;
180 }
181
182 } // namespace lyx