]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.C
This commit do the Model/View separation of the LyXText and Paragraph classes. The...
[lyx.git] / src / TextMetrics.C
1 /**
2  * \file src/TextMetrics.C
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 John Levon
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Jürgen Vigna
13  * \author Abdelrazak Younes
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "TextMetrics.h"
21
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "pariterator.h"
26 #include "coordcache.h"
27 #include "debug.h"
28 #include "funcrequest.h"
29 #include "FontIterator.h"
30 #include "LColor.h"
31 #include "lyxlength.h"
32 #include "lyxtext.h"
33 #include "metricsinfo.h"
34 #include "ParagraphParameters.h"
35 #include "vspace.h"
36
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
39
40 using std::max;
41 using std::min;
42 using std::endl;
43
44 namespace lyx {
45
46 using frontend::FontMetrics;
47
48 namespace {
49
50 int numberOfSeparators(Paragraph const & par, Row const & row)
51 {
52         pos_type const first = max(row.pos(), par.beginOfBody());
53         pos_type const last = row.endpos() - 1;
54         int n = 0;
55         for (pos_type p = first; p < last; ++p) {
56                 if (par.isSeparator(p))
57                         ++n;
58         }
59         return n;
60 }
61
62
63 int numberOfLabelHfills(Paragraph const & par, Row const & row)
64 {
65         pos_type last = row.endpos() - 1;
66         pos_type first = row.pos();
67
68         // hfill *DO* count at the beginning of paragraphs!
69         if (first) {
70                 while (first < last && par.isHfill(first))
71                         ++first;
72         }
73
74         last = min(last, par.beginOfBody());
75         int n = 0;
76         for (pos_type p = first; p < last; ++p) {
77                 if (par.isHfill(p))
78                         ++n;
79         }
80         return n;
81 }
82
83
84 int numberOfHfills(Paragraph const & par, Row const & row)
85 {
86         pos_type const last = row.endpos();
87         pos_type first = row.pos();
88
89         // hfill *DO* count at the beginning of paragraphs!
90         if (first) {
91                 while (first < last && par.isHfill(first))
92                         ++first;
93         }
94
95         first = max(first, par.beginOfBody());
96
97         int n = 0;
98         for (pos_type p = first; p < last; ++p) {
99                 if (par.isHfill(p))
100                         ++n;
101         }
102         return n;
103 }
104
105 } // namespace anon
106
107 TextMetrics::TextMetrics(BufferView * bv, LyXText * text)
108         : bv_(bv), text_(text)
109 {
110         BOOST_ASSERT(bv_);
111         max_width_ = bv_->workWidth();
112         dim_.wid = max_width_;
113         dim_.asc = 10;
114         dim_.des = 10;
115
116         //text_->updateLabels(*bv->buffer());
117 }
118
119
120 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
121 {
122         return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
123 }
124
125
126 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
127                 bool redo)
128 {
129         ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
130         if (pmc_it == par_metrics_.end()) {
131                 pmc_it = par_metrics_.insert(
132                         std::make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
133         }
134         if (pmc_it->second.rows().empty() && redo) {
135                 redoParagraph(pit);
136         }
137         return pmc_it->second;
138 }
139
140
141 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim)
142 {
143         BOOST_ASSERT(mi.base.textwidth);
144         max_width_ = mi.base.textwidth;
145
146         //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
147         //      << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
148         
149         bool changed = false;
150
151         unsigned int h = 0;
152         unsigned int w = 0;
153         for (pit_type pit = 0, n = text_->paragraphs().size(); pit != n; ++pit) {
154                 changed |= redoParagraph(pit);
155                 ParagraphMetrics const & pm = parMetrics(pit);
156                 h += pm.height();
157                 if (w < pm.width())
158                         w = pm.width();
159         }
160
161         dim.wid = w;
162         dim.asc = parMetrics(0).ascent();
163         dim.des = h - dim.asc;
164
165         changed |= dim_ != dim;
166         dim_ = dim;
167         return changed;
168 }
169
170
171 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
172 {
173         return main_text_? pm.rightMargin(*bv_->buffer()) : 0;
174 }
175
176
177 int TextMetrics::rightMargin(pit_type const pit) const
178 {
179         return main_text_? par_metrics_[pit].rightMargin(*bv_->buffer()) : 0;
180 }
181
182
183 bool TextMetrics::redoParagraph(pit_type const pit)
184 {
185         Paragraph & par = text_->getPar(pit);
186         ParagraphMetrics pm(par);
187         Buffer & buffer = *bv_->buffer();
188         main_text_ = (text_ == &buffer.text());
189         bool changed = false;
190
191         // FIXME: this has nothing to do here and is the reason why text_ is not
192         // const.
193         if (par.checkBiblio(buffer.params().trackChanges))
194                 const_cast<LCursor &>(bv_->cursor()).posRight();
195
196         // Optimisation: this is used in the next two loops
197         // so better to calculate that once here.
198         int const right_margin = rightMargin(pm);
199
200         // redo insets
201         // FIXME: We should always use getFont(), see documentation of
202         // noFontChange() in insetbase.h.
203         LyXFont const bufferfont = buffer.params().getFont();
204         InsetList::const_iterator ii = par.insetlist.begin();
205         InsetList::const_iterator iend = par.insetlist.end();
206         for (; ii != iend; ++ii) {
207                 Dimension dim;
208                 int const w = max_width_ - text_->leftMargin(buffer, max_width_, pit, ii->pos)
209                         - right_margin;
210                 LyXFont const & font = ii->inset->noFontChange() ?
211                         bufferfont : text_->getFont(buffer, par, ii->pos);
212                 MetricsInfo mi(bv_, font, w);
213                 changed |= ii->inset->metrics(mi, dim);
214         }
215
216         // rebreak the paragraph
217         pm.rows().clear();
218
219         par.setBeginOfBody();
220         pos_type z = 0;
221         do {
222                 Row row(z);
223                 text_->rowBreakPoint(buffer, right_margin, max_width_, pit, row);
224                 text_->setRowWidth(buffer, right_margin, max_width_, pit, row);
225                 text_->setHeightOfRow(*bv_, pit, row);
226                 pm.rows().push_back(row);
227                 pm.dim().wid = std::max(pm.dim().wid, row.width());
228                 pm.dim().des += row.height();
229                 z = row.endpos();
230         } while (z < par.size());
231
232         // Make sure that if a par ends in newline, there is one more row
233         // under it
234         // FIXME this is a dirty trick. Now the _same_ position in the
235         // paragraph occurs in _two_ different rows, and has two different
236         // display positions, leading to weird behaviour when moving up/down.
237         if (z > 0 && par.isNewline(z - 1)) {
238                 Row row(z - 1);
239                 row.endpos(z - 1);
240                 text_->setRowWidth(buffer, right_margin, max_width_, pit, row);
241                 text_->setHeightOfRow(*bv_, pit, row);
242                 pm.rows().push_back(row);
243                 pm.dim().des += row.height();
244         }
245
246         pm.dim().asc += pm.rows()[0].ascent();
247         pm.dim().des -= pm.rows()[0].ascent();
248
249         // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
250         // redoParagraph() recursively inside parMetrics.
251         Dimension old_dim = parMetrics(pit, false).dim();
252
253         changed |= old_dim.height() != pm.dim().height();
254
255         par_metrics_[pit] = pm;
256
257         return changed;
258 }
259
260 RowMetrics TextMetrics::computeRowMetrics(pit_type const pit,
261                 Row const & row) const
262 {
263         RowMetrics result;
264         Buffer & buffer = *bv_->buffer();
265         Paragraph const & par = text_->getPar(pit);
266
267         double w = dim_.wid - row.width();
268
269         bool const is_rtl = text_->isRTL(buffer, par);
270         if (is_rtl)
271                 result.x = rightMargin(pit);
272         else
273                 result.x = text_->leftMargin(buffer, max_width_, pit, row.pos());
274
275         // is there a manual margin with a manual label
276         LyXLayout_ptr const & layout = par.layout();
277
278         if (layout->margintype == MARGIN_MANUAL
279             && layout->labeltype == LABEL_MANUAL) {
280                 /// We might have real hfills in the label part
281                 int nlh = numberOfLabelHfills(par, row);
282
283                 // A manual label par (e.g. List) has an auto-hfill
284                 // between the label text and the body of the
285                 // paragraph too.
286                 // But we don't want to do this auto hfill if the par
287                 // is empty.
288                 if (!par.empty())
289                         ++nlh;
290
291                 if (nlh && !par.getLabelWidthString().empty())
292                         result.label_hfill = labelFill(par, row) / double(nlh);
293         }
294
295         // are there any hfills in the row?
296         int const nh = numberOfHfills(par, row);
297
298         if (nh) {
299                 if (w > 0)
300                         result.hfill = w / nh;
301         // we don't have to look at the alignment if it is ALIGN_LEFT and
302         // if the row is already larger then the permitted width as then
303         // we force the LEFT_ALIGN'edness!
304         } else if (int(row.width()) < max_width_) {
305                 // is it block, flushleft or flushright?
306                 // set x how you need it
307                 int align;
308                 if (par.params().align() == LYX_ALIGN_LAYOUT)
309                         align = layout->align;
310                 else
311                         align = par.params().align();
312
313                 // Display-style insets should always be on a centred row
314                 // The test on par.size() is to catch zero-size pars, which
315                 // would trigger the assert in Paragraph::getInset().
316                 //inset = par.size() ? par.getInset(row.pos()) : 0;
317                 if (!par.empty()
318                     && par.isInset(row.pos())
319                     && par.getInset(row.pos())->display())
320                 {
321                         align = LYX_ALIGN_CENTER;
322                 }
323
324                 switch (align) {
325                 case LYX_ALIGN_BLOCK: {
326                         int const ns = numberOfSeparators(par, row);
327                         bool disp_inset = false;
328                         if (row.endpos() < par.size()) {
329                                 InsetBase const * in = par.getInset(row.endpos());
330                                 if (in)
331                                         disp_inset = in->display();
332                         }
333                         // If we have separators, this is not the last row of a
334                         // par, does not end in newline, and is not row above a
335                         // display inset... then stretch it
336                         if (ns
337                             && row.endpos() < par.size()
338                             && !par.isNewline(row.endpos() - 1)
339                             && !disp_inset
340                                 ) {
341                                 result.separator = w / ns;
342                         } else if (is_rtl) {
343                                 result.x += w;
344                         }
345                         break;
346                 }
347                 case LYX_ALIGN_RIGHT:
348                         result.x += w;
349                         break;
350                 case LYX_ALIGN_CENTER:
351                         result.x += w / 2;
352                         break;
353                 }
354         }
355
356         text_->bidi.computeTables(par, buffer, row);
357         if (is_rtl) {
358                 pos_type body_pos = par.beginOfBody();
359                 pos_type end = row.endpos();
360
361                 if (body_pos > 0
362                     && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
363                 {
364                         docstring const lsep = from_utf8(layout->labelsep);
365                         result.x += theFontMetrics(text_->getLabelFont(buffer, par)).width(lsep);
366                         if (body_pos <= end)
367                                 result.x += result.label_hfill;
368                 }
369         }
370
371         return result;
372 }
373
374
375 int TextMetrics::labelFill(Paragraph const & par, Row const & row) const
376 {
377         Buffer & buffer = *bv_->buffer();
378
379         pos_type last = par.beginOfBody();
380         BOOST_ASSERT(last > 0);
381
382         // -1 because a label ends with a space that is in the label
383         --last;
384
385         // a separator at this end does not count
386         if (par.isLineSeparator(last))
387                 --last;
388
389         int w = 0;
390         for (pos_type i = row.pos(); i <= last; ++i)
391                 w += text_->singleWidth(buffer, par, i);
392
393         docstring const & label = par.params().labelWidthString();
394         if (label.empty())
395                 return 0;
396
397         FontMetrics const & fm 
398                 = theFontMetrics(text_->getLabelFont(buffer, par));
399
400         return max(0, fm.width(label) - w);
401 }
402
403
404 int defaultRowHeight()
405 {
406         return int(theFontMetrics(LyXFont(LyXFont::ALL_SANE)).maxHeight() *  1.2);
407 }
408
409 } // namespace lyx