]> git.lyx.org Git - lyx.git/blob - src/rowpainter.cpp
some de-boostification
[lyx.git] / src / rowpainter.cpp
1 /**
2  * \file rowpainter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "rowpainter.h"
15
16 #include "Bidi.h"
17 #include "Buffer.h"
18 #include "CoordCache.h"
19 #include "Cursor.h"
20 #include "support/debug.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Changes.h"
24 #include "Encoding.h"
25 #include "support/gettext.h"
26 #include "Language.h"
27 #include "Layout.h"
28 #include "LyXRC.h"
29 #include "Row.h"
30 #include "MetricsInfo.h"
31 #include "Paragraph.h"
32 #include "ParagraphMetrics.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "TextMetrics.h"
36 #include "VSpace.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include "insets/InsetText.h"
42
43 #include "support/textutils.h"
44
45 #include <boost/crc.hpp>
46
47 #include <ostream>
48
49 using std::endl;
50 using std::max;
51 using std::string;
52
53
54 namespace lyx {
55
56 using frontend::Painter;
57 using frontend::FontMetrics;
58
59 RowPainter::RowPainter(PainterInfo & pi,
60         Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
61         : pi_(pi), text_(text),
62           text_metrics_(pi_.base.bv->textMetrics(&text)),
63           pars_(text.paragraphs()),
64           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
65           pm_(text_metrics_.parMetrics(pit)),
66           bidi_(bidi), erased_(pi_.erased_),
67           xo_(x), yo_(y), width_(text_metrics_.width())
68 {
69         bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
70         x_ = row_.x + xo_;
71
72         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
73         //row_.dump();
74
75         BOOST_ASSERT(pit >= 0);
76         BOOST_ASSERT(pit < int(text.paragraphs().size()));
77 }
78
79
80 FontInfo const RowPainter::getLabelFont() const
81 {
82         return text_.getLabelFont(pi_.base.bv->buffer(), par_);
83 }
84
85
86 int RowPainter::leftMargin() const
87 {
88         return text_metrics_.leftMargin(text_metrics_.width(), pit_,
89                 row_.pos());
90 }
91
92
93 void RowPainter::paintHfill(pos_type const pos, pos_type const body_pos)
94 {
95         x_ += 1;
96
97         int const y0 = yo_;
98         int const y1 = y0 - defaultRowHeight() / 2;
99
100         pi_.pain.line(int(x_), y1, int(x_), y0, Color_added_space);
101
102         if (pm_.hfillExpansion(row_, pos)) {
103                 int const y2 = (y0 + y1) / 2;
104
105                 if (pos >= body_pos) {
106                         pi_.pain.line(int(x_), y2, int(x_ + row_.hfill), y2,
107                                 Color_added_space,
108                                 Painter::line_onoffdash);
109                         x_ += row_.hfill;
110                 } else {
111                         pi_.pain.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
112                                 Color_added_space,
113                                 Painter::line_onoffdash);
114                         x_ += row_.label_hfill;
115                 }
116                 pi_.pain.line(int(x_), y1, int(x_), y0, Color_added_space);
117         }
118         x_ += 2;
119 }
120
121
122 // If you want to debug inset metrics uncomment the following line:
123 //#define DEBUG_METRICS
124 // This draws green lines around each inset.
125
126
127 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
128 {
129         Font const font = text_metrics_.getDisplayFont(pit_, pos);
130
131         BOOST_ASSERT(inset);
132         // FIXME: We should always use font, see documentation of
133         // noFontChange() in Inset.h.
134         pi_.base.font = inset->noFontChange() ?
135                 pi_.base.bv->buffer().params().getFont().fontInfo() :
136                 font.fontInfo();
137         pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
138         pi_.erased_ = erased_ || par_.isDeleted(pos);
139         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
140         // insets are painted completely. Recursive
141         inset->drawSelection(pi_, int(x_), yo_);
142         inset->draw(pi_, int(x_), yo_);
143
144         Dimension const & dim = pm_.insetDimension(inset);
145
146         paintForeignMark(x_, font.language(), dim.descent());
147
148         x_ += dim.width();
149
150 #ifdef DEBUG_METRICS
151         int const x1 = int(x_ - dim.width());
152         Dimension dim2;
153         BOOST_ASSERT(max_witdh_ > 0);
154         int right_margin = text_metrics_.rightMargin(pm_);
155         int const w = max_witdh_ - leftMargin() - right_margin;
156         MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
157         inset->metrics(mi, dim2);
158         if (dim.wid != dim2.wid)
159                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
160                        << " draw width " << dim.width()
161                        << "> metrics width " << dim2.wid << "." << std::endl;
162         if (dim->asc != dim2.asc)
163                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
164                        << " draw ascent " << dim.ascent()
165                        << "> metrics ascent " << dim2.asc << "." << std::endl;
166         if (dim2.descent() != dim.des)
167                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
168                        << " draw ascent " << dim.descent()
169                        << "> metrics descent " << dim2.des << "." << std::endl;
170         BOOST_ASSERT(dim2.wid == dim.wid);
171         BOOST_ASSERT(dim2.asc == dim.asc);
172         BOOST_ASSERT(dim2.des == dim.des);
173         int const x2 = x1 + dim.wid;
174         int const y1 = yo_ + dim.des;
175         int const y2 = yo_ - dim.asc;
176         pi_.pain.line(x1, y1, x1, y2, Color_green);
177         pi_.pain.line(x1, y1, x2, y1, Color_green);
178         pi_.pain.line(x2, y1, x2, y2, Color_green);
179         pi_.pain.line(x1, y2, x2, y2, Color_green);
180 #endif
181 }
182
183
184 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
185 {
186         pos_type pos = bidi_.vis2log(vpos);
187
188         docstring str;
189
190         // first char
191         char_type c = par_.getChar(pos);
192         str += c;
193         ++vpos;
194
195         int const width = theFontMetrics(font).width(c);
196         int dx = 0;
197
198         for (pos_type i = pos - 1; i >= 0; --i) {
199                 c = par_.getChar(i);
200                 if (!Encodings::isComposeChar_hebrew(c)) {
201                         if (isPrintableNonspace(c)) {
202                                 int const width2 = pm_.singleWidth(i,
203                                         text_metrics_.getDisplayFont(pit_, i));
204                                 dx = (c == 0x05e8 || // resh
205                                       c == 0x05d3)   // dalet
206                                         ? width2 - width
207                                         : (width2 - width) / 2;
208                         }
209                         break;
210                 }
211         }
212
213         // Draw nikud
214         pi_.pain.text(int(x_) + dx, yo_, str, font);
215 }
216
217
218 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
219 {
220         pos_type pos = bidi_.vis2log(vpos);
221         docstring str;
222
223         // first char
224         char_type c = par_.getChar(pos);
225         c = par_.transformChar(c, pos);
226         str += c;
227         ++vpos;
228
229         int const width = theFontMetrics(font).width(c);
230         int dx = 0;
231
232         for (pos_type i = pos - 1; i >= 0; --i) {
233                 c = par_.getChar(i);
234                 if (!Encodings::isComposeChar_arabic(c)) {
235                         if (isPrintableNonspace(c)) {
236                                 int const width2 = pm_.singleWidth(i,
237                                                 text_metrics_.getDisplayFont(pit_, i));
238                                 dx = (width2 - width) / 2;
239                         }
240                         break;
241                 }
242         }
243         // Draw nikud
244         pi_.pain.text(int(x_) + dx, yo_, str, font);
245 }
246
247
248 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
249                             bool hebrew, bool arabic)
250 {
251         // This method takes up 70% of time when typing
252         pos_type pos = bidi_.vis2log(vpos);
253         pos_type const end = row_.endpos();
254         FontSpan const font_span = par_.fontSpan(pos);
255         Change::Type const prev_change = par_.lookupChange(pos).type;
256
257         // first character
258         std::vector<char_type> str;
259         str.reserve(100);
260         str.push_back(par_.getChar(pos));
261
262         if (arabic) {
263                 char_type c = str[0];
264                 if (c == '(')
265                         c = ')';
266                 else if (c == ')')
267                         c = '(';
268                 str[0] = par_.transformChar(c, pos);
269         }
270
271         // collect as much similar chars as we can
272         for (++vpos ; vpos < end ; ++vpos) {
273                 pos = bidi_.vis2log(vpos);
274                 if (pos < font_span.first || pos > font_span.last)
275                         break;
276
277                 if (prev_change != par_.lookupChange(pos).type)
278                         break;
279
280                 char_type c = par_.getChar(pos);
281
282                 if (!isPrintableNonspace(c))
283                         break;
284
285                 /* Because we do our own bidi, at this point the strings are
286                  * already in visual order. However, Qt also applies its own
287                  * bidi algorithm to strings that it paints to the screen.
288                  * Therefore, if we were to paint Hebrew/Arabic words as a
289                  * single string, the letters in the words would get reversed
290                  * again. In order to avoid that, we don't collect Hebrew/
291                  * Arabic characters, but rather paint them one at a time.
292                  * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
293                  */
294                 if (hebrew)
295                         break;
296
297                 /* FIXME: these checks are irrelevant, since 'arabic' and
298                  * 'hebrew' alone are already going to trigger a break.
299                  * However, this should not be removed completely, because
300                  * if an alternative solution is found which allows grouping
301                  * of arabic and hebrew characters, then these breaks may have
302                  * to be re-applied.
303
304                 if (arabic && Encodings::isComposeChar_arabic(c))
305                         break;
306
307                 if (hebrew && Encodings::isComposeChar_hebrew(c))
308                         break;
309                 */
310
311                 if (arabic) {
312                         if (c == '(')
313                                 c = ')';
314                         else if (c == ')')
315                                 c = '(';
316                         c = par_.transformChar(c, pos);
317                         /* see comment in hebrew, explaining why we break */
318                         break;
319                 }
320
321                 str.push_back(c);
322         }
323
324         docstring s(&str[0], str.size());
325
326         if (prev_change != Change::UNCHANGED) {
327                 FontInfo copy = font;
328                 if (prev_change == Change::DELETED) {
329                         copy.setColor(Color_deletedtext);
330                 } else if (prev_change == Change::INSERTED) {
331                         copy.setColor(Color_addedtext);
332                 }
333                 x_ += pi_.pain.text(int(x_), yo_, s, copy);
334         } else {
335                 x_ += pi_.pain.text(int(x_), yo_, s, font);
336         }
337 }
338
339
340 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
341                 int desc)
342 {
343         if (!lyxrc.mark_foreign_language)
344                 return;
345         if (lang == latex_language)
346                 return;
347         if (lang == pi_.base.bv->buffer().params().language)
348                 return;
349
350         int const y = yo_ + 1 + desc;
351         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
352 }
353
354
355 void RowPainter::paintFromPos(pos_type & vpos)
356 {
357         pos_type const pos = bidi_.vis2log(vpos);
358         Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
359         double const orig_x = x_;
360
361         // usual characters, no insets
362         char_type const c = par_.getChar(pos);
363
364         // special case languages
365         std::string const & lang = orig_font.language()->lang();
366         bool const hebrew = lang == "hebrew";
367         bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" || 
368                                                 lang == "farsi";
369
370         // draw as many chars as we can
371         if ((!hebrew && !arabic)
372                 || (hebrew && !Encodings::isComposeChar_hebrew(c))
373                 || (arabic && !Encodings::isComposeChar_arabic(c))) {
374                 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
375         } else if (hebrew) {
376                 paintHebrewComposeChar(vpos, orig_font.fontInfo());
377         } else if (arabic) {
378                 paintArabicComposeChar(vpos, orig_font.fontInfo());
379         }
380
381         paintForeignMark(orig_x, orig_font.language());
382 }
383
384
385 void RowPainter::paintChangeBar()
386 {
387         pos_type const start = row_.pos();
388         pos_type end = row_.endpos();
389
390         if (par_.size() == end) {
391                 // this is the last row of the paragraph;
392                 // thus, we must also consider the imaginary end-of-par character
393                 end++;
394         }
395
396         if (start == end || !par_.isChanged(start, end))
397                 return;
398
399         int const height = text_metrics_.isLastRow(pit_, row_)
400                 ? row_.ascent()
401                 : row_.height();
402
403         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
404 }
405
406
407 void RowPainter::paintAppendix()
408 {
409         // only draw the appendix frame once (for the main text)
410         if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
411                 return;
412
413         int y = yo_ - row_.ascent();
414
415         if (par_.params().startOfAppendix())
416                 y += 2 * defaultRowHeight();
417
418         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
419         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
420 }
421
422
423 void RowPainter::paintDepthBar()
424 {
425         depth_type const depth = par_.getDepth();
426
427         if (depth <= 0)
428                 return;
429
430         depth_type prev_depth = 0;
431         if (!text_metrics_.isFirstRow(pit_, row_)) {
432                 pit_type pit2 = pit_;
433                 if (row_.pos() == 0)
434                         --pit2;
435                 prev_depth = pars_[pit2].getDepth();
436         }
437
438         depth_type next_depth = 0;
439         if (!text_metrics_.isLastRow(pit_, row_)) {
440                 pit_type pit2 = pit_;
441                 if (row_.endpos() >= pars_[pit2].size())
442                         ++pit2;
443                 next_depth = pars_[pit2].getDepth();
444         }
445
446         for (depth_type i = 1; i <= depth; ++i) {
447                 int const w = nestMargin() / 5;
448                 int x = int(xo_) + w * i;
449                 // only consider the changebar space if we're drawing outermost text
450                 if (text_.isMainText(pi_.base.bv->buffer()))
451                         x += changebarMargin();
452
453                 int const starty = yo_ - row_.ascent();
454                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
455
456                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
457
458                 if (i > prev_depth)
459                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
460                 if (i > next_depth)
461                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
462         }
463 }
464
465
466 int RowPainter::paintAppendixStart(int y)
467 {
468         FontInfo pb_font = sane_font;
469         pb_font.setColor(Color_appendix);
470         pb_font.decSize();
471
472         int w = 0;
473         int a = 0;
474         int d = 0;
475
476         docstring const label = _("Appendix");
477         theFontMetrics(pb_font).rectText(label, w, a, d);
478
479         int const text_start = int(xo_ + (width_ - w) / 2);
480         int const text_end = text_start + w;
481
482         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
483
484         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
485         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
486
487         return 3 * defaultRowHeight();
488 }
489
490
491 void RowPainter::paintFirst()
492 {
493         ParagraphParameters const & parparams = par_.params();
494
495         int y_top = 0;
496
497         // start of appendix?
498         if (parparams.startOfAppendix())
499                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
500
501         Buffer const & buffer = pi_.base.bv->buffer();
502
503         LayoutPtr const & layout = par_.layout();
504
505         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
506                 if (pit_ != 0) {
507                         if (layout->latextype == LATEX_PARAGRAPH
508                                 && !par_.getDepth()) {
509                                 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
510                         } else {
511                                 LayoutPtr const & playout = pars_[pit_ - 1].layout();
512                                 if (playout->latextype == LATEX_PARAGRAPH
513                                         && !pars_[pit_ - 1].getDepth()) {
514                                         // is it right to use defskip here, too? (AS)
515                                         y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
516                                 }
517                         }
518                 }
519         }
520
521         bool const is_rtl = text_.isRTL(buffer, par_);
522         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
523         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
524
525         // should we print a label?
526         if (layout->labeltype >= LABEL_STATIC
527             && (layout->labeltype != LABEL_STATIC
528                       || layout->latextype != LATEX_ENVIRONMENT
529                       || is_seq)) {
530
531                 FontInfo const font = getLabelFont();
532                 FontMetrics const & fm = theFontMetrics(font);
533
534                 docstring const str = par_.getLabelstring();
535                 if (!str.empty()) {
536                         double x = x_;
537
538                         // this is special code for the chapter layout. This is
539                         // printed in an extra row and has a pagebreak at
540                         // the top.
541                         if (layout->counter == "chapter") {
542                                 double spacing_val = 1.0;
543                                 if (!parparams.spacing().isDefault()) {
544                                         spacing_val = parparams.spacing().getValue();
545                                 } else {
546                                         spacing_val = buffer.params().spacing().getValue();
547                                 }
548
549                                 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
550
551                                 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
552                                         + int(layout->parsep) * defaultRowHeight();
553
554                                 if (is_rtl) {
555                                         x = width_ - leftMargin() -
556                                                 fm.width(str);
557                                 }
558
559                                 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
560                         } else {
561                                 if (is_rtl) {
562                                         x = width_ - leftMargin()
563                                                 + fm.width(layout->labelsep);
564                                 } else {
565                                         x = x_ - fm.width(layout->labelsep)
566                                                 - fm.width(str);
567                                 }
568
569                                 pi_.pain.text(int(x), yo_, str, font);
570                         }
571                 }
572
573         // the labels at the top of an environment.
574         // More or less for bibliography
575         } else if (is_seq &&
576                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
577                 layout->labeltype == LABEL_BIBLIO ||
578                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
579                 FontInfo const font = getLabelFont();
580                 if (!par_.getLabelstring().empty()) {
581                         docstring const str = par_.getLabelstring();
582                         double spacing_val = 1.0;
583                         if (!parparams.spacing().isDefault())
584                                 spacing_val = parparams.spacing().getValue();
585                         else
586                                 spacing_val = buffer.params().spacing().getValue();
587
588                         FontMetrics const & fm = theFontMetrics(font);
589
590                         int const labeladdon = int(fm.maxHeight()
591                                 * layout->spacing.getValue() * spacing_val);
592
593                         int maxdesc =
594                                 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
595                                 + (layout->labelbottomsep * defaultRowHeight()));
596
597                         double x = x_;
598                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
599                                 if (is_rtl)
600                                         x = leftMargin();
601                                 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
602                                 x -= fm.width(str) / 2;
603                         } else if (is_rtl) {
604                                 x = width_ - leftMargin() -     fm.width(str);
605                         }
606                         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
607                 }
608         }
609 }
610
611
612 void RowPainter::paintLast()
613 {
614         bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
615         int const endlabel = getEndLabel(pit_, text_.paragraphs());
616
617         // paint imaginary end-of-paragraph character
618
619         if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
620                 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
621                 int const length = fm.maxAscent() / 2;
622                 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
623
624                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
625                            Painter::line_solid, Painter::line_thick);
626                 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
627                            Painter::line_solid, Painter::line_thick);
628         }
629
630         // draw an endlabel
631
632         switch (endlabel) {
633         case END_LABEL_BOX:
634         case END_LABEL_FILLED_BOX: {
635                 FontInfo const font = getLabelFont();
636                 FontMetrics const & fm = theFontMetrics(font);
637                 int const size = int(0.75 * fm.maxAscent());
638                 int const y = yo_ - size;
639                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
640
641                 if (width_ - int(row_.width()) <= size)
642                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
643
644                 if (endlabel == END_LABEL_BOX)
645                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
646                 else
647                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
648                 break;
649         }
650
651         case END_LABEL_STATIC: {
652                 FontInfo const font = getLabelFont();
653                 FontMetrics const & fm = theFontMetrics(font);
654                 docstring const & str = par_.layout()->endlabelstring();
655                 double const x = is_rtl ?
656                         x_ - fm.width(str)
657                         : - text_metrics_.rightMargin(pm_) - row_.width();
658                 pi_.pain.text(int(x), yo_, str, font);
659                 break;
660         }
661
662         case END_LABEL_NO_LABEL:
663                 break;
664         }
665 }
666
667
668 void RowPainter::paintOnlyInsets()
669 {
670         pos_type const end = row_.endpos();
671         for (pos_type pos = row_.pos(); pos != end; ++pos) {
672                 if (!par_.isInset(pos))
673                         continue;
674
675                 // If outer row has changed, nested insets are repaint completely.
676                 Inset const * inset = par_.getInset(pos);
677
678                 if (x_ > pi_.base.bv->workWidth())
679                         continue;
680
681                 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
682                 paintInset(inset, pos);
683         }
684 }
685
686
687 void RowPainter::paintText()
688 {
689         pos_type const end = row_.endpos();
690         // Spaces at logical line breaks in bidi text must be skipped during 
691         // painting. However, they may appear visually in the middle
692         // of a row; they must be skipped, wherever they are...
693         // * logically "abc_[HEBREW_\nHEBREW]"
694         // * visually "abc_[_WERBEH\nWERBEH]"
695         pos_type skipped_sep_vpos = -1;
696         pos_type body_pos = par_.beginOfBody();
697         if (body_pos > 0 &&
698                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
699                 body_pos = 0;
700         }
701
702         LayoutPtr const & layout = par_.layout();
703
704         bool running_strikeout = false;
705         bool is_struckout = false;
706         int last_strikeout_x = 0;
707
708         // Use font span to speed things up, see below
709         FontSpan font_span;
710         Font font;
711
712         // If the last logical character is a separator, don't paint it, unless
713         // it's in the last row of a paragraph; see skipped_sep_vpos declaration
714         if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
715                 skipped_sep_vpos = bidi_.log2vis(end - 1);
716         
717         for (pos_type vpos = row_.pos(); vpos < end; ) {
718                 if (x_ > pi_.base.bv->workWidth())
719                         break;
720
721                 // Skip the separator at the logical end of the row
722                 if (vpos == skipped_sep_vpos) {
723                         ++vpos;
724                         continue;
725                 }
726
727                 pos_type const pos = bidi_.vis2log(vpos);
728
729                 if (pos >= par_.size()) {
730                         ++vpos;
731                         continue;
732                 }
733
734                 // Use font span to speed things up, see above
735                 if (vpos < font_span.first || vpos > font_span.last) {
736                         font_span = par_.fontSpan(vpos);
737                         font = text_metrics_.getDisplayFont(pit_, vpos);
738                 }
739
740                 const int width_pos = pm_.singleWidth(pos, font);
741
742                 if (x_ + width_pos < 0) {
743                         x_ += width_pos;
744                         ++vpos;
745                         continue;
746                 }
747
748                 is_struckout = par_.isDeleted(pos);
749
750                 if (is_struckout && !running_strikeout) {
751                         running_strikeout = true;
752                         last_strikeout_x = int(x_);
753                 }
754
755                 bool const highly_editable_inset = par_.isInset(pos)
756                         && par_.getInset(pos)->editable() == Inset::HIGHLY_EDITABLE;
757
758                 // If we reach the end of a struck out range, paint it.
759                 // We also don't paint across things like tables
760                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
761                         // Calculate 1/3 height of the buffer's default font
762                         FontMetrics const & fm
763                                 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
764                         int const middle = yo_ - fm.maxAscent() / 3;
765                         pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
766                                 Color_deletedtext, Painter::line_solid, Painter::line_thin);
767                         running_strikeout = false;
768                 }
769
770                 if (body_pos > 0 && pos == body_pos - 1) {
771                         int const lwidth = theFontMetrics(getLabelFont())
772                                 .width(layout->labelsep);
773
774                         x_ += row_.label_hfill + lwidth - width_pos;
775                 }
776
777                 if (par_.isHfill(pos)) {
778                         Inset const * inset = par_.getInset(pos);
779                         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
780                         paintHfill(pos, body_pos);
781                         ++vpos;
782
783                 } else if (par_.isSeparator(pos)) {
784                         Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
785                         double const orig_x = x_;
786                         x_ += width_pos;
787                         if (pos >= body_pos)
788                                 x_ += row_.separator;
789                         paintForeignMark(orig_x, orig_font.language());
790                         ++vpos;
791
792                 } else if (par_.isInset(pos)) {
793                         // If outer row has changed, nested insets are repaint completely.
794                         Inset const * inset = par_.getInset(pos);
795                         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
796                         paintInset(inset, pos);
797                         ++vpos;
798
799                 } else {
800                         // paint as many characters as possible.
801                         paintFromPos(vpos);
802                 }
803         }
804
805         // if we reach the end of a struck out range, paint it
806         if (running_strikeout) {
807                 // calculate 1/3 height of the buffer's default font
808                 FontMetrics const & fm
809                         = theFontMetrics(pi_.base.bv->buffer().params().getFont());
810                 int const middle = yo_ - fm.maxAscent() / 3;
811                 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
812                         Color_deletedtext, Painter::line_solid, Painter::line_thin);
813                 running_strikeout = false;
814         }
815 }
816
817 } // namespace lyx