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