]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.C
Transfer these methods from LyXText to TextMetrics:
[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 "paragraph_funcs.h"
26 #include "pariterator.h"
27 #include "coordcache.h"
28 #include "debug.h"
29 #include "funcrequest.h"
30 #include "FontIterator.h"
31 #include "LColor.h"
32 #include "lyxlength.h"
33 #include "lyxtext.h"
34 #include "metricsinfo.h"
35 #include "ParagraphParameters.h"
36 #include "vspace.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 using std::max;
42 using std::min;
43 using std::endl;
44
45 namespace lyx {
46
47 using frontend::FontMetrics;
48
49 namespace {
50
51 int numberOfSeparators(Paragraph const & par, Row const & row)
52 {
53         pos_type const first = max(row.pos(), par.beginOfBody());
54         pos_type const last = row.endpos() - 1;
55         int n = 0;
56         for (pos_type p = first; p < last; ++p) {
57                 if (par.isSeparator(p))
58                         ++n;
59         }
60         return n;
61 }
62
63
64 int numberOfLabelHfills(Paragraph const & par, Row const & row)
65 {
66         pos_type last = row.endpos() - 1;
67         pos_type first = row.pos();
68
69         // hfill *DO* count at the beginning of paragraphs!
70         if (first) {
71                 while (first < last && par.isHfill(first))
72                         ++first;
73         }
74
75         last = min(last, par.beginOfBody());
76         int n = 0;
77         for (pos_type p = first; p < last; ++p) {
78                 if (par.isHfill(p))
79                         ++n;
80         }
81         return n;
82 }
83
84
85 int numberOfHfills(Paragraph const & par, Row const & row)
86 {
87         pos_type const last = row.endpos();
88         pos_type first = row.pos();
89
90         // hfill *DO* count at the beginning of paragraphs!
91         if (first) {
92                 while (first < last && par.isHfill(first))
93                         ++first;
94         }
95
96         first = max(first, par.beginOfBody());
97
98         int n = 0;
99         for (pos_type p = first; p < last; ++p) {
100                 if (par.isHfill(p))
101                         ++n;
102         }
103         return n;
104 }
105
106 } // namespace anon
107
108 TextMetrics::TextMetrics(BufferView * bv, LyXText * text)
109         : bv_(bv), text_(text)
110 {
111         BOOST_ASSERT(bv_);
112         max_width_ = bv_->workWidth();
113         dim_.wid = max_width_;
114         dim_.asc = 10;
115         dim_.des = 10;
116
117         //text_->updateLabels(*bv->buffer());
118 }
119
120
121 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
122 {
123         return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
124 }
125
126
127 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
128                 bool redo)
129 {
130         ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
131         if (pmc_it == par_metrics_.end()) {
132                 pmc_it = par_metrics_.insert(
133                         std::make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
134         }
135         if (pmc_it->second.rows().empty() && redo) {
136                 redoParagraph(pit);
137         }
138         return pmc_it->second;
139 }
140
141
142 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim)
143 {
144         BOOST_ASSERT(mi.base.textwidth);
145         max_width_ = mi.base.textwidth;
146
147         //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
148         //      << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
149         
150         bool changed = false;
151
152         unsigned int h = 0;
153         unsigned int w = 0;
154         for (pit_type pit = 0, n = text_->paragraphs().size(); pit != n; ++pit) {
155                 changed |= redoParagraph(pit);
156                 ParagraphMetrics const & pm = parMetrics(pit);
157                 h += pm.height();
158                 if (w < pm.width())
159                         w = pm.width();
160         }
161
162         dim.wid = w;
163         dim.asc = parMetrics(0).ascent();
164         dim.des = h - dim.asc;
165
166         changed |= dim_ != dim;
167         dim_ = dim;
168         return changed;
169 }
170
171
172 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
173 {
174         return main_text_? pm.rightMargin(*bv_->buffer()) : 0;
175 }
176
177
178 int TextMetrics::rightMargin(pit_type const pit) const
179 {
180         return main_text_? par_metrics_[pit].rightMargin(*bv_->buffer()) : 0;
181 }
182
183
184 bool TextMetrics::redoParagraph(pit_type const pit)
185 {
186         Paragraph & par = text_->getPar(pit);
187         ParagraphMetrics pm(par);
188         Buffer & buffer = *bv_->buffer();
189         main_text_ = (text_ == &buffer.text());
190         bool changed = false;
191
192         // FIXME: this has nothing to do here and is the reason why text_ is not
193         // const.
194         if (par.checkBiblio(buffer.params().trackChanges))
195                 const_cast<LCursor &>(bv_->cursor()).posRight();
196
197         // Optimisation: this is used in the next two loops
198         // so better to calculate that once here.
199         int const right_margin = rightMargin(pm);
200
201         // redo insets
202         // FIXME: We should always use getFont(), see documentation of
203         // noFontChange() in insetbase.h.
204         LyXFont const bufferfont = buffer.params().getFont();
205         InsetList::const_iterator ii = par.insetlist.begin();
206         InsetList::const_iterator iend = par.insetlist.end();
207         for (; ii != iend; ++ii) {
208                 Dimension dim;
209                 int const w = max_width_ - text_->leftMargin(buffer, max_width_, pit, ii->pos)
210                         - right_margin;
211                 LyXFont const & font = ii->inset->noFontChange() ?
212                         bufferfont : text_->getFont(buffer, par, ii->pos);
213                 MetricsInfo mi(bv_, font, w);
214                 changed |= ii->inset->metrics(mi, dim);
215         }
216
217         // rebreak the paragraph
218         pm.rows().clear();
219
220         par.setBeginOfBody();
221         pos_type z = 0;
222         // maximum pixel width of a row
223         int width = max_width_ - right_margin; // - leftMargin(buffer, max_width_, pit, row);
224         do {
225                 Row row(z);
226                 rowBreakPoint(width, pit, row);
227                 setRowWidth(right_margin, pit, row);
228                 setHeightOfRow(pit, row);
229                 pm.rows().push_back(row);
230                 pm.dim().wid = std::max(pm.dim().wid, row.width());
231                 pm.dim().des += row.height();
232                 z = row.endpos();
233         } while (z < par.size());
234
235         // Make sure that if a par ends in newline, there is one more row
236         // under it
237         // FIXME this is a dirty trick. Now the _same_ position in the
238         // paragraph occurs in _two_ different rows, and has two different
239         // display positions, leading to weird behaviour when moving up/down.
240         if (z > 0 && par.isNewline(z - 1)) {
241                 Row row(z - 1);
242                 row.endpos(z - 1);
243                 setRowWidth(right_margin, pit, row);
244                 setHeightOfRow(pit, row);
245                 pm.rows().push_back(row);
246                 pm.dim().des += row.height();
247         }
248
249         pm.dim().asc += pm.rows()[0].ascent();
250         pm.dim().des -= pm.rows()[0].ascent();
251
252         // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
253         // redoParagraph() recursively inside parMetrics.
254         Dimension old_dim = parMetrics(pit, false).dim();
255
256         changed |= old_dim.height() != pm.dim().height();
257
258         par_metrics_[pit] = pm;
259
260         return changed;
261 }
262
263 RowMetrics TextMetrics::computeRowMetrics(pit_type const pit,
264                 Row const & row) const
265 {
266         RowMetrics result;
267         Buffer & buffer = *bv_->buffer();
268         Paragraph const & par = text_->getPar(pit);
269
270         double w = dim_.wid - row.width();
271
272         bool const is_rtl = text_->isRTL(buffer, par);
273         if (is_rtl)
274                 result.x = rightMargin(pit);
275         else
276                 result.x = text_->leftMargin(buffer, max_width_, pit, row.pos());
277
278         // is there a manual margin with a manual label
279         LyXLayout_ptr const & layout = par.layout();
280
281         if (layout->margintype == MARGIN_MANUAL
282             && layout->labeltype == LABEL_MANUAL) {
283                 /// We might have real hfills in the label part
284                 int nlh = numberOfLabelHfills(par, row);
285
286                 // A manual label par (e.g. List) has an auto-hfill
287                 // between the label text and the body of the
288                 // paragraph too.
289                 // But we don't want to do this auto hfill if the par
290                 // is empty.
291                 if (!par.empty())
292                         ++nlh;
293
294                 if (nlh && !par.getLabelWidthString().empty())
295                         result.label_hfill = labelFill(par, row) / double(nlh);
296         }
297
298         // are there any hfills in the row?
299         int const nh = numberOfHfills(par, row);
300
301         if (nh) {
302                 if (w > 0)
303                         result.hfill = w / nh;
304         // we don't have to look at the alignment if it is ALIGN_LEFT and
305         // if the row is already larger then the permitted width as then
306         // we force the LEFT_ALIGN'edness!
307         } else if (int(row.width()) < max_width_) {
308                 // is it block, flushleft or flushright?
309                 // set x how you need it
310                 int align;
311                 if (par.params().align() == LYX_ALIGN_LAYOUT)
312                         align = layout->align;
313                 else
314                         align = par.params().align();
315
316                 // Display-style insets should always be on a centred row
317                 // The test on par.size() is to catch zero-size pars, which
318                 // would trigger the assert in Paragraph::getInset().
319                 //inset = par.size() ? par.getInset(row.pos()) : 0;
320                 if (!par.empty()
321                     && par.isInset(row.pos())
322                     && par.getInset(row.pos())->display())
323                 {
324                         align = LYX_ALIGN_CENTER;
325                 }
326
327                 switch (align) {
328                 case LYX_ALIGN_BLOCK: {
329                         int const ns = numberOfSeparators(par, row);
330                         bool disp_inset = false;
331                         if (row.endpos() < par.size()) {
332                                 InsetBase const * in = par.getInset(row.endpos());
333                                 if (in)
334                                         disp_inset = in->display();
335                         }
336                         // If we have separators, this is not the last row of a
337                         // par, does not end in newline, and is not row above a
338                         // display inset... then stretch it
339                         if (ns
340                             && row.endpos() < par.size()
341                             && !par.isNewline(row.endpos() - 1)
342                             && !disp_inset
343                                 ) {
344                                 result.separator = w / ns;
345                         } else if (is_rtl) {
346                                 result.x += w;
347                         }
348                         break;
349                 }
350                 case LYX_ALIGN_RIGHT:
351                         result.x += w;
352                         break;
353                 case LYX_ALIGN_CENTER:
354                         result.x += w / 2;
355                         break;
356                 }
357         }
358
359         text_->bidi.computeTables(par, buffer, row);
360         if (is_rtl) {
361                 pos_type body_pos = par.beginOfBody();
362                 pos_type end = row.endpos();
363
364                 if (body_pos > 0
365                     && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
366                 {
367                         docstring const lsep = from_utf8(layout->labelsep);
368                         result.x += theFontMetrics(text_->getLabelFont(buffer, par)).width(lsep);
369                         if (body_pos <= end)
370                                 result.x += result.label_hfill;
371                 }
372         }
373
374         return result;
375 }
376
377
378 int TextMetrics::labelFill(Paragraph const & par, Row const & row) const
379 {
380         Buffer & buffer = *bv_->buffer();
381
382         pos_type last = par.beginOfBody();
383         BOOST_ASSERT(last > 0);
384
385         // -1 because a label ends with a space that is in the label
386         --last;
387
388         // a separator at this end does not count
389         if (par.isLineSeparator(last))
390                 --last;
391
392         int w = 0;
393         for (pos_type i = row.pos(); i <= last; ++i)
394                 w += text_->singleWidth(buffer, par, i);
395
396         docstring const & label = par.params().labelWidthString();
397         if (label.empty())
398                 return 0;
399
400         FontMetrics const & fm 
401                 = theFontMetrics(text_->getLabelFont(buffer, par));
402
403         return max(0, fm.width(label) - w);
404 }
405
406
407 namespace {
408
409 // this needs special handling - only newlines count as a break point
410 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
411 {
412         pos_type const end = par.size();
413
414         for (; i < end; ++i)
415                 if (par.isNewline(i))
416                         return i + 1;
417
418         return end;
419 }
420
421 };
422
423
424 int TextMetrics::labelEnd(pit_type const pit) const
425 {
426         // labelEnd is only needed if the layout fills a flushleft label.
427         if (text_->getPar(pit).layout()->margintype != MARGIN_MANUAL)
428                 return 0;
429         // return the beginning of the body
430         return text_->leftMargin(*bv_->buffer(), max_width_, pit);
431 }
432
433
434 void TextMetrics::rowBreakPoint(int width, pit_type const pit,
435                 Row & row) const
436 {
437         Buffer & buffer = *bv_->buffer();
438         Paragraph const & par = text_->getPar(pit);
439         pos_type const end = par.size();
440         pos_type const pos = row.pos();
441         if (pos == end) {
442                 row.endpos(end);
443                 return;
444         }
445
446         if (width < 0) {
447                 row.endpos(end);
448                 return;
449         }
450
451         LyXLayout_ptr const & layout = par.layout();
452
453         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
454                 row.endpos(addressBreakPoint(pos, par));
455                 return;
456         }
457
458         pos_type const body_pos = par.beginOfBody();
459
460
461         // Now we iterate through until we reach the right margin
462         // or the end of the par, then choose the possible break
463         // nearest that.
464
465         int label_end = labelEnd(pit);
466         int const left = text_->leftMargin(buffer, max_width_, pit, pos);
467         int x = left;
468
469         // pixel width since last breakpoint
470         int chunkwidth = 0;
471
472         FontIterator fi = FontIterator(buffer, *text_, par, pos);
473         pos_type point = end;
474         pos_type i = pos;
475         FontMetrics const & fm = theFontMetrics(text_->getLabelFont(buffer, par));
476         for ( ; i < end; ++i, ++fi) {
477                 char_type const c = par.getChar(i);
478                 int thiswidth = text_->singleWidth(par, i, c, *fi);
479
480                 // add the auto-hfill from label end to the body
481                 if (body_pos && i == body_pos) {
482                         docstring lsep = from_utf8(layout->labelsep);
483                         int add = fm.width(lsep);
484                         if (par.isLineSeparator(i - 1))
485                                 add -= text_->singleWidth(buffer, par, i - 1);
486
487                         add = std::max(add, label_end - x);
488                         thiswidth += add;
489                 }
490
491                 x += thiswidth;
492                 chunkwidth += thiswidth;
493
494                 // break before a character that will fall off
495                 // the right of the row
496                 if (x >= width) {
497                         // if no break before, break here
498                         if (point == end || chunkwidth >= width - left) {
499                                 if (i > pos)
500                                         point = i;
501                                 else
502                                         point = i + 1;
503
504                         }
505                         // exit on last registered breakpoint:
506                         break;
507                 }
508
509                 if (par.isNewline(i)) {
510                         point = i + 1;
511                         break;
512                 }
513                 // Break before...
514                 if (i + 1 < end) {
515                         if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
516                                 point = i + 1;
517                                 break;
518                         }
519                         // ...and after.
520                         if (par.isInset(i) && par.getInset(i)->display()) {
521                                 point = i + 1;
522                                 break;
523                         }
524                 }
525
526                 if (!par.isInset(i) || par.getInset(i)->isChar()) {
527                         // some insets are line separators too
528                         if (par.isLineSeparator(i)) {
529                                 // register breakpoint:
530                                 point = i + 1;
531                                 chunkwidth = 0;
532                         }
533                 }
534         }
535
536         // maybe found one, but the par is short enough.
537         if (i == end && x < width)
538                 point = end;
539
540         // manual labels cannot be broken in LaTeX. But we
541         // want to make our on-screen rendering of footnotes
542         // etc. still break
543         if (body_pos && point < body_pos)
544                 point = body_pos;
545
546         row.endpos(point);
547 }
548
549
550 void TextMetrics::setRowWidth(int right_margin,
551                 pit_type const pit, Row & row) const
552 {
553         Buffer & buffer = *bv_->buffer();
554         // get the pure distance
555         pos_type const end = row.endpos();
556
557         Paragraph const & par = text_->getPar(pit);
558         docstring const labelsep = from_utf8(par.layout()->labelsep);
559         int w = text_->leftMargin(buffer, max_width_, pit, row.pos());
560         int label_end = labelEnd(pit);
561
562         pos_type const body_pos = par.beginOfBody();
563         pos_type i = row.pos();
564
565         FontMetrics const & fm = theFontMetrics(text_->getLabelFont(buffer, par));
566
567         if (i < end) {
568                 FontIterator fi = FontIterator(buffer, *text_, par, i);
569                 for ( ; i < end; ++i, ++fi) {
570                         if (body_pos > 0 && i == body_pos) {
571                                 w += fm.width(labelsep);
572                                 if (par.isLineSeparator(i - 1))
573                                         w -= text_->singleWidth(buffer, par, i - 1);
574                                 w = max(w, label_end);
575                         }
576                         char_type const c = par.getChar(i);
577                         w += text_->singleWidth(par, i, c, *fi);
578                 }
579         }
580
581         if (body_pos > 0 && body_pos >= end) {
582                 w += fm.width(labelsep);
583                 if (end > 0 && par.isLineSeparator(end - 1))
584                         w -= text_->singleWidth(buffer, par, end - 1);
585                 w = max(w, label_end);
586         }
587
588         row.width(w + right_margin);
589 }
590
591
592 void TextMetrics::setHeightOfRow(pit_type const pit,
593                 Row & row)
594 {
595         Paragraph const & par = text_->getPar(pit);
596         // get the maximum ascent and the maximum descent
597         double layoutasc = 0;
598         double layoutdesc = 0;
599         double const dh = defaultRowHeight();
600
601         // ok, let us initialize the maxasc and maxdesc value.
602         // Only the fontsize count. The other properties
603         // are taken from the layoutfont. Nicer on the screen :)
604         LyXLayout_ptr const & layout = par.layout();
605
606         // as max get the first character of this row then it can
607         // increase but not decrease the height. Just some point to
608         // start with so we don't have to do the assignment below too
609         // often.
610         Buffer const & buffer = *bv_->buffer();
611         LyXFont font = text_->getFont(buffer, par, row.pos());
612         LyXFont::FONT_SIZE const tmpsize = font.size();
613         font = text_->getLayoutFont(buffer, pit);
614         LyXFont::FONT_SIZE const size = font.size();
615         font.setSize(tmpsize);
616
617         LyXFont labelfont = text_->getLabelFont(buffer, par);
618
619         FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
620         FontMetrics const & fontmetrics = theFontMetrics(font);
621
622         // these are minimum values
623         double const spacing_val = layout->spacing.getValue()
624                 * text_->spacing(buffer, par);
625         //lyxerr << "spacing_val = " << spacing_val << endl;
626         int maxasc  = int(fontmetrics.maxAscent()  * spacing_val);
627         int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
628
629         // insets may be taller
630         InsetList::const_iterator ii = par.insetlist.begin();
631         InsetList::const_iterator iend = par.insetlist.end();
632         for ( ; ii != iend; ++ii) {
633                 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
634                         maxasc  = max(maxasc,  ii->inset->ascent());
635                         maxdesc = max(maxdesc, ii->inset->descent());
636                 }
637         }
638
639         // Check if any custom fonts are larger (Asger)
640         // This is not completely correct, but we can live with the small,
641         // cosmetic error for now.
642         int labeladdon = 0;
643         pos_type const pos_end = row.endpos();
644
645         LyXFont::FONT_SIZE maxsize =
646                 par.highestFontInRange(row.pos(), pos_end, size);
647         if (maxsize > font.size()) {
648                 font.setSize(maxsize);
649                 maxasc  = max(maxasc,  fontmetrics.maxAscent());
650                 maxdesc = max(maxdesc, fontmetrics.maxDescent());
651         }
652
653         // This is nicer with box insets:
654         ++maxasc;
655         ++maxdesc;
656
657         row.ascent(maxasc);
658         ParagraphList const & pars = text_->paragraphs();
659
660         // is it a top line?
661         if (row.pos() == 0) {
662                 BufferParams const & bufparams = buffer.params();
663                 // some parksips VERY EASY IMPLEMENTATION
664                 if (bufparams.paragraph_separation
665                     == BufferParams::PARSEP_SKIP
666                         && pit > 0
667                         && ((layout->isParagraph() && par.getDepth() == 0)
668                             || (pars[pit - 1].layout()->isParagraph()
669                                 && pars[pit - 1].getDepth() == 0)))
670                 {
671                                 maxasc += bufparams.getDefSkip().inPixels(*bv_);
672                 }
673
674                 if (par.params().startOfAppendix())
675                         maxasc += int(3 * dh);
676
677                 // This is special code for the chapter, since the label of this
678                 // layout is printed in an extra row
679                 if (layout->counter == "chapter"
680                     && !par.params().labelString().empty()) {
681                         labeladdon = int(labelfont_metrics.maxHeight()
682                                      * layout->spacing.getValue()
683                                      * text_->spacing(buffer, par));
684                 }
685
686                 // special code for the top label
687                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
688                      || layout->labeltype == LABEL_BIBLIO
689                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
690                     && isFirstInSequence(pit, pars)
691                     && !par.getLabelstring().empty())
692                 {
693                         labeladdon = int(
694                                   labelfont_metrics.maxHeight()
695                                         * layout->spacing.getValue()
696                                         * text_->spacing(buffer, par)
697                                 + (layout->topsep + layout->labelbottomsep) * dh);
698                 }
699
700                 // Add the layout spaces, for example before and after
701                 // a section, or between the items of a itemize or enumerate
702                 // environment.
703
704                 pit_type prev = depthHook(pit, pars, par.getDepth());
705                 if (prev != pit
706                     && pars[prev].layout() == layout
707                     && pars[prev].getDepth() == par.getDepth()
708                     && pars[prev].getLabelWidthString() == par.getLabelWidthString())
709                 {
710                         layoutasc = layout->itemsep * dh;
711                 } else if (pit != 0 || row.pos() != 0) {
712                         if (layout->topsep > 0)
713                                 layoutasc = layout->topsep * dh;
714                 }
715
716                 prev = outerHook(pit, pars);
717                 if (prev != pit_type(pars.size())) {
718                         maxasc += int(pars[prev].layout()->parsep * dh);
719                 } else if (pit != 0) {
720                         if (pars[pit - 1].getDepth() != 0 ||
721                                         pars[pit - 1].layout() == layout) {
722                                 maxasc += int(layout->parsep * dh);
723                         }
724                 }
725         }
726
727         // is it a bottom line?
728         if (row.endpos() >= par.size()) {
729                 // add the layout spaces, for example before and after
730                 // a section, or between the items of a itemize or enumerate
731                 // environment
732                 pit_type nextpit = pit + 1;
733                 if (nextpit != pit_type(pars.size())) {
734                         pit_type cpit = pit;
735                         double usual = 0;
736                         double unusual = 0;
737
738                         if (pars[cpit].getDepth() > pars[nextpit].getDepth()) {
739                                 usual = pars[cpit].layout()->bottomsep * dh;
740                                 cpit = depthHook(cpit, pars, pars[nextpit].getDepth());
741                                 if (pars[cpit].layout() != pars[nextpit].layout()
742                                         || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
743                                 {
744                                         unusual = pars[cpit].layout()->bottomsep * dh;
745                                 }
746                                 layoutdesc = max(unusual, usual);
747                         } else if (pars[cpit].getDepth() == pars[nextpit].getDepth()) {
748                                 if (pars[cpit].layout() != pars[nextpit].layout()
749                                         || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
750                                         layoutdesc = int(pars[cpit].layout()->bottomsep * dh);
751                         }
752                 }
753         }
754
755         // incalculate the layout spaces
756         maxasc  += int(layoutasc  * 2 / (2 + pars[pit].getDepth()));
757         maxdesc += int(layoutdesc * 2 / (2 + pars[pit].getDepth()));
758
759         // FIXME: the correct way is to do the following is to move the 
760         // following code in another method specially tailored for the 
761         // main LyXText. The following test is thus bogus.
762         // Top and bottom margin of the document (only at top-level)
763         if (main_text_) {
764                 if (pit == 0 && row.pos() == 0)
765                         maxasc += 20;
766                 if (pit + 1 == pit_type(pars.size()) &&
767                     row.endpos() == par.size())
768                         maxdesc += 20;
769         }
770
771         row.ascent(maxasc + labeladdon);
772         row.descent(maxdesc);
773 }
774
775
776 int defaultRowHeight()
777 {
778         return int(theFontMetrics(LyXFont(LyXFont::ALL_SANE)).maxHeight() *  1.2);
779 }
780
781 } // namespace lyx