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 const last = row.endpos();
87 pos_type first = row.pos();
89 // hfill *DO* count at the beginning of paragraphs!
91 while (first < last && par.isHfill(first))
95 first = max(first, par.beginOfBody());
98 for (pos_type p = first; p < last; ++p) {
107 TextMetrics::TextMetrics(BufferView * bv, LyXText * text)
108 : bv_(bv), text_(text)
111 max_width_ = bv_->workWidth();
112 dim_.wid = max_width_;
116 //text_->updateLabels(*bv->buffer());
120 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
122 return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
126 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
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;
134 if (pmc_it->second.rows().empty() && redo) {
137 return pmc_it->second;
141 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim)
143 BOOST_ASSERT(mi.base.textwidth);
144 max_width_ = mi.base.textwidth;
146 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
147 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
149 bool changed = false;
153 for (pit_type pit = 0, n = text_->paragraphs().size(); pit != n; ++pit) {
154 changed |= redoParagraph(pit);
155 ParagraphMetrics const & pm = parMetrics(pit);
162 dim.asc = parMetrics(0).ascent();
163 dim.des = h - dim.asc;
165 changed |= dim_ != dim;
171 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
173 return main_text_? pm.rightMargin(*bv_->buffer()) : 0;
177 int TextMetrics::rightMargin(pit_type const pit) const
179 return main_text_? par_metrics_[pit].rightMargin(*bv_->buffer()) : 0;
183 bool TextMetrics::redoParagraph(pit_type const pit)
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;
191 // FIXME: this has nothing to do here and is the reason why text_ is not
193 if (par.checkBiblio(buffer.params().trackChanges))
194 const_cast<LCursor &>(bv_->cursor()).posRight();
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);
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) {
208 int const w = max_width_ - text_->leftMargin(buffer, max_width_, pit, ii->pos)
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);
216 // rebreak the paragraph
219 par.setBeginOfBody();
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();
230 } while (z < par.size());
232 // Make sure that if a par ends in newline, there is one more row
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)) {
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();
246 pm.dim().asc += pm.rows()[0].ascent();
247 pm.dim().des -= pm.rows()[0].ascent();
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();
253 changed |= old_dim.height() != pm.dim().height();
255 par_metrics_[pit] = pm;
260 RowMetrics TextMetrics::computeRowMetrics(pit_type const pit,
261 Row const & row) const
264 Buffer & buffer = *bv_->buffer();
265 Paragraph const & par = text_->getPar(pit);
267 double w = dim_.wid - row.width();
269 bool const is_rtl = text_->isRTL(buffer, par);
271 result.x = rightMargin(pit);
273 result.x = text_->leftMargin(buffer, max_width_, pit, row.pos());
275 // is there a manual margin with a manual label
276 LyXLayout_ptr const & layout = par.layout();
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);
283 // A manual label par (e.g. List) has an auto-hfill
284 // between the label text and the body of the
286 // But we don't want to do this auto hfill if the par
291 if (nlh && !par.getLabelWidthString().empty())
292 result.label_hfill = labelFill(par, row) / double(nlh);
295 // are there any hfills in the row?
296 int const nh = numberOfHfills(par, row);
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
308 if (par.params().align() == LYX_ALIGN_LAYOUT)
309 align = layout->align;
311 align = par.params().align();
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;
318 && par.isInset(row.pos())
319 && par.getInset(row.pos())->display())
321 align = LYX_ALIGN_CENTER;
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());
331 disp_inset = in->display();
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
337 && row.endpos() < par.size()
338 && !par.isNewline(row.endpos() - 1)
341 result.separator = w / ns;
347 case LYX_ALIGN_RIGHT:
350 case LYX_ALIGN_CENTER:
356 text_->bidi.computeTables(par, buffer, row);
358 pos_type body_pos = par.beginOfBody();
359 pos_type end = row.endpos();
362 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
364 docstring const lsep = from_utf8(layout->labelsep);
365 result.x += theFontMetrics(text_->getLabelFont(buffer, par)).width(lsep);
367 result.x += result.label_hfill;
375 int TextMetrics::labelFill(Paragraph const & par, Row const & row) const
377 Buffer & buffer = *bv_->buffer();
379 pos_type last = par.beginOfBody();
380 BOOST_ASSERT(last > 0);
382 // -1 because a label ends with a space that is in the label
385 // a separator at this end does not count
386 if (par.isLineSeparator(last))
390 for (pos_type i = row.pos(); i <= last; ++i)
391 w += text_->singleWidth(buffer, par, i);
393 docstring const & label = par.params().labelWidthString();
397 FontMetrics const & fm
398 = theFontMetrics(text_->getLabelFont(buffer, par));
400 return max(0, fm.width(label) - w);
404 int defaultRowHeight()
406 return int(theFontMetrics(LyXFont(LyXFont::ALL_SANE)).maxHeight() * 1.2);