+
+ // don't use the pixmap cache,
+ // draw directly onto the painting device
+ setQPainterPen(computeColor(f.realColor()));
+ if (font() != ff)
+ setFont(ff);
+
+ /* In LyX, the character direction is forced by the language.
+ * Therefore, we have to signal that fact to Qt.
+ */
+#if 1
+ /* Use unicode override characters to enforce drawing direction
+ * Source: http://www.iamcal.com/understanding-bidirectional-text/
+ */
+ if (rtl)
+ // Right-to-left override: forces to draw text right-to-left
+ str = QChar(0x202E) + str;
+ else
+ // Left-to-right override: forces to draw text left-to-right
+ str = QChar(0x202D) + str;
+ drawText(x, y, str);
+#else
+ /* This looks like a cleaner solution, but it has drawbacks
+ * - does not work reliably (Mac OS X, ...)
+ * - it is not really documented
+ * Keep it here for now, in case it can be helpful
+ */
+ //This is much stronger than setLayoutDirection.
+ int flag = rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight;
+ drawText(x + (rtl ? textwidth : 0), y - fm.maxAscent(), 0, 0,
+ flag | Qt::TextDontClip,
+ str);
+#endif
+ //LYXERR(Debug::PAINTING, "draw " << string(str.toUtf8())
+ // << " at " << x << "," << y);
+ return textwidth;
+}
+
+
+int GuiPainter::text(int x, int y, docstring const & str, Font const & f,
+ double const wordspacing)
+{
+ return text(x, y, str, f.fontInfo(), f.isVisibleRightToLeft(), wordspacing);
+}
+
+
+int GuiPainter::text(int x, int y, docstring const & str, Font const & f,
+ Color other, size_type const from, size_type const to,
+ double const wordspacing)
+{
+ GuiFontMetrics const & fm = getFontMetrics(f.fontInfo());
+ FontInfo fi = f.fontInfo();
+ bool const rtl = f.isVisibleRightToLeft();
+
+ // dimensions
+ int const ascent = fm.maxAscent();
+ int const height = fm.maxAscent() + fm.maxDescent();
+ int xmin = fm.pos2x(str, from, rtl, wordspacing);
+ int xmax = fm.pos2x(str, to, rtl, wordspacing);
+ if (xmin > xmax)
+ swap(xmin, xmax);
+
+ // First the part in other color
+ Color const orig = fi.realColor();
+ fi.setPaintColor(other);
+ QRegion const clip(x + xmin, y - ascent, xmax - xmin, height);
+ setClipRegion(clip);
+ int const textwidth = text(x, y, str, fi, rtl, wordspacing);
+
+ // Then the part in normal color
+ // Note that in Qt5, it is not possible to use Qt::UniteClip,
+ // therefore QRegion is used.
+ fi.setPaintColor(orig);
+ QRegion region(viewport());
+ setClipRegion(region - clip);
+ text(x, y, str, fi, rtl, wordspacing);
+ setClipping(false);