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.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
13 * \author Abdelrazak Younes
15 * Full author contact details are available in file CREDITS.
20 #include "TextMetrics.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "pariterator.h"
26 #include "coordcache.h"
28 #include "funcrequest.h"
29 #include "FontIterator.h"
31 #include "lyxlength.h"
33 #include "metricsinfo.h"
34 #include "ParagraphParameters.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
46 using frontend::FontMetrics;
50 int numberOfSeparators(Paragraph const & par, Row const & row)
52 pos_type const first = max(row.pos(), par.beginOfBody());
53 pos_type const last = row.endpos() - 1;
55 for (pos_type p = first; p < last; ++p) {
56 if (par.isSeparator(p))
63 int numberOfLabelHfills(Paragraph const & par, Row const & row)
65 pos_type last = row.endpos() - 1;
66 pos_type first = row.pos();
68 // hfill *DO* count at the beginning of paragraphs!
70 while (first < last && par.isHfill(first))
74 last = min(last, par.beginOfBody());
76 for (pos_type p = first; p < last; ++p) {
84 int numberOfHfills(Paragraph const & par, Row const & row)
86 pos_type last = row.endpos();
87 pos_type first = row.pos();
88 pos_type const par_size = par.size();
90 // hfill *DO* count at the beginning of paragraphs!
92 while (first < last && first < par_size && par.isHfill(first))
96 first = max(first, par.beginOfBody());
97 last = min(last, par_size);
100 for (pos_type p = first; p < last; ++p) {
109 TextMetrics::TextMetrics(BufferView * bv, LyXText * text)
110 : bv_(bv), text_(text)
113 max_width_ = bv_->workWidth();
114 dim_.wid = max_width_;
118 //text_->updateLabels(*bv->buffer());
122 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
124 return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
128 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
131 ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
132 if (pmc_it == par_metrics_.end()) {
133 pmc_it = par_metrics_.insert(
134 std::make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
136 if (pmc_it->second.rows().empty() && redo) {
139 return pmc_it->second;
143 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim)
145 BOOST_ASSERT(mi.base.textwidth);
146 max_width_ = mi.base.textwidth;
148 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
149 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
151 bool changed = false;
155 for (pit_type pit = 0, n = text_->paragraphs().size(); pit != n; ++pit) {
156 changed |= redoParagraph(pit);
157 ParagraphMetrics const & pm = parMetrics(pit);
164 dim.asc = parMetrics(0).ascent();
165 dim.des = h - dim.asc;
167 changed |= dim_ != dim;
173 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
175 return main_text_? pm.rightMargin(*bv_->buffer()) : 0;
179 int TextMetrics::rightMargin(pit_type const pit) const
181 return main_text_? par_metrics_[pit].rightMargin(*bv_->buffer()) : 0;
185 bool TextMetrics::redoParagraph(pit_type const pit)
187 Paragraph & par = text_->getPar(pit);
188 ParagraphMetrics pm(par);
189 Buffer & buffer = *bv_->buffer();
190 main_text_ = (text_ == &buffer.text());
191 bool changed = false;
193 // FIXME: this has nothing to do here and is the reason why text_ is not
195 if (par.checkBiblio(buffer.params().trackChanges))
196 const_cast<LCursor &>(bv_->cursor()).posRight();
198 // Optimisation: this is used in the next two loops
199 // so better to calculate that once here.
200 int const right_margin = rightMargin(pm);
203 // FIXME: We should always use getFont(), see documentation of
204 // noFontChange() in insetbase.h.
205 LyXFont const bufferfont = buffer.params().getFont();
206 InsetList::const_iterator ii = par.insetlist.begin();
207 InsetList::const_iterator iend = par.insetlist.end();
208 for (; ii != iend; ++ii) {
210 int const w = max_width_ - text_->leftMargin(buffer, max_width_, pit, ii->pos)
212 LyXFont const & font = ii->inset->noFontChange() ?
213 bufferfont : text_->getFont(buffer, par, ii->pos);
214 MetricsInfo mi(bv_, font, w);
215 changed |= ii->inset->metrics(mi, dim);
218 // rebreak the paragraph
221 par.setBeginOfBody();
225 text_->rowBreakPoint(buffer, right_margin, max_width_, pit, row);
226 text_->setRowWidth(buffer, right_margin, max_width_, pit, row);
227 text_->setHeightOfRow(*bv_, pit, row);
228 pm.rows().push_back(row);
229 pm.dim().wid = std::max(pm.dim().wid, row.width());
230 pm.dim().des += row.height();
232 } while (z < par.size());
234 // Make sure that if a par ends in newline, there is one more row
236 // FIXME this is a dirty trick. Now the _same_ position in the
237 // paragraph occurs in _two_ different rows, and has two different
238 // display positions, leading to weird behaviour when moving up/down.
239 if (z > 0 && par.isNewline(z - 1)) {
242 text_->setRowWidth(buffer, right_margin, max_width_, pit, row);
243 text_->setHeightOfRow(*bv_, pit, row);
244 pm.rows().push_back(row);
245 pm.dim().des += row.height();
248 pm.dim().asc += pm.rows()[0].ascent();
249 pm.dim().des -= pm.rows()[0].ascent();
251 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
252 // redoParagraph() recursively inside parMetrics.
253 Dimension old_dim = parMetrics(pit, false).dim();
255 changed |= old_dim.height() != pm.dim().height();
257 par_metrics_[pit] = pm;
262 RowMetrics TextMetrics::computeRowMetrics(pit_type const pit,
263 Row const & row) const
266 Buffer & buffer = *bv_->buffer();
267 Paragraph const & par = text_->getPar(pit);
269 double w = dim_.wid - row.width();
271 bool const is_rtl = text_->isRTL(buffer, par);
273 result.x = rightMargin(pit);
275 result.x = text_->leftMargin(buffer, max_width_, pit, row.pos());
277 // is there a manual margin with a manual label
278 LyXLayout_ptr const & layout = par.layout();
280 if (layout->margintype == MARGIN_MANUAL
281 && layout->labeltype == LABEL_MANUAL) {
282 /// We might have real hfills in the label part
283 int nlh = numberOfLabelHfills(par, row);
285 // A manual label par (e.g. List) has an auto-hfill
286 // between the label text and the body of the
288 // But we don't want to do this auto hfill if the par
293 if (nlh && !par.getLabelWidthString().empty())
294 result.label_hfill = labelFill(par, row) / double(nlh);
297 // are there any hfills in the row?
298 int const nh = numberOfHfills(par, row);
302 result.hfill = w / nh;
303 // we don't have to look at the alignment if it is ALIGN_LEFT and
304 // if the row is already larger then the permitted width as then
305 // we force the LEFT_ALIGN'edness!
306 } else if (int(row.width()) < max_width_) {
307 // is it block, flushleft or flushright?
308 // set x how you need it
310 if (par.params().align() == LYX_ALIGN_LAYOUT)
311 align = layout->align;
313 align = par.params().align();
315 // Display-style insets should always be on a centred row
316 // The test on par.size() is to catch zero-size pars, which
317 // would trigger the assert in Paragraph::getInset().
318 //inset = par.size() ? par.getInset(row.pos()) : 0;
320 && par.isInset(row.pos())
321 && par.getInset(row.pos())->display())
323 align = LYX_ALIGN_CENTER;
327 case LYX_ALIGN_BLOCK: {
328 int const ns = numberOfSeparators(par, row);
329 bool disp_inset = false;
330 if (row.endpos() < par.size()) {
331 InsetBase const * in = par.getInset(row.endpos());
333 disp_inset = in->display();
335 // If we have separators, this is not the last row of a
336 // par, does not end in newline, and is not row above a
337 // display inset... then stretch it
339 && row.endpos() < par.size()
340 && !par.isNewline(row.endpos() - 1)
343 result.separator = w / ns;
349 case LYX_ALIGN_RIGHT:
352 case LYX_ALIGN_CENTER:
358 text_->bidi.computeTables(par, buffer, row);
360 pos_type body_pos = par.beginOfBody();
361 pos_type end = row.endpos();
364 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
366 docstring const lsep = from_utf8(layout->labelsep);
367 result.x += theFontMetrics(text_->getLabelFont(buffer, par)).width(lsep);
369 result.x += result.label_hfill;
377 int TextMetrics::labelFill(Paragraph const & par, Row const & row) const
379 Buffer & buffer = *bv_->buffer();
381 pos_type last = par.beginOfBody();
382 BOOST_ASSERT(last > 0);
384 // -1 because a label ends with a space that is in the label
387 // a separator at this end does not count
388 if (par.isLineSeparator(last))
392 for (pos_type i = row.pos(); i <= last; ++i)
393 w += text_->singleWidth(buffer, par, i);
395 docstring const & label = par.params().labelWidthString();
399 FontMetrics const & fm
400 = theFontMetrics(text_->getLabelFont(buffer, par));
402 return max(0, fm.width(label) - w);
406 int defaultRowHeight()
408 return int(theFontMetrics(LyXFont(LyXFont::ALL_SANE)).maxHeight() * 1.2);