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