2 * \file src/TextMetrics.cpp
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"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "FuncRequest.h"
31 #include "InsetList.h"
35 #include "MetricsInfo.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
38 #include "ParIterator.h"
39 #include "rowpainter.h"
41 #include "TextClass.h"
44 #include "mathed/MacroTable.h"
45 #include "mathed/MathMacroTemplate.h"
47 #include "frontends/FontMetrics.h"
48 #include "frontends/Painter.h"
50 #include "support/debug.h"
52 #include "support/lassert.h"
59 using frontend::FontMetrics
;
61 static int numberOfSeparators(Paragraph
const & par
, Row
const & row
)
63 pos_type
const first
= max(row
.pos(), par
.beginOfBody());
64 pos_type
const last
= row
.endpos() - 1;
66 for (pos_type p
= first
; p
< last
; ++p
) {
67 if (par
.isSeparator(p
))
74 static int numberOfLabelHfills(Paragraph
const & par
, Row
const & row
)
76 pos_type last
= row
.endpos() - 1;
77 pos_type first
= row
.pos();
79 // hfill *DO* count at the beginning of paragraphs!
81 while (first
< last
&& par
.isHfill(first
))
85 last
= min(last
, par
.beginOfBody());
87 for (pos_type p
= first
; p
< last
; ++p
) {
95 static int numberOfHfills(Paragraph
const & par
, Row
const & row
)
97 pos_type
const last
= row
.endpos();
98 pos_type first
= row
.pos();
100 // hfill *DO* count at the beginning of paragraphs!
102 while (first
< last
&& par
.isHfill(first
))
106 first
= max(first
, par
.beginOfBody());
109 for (pos_type p
= first
; p
< last
; ++p
) {
117 /////////////////////////////////////////////////////////////////////
121 /////////////////////////////////////////////////////////////////////
124 TextMetrics::TextMetrics(BufferView
* bv
, Text
* text
)
125 : bv_(bv
), text_(text
)
128 max_width_
= bv_
->workWidth();
129 dim_
.wid
= max_width_
;
133 //text_->updateLabels(bv->buffer());
137 bool TextMetrics::contains(pit_type pit
) const
139 return par_metrics_
.find(pit
) != par_metrics_
.end();
143 ParagraphMetrics
const & TextMetrics::parMetrics(pit_type pit
) const
145 return const_cast<TextMetrics
*>(this)->parMetrics(pit
, true);
150 pair
<pit_type
, ParagraphMetrics
const *> TextMetrics::first() const
152 ParMetricsCache::const_iterator it
= par_metrics_
.begin();
153 return make_pair(it
->first
, &it
->second
);
157 pair
<pit_type
, ParagraphMetrics
const *> TextMetrics::last() const
159 ParMetricsCache::const_reverse_iterator it
= par_metrics_
.rbegin();
160 return make_pair(it
->first
, &it
->second
);
164 ParagraphMetrics
& TextMetrics::parMetrics(pit_type pit
, bool redo
)
166 ParMetricsCache::iterator pmc_it
= par_metrics_
.find(pit
);
167 if (pmc_it
== par_metrics_
.end()) {
168 pmc_it
= par_metrics_
.insert(
169 make_pair(pit
, ParagraphMetrics(text_
->getPar(pit
)))).first
;
171 if (pmc_it
->second
.rows().empty() && redo
)
173 return pmc_it
->second
;
177 int TextMetrics::parPosition(pit_type pit
) const
179 if (pit
< par_metrics_
.begin()->first
)
181 if (pit
> par_metrics_
.rbegin()->first
)
184 return par_metrics_
[pit
].position();
188 bool TextMetrics::metrics(MetricsInfo
& mi
, Dimension
& dim
, int min_width
)
190 LASSERT(mi
.base
.textwidth
, /**/);
191 max_width_
= mi
.base
.textwidth
;
192 // backup old dimension.
193 Dimension
const old_dim
= dim_
;
196 dim_
.wid
= min_width
;
197 pit_type
const npar
= text_
->paragraphs().size();
199 // If there is more than one row, expand the text to
200 // the full allowable width.
201 dim_
.wid
= max_width_
;
203 //lyxerr << "TextMetrics::metrics: width: " << mi.base.textwidth
204 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
206 bool changed
= false;
208 for (pit_type pit
= 0; pit
!= npar
; ++pit
) {
209 changed
|= redoParagraph(pit
);
210 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
212 if (dim_
.wid
< pm
.width())
213 dim_
.wid
= pm
.width();
216 dim_
.asc
= par_metrics_
[0].ascent();
217 dim_
.des
= h
- dim_
.asc
;
218 //lyxerr << "dim_.wid " << dim_.wid << endl;
219 //lyxerr << "dim_.asc " << dim_.asc << endl;
220 //lyxerr << "dim_.des " << dim_.des << endl;
222 changed
|= dim_
!= old_dim
;
228 int TextMetrics::rightMargin(ParagraphMetrics
const & pm
) const
230 return main_text_
? pm
.rightMargin(*bv_
) : 0;
234 int TextMetrics::rightMargin(pit_type
const pit
) const
236 return main_text_
? par_metrics_
[pit
].rightMargin(*bv_
) : 0;
240 void TextMetrics::applyOuterFont(Font
& font
) const
243 lf
.fontInfo().reduce(bv_
->buffer().params().getFont().fontInfo());
244 lf
.fontInfo().realize(font
.fontInfo());
245 lf
.setLanguage(font
.language());
250 Font
TextMetrics::displayFont(pit_type pit
, pos_type pos
) const
252 LASSERT(pos
>= 0, /**/);
254 ParagraphList
const & pars
= text_
->paragraphs();
255 Paragraph
const & par
= pars
[pit
];
256 Layout
const & layout
= par
.layout();
257 Buffer
const & buffer
= bv_
->buffer();
259 BufferParams
const & params
= buffer
.params();
260 pos_type
const body_pos
= par
.beginOfBody();
262 // We specialize the 95% common case:
263 if (!par
.getDepth()) {
264 Font f
= par
.getFontSettings(params
, pos
);
265 if (!text_
->isMainText(buffer
))
267 bool lab
= layout
.labeltype
== LABEL_MANUAL
&& pos
< body_pos
;
269 FontInfo
const & lf
= lab
? layout
.labelfont
: layout
.font
;
270 FontInfo rlf
= lab
? layout
.reslabelfont
: layout
.resfont
;
272 // In case the default family has been customized
273 if (lf
.family() == INHERIT_FAMILY
)
274 rlf
.setFamily(params
.getFont().fontInfo().family());
275 f
.fontInfo().realize(rlf
);
279 // The uncommon case need not be optimized as much
280 FontInfo
const & layoutfont
= pos
< body_pos
?
281 layout
.labelfont
: layout
.font
;
283 Font font
= par
.getFontSettings(params
, pos
);
284 font
.fontInfo().realize(layoutfont
);
286 if (!text_
->isMainText(buffer
))
287 applyOuterFont(font
);
289 // Realize against environment font information
290 // NOTE: the cast to pit_type should be removed when pit_type
291 // changes to a unsigned integer.
292 if (pit
< pit_type(pars
.size()))
293 font
.fontInfo().realize(outerFont(pit
, pars
).fontInfo());
295 // Realize with the fonts of lesser depth.
296 font
.fontInfo().realize(params
.getFont().fontInfo());
302 bool TextMetrics::isRTL(CursorSlice
const & sl
, bool boundary
) const
304 if (!lyxrc
.rtl_support
|| !sl
.text())
308 if (boundary
&& sl
.pos() > 0)
311 return displayFont(sl
.pit(), sl
.pos() + correction
).isVisibleRightToLeft();
315 bool TextMetrics::isRTLBoundary(pit_type pit
, pos_type pos
) const
317 if (!lyxrc
.rtl_support
)
320 // no RTL boundary at line start
324 Paragraph
const & par
= text_
->getPar(pit
);
326 bool left
= displayFont(pit
, pos
- 1).isVisibleRightToLeft();
328 if (pos
== par
.size())
329 right
= par
.isRTL(bv_
->buffer().params());
331 right
= displayFont(pit
, pos
).isVisibleRightToLeft();
332 return left
!= right
;
336 bool TextMetrics::isRTLBoundary(pit_type pit
, pos_type pos
,
337 Font
const & font
) const
339 if (!lyxrc
.rtl_support
)
342 Paragraph
const & par
= text_
->getPar(pit
);
343 bool left
= font
.isVisibleRightToLeft();
345 if (pos
== par
.size())
346 right
= par
.isRTL(bv_
->buffer().params());
348 right
= displayFont(pit
, pos
).isVisibleRightToLeft();
349 return left
!= right
;
353 bool TextMetrics::redoParagraph(pit_type
const pit
)
355 Paragraph
& par
= text_
->getPar(pit
);
356 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
357 // redoParagraph() recursively inside parMetrics.
358 Dimension old_dim
= parMetrics(pit
, false).dim();
359 ParagraphMetrics
& pm
= par_metrics_
[pit
];
362 Buffer
& buffer
= bv_
->buffer();
363 main_text_
= (text_
== &buffer
.text());
364 bool changed
= false;
366 // FIXME: This check ought to be done somewhere else. It is the reason
367 // why text_ is not const. But then, where else to do it?
368 // Well, how can you end up with either (a) a biblio environment that
369 // has no InsetBibitem or (b) a biblio environment with more than one
370 // InsetBibitem? I think the answer is: when paragraphs are merged;
371 // when layout is set; when material is pasted.
372 int const moveCursor
= par
.checkBiblio(buffer
);
374 const_cast<Cursor
&>(bv_
->cursor()).posForward();
375 else if (moveCursor
< 0) {
376 Cursor
& cursor
= const_cast<Cursor
&>(bv_
->cursor());
377 if (cursor
.pos() >= -moveCursor
)
378 cursor
.posBackward();
381 // Optimisation: this is used in the next two loops
382 // so better to calculate that once here.
383 int const right_margin
= rightMargin(pm
);
385 // iterator pointing to paragraph to resolve macros
386 DocIterator parPos
= text_
->macrocontextPosition();
390 LYXERR(Debug::INFO
, "MacroContext not initialised!"
391 << " Going through the buffer again and hope"
392 << " the context is better then.");
393 updateLabels(bv_
->buffer());
394 parPos
= text_
->macrocontextPosition();
395 LASSERT(!parPos
.empty(), /**/);
400 // FIXME: We should always use getFont(), see documentation of
401 // noFontChange() in Inset.h.
402 Font
const bufferfont
= buffer
.params().getFont();
403 InsetList::const_iterator ii
= par
.insetList().begin();
404 InsetList::const_iterator iend
= par
.insetList().end();
405 for (; ii
!= iend
; ++ii
) {
406 // position already initialized?
407 if (!parPos
.empty()) {
408 parPos
.pos() = ii
->pos
;
410 // A macro template would normally not be visible
411 // by itself. But the tex macro semantics allow
412 // recursion, so we artifically take the context
413 // after the macro template to simulate this.
414 if (ii
->inset
->lyxCode() == MATHMACRO_CODE
)
418 // do the metric calculation
420 int const w
= max_width_
- leftMargin(max_width_
, pit
, ii
->pos
)
422 Font
const & font
= ii
->inset
->noFontChange() ?
423 bufferfont
: displayFont(pit
, ii
->pos
);
424 MacroContext
mc(buffer
, parPos
);
425 MetricsInfo
mi(bv_
, font
.fontInfo(), w
, mc
);
426 ii
->inset
->metrics(mi
, dim
);
427 Dimension
const old_dim
= pm
.insetDimension(ii
->inset
);
428 if (old_dim
!= dim
) {
429 pm
.setInsetDimension(ii
->inset
, dim
);
434 par
.setBeginOfBody();
436 size_t row_index
= 0;
437 // maximum pixel width of a row
438 int width
= max_width_
- right_margin
; // - leftMargin(max_width_, pit, row);
441 pos_type end
= rowBreakPoint(width
, pit
, first
);
442 if (row_index
|| end
< par
.size())
443 // If there is more than one row, expand the text to
444 // the full allowable width. This setting here is needed
445 // for the computeRowMetrics() below.
446 dim_
.wid
= max_width_
;
448 dim
= rowHeight(pit
, first
, end
);
449 dim
.wid
= rowWidth(right_margin
, pit
, first
, end
);
450 if (row_index
== pm
.rows().size())
451 pm
.rows().push_back(Row());
452 Row
& row
= pm
.rows()[row_index
];
453 row
.setChanged(false);
456 row
.setDimension(dim
);
457 int const max_row_width
= max(dim_
.wid
, dim
.wid
);
458 computeRowMetrics(pit
, row
, max_row_width
);
462 pm
.dim().wid
= max(pm
.dim().wid
, dim
.wid
);
463 pm
.dim().des
+= dim
.height();
464 } while (first
< par
.size());
466 if (row_index
< pm
.rows().size())
467 pm
.rows().resize(row_index
);
469 // Make sure that if a par ends in newline, there is one more row
471 if (first
> 0 && par
.isNewline(first
- 1)) {
472 Dimension dim
= rowHeight(pit
, first
, first
);
473 dim
.wid
= rowWidth(right_margin
, pit
, first
, first
);
474 if (row_index
== pm
.rows().size())
475 pm
.rows().push_back(Row());
476 Row
& row
= pm
.rows()[row_index
];
477 row
.setChanged(false);
480 row
.setDimension(dim
);
481 int const max_row_width
= max(dim_
.wid
, dim
.wid
);
482 computeRowMetrics(pit
, row
, max_row_width
);
483 pm
.dim().des
+= dim
.height();
486 pm
.dim().asc
+= pm
.rows()[0].ascent();
487 pm
.dim().des
-= pm
.rows()[0].ascent();
489 changed
|= old_dim
.height() != pm
.dim().height();
495 void TextMetrics::computeRowMetrics(pit_type
const pit
,
496 Row
& row
, int width
) const
501 Buffer
& buffer
= bv_
->buffer();
502 Paragraph
const & par
= text_
->getPar(pit
);
504 double w
= width
- row
.width();
505 // FIXME: put back this assertion when the crash on new doc is solved.
506 //LASSERT(w >= 0, /**/);
508 //lyxerr << "\ndim_.wid " << dim_.wid << endl;
509 //lyxerr << "row.width() " << row.width() << endl;
510 //lyxerr << "w " << w << endl;
512 bool const is_rtl
= text_
->isRTL(buffer
, par
);
514 row
.x
= rightMargin(pit
);
516 row
.x
= leftMargin(max_width_
, pit
, row
.pos());
518 // is there a manual margin with a manual label
519 Layout
const & layout
= par
.layout();
521 if (layout
.margintype
== MARGIN_MANUAL
522 && layout
.labeltype
== LABEL_MANUAL
) {
523 /// We might have real hfills in the label part
524 int nlh
= numberOfLabelHfills(par
, row
);
526 // A manual label par (e.g. List) has an auto-hfill
527 // between the label text and the body of the
529 // But we don't want to do this auto hfill if the par
534 if (nlh
&& !par
.getLabelWidthString().empty())
535 row
.label_hfill
= labelFill(pit
, row
) / double(nlh
);
539 // are there any hfills in the row?
540 if (int const nh
= numberOfHfills(par
, row
)) {
542 hfill
= w
/ double(nh
);
543 // we don't have to look at the alignment if it is ALIGN_LEFT and
544 // if the row is already larger then the permitted width as then
545 // we force the LEFT_ALIGN'edness!
546 } else if (int(row
.width()) < max_width_
) {
547 // is it block, flushleft or flushright?
548 // set x how you need it
550 if (par
.params().align() == LYX_ALIGN_LAYOUT
)
551 align
= layout
.align
;
553 align
= par
.params().align();
555 // handle alignment inside tabular cells
556 Inset
const & owner
= par
.inInset();
557 switch (owner
.contentAlignment()) {
558 case LYX_ALIGN_CENTER
:
560 case LYX_ALIGN_RIGHT
:
561 if (align
== LYX_ALIGN_NONE
562 || align
== LYX_ALIGN_BLOCK
)
563 align
= owner
.contentAlignment();
566 // unchanged (use align)
570 // Display-style insets should always be on a centred row
571 if (Inset
const * inset
= par
.getInset(row
.pos())) {
572 switch (inset
->display()) {
573 case Inset::AlignLeft
:
574 align
= LYX_ALIGN_BLOCK
;
576 case Inset::AlignCenter
:
577 align
= LYX_ALIGN_CENTER
;
580 case Inset::AlignRight
:
581 // unchanged (use align)
587 case LYX_ALIGN_BLOCK
: {
588 int const ns
= numberOfSeparators(par
, row
);
589 bool disp_inset
= false;
590 if (row
.endpos() < par
.size()) {
591 Inset
const * in
= par
.getInset(row
.endpos());
593 disp_inset
= in
->display();
595 // If we have separators, this is not the last row of a
596 // par, does not end in newline, and is not row above a
597 // display inset... then stretch it
599 && row
.endpos() < par
.size()
600 && !par
.isNewline(row
.endpos() - 1)
603 row
.separator
= w
/ ns
;
604 //lyxerr << "row.separator " << row.separator << endl;
605 //lyxerr << "ns " << ns << endl;
611 case LYX_ALIGN_RIGHT
:
614 case LYX_ALIGN_CENTER
:
621 pos_type body_pos
= par
.beginOfBody();
622 pos_type end
= row
.endpos();
625 && (body_pos
> end
|| !par
.isLineSeparator(body_pos
- 1)))
627 row
.x
+= theFontMetrics(text_
->labelFont(buffer
, par
)).
628 width(layout
.labelsep
);
630 row
.x
+= row
.label_hfill
;
634 pos_type
const endpos
= row
.endpos();
635 pos_type body_pos
= par
.beginOfBody();
637 && (body_pos
> endpos
|| !par
.isLineSeparator(body_pos
- 1)))
640 ParagraphMetrics
& pm
= par_metrics_
[pit
];
641 InsetList::const_iterator ii
= par
.insetList().begin();
642 InsetList::const_iterator iend
= par
.insetList().end();
643 for ( ; ii
!= iend
; ++ii
) {
644 if (ii
->pos
>= endpos
|| ii
->pos
< row
.pos()
645 || (ii
->inset
->lyxCode() != SPACE_CODE
||
646 !ii
->inset
->isStretchableSpace()))
648 Dimension dim
= row
.dimension();
649 if (pm
.hfillExpansion(row
, ii
->pos
))
650 dim
.wid
= int(ii
->pos
>= body_pos
?
651 max(hfill
, 5.0) : row
.label_hfill
);
654 // Cache the inset dimension.
655 bv_
->coordCache().insets().add(ii
->inset
, dim
);
656 pm
.setInsetDimension(ii
->inset
, dim
);
661 int TextMetrics::labelFill(pit_type
const pit
, Row
const & row
) const
663 Buffer
& buffer
= bv_
->buffer();
664 Paragraph
const & par
= text_
->getPar(pit
);
666 pos_type last
= par
.beginOfBody();
667 LASSERT(last
> 0, /**/);
669 // -1 because a label ends with a space that is in the label
672 // a separator at this end does not count
673 if (par
.isLineSeparator(last
))
677 for (pos_type i
= row
.pos(); i
<= last
; ++i
)
678 w
+= singleWidth(pit
, i
);
680 docstring
const & label
= par
.params().labelWidthString();
684 FontMetrics
const & fm
685 = theFontMetrics(text_
->labelFont(buffer
, par
));
687 return max(0, fm
.width(label
) - w
);
691 // this needs special handling - only newlines count as a break point
692 static pos_type
addressBreakPoint(pos_type i
, Paragraph
const & par
)
694 pos_type
const end
= par
.size();
697 if (par
.isNewline(i
))
704 int TextMetrics::labelEnd(pit_type
const pit
) const
706 // labelEnd is only needed if the layout fills a flushleft label.
707 if (text_
->getPar(pit
).layout().margintype
!= MARGIN_MANUAL
)
709 // return the beginning of the body
710 return leftMargin(max_width_
, pit
);
716 * Calling Text::getFont is slow. While rebreaking we scan a
717 * paragraph from left to right calling getFont for every char. This
718 * simple class address this problem by hidding an optimization trick
719 * (not mine btw -AB): the font is reused in the whole font span. The
720 * class handles transparently the "hidden" (not part of the fontlist)
721 * label font (as getFont does).
727 FontIterator(TextMetrics
const & tm
,
728 Paragraph
const & par
, pit_type pit
, pos_type pos
)
729 : tm_(tm
), par_(par
), pit_(pit
), pos_(pos
),
730 font_(tm
.displayFont(pit
, pos
)),
731 endspan_(par
.fontSpan(pos
).last
),
732 bodypos_(par
.beginOfBody())
736 Font
const & operator*() const { return font_
; }
739 FontIterator
& operator++()
742 if (pos_
> endspan_
|| pos_
== bodypos_
) {
743 font_
= tm_
.displayFont(pit_
, pos_
);
744 endspan_
= par_
.fontSpan(pos_
).last
;
750 Font
* operator->() { return &font_
; }
754 TextMetrics
const & tm_
;
756 Paragraph
const & par_
;
771 pit_type
TextMetrics::rowBreakPoint(int width
, pit_type
const pit
,
774 Buffer
& buffer
= bv_
->buffer();
775 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
776 Paragraph
const & par
= text_
->getPar(pit
);
777 pos_type
const end
= par
.size();
778 if (pos
== end
|| width
< 0)
781 Layout
const & layout
= par
.layout();
783 if (layout
.margintype
== MARGIN_RIGHT_ADDRESS_BOX
)
784 return addressBreakPoint(pos
, par
);
786 pos_type
const body_pos
= par
.beginOfBody();
788 // check for possible inline completion
789 DocIterator
const & inlineCompletionPos
= bv_
->inlineCompletionPos();
790 pos_type inlineCompletionLPos
= -1;
791 if (inlineCompletionPos
.inTexted()
792 && inlineCompletionPos
.text() == text_
793 && inlineCompletionPos
.pit() == pit
) {
794 // draw logically behind the previous character
795 inlineCompletionLPos
= inlineCompletionPos
.pos() - 1;
798 // Now we iterate through until we reach the right margin
799 // or the end of the par, then choose the possible break
802 int label_end
= labelEnd(pit
);
803 int const left
= leftMargin(max_width_
, pit
, pos
);
806 // pixel width since last breakpoint
809 FontIterator fi
= FontIterator(*this, par
, pit
, pos
);
810 pos_type point
= end
;
812 for ( ; i
< end
; ++i
, ++fi
) {
813 int thiswidth
= pm
.singleWidth(i
, *fi
);
815 // add inline completion width
816 if (inlineCompletionLPos
== i
) {
817 docstring
const & completion
= bv_
->inlineCompletion();
818 if (completion
.length() > 0)
819 thiswidth
+= theFontMetrics(*fi
).width(completion
);
822 // add the auto-hfill from label end to the body
823 if (body_pos
&& i
== body_pos
) {
824 FontMetrics
const & fm
= theFontMetrics(
825 text_
->labelFont(buffer
, par
));
826 int add
= fm
.width(layout
.labelsep
);
827 if (par
.isLineSeparator(i
- 1))
828 add
-= singleWidth(pit
, i
- 1);
830 add
= max(add
, label_end
- x
);
835 chunkwidth
+= thiswidth
;
837 // break before a character that will fall off
838 // the right of the row
840 // if no break before, break here
841 if (point
== end
|| chunkwidth
>= width
- left
) {
847 // exit on last registered breakpoint:
851 if (par
.isNewline(i
)) {
855 Inset
const * inset
= 0;
858 if ((inset
= par
.getInset(i
+ 1)) && inset
->display()) {
863 if ((inset
= par
.getInset(i
)) && inset
->display()) {
869 inset
= par
.getInset(i
);
870 if (!inset
|| inset
->isChar()) {
871 // some insets are line separators too
872 if (par
.isLineSeparator(i
)) {
873 // register breakpoint:
880 // maybe found one, but the par is short enough.
881 if (i
== end
&& x
< width
)
884 // manual labels cannot be broken in LaTeX. But we
885 // want to make our on-screen rendering of footnotes
887 if (body_pos
&& point
< body_pos
)
894 int TextMetrics::rowWidth(int right_margin
, pit_type
const pit
,
895 pos_type
const first
, pos_type
const end
) const
897 Buffer
& buffer
= bv_
->buffer();
898 // get the pure distance
899 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
900 Paragraph
const & par
= text_
->getPar(pit
);
901 int w
= leftMargin(max_width_
, pit
, first
);
902 int label_end
= labelEnd(pit
);
904 // check for possible inline completion
905 DocIterator
const & inlineCompletionPos
= bv_
->inlineCompletionPos();
906 pos_type inlineCompletionLPos
= -1;
907 if (inlineCompletionPos
.inTexted()
908 && inlineCompletionPos
.text() == text_
909 && inlineCompletionPos
.pit() == pit
) {
910 // draw logically behind the previous character
911 inlineCompletionLPos
= inlineCompletionPos
.pos() - 1;
914 pos_type
const body_pos
= par
.beginOfBody();
918 FontIterator fi
= FontIterator(*this, par
, pit
, i
);
919 for ( ; i
< end
; ++i
, ++fi
) {
920 if (body_pos
> 0 && i
== body_pos
) {
921 FontMetrics
const & fm
= theFontMetrics(
922 text_
->labelFont(buffer
, par
));
923 w
+= fm
.width(par
.layout().labelsep
);
924 if (par
.isLineSeparator(i
- 1))
925 w
-= singleWidth(pit
, i
- 1);
926 w
= max(w
, label_end
);
928 w
+= pm
.singleWidth(i
, *fi
);
930 // add inline completion width
931 if (inlineCompletionLPos
== i
) {
932 docstring
const & completion
= bv_
->inlineCompletion();
933 if (completion
.length() > 0)
934 w
+= theFontMetrics(*fi
).width(completion
);
939 if (body_pos
> 0 && body_pos
>= end
) {
940 FontMetrics
const & fm
= theFontMetrics(
941 text_
->labelFont(buffer
, par
));
942 w
+= fm
.width(par
.layout().labelsep
);
943 if (end
> 0 && par
.isLineSeparator(end
- 1))
944 w
-= singleWidth(pit
, end
- 1);
945 w
= max(w
, label_end
);
948 return w
+ right_margin
;
952 Dimension
TextMetrics::rowHeight(pit_type
const pit
, pos_type
const first
,
953 pos_type
const end
, bool topBottomSpace
) const
955 Paragraph
const & par
= text_
->getPar(pit
);
956 // get the maximum ascent and the maximum descent
957 double layoutasc
= 0;
958 double layoutdesc
= 0;
959 double const dh
= defaultRowHeight();
961 // ok, let us initialize the maxasc and maxdesc value.
962 // Only the fontsize count. The other properties
963 // are taken from the layoutfont. Nicer on the screen :)
964 Layout
const & layout
= par
.layout();
966 // as max get the first character of this row then it can
967 // increase but not decrease the height. Just some point to
968 // start with so we don't have to do the assignment below too
970 Buffer
const & buffer
= bv_
->buffer();
971 Font font
= displayFont(pit
, first
);
972 FontSize
const tmpsize
= font
.fontInfo().size();
973 font
.fontInfo() = text_
->layoutFont(buffer
, pit
);
974 FontSize
const size
= font
.fontInfo().size();
975 font
.fontInfo().setSize(tmpsize
);
977 FontInfo labelfont
= text_
->labelFont(buffer
, par
);
979 FontMetrics
const & labelfont_metrics
= theFontMetrics(labelfont
);
980 FontMetrics
const & fontmetrics
= theFontMetrics(font
);
982 // these are minimum values
983 double const spacing_val
= layout
.spacing
.getValue()
984 * text_
->spacing(buffer
, par
);
985 //lyxerr << "spacing_val = " << spacing_val << endl;
986 int maxasc
= int(fontmetrics
.maxAscent() * spacing_val
);
987 int maxdesc
= int(fontmetrics
.maxDescent() * spacing_val
);
989 // insets may be taller
990 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
991 InsetList::const_iterator ii
= par
.insetList().begin();
992 InsetList::const_iterator iend
= par
.insetList().end();
993 for ( ; ii
!= iend
; ++ii
) {
994 Dimension
const & dim
= pm
.insetDimension(ii
->inset
);
995 if (ii
->pos
>= first
&& ii
->pos
< end
) {
996 maxasc
= max(maxasc
, dim
.ascent());
997 maxdesc
= max(maxdesc
, dim
.descent());
1001 // Check if any custom fonts are larger (Asger)
1002 // This is not completely correct, but we can live with the small,
1003 // cosmetic error for now.
1007 par
.highestFontInRange(first
, end
, size
);
1008 if (maxsize
> font
.fontInfo().size()) {
1009 // use standard paragraph font with the maximal size
1010 FontInfo maxfont
= font
.fontInfo();
1011 maxfont
.setSize(maxsize
);
1012 FontMetrics
const & maxfontmetrics
= theFontMetrics(maxfont
);
1013 maxasc
= max(maxasc
, maxfontmetrics
.maxAscent());
1014 maxdesc
= max(maxdesc
, maxfontmetrics
.maxDescent());
1017 // This is nicer with box insets:
1021 ParagraphList
const & pars
= text_
->paragraphs();
1023 // is it a top line?
1024 if (first
== 0 && topBottomSpace
) {
1025 BufferParams
const & bufparams
= buffer
.params();
1026 // some parskips VERY EASY IMPLEMENTATION
1027 if (bufparams
.paragraph_separation
1028 == BufferParams::ParagraphSkipSeparation
1029 && par
.ownerCode() != ERT_CODE
1030 && par
.ownerCode() != LISTINGS_CODE
1032 && ((layout
.isParagraph() && par
.getDepth() == 0)
1033 || (pars
[pit
- 1].layout().isParagraph()
1034 && pars
[pit
- 1].getDepth() == 0)))
1036 maxasc
+= bufparams
.getDefSkip().inPixels(*bv_
);
1039 if (par
.params().startOfAppendix())
1040 maxasc
+= int(3 * dh
);
1042 // This is special code for the chapter, since the label of this
1043 // layout is printed in an extra row
1044 if (layout
.counter
== "chapter"
1045 && !par
.params().labelString().empty()) {
1046 labeladdon
= int(labelfont_metrics
.maxHeight()
1047 * layout
.spacing
.getValue()
1048 * text_
->spacing(buffer
, par
));
1051 // special code for the top label
1052 if ((layout
.labeltype
== LABEL_TOP_ENVIRONMENT
1053 || layout
.labeltype
== LABEL_BIBLIO
1054 || layout
.labeltype
== LABEL_CENTERED_TOP_ENVIRONMENT
)
1055 && isFirstInSequence(pit
, pars
)
1056 && !par
.labelString().empty())
1059 labelfont_metrics
.maxHeight()
1060 * layout
.spacing
.getValue()
1061 * text_
->spacing(buffer
, par
)
1062 + (layout
.topsep
+ layout
.labelbottomsep
) * dh
);
1065 // Add the layout spaces, for example before and after
1066 // a section, or between the items of a itemize or enumerate
1069 pit_type prev
= depthHook(pit
, pars
, par
.getDepth());
1070 Paragraph
const & prevpar
= pars
[prev
];
1072 && prevpar
.layout() == layout
1073 && prevpar
.getDepth() == par
.getDepth()
1074 && prevpar
.getLabelWidthString()
1075 == par
.getLabelWidthString()) {
1076 layoutasc
= layout
.itemsep
* dh
;
1077 } else if (pit
!= 0 || first
!= 0) {
1078 if (layout
.topsep
> 0)
1079 layoutasc
= layout
.topsep
* dh
;
1082 prev
= outerHook(pit
, pars
);
1083 if (prev
!= pit_type(pars
.size())) {
1084 maxasc
+= int(pars
[prev
].layout().parsep
* dh
);
1085 } else if (pit
!= 0) {
1086 Paragraph
const & prevpar
= pars
[pit
- 1];
1087 if (prevpar
.getDepth() != 0 ||
1088 prevpar
.layout() == layout
) {
1089 maxasc
+= int(layout
.parsep
* dh
);
1094 // is it a bottom line?
1095 if (end
>= par
.size() && topBottomSpace
) {
1096 // add the layout spaces, for example before and after
1097 // a section, or between the items of a itemize or enumerate
1099 pit_type nextpit
= pit
+ 1;
1100 if (nextpit
!= pit_type(pars
.size())) {
1101 pit_type cpit
= pit
;
1105 if (pars
[cpit
].getDepth() > pars
[nextpit
].getDepth()) {
1106 usual
= pars
[cpit
].layout().bottomsep
* dh
;
1107 cpit
= depthHook(cpit
, pars
, pars
[nextpit
].getDepth());
1108 if (pars
[cpit
].layout() != pars
[nextpit
].layout()
1109 || pars
[nextpit
].getLabelWidthString() != pars
[cpit
].getLabelWidthString())
1111 unusual
= pars
[cpit
].layout().bottomsep
* dh
;
1113 layoutdesc
= max(unusual
, usual
);
1114 } else if (pars
[cpit
].getDepth() == pars
[nextpit
].getDepth()) {
1115 if (pars
[cpit
].layout() != pars
[nextpit
].layout()
1116 || pars
[nextpit
].getLabelWidthString() != pars
[cpit
].getLabelWidthString())
1117 layoutdesc
= int(pars
[cpit
].layout().bottomsep
* dh
);
1122 // incalculate the layout spaces
1123 maxasc
+= int(layoutasc
* 2 / (2 + pars
[pit
].getDepth()));
1124 maxdesc
+= int(layoutdesc
* 2 / (2 + pars
[pit
].getDepth()));
1126 // FIXME: the correct way is to do the following is to move the
1127 // following code in another method specially tailored for the
1128 // main Text. The following test is thus bogus.
1129 // Top and bottom margin of the document (only at top-level)
1130 if (main_text_
&& topBottomSpace
) {
1131 if (pit
== 0 && first
== 0)
1133 if (pit
+ 1 == pit_type(pars
.size()) &&
1134 end
== par
.size() &&
1135 !(end
> 0 && par
.isNewline(end
- 1)))
1139 return Dimension(0, maxasc
+ labeladdon
, maxdesc
);
1143 // x is an absolute screen coord
1144 // returns the column near the specified x-coordinate of the row
1145 // x is set to the real beginning of this column
1146 pos_type
TextMetrics::getColumnNearX(pit_type
const pit
,
1147 Row
const & row
, int & x
, bool & boundary
) const
1149 Buffer
const & buffer
= bv_
->buffer();
1151 /// For the main Text, it is possible that this pit is not
1152 /// yet in the CoordCache when moving cursor up.
1153 /// x Paragraph coordinate is always 0 for main text anyway.
1154 int const xo
= origin_
.x_
;
1156 Paragraph
const & par
= text_
->getPar(pit
);
1158 bidi
.computeTables(par
, buffer
, row
);
1160 pos_type vc
= row
.pos();
1161 pos_type end
= row
.endpos();
1163 Layout
const & layout
= par
.layout();
1165 bool left_side
= false;
1167 pos_type body_pos
= par
.beginOfBody();
1169 double tmpx
= row
.x
;
1170 double last_tmpx
= tmpx
;
1173 (body_pos
> end
|| !par
.isLineSeparator(body_pos
- 1)))
1176 // check for empty row
1182 while (vc
< end
&& tmpx
<= x
) {
1183 c
= bidi
.vis2log(vc
);
1185 if (body_pos
> 0 && c
== body_pos
- 1) {
1186 FontMetrics
const & fm
= theFontMetrics(
1187 text_
->labelFont(buffer
, par
));
1188 tmpx
+= row
.label_hfill
+ fm
.width(layout
.labelsep
);
1189 if (par
.isLineSeparator(body_pos
- 1))
1190 tmpx
-= singleWidth(pit
, body_pos
- 1);
1193 tmpx
+= singleWidth(pit
, c
);
1194 if (par
.isSeparator(c
) && c
>= body_pos
)
1195 tmpx
+= row
.separator
;
1199 if ((tmpx
+ last_tmpx
) / 2 > x
) {
1204 LASSERT(vc
<= end
, /**/); // This shouldn't happen.
1207 // This (rtl_support test) is not needed, but gives
1208 // some speedup if rtl_support == false
1209 bool const lastrow
= lyxrc
.rtl_support
&& row
.endpos() == par
.size();
1211 // If lastrow is false, we don't need to compute
1212 // the value of rtl.
1213 bool const rtl
= lastrow
? text_
->isRTL(buffer
, par
) : false;
1215 ((rtl
&& left_side
&& vc
== row
.pos() && x
< tmpx
- 5) ||
1216 (!rtl
&& !left_side
&& vc
== end
&& x
> tmpx
+ 5))) {
1217 if (!par
.isNewline(end
- 1))
1219 } else if (vc
== row
.pos()) {
1220 c
= bidi
.vis2log(vc
);
1221 if (bidi
.level(c
) % 2 == 1)
1224 c
= bidi
.vis2log(vc
- 1);
1225 bool const rtl
= (bidi
.level(c
) % 2 == 1);
1226 if (left_side
== rtl
) {
1228 boundary
= isRTLBoundary(pit
, c
);
1232 // I believe this code is not needed anymore (Jug 20050717)
1234 // The following code is necessary because the cursor position past
1235 // the last char in a row is logically equivalent to that before
1236 // the first char in the next row. That's why insets causing row
1237 // divisions -- Newline and display-style insets -- must be treated
1238 // specially, so cursor up/down doesn't get stuck in an air gap -- MV
1239 // Newline inset, air gap below:
1240 if (row
.pos() < end
&& c
>= end
&& par
.isNewline(end
- 1)) {
1241 if (bidi
.level(end
-1) % 2 == 0)
1242 tmpx
-= singleWidth(pit
, end
- 1);
1244 tmpx
+= singleWidth(pit
, end
- 1);
1248 // Air gap above display inset:
1249 if (row
.pos() < end
&& c
>= end
&& end
< par
.size()
1250 && par
.isInset(end
) && par
.getInset(end
)->display()) {
1253 // Air gap below display inset:
1254 if (row
.pos() < end
&& c
>= end
&& par
.isInset(end
- 1)
1255 && par
.getInset(end
- 1)->display()) {
1261 pos_type
const col
= c
- row
.pos();
1263 if (!c
|| end
== par
.size())
1266 if (c
==end
&& !par
.isLineSeparator(c
-1) && !par
.isNewline(c
-1)) {
1271 return min(col
, end
- 1 - row
.pos());
1275 pos_type
TextMetrics::x2pos(pit_type pit
, int row
, int x
) const
1277 // We play safe and use parMetrics(pit) to make sure the
1278 // ParagraphMetrics will be redone and OK to use if needed.
1279 // Otherwise we would use an empty ParagraphMetrics in
1280 // upDownInText() while in selection mode.
1281 ParagraphMetrics
const & pm
= parMetrics(pit
);
1283 LASSERT(row
< int(pm
.rows().size()), /**/);
1285 Row
const & r
= pm
.rows()[row
];
1286 return r
.pos() + getColumnNearX(pit
, r
, x
, bound
);
1290 void TextMetrics::newParMetricsDown()
1292 pair
<pit_type
, ParagraphMetrics
> const & last
= *par_metrics_
.rbegin();
1293 pit_type
const pit
= last
.first
+ 1;
1294 if (pit
== int(text_
->paragraphs().size()))
1297 // do it and update its position.
1299 par_metrics_
[pit
].setPosition(last
.second
.position()
1300 + last
.second
.descent() + par_metrics_
[pit
].ascent());
1304 void TextMetrics::newParMetricsUp()
1306 pair
<pit_type
, ParagraphMetrics
> const & first
= *par_metrics_
.begin();
1307 if (first
.first
== 0)
1310 pit_type
const pit
= first
.first
- 1;
1311 // do it and update its position.
1313 par_metrics_
[pit
].setPosition(first
.second
.position()
1314 - first
.second
.ascent() - par_metrics_
[pit
].descent());
1317 // y is screen coordinate
1318 pit_type
TextMetrics::getPitNearY(int y
)
1320 LASSERT(!text_
->paragraphs().empty(), /**/);
1321 LYXERR(Debug::DEBUG
, "y: " << y
<< " cache size: " << par_metrics_
.size());
1323 // look for highest numbered paragraph with y coordinate less than given y
1326 ParMetricsCache::const_iterator it
= par_metrics_
.begin();
1327 ParMetricsCache::const_iterator et
= par_metrics_
.end();
1328 ParMetricsCache::const_iterator last
= et
; last
--;
1330 ParagraphMetrics
const & pm
= it
->second
;
1332 if (y
< it
->second
.position() - int(pm
.ascent())) {
1333 // We are looking for a position that is before the first paragraph in
1334 // the cache (which is in priciple off-screen, that is before the
1337 // We are already at the first paragraph in the inset.
1339 // OK, this is the paragraph we are looking for.
1340 pit
= it
->first
- 1;
1345 ParagraphMetrics
const & pm_last
= par_metrics_
[last
->first
];
1347 if (y
>= last
->second
.position() + int(pm_last
.descent())) {
1348 // We are looking for a position that is after the last paragraph in
1349 // the cache (which is in priciple off-screen, that is before the
1351 pit
= last
->first
+ 1;
1352 if (pit
== int(text_
->paragraphs().size()))
1353 // We are already at the last paragraph in the inset.
1355 // OK, this is the paragraph we are looking for.
1356 newParMetricsDown();
1360 for (; it
!= et
; ++it
) {
1361 LYXERR(Debug::DEBUG
, "examining: pit: " << it
->first
1362 << " y: " << it
->second
.position());
1364 ParagraphMetrics
const & pm
= par_metrics_
[it
->first
];
1366 if (it
->first
>= pit
&& int(it
->second
.position()) - int(pm
.ascent()) <= y
) {
1368 yy
= it
->second
.position();
1372 LYXERR(Debug::DEBUG
, "found best y: " << yy
<< " for pit: " << pit
);
1378 Row
const & TextMetrics::getRowNearY(int y
, pit_type pit
) const
1380 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
1382 int yy
= pm
.position() - pm
.ascent();
1383 LASSERT(!pm
.rows().empty(), /**/);
1384 RowList::const_iterator rit
= pm
.rows().begin();
1385 RowList::const_iterator rlast
= pm
.rows().end();
1387 for (; rit
!= rlast
; yy
+= rit
->height(), ++rit
)
1388 if (yy
+ rit
->height() > y
)
1394 // x,y are absolute screen coordinates
1395 // sets cursor recursively descending into nested editable insets
1396 Inset
* TextMetrics::editXY(Cursor
& cur
, int x
, int y
)
1398 if (lyxerr
.debugging(Debug::WORKAREA
)) {
1399 LYXERR0("TextMetrics::editXY(cur, " << x
<< ", " << y
<< ")");
1400 cur
.bv().coordCache().dump();
1402 pit_type pit
= getPitNearY(y
);
1403 LASSERT(pit
!= -1, /**/);
1405 Row
const & row
= getRowNearY(y
, pit
);
1408 int xx
= x
; // is modified by getColumnNearX
1409 pos_type
const pos
= row
.pos()
1410 + getColumnNearX(pit
, row
, xx
, bound
);
1413 cur
.boundary(bound
);
1416 // try to descend into nested insets
1417 Inset
* inset
= checkInsetHit(x
, y
);
1418 //lyxerr << "inset " << inset << " hit at x: " << x << " y: " << y << endl;
1420 // Either we deconst editXY or better we move current_font
1421 // and real_current_font to Cursor
1422 // FIXME: what is needed now that current_font and real_current_font
1424 cur
.setCurrentFont();
1428 ParagraphList
const & pars
= text_
->paragraphs();
1429 Inset
const * insetBefore
= pos
? pars
[pit
].getInset(pos
- 1): 0;
1430 //Inset * insetBehind = pars[pit].getInset(pos);
1432 // This should be just before or just behind the
1433 // cursor position set above.
1434 LASSERT((pos
!= 0 && inset
== insetBefore
)
1435 || inset
== pars
[pit
].getInset(pos
), /**/);
1437 // Make sure the cursor points to the position before
1439 if (inset
== insetBefore
) {
1441 cur
.boundary(false);
1444 // Try to descend recursively inside the inset.
1445 inset
= inset
->editXY(cur
, x
, y
);
1447 if (cur
.top().text() == text_
)
1448 cur
.setCurrentFont();
1453 void TextMetrics::setCursorFromCoordinates(Cursor
& cur
, int const x
, int const y
)
1455 LASSERT(text_
== cur
.text(), /**/);
1456 pit_type pit
= getPitNearY(y
);
1458 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
1460 int yy
= pm
.position() - pm
.ascent();
1461 LYXERR(Debug::DEBUG
, "x: " << x
<< " y: " << y
<<
1462 " pit: " << pit
<< " yy: " << yy
);
1465 LASSERT(pm
.rows().size(), /**/);
1466 for (; r
< int(pm
.rows().size()) - 1; ++r
) {
1467 Row
const & row
= pm
.rows()[r
];
1468 if (int(yy
+ row
.height()) > y
)
1473 Row
const & row
= pm
.rows()[r
];
1475 LYXERR(Debug::DEBUG
, "row " << r
<< " from pos: " << row
.pos());
1479 pos_type
const pos
= row
.pos() + getColumnNearX(pit
, row
, xx
, bound
);
1481 LYXERR(Debug::DEBUG
, "setting cursor pit: " << pit
<< " pos: " << pos
);
1483 text_
->setCursor(cur
, pit
, pos
, true, bound
);
1484 // remember new position.
1489 //takes screen x,y coordinates
1490 Inset
* TextMetrics::checkInsetHit(int x
, int y
)
1492 pit_type pit
= getPitNearY(y
);
1493 LASSERT(pit
!= -1, /**/);
1495 Paragraph
const & par
= text_
->paragraphs()[pit
];
1496 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
1498 LYXERR(Debug::DEBUG
, "x: " << x
<< " y: " << y
<< " pit: " << pit
);
1500 InsetList::const_iterator iit
= par
.insetList().begin();
1501 InsetList::const_iterator iend
= par
.insetList().end();
1502 for (; iit
!= iend
; ++iit
) {
1503 Inset
* inset
= iit
->inset
;
1505 LYXERR(Debug::DEBUG
, "examining inset " << inset
);
1507 if (!bv_
->coordCache().getInsets().has(inset
)) {
1508 LYXERR(Debug::DEBUG
, "inset has no cached position");
1512 Dimension
const & dim
= pm
.insetDimension(inset
);
1513 Point p
= bv_
->coordCache().getInsets().xy(inset
);
1515 LYXERR(Debug::DEBUG
, "xo: " << p
.x_
<< "..." << p
.x_
+ dim
.wid
1516 << " yo: " << p
.y_
- dim
.asc
<< "..." << p
.y_
+ dim
.des
);
1519 && x
<= p
.x_
+ dim
.wid
1520 && y
>= p
.y_
- dim
.asc
1521 && y
<= p
.y_
+ dim
.des
) {
1522 LYXERR(Debug::DEBUG
, "Hit inset: " << inset
);
1527 LYXERR(Debug::DEBUG
, "No inset hit. ");
1532 int TextMetrics::cursorX(CursorSlice
const & sl
,
1533 bool boundary
) const
1535 LASSERT(sl
.text() == text_
, /**/);
1536 pit_type
const pit
= sl
.pit();
1537 Paragraph
const & par
= text_
->paragraphs()[pit
];
1538 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
1539 if (pm
.rows().empty())
1542 pos_type ppos
= sl
.pos();
1543 // Correct position in front of big insets
1544 bool const boundary_correction
= ppos
!= 0 && boundary
;
1545 if (boundary_correction
)
1548 Row
const & row
= pm
.getRow(sl
.pos(), boundary
);
1550 pos_type cursor_vpos
= 0;
1552 Buffer
const & buffer
= bv_
->buffer();
1555 bidi
.computeTables(par
, buffer
, row
);
1557 pos_type
const row_pos
= row
.pos();
1558 pos_type
const end
= row
.endpos();
1559 // Spaces at logical line breaks in bidi text must be skipped during
1560 // cursor positioning. However, they may appear visually in the middle
1561 // of a row; they must be skipped, wherever they are...
1562 // * logically "abc_[HEBREW_\nHEBREW]"
1563 // * visually "abc_[_WERBEH\nWERBEH]"
1564 pos_type skipped_sep_vpos
= -1;
1567 cursor_vpos
= row_pos
;
1568 else if (ppos
>= end
)
1569 cursor_vpos
= text_
->isRTL(buffer
, par
) ? row_pos
: end
;
1570 else if (ppos
> row_pos
&& ppos
>= end
)
1571 // Place cursor after char at (logical) position pos - 1
1572 cursor_vpos
= (bidi
.level(ppos
- 1) % 2 == 0)
1573 ? bidi
.log2vis(ppos
- 1) + 1 : bidi
.log2vis(ppos
- 1);
1575 // Place cursor before char at (logical) position ppos
1576 cursor_vpos
= (bidi
.level(ppos
) % 2 == 0)
1577 ? bidi
.log2vis(ppos
) : bidi
.log2vis(ppos
) + 1;
1579 pos_type body_pos
= par
.beginOfBody();
1581 (body_pos
> end
|| !par
.isLineSeparator(body_pos
- 1)))
1584 // check for possible inline completion in this row
1585 DocIterator
const & inlineCompletionPos
= bv_
->inlineCompletionPos();
1586 pos_type inlineCompletionVPos
= -1;
1587 if (inlineCompletionPos
.inTexted()
1588 && inlineCompletionPos
.text() == text_
1589 && inlineCompletionPos
.pit() == pit
1590 && inlineCompletionPos
.pos() - 1 >= row_pos
1591 && inlineCompletionPos
.pos() - 1 < end
) {
1592 // draw logically behind the previous character
1593 inlineCompletionVPos
= bidi
.log2vis(inlineCompletionPos
.pos() - 1);
1596 // Use font span to speed things up, see below
1600 // If the last logical character is a separator, skip it, unless
1601 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
1602 if (end
> 0 && end
< par
.size() && par
.isSeparator(end
- 1))
1603 skipped_sep_vpos
= bidi
.log2vis(end
- 1);
1605 // Inline completion RTL special case row_pos == cursor_pos:
1606 // "__|b" => cursor_pos is right of __
1607 if (row_pos
== inlineCompletionVPos
&& row_pos
== cursor_vpos
) {
1608 font
= displayFont(pit
, row_pos
+ 1);
1609 docstring
const & completion
= bv_
->inlineCompletion();
1610 if (font
.isRightToLeft() && completion
.length() > 0)
1611 x
+= theFontMetrics(font
.fontInfo()).width(completion
);
1614 for (pos_type vpos
= row_pos
; vpos
< cursor_vpos
; ++vpos
) {
1615 // Skip the separator which is at the logical end of the row
1616 if (vpos
== skipped_sep_vpos
)
1618 pos_type pos
= bidi
.vis2log(vpos
);
1619 if (body_pos
> 0 && pos
== body_pos
- 1) {
1620 FontMetrics
const & labelfm
= theFontMetrics(
1621 text_
->labelFont(buffer
, par
));
1622 x
+= row
.label_hfill
+ labelfm
.width(par
.layout().labelsep
);
1623 if (par
.isLineSeparator(body_pos
- 1))
1624 x
-= singleWidth(pit
, body_pos
- 1);
1627 // Use font span to speed things up, see above
1628 if (pos
< font_span
.first
|| pos
> font_span
.last
) {
1629 font_span
= par
.fontSpan(pos
);
1630 font
= displayFont(pit
, pos
);
1633 x
+= pm
.singleWidth(pos
, font
);
1635 // Inline completion RTL case:
1636 // "a__|b", __ of b => non-boundary a-pos is right of __
1637 if (vpos
+ 1 == inlineCompletionVPos
1638 && (vpos
+ 1 < cursor_vpos
|| !boundary_correction
)) {
1639 font
= displayFont(pit
, vpos
+ 1);
1640 docstring
const & completion
= bv_
->inlineCompletion();
1641 if (font
.isRightToLeft() && completion
.length() > 0)
1642 x
+= theFontMetrics(font
.fontInfo()).width(completion
);
1645 // Inline completion LTR case:
1646 // "b|__a", __ of b => non-boundary a-pos is in front of __
1647 if (vpos
== inlineCompletionVPos
1648 && (vpos
+ 1 < cursor_vpos
|| boundary_correction
)) {
1649 font
= displayFont(pit
, vpos
);
1650 docstring
const & completion
= bv_
->inlineCompletion();
1651 if (!font
.isRightToLeft() && completion
.length() > 0)
1652 x
+= theFontMetrics(font
.fontInfo()).width(completion
);
1655 if (par
.isSeparator(pos
) && pos
>= body_pos
)
1659 // see correction above
1660 if (boundary_correction
) {
1661 if (isRTL(sl
, boundary
))
1662 x
-= singleWidth(pit
, ppos
);
1664 x
+= singleWidth(pit
, ppos
);
1671 int TextMetrics::cursorY(CursorSlice
const & sl
, bool boundary
) const
1673 //lyxerr << "TextMetrics::cursorY: boundary: " << boundary << endl;
1674 ParagraphMetrics
const & pm
= par_metrics_
[sl
.pit()];
1675 if (pm
.rows().empty())
1679 h
-= par_metrics_
[0].rows()[0].ascent();
1680 for (pit_type pit
= 0; pit
< sl
.pit(); ++pit
) {
1681 h
+= par_metrics_
[pit
].height();
1684 if (pos
&& boundary
)
1686 size_t const rend
= pm
.pos2row(pos
);
1687 for (size_t rit
= 0; rit
!= rend
; ++rit
)
1688 h
+= pm
.rows()[rit
].height();
1689 h
+= pm
.rows()[rend
].ascent();
1694 // the cursor set functions have a special mechanism. When they
1695 // realize you left an empty paragraph, they will delete it.
1697 bool TextMetrics::cursorHome(Cursor
& cur
)
1699 LASSERT(text_
== cur
.text(), /**/);
1700 ParagraphMetrics
const & pm
= par_metrics_
[cur
.pit()];
1701 Row
const & row
= pm
.getRow(cur
.pos(),cur
.boundary());
1702 return text_
->setCursor(cur
, cur
.pit(), row
.pos());
1706 bool TextMetrics::cursorEnd(Cursor
& cur
)
1708 LASSERT(text_
== cur
.text(), /**/);
1709 // if not on the last row of the par, put the cursor before
1710 // the final space exept if I have a spanning inset or one string
1711 // is so long that we force a break.
1712 pos_type end
= cur
.textRow().endpos();
1714 // empty text, end-1 is no valid position
1716 bool boundary
= false;
1717 if (end
!= cur
.lastpos()) {
1718 if (!cur
.paragraph().isLineSeparator(end
-1)
1719 && !cur
.paragraph().isNewline(end
-1))
1724 return text_
->setCursor(cur
, cur
.pit(), end
, true, boundary
);
1728 void TextMetrics::deleteLineForward(Cursor
& cur
)
1730 LASSERT(text_
== cur
.text(), /**/);
1731 if (cur
.lastpos() == 0) {
1732 // Paragraph is empty, so we just go forward
1733 text_
->cursorForward(cur
);
1736 cur
.selection() = true; // to avoid deletion
1739 // What is this test for ??? (JMarc)
1740 if (!cur
.selection())
1741 text_
->deleteWordForward(cur
);
1743 cap::cutSelection(cur
, true, false);
1744 cur
.checkBufferStructure();
1749 bool TextMetrics::isLastRow(pit_type pit
, Row
const & row
) const
1751 ParagraphList
const & pars
= text_
->paragraphs();
1752 return row
.endpos() >= pars
[pit
].size()
1753 && pit
+ 1 == pit_type(pars
.size());
1757 bool TextMetrics::isFirstRow(pit_type pit
, Row
const & row
) const
1759 return row
.pos() == 0 && pit
== 0;
1763 int TextMetrics::leftMargin(int max_width
, pit_type pit
) const
1765 LASSERT(pit
>= 0, /**/);
1766 LASSERT(pit
< int(text_
->paragraphs().size()), /**/);
1767 return leftMargin(max_width
, pit
, text_
->paragraphs()[pit
].size());
1771 int TextMetrics::leftMargin(int max_width
,
1772 pit_type
const pit
, pos_type
const pos
) const
1774 ParagraphList
const & pars
= text_
->paragraphs();
1776 LASSERT(pit
>= 0, /**/);
1777 LASSERT(pit
< int(pars
.size()), /**/);
1778 Paragraph
const & par
= pars
[pit
];
1779 LASSERT(pos
>= 0, /**/);
1780 LASSERT(pos
<= par
.size(), /**/);
1781 Buffer
const & buffer
= bv_
->buffer();
1782 //lyxerr << "TextMetrics::leftMargin: pit: " << pit << " pos: " << pos << endl;
1783 DocumentClass
const & tclass
= buffer
.params().documentClass();
1784 Layout
const & layout
= par
.layout();
1786 docstring parindent
= layout
.parindent
;
1790 if (text_
->isMainText(buffer
))
1791 l_margin
+= bv_
->leftMargin();
1793 l_margin
+= theFontMetrics(buffer
.params().getFont()).signedWidth(
1794 tclass
.leftmargin());
1796 if (par
.getDepth() != 0) {
1797 // find the next level paragraph
1798 pit_type newpar
= outerHook(pit
, pars
);
1799 if (newpar
!= pit_type(pars
.size())) {
1800 if (pars
[newpar
].layout().isEnvironment()) {
1801 l_margin
= leftMargin(max_width
, newpar
);
1803 if (tclass
.isDefaultLayout(par
.layout())
1804 || tclass
.isPlainLayout(par
.layout())) {
1805 if (pars
[newpar
].params().noindent())
1808 parindent
= pars
[newpar
].layout().parindent
;
1813 // This happens after sections in standard classes. The 1.3.x
1814 // code compared depths too, but it does not seem necessary
1816 if (tclass
.isDefaultLayout(par
.layout())
1817 && pit
> 0 && pars
[pit
- 1].layout().nextnoindent
)
1820 FontInfo
const labelfont
= text_
->labelFont(buffer
, par
);
1821 FontMetrics
const & labelfont_metrics
= theFontMetrics(labelfont
);
1823 switch (layout
.margintype
) {
1824 case MARGIN_DYNAMIC
:
1825 if (!layout
.leftmargin
.empty()) {
1826 l_margin
+= theFontMetrics(buffer
.params().getFont()).signedWidth(
1829 if (!par
.labelString().empty()) {
1830 l_margin
+= labelfont_metrics
.signedWidth(layout
.labelindent
);
1831 l_margin
+= labelfont_metrics
.width(par
.labelString());
1832 l_margin
+= labelfont_metrics
.width(layout
.labelsep
);
1836 case MARGIN_MANUAL
: {
1837 l_margin
+= labelfont_metrics
.signedWidth(layout
.labelindent
);
1838 // The width of an empty par, even with manual label, should be 0
1839 if (!par
.empty() && pos
>= par
.beginOfBody()) {
1840 if (!par
.getLabelWidthString().empty()) {
1841 docstring labstr
= par
.getLabelWidthString();
1842 l_margin
+= labelfont_metrics
.width(labstr
);
1843 l_margin
+= labelfont_metrics
.width(layout
.labelsep
);
1849 case MARGIN_STATIC
: {
1850 l_margin
+= theFontMetrics(buffer
.params().getFont()).
1851 signedWidth(layout
.leftmargin
) * 4 / (par
.getDepth() + 4);
1855 case MARGIN_FIRST_DYNAMIC
:
1856 if (layout
.labeltype
== LABEL_MANUAL
) {
1857 if (pos
>= par
.beginOfBody()) {
1858 l_margin
+= labelfont_metrics
.signedWidth(layout
.leftmargin
);
1860 l_margin
+= labelfont_metrics
.signedWidth(layout
.labelindent
);
1863 // Special case to fix problems with
1865 || (layout
.labeltype
== LABEL_STATIC
1866 && layout
.latextype
== LATEX_ENVIRONMENT
1867 && !isFirstInSequence(pit
, pars
))) {
1868 l_margin
+= labelfont_metrics
.signedWidth(layout
.leftmargin
);
1869 } else if (layout
.labeltype
!= LABEL_TOP_ENVIRONMENT
1870 && layout
.labeltype
!= LABEL_BIBLIO
1871 && layout
.labeltype
!=
1872 LABEL_CENTERED_TOP_ENVIRONMENT
) {
1873 l_margin
+= labelfont_metrics
.signedWidth(layout
.labelindent
);
1874 l_margin
+= labelfont_metrics
.width(layout
.labelsep
);
1875 l_margin
+= labelfont_metrics
.width(par
.labelString());
1879 case MARGIN_RIGHT_ADDRESS_BOX
: {
1881 // ok, a terrible hack. The left margin depends on the widest
1882 // row in this paragraph.
1883 RowList::iterator rit
= par
.rows().begin();
1884 RowList::iterator end
= par
.rows().end();
1885 // FIXME: This is wrong.
1886 int minfill
= max_width
;
1887 for ( ; rit
!= end
; ++rit
)
1888 if (rit
->fill() < minfill
)
1889 minfill
= rit
->fill();
1890 l_margin
+= theFontMetrics(params
.getFont()).signedWidth(layout
.leftmargin
);
1891 l_margin
+= minfill
;
1893 // also wrong, but much shorter.
1894 l_margin
+= max_width
/ 2;
1899 if (!par
.params().leftIndent().zero())
1900 l_margin
+= par
.params().leftIndent().inPixels(max_width
);
1904 if (par
.params().align() == LYX_ALIGN_LAYOUT
)
1905 align
= layout
.align
;
1907 align
= par
.params().align();
1909 // set the correct parindent
1911 && (layout
.labeltype
== LABEL_NO_LABEL
1912 || layout
.labeltype
== LABEL_TOP_ENVIRONMENT
1913 || layout
.labeltype
== LABEL_CENTERED_TOP_ENVIRONMENT
1914 || (layout
.labeltype
== LABEL_STATIC
1915 && layout
.latextype
== LATEX_ENVIRONMENT
1916 && !isFirstInSequence(pit
, pars
)))
1917 && align
== LYX_ALIGN_BLOCK
1918 && !par
.params().noindent()
1919 // in some insets, paragraphs are never indented
1920 && !par
.inInset().neverIndent()
1921 // display style insets are always centered, omit indentation
1924 && par
.getInset(pos
)->display())
1925 && (!(tclass
.isDefaultLayout(par
.layout())
1926 || tclass
.isPlainLayout(par
.layout()))
1927 || buffer
.params().paragraph_separation
== BufferParams::ParagraphIndentSeparation
)
1930 l_margin
+= theFontMetrics(buffer
.params().getFont()).signedWidth(
1938 int TextMetrics::singleWidth(pit_type pit
, pos_type pos
) const
1940 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
1942 return pm
.singleWidth(pos
, displayFont(pit
, pos
));
1946 void TextMetrics::draw(PainterInfo
& pi
, int x
, int y
) const
1948 if (par_metrics_
.empty())
1954 ParMetricsCache::iterator it
= par_metrics_
.begin();
1955 ParMetricsCache::iterator
const pm_end
= par_metrics_
.end();
1956 y
-= it
->second
.ascent();
1957 for (; it
!= pm_end
; ++it
) {
1958 ParagraphMetrics
const & pmi
= it
->second
;
1960 pit_type
const pit
= it
->first
;
1961 // Save the paragraph position in the cache.
1962 it
->second
.setPosition(y
);
1963 drawParagraph(pi
, pit
, x
, y
);
1969 void TextMetrics::drawParagraph(PainterInfo
& pi
, pit_type pit
, int x
, int y
) const
1971 BufferParams
const & bparams
= bv_
->buffer().params();
1972 ParagraphMetrics
const & pm
= par_metrics_
[pit
];
1973 if (pm
.rows().empty())
1977 bool const original_drawing_state
= pi
.pain
.isDrawingEnabled();
1978 int const ww
= bv_
->workHeight();
1979 size_t const nrows
= pm
.rows().size();
1981 Cursor
const & cur
= bv_
->cursor();
1982 DocIterator sel_beg
= cur
.selectionBegin();
1983 DocIterator sel_end
= cur
.selectionEnd();
1984 bool selection
= cur
.selection()
1985 // This is our text.
1986 && cur
.text() == text_
1987 // if the anchor is outside, this is not our selection
1988 && cur
.anchor().text() == text_
1989 && pit
>= sel_beg
.pit() && pit
<= sel_end
.pit();
1991 // We store the begin and end pos of the selection relative to this par
1992 DocIterator sel_beg_par
= cur
.selectionBegin();
1993 DocIterator sel_end_par
= cur
.selectionEnd();
1995 // We care only about visible selection.
1997 if (pit
!= sel_beg
.pit()) {
1998 sel_beg_par
.pit() = pit
;
1999 sel_beg_par
.pos() = 0;
2001 if (pit
!= sel_end
.pit()) {
2002 sel_end_par
.pit() = pit
;
2003 sel_end_par
.pos() = sel_end_par
.lastpos();
2007 for (size_t i
= 0; i
!= nrows
; ++i
) {
2009 Row
const & row
= pm
.rows()[i
];
2013 bool const inside
= (y
+ row
.descent() >= 0
2014 && y
- row
.ascent() < ww
);
2015 // It is not needed to draw on screen if we are not inside.
2016 pi
.pain
.setDrawingEnabled(inside
&& original_drawing_state
);
2017 RowPainter
rp(pi
, *text_
, pit
, row
, bidi
, x
, y
);
2020 row
.setSelectionAndMargins(sel_beg_par
, sel_end_par
);
2022 row
.setSelection(-1, -1);
2024 // The row knows nothing about the paragraph, so we have to check
2025 // whether this row is the first or last and update the margins.
2026 if (row
.selection()) {
2027 if (row
.sel_beg
== 0)
2028 row
.left_margin_sel
= sel_beg
.pit() < pit
;
2029 if (row
.sel_end
== sel_end_par
.lastpos())
2030 row
.right_margin_sel
= sel_end
.pit() > pit
;
2033 // Row signature; has row changed since last paint?
2034 row
.setCrc(pm
.computeRowSignature(row
, bparams
));
2035 bool row_has_changed
= row
.changed();
2037 // Don't paint the row if a full repaint has not been requested
2038 // and if it has not changed.
2039 if (!pi
.full_repaint
&& !row_has_changed
) {
2040 // Paint only the insets if the text itself is
2042 rp
.paintOnlyInsets();
2047 // Clear background of this row if paragraph background was not
2048 // already cleared because of a full repaint.
2049 if (!pi
.full_repaint
&& row_has_changed
) {
2050 pi
.pain
.fillRectangle(x
, y
- row
.ascent(),
2051 width(), row
.height(), pi
.background_color
);
2054 if (row
.selection())
2055 drawRowSelection(pi
, x
, row
, cur
, pit
);
2057 // Instrumentation for testing row cache (see also
2059 if (lyxerr
.debugging(Debug::PAINTING
) && inside
2060 && (row
.selection() || pi
.full_repaint
|| row_has_changed
)) {
2061 string
const foreword
= text_
->isMainText(bv_
->buffer()) ?
2062 "main text redraw " : "inset text redraw: ";
2063 LYXERR(Debug::PAINTING
, foreword
<< "pit=" << pit
<< " row=" << i
2064 << " row_selection=" << row
.selection()
2065 << " full_repaint=" << pi
.full_repaint
2066 << " row_has_changed=" << row_has_changed
);
2069 // Backup full_repaint status and force full repaint
2070 // for inner insets as the Row has been cleared out.
2071 bool tmp
= pi
.full_repaint
;
2072 pi
.full_repaint
= true;
2075 rp
.paintChangeBar();
2082 // Restore full_repaint status.
2083 pi
.full_repaint
= tmp
;
2085 // Re-enable screen drawing for future use of the painter.
2086 pi
.pain
.setDrawingEnabled(original_drawing_state
);
2088 //LYXERR(Debug::PAINTING, ".");
2092 void TextMetrics::drawRowSelection(PainterInfo
& pi
, int x
, Row
const & row
,
2093 Cursor
const & curs
, pit_type pit
) const
2095 DocIterator beg
= curs
.selectionBegin();
2097 beg
.pos() = row
.sel_beg
;
2099 DocIterator end
= curs
.selectionEnd();
2101 end
.pos() = row
.sel_end
;
2103 bool const begin_boundary
= beg
.pos() >= row
.endpos();
2104 bool const end_boundary
= row
.sel_end
== row
.endpos();
2106 Buffer
& buffer
= bv_
->buffer();
2107 DocIterator cur
= beg
;
2108 cur
.boundary(begin_boundary
);
2109 int x1
= cursorX(beg
.top(), begin_boundary
);
2110 int x2
= cursorX(end
.top(), end_boundary
);
2111 int y1
= bv_
->getPos(cur
, cur
.boundary()).y_
- row
.ascent();
2112 int y2
= y1
+ row
.height();
2115 if (row
.left_margin_sel
) {
2116 if (text_
->isRTL(buffer
, beg
.paragraph())) {
2117 int const w
= width() - bv_
->leftMargin() - x1
;
2118 pi
.pain
.fillRectangle(x
+ x1
, y1
, w
, y2
- y1
, Color_selection
);
2120 int const rm
= bv_
->rightMargin();
2121 pi
.pain
.fillRectangle(rm
, y1
, x1
- rm
, y2
- y1
, Color_selection
);
2125 if (row
.right_margin_sel
) {
2126 if (text_
->isRTL(buffer
, beg
.paragraph())) {
2127 int rm
= bv_
->rightMargin();
2128 pi
.pain
.fillRectangle(x
+ rm
, y1
, x2
- rm
, y2
- y1
, Color_selection
);
2130 int lm
= bv_
->leftMargin();
2131 pi
.pain
.fillRectangle(x
+ x2
, y1
, width() - lm
- x2
, y2
- y1
, Color_selection
);
2135 // if we are on a boundary from the beginning, it's probably
2136 // a RTL boundary and we jump to the other side directly as this
2137 // segement is 0-size and confuses the logic below
2139 cur
.boundary(false);
2141 // go through row and draw from RTL boundary to RTL boundary
2143 bool drawNow
= false;
2145 // simplified cursorForward code below which does not
2146 // descend into insets and which does not go into the
2147 // next line. Compare the logic with the original cursorForward
2149 // if left of boundary -> just jump to right side
2150 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
2151 if (cur
.boundary()) {
2152 cur
.boundary(false);
2153 } else if (isRTLBoundary(cur
.pit(), cur
.pos() + 1)) {
2154 // in front of RTL boundary -> Stay on this side of the boundary because:
2155 // ab|cDDEEFFghi -> abc|DDEEFFghi
2164 if (cur
.pos() == row
.endpos())
2169 // the previous segment was just drawn, now the next starts
2170 x1
= cursorX(cur
.top(), cur
.boundary());
2173 if (!(cur
< end
) || drawNow
) {
2174 x2
= cursorX(cur
.top(), cur
.boundary());
2175 pi
.pain
.fillRectangle(x
+ min(x1
,x2
), y1
, abs(x2
- x1
), y2
- y1
,
2178 // reset x1, so it is set again next round (which will be on the
2179 // right side of a boundary or at the selection end)
2186 void TextMetrics::completionPosAndDim(Cursor
const & cur
, int & x
, int & y
,
2187 Dimension
& dim
) const
2189 Cursor
const & bvcur
= cur
.bv().cursor();
2191 // get word in front of cursor
2192 docstring word
= text_
->previousWord(bvcur
.top());
2193 DocIterator wordStart
= bvcur
;
2194 wordStart
.pos() -= word
.length();
2196 // get position on screen of the word start and end
2197 Point lxy
= cur
.bv().getPos(wordStart
, false);
2198 Point rxy
= cur
.bv().getPos(bvcur
, bvcur
.boundary());
2200 // calculate dimensions of the word
2201 dim
= rowHeight(bvcur
.pit(), wordStart
.pos(), bvcur
.pos(), false);
2202 dim
.wid
= abs(rxy
.x_
- lxy
.x_
);
2204 // calculate position of word
2206 x
= min(rxy
.x_
, lxy
.x_
);
2208 //lyxerr << "wid=" << dim.width() << " x=" << x << " y=" << y << " lxy.x_=" << lxy.x_ << " rxy.x_=" << rxy.x_ << " word=" << word << std::endl;
2209 //lyxerr << " wordstart=" << wordStart << " bvcur=" << bvcur << " cur=" << cur << std::endl;
2212 //int TextMetrics::pos2x(pit_type pit, pos_type pos) const
2214 // ParagraphMetrics const & pm = par_metrics_[pit];
2215 // Row const & r = pm.rows()[row];
2221 int defaultRowHeight()
2223 return int(theFontMetrics(sane_font
).maxHeight() * 1.2);