2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-08-31 05:53:55 +00:00
|
|
|
* \file GuiPainter.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiPainter.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-05-23 08:43:07 +00:00
|
|
|
#include "ColorCache.h"
|
2006-09-26 09:57:47 +00:00
|
|
|
#include "GuiApplication.h"
|
2008-03-20 00:00:53 +00:00
|
|
|
#include "GuiFontLoader.h"
|
2006-10-27 21:27:03 +00:00
|
|
|
#include "GuiFontMetrics.h"
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiImage.h"
|
2006-08-30 14:59:07 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-06-26 16:55:35 +00:00
|
|
|
|
2014-05-23 16:59:53 +00:00
|
|
|
#include "Font.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Language.h"
|
2007-11-02 14:43:09 +00:00
|
|
|
#include "LyXRC.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-05-23 08:43:07 +00:00
|
|
|
#include "insets/Inset.h"
|
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2014-05-23 16:59:53 +00:00
|
|
|
#include "support/lstrings.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QTextLayout>
|
|
|
|
|
2007-08-17 07:14:03 +00:00
|
|
|
// Set USE_PIXMAP_CACHE to 1 for enabling the use of a Pixmap cache when
|
|
|
|
// drawing text. This is especially useful for older PPC/Mac systems.
|
2007-11-14 14:24:59 +00:00
|
|
|
#if defined(Q_WS_X11)
|
2007-08-17 07:14:03 +00:00
|
|
|
#define USE_PIXMAP_CACHE 0
|
|
|
|
#else
|
2007-08-13 17:35:09 +00:00
|
|
|
#define USE_PIXMAP_CACHE 1
|
2007-08-17 07:14:03 +00:00
|
|
|
#endif
|
2007-08-13 17:35:09 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2014-05-23 16:59:53 +00:00
|
|
|
using namespace lyx::support;
|
2007-11-07 20:44:36 +00:00
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
2010-09-07 05:14:57 +00:00
|
|
|
|
2011-03-08 15:35:24 +00:00
|
|
|
const float Painter::thin_line = 0.0;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
GuiPainter::GuiPainter(QPaintDevice * device)
|
2007-11-02 14:43:09 +00:00
|
|
|
: QPainter(device), Painter(),
|
|
|
|
use_pixmap_cache_(lyxrc.use_pixmap_cache && USE_PIXMAP_CACHE)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-06-25 11:18:56 +00:00
|
|
|
// new QPainter has default QPen:
|
2007-11-01 10:52:51 +00:00
|
|
|
current_color_ = guiApp->colorCache().get(Color_black);
|
2006-06-25 11:18:56 +00:00
|
|
|
current_ls_ = line_solid;
|
2010-09-07 05:14:57 +00:00
|
|
|
current_lw_ = thin_line;
|
2006-06-23 14:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
GuiPainter::~GuiPainter()
|
2006-06-23 14:12:17 +00:00
|
|
|
{
|
2006-10-23 08:46:09 +00:00
|
|
|
QPainter::end();
|
2007-08-31 22:16:11 +00:00
|
|
|
//lyxerr << "GuiPainter::end()" << endl;
|
2006-06-23 14:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-01 10:52:51 +00:00
|
|
|
void GuiPainter::setQPainterPen(QColor const & col,
|
2010-09-05 14:46:58 +00:00
|
|
|
Painter::line_style ls, float lw)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-06-14 14:23:25 +00:00
|
|
|
if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
|
|
|
|
return;
|
2006-06-11 21:22:36 +00:00
|
|
|
|
2006-06-14 14:23:25 +00:00
|
|
|
current_color_ = col;
|
2006-06-11 21:22:36 +00:00
|
|
|
current_ls_ = ls;
|
|
|
|
current_lw_ = lw;
|
|
|
|
|
2006-10-23 08:46:09 +00:00
|
|
|
QPen pen = QPainter::pen();
|
2007-11-01 10:52:51 +00:00
|
|
|
pen.setColor(col);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
switch (ls) {
|
|
|
|
case line_solid: pen.setStyle(Qt::SolidLine); break;
|
|
|
|
case line_onoffdash: pen.setStyle(Qt::DotLine); break;
|
|
|
|
}
|
|
|
|
|
2010-09-25 14:14:50 +00:00
|
|
|
pen.setWidthF(lw);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-23 08:46:09 +00:00
|
|
|
setPen(pen);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
|
2007-11-01 10:52:51 +00:00
|
|
|
QString GuiPainter::generateStringSignature(QString const & str, FontInfo const & f)
|
|
|
|
{
|
|
|
|
QString sig = str;
|
|
|
|
sig.append(QChar(static_cast<short>(f.family())));
|
|
|
|
sig.append(QChar(static_cast<short>(f.series())));
|
|
|
|
sig.append(QChar(static_cast<short>(f.realShape())));
|
|
|
|
sig.append(QChar(static_cast<short>(f.size())));
|
2012-04-29 12:26:24 +00:00
|
|
|
Color const & color = f.realColor();
|
|
|
|
sig.append(QChar(static_cast<short>(color.baseColor)));
|
|
|
|
sig.append(QChar(static_cast<short>(color.mergeColor)));
|
2007-11-01 10:52:51 +00:00
|
|
|
if (!monochrome_min_.empty()) {
|
|
|
|
QColor const & min = monochrome_min_.top();
|
|
|
|
QColor const & max = monochrome_max_.top();
|
|
|
|
sig.append(QChar(static_cast<short>(min.red())));
|
|
|
|
sig.append(QChar(static_cast<short>(min.green())));
|
|
|
|
sig.append(QChar(static_cast<short>(min.blue())));
|
|
|
|
sig.append(QChar(static_cast<short>(max.red())));
|
|
|
|
sig.append(QChar(static_cast<short>(max.green())));
|
|
|
|
sig.append(QChar(static_cast<short>(max.blue())));
|
|
|
|
}
|
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 20:10:11 +00:00
|
|
|
QColor GuiPainter::computeColor(Color col)
|
2007-11-01 10:52:51 +00:00
|
|
|
{
|
|
|
|
return filterColor(guiApp->colorCache().get(col));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QColor GuiPainter::filterColor(QColor const & col)
|
|
|
|
{
|
|
|
|
if (monochrome_min_.empty())
|
|
|
|
return col;
|
|
|
|
|
|
|
|
// map into [min,max] interval
|
|
|
|
QColor const & min = monochrome_min_.top();
|
|
|
|
QColor const & max = monochrome_max_.top();
|
|
|
|
|
|
|
|
qreal v = col.valueF();
|
2007-11-08 08:00:31 +00:00
|
|
|
v *= v; // make it a bit steeper (i.e. darker)
|
2007-11-01 10:52:51 +00:00
|
|
|
|
|
|
|
qreal minr, ming, minb;
|
|
|
|
qreal maxr, maxg, maxb;
|
|
|
|
min.getRgbF(&minr, &ming, &minb);
|
|
|
|
max.getRgbF(&maxr, &maxg, &maxb);
|
|
|
|
|
2007-11-07 20:44:36 +00:00
|
|
|
QColor c;
|
2007-11-08 08:00:31 +00:00
|
|
|
c.setRgbF(
|
|
|
|
v * (minr - maxr) + maxr,
|
|
|
|
v * (ming - maxg) + maxg,
|
|
|
|
v * (minb - maxb) + maxb);
|
2007-11-07 20:44:36 +00:00
|
|
|
return c;
|
2007-11-01 10:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 20:10:11 +00:00
|
|
|
void GuiPainter::enterMonochromeMode(Color const & min, Color const & max)
|
2007-11-01 10:52:51 +00:00
|
|
|
{
|
|
|
|
QColor qmin = filterColor(guiApp->colorCache().get(min));
|
|
|
|
QColor qmax = filterColor(guiApp->colorCache().get(max));
|
|
|
|
monochrome_min_.push(qmin);
|
|
|
|
monochrome_max_.push(qmax);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiPainter::leaveMonochromeMode()
|
|
|
|
{
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(!monochrome_min_.empty(), return);
|
2007-11-01 10:52:51 +00:00
|
|
|
monochrome_min_.pop();
|
|
|
|
monochrome_max_.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 20:10:11 +00:00
|
|
|
void GuiPainter::point(int x, int y, Color col)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(col));
|
2006-10-23 08:46:09 +00:00
|
|
|
drawPoint(x, y);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
void GuiPainter::line(int x1, int y1, int x2, int y2,
|
2009-02-09 20:10:11 +00:00
|
|
|
Color col,
|
2006-03-05 17:24:44 +00:00
|
|
|
line_style ls,
|
2010-09-05 14:46:58 +00:00
|
|
|
float lw)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(col), ls, lw);
|
2007-05-04 09:24:44 +00:00
|
|
|
bool const do_antialiasing = renderHints() & TextAntialiasing
|
|
|
|
&& x1 != x2 && y1 != y2;
|
2007-05-04 08:52:40 +00:00
|
|
|
setRenderHint(Antialiasing, do_antialiasing);
|
2006-10-23 08:46:09 +00:00
|
|
|
drawLine(x1, y1, x2, y2);
|
2007-03-29 23:08:29 +00:00
|
|
|
setRenderHint(Antialiasing, false);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
void GuiPainter::lines(int const * xp, int const * yp, int np,
|
2009-02-09 20:10:11 +00:00
|
|
|
Color col,
|
2006-03-05 17:24:44 +00:00
|
|
|
line_style ls,
|
2010-09-05 14:46:58 +00:00
|
|
|
float lw)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-05-20 08:50:54 +00:00
|
|
|
// double the size if needed
|
2013-10-07 22:59:05 +00:00
|
|
|
// FIXME THREAD
|
2007-05-20 08:50:54 +00:00
|
|
|
static QVector<QPoint> points(32);
|
|
|
|
if (np > points.size())
|
|
|
|
points.resize(2 * np);
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-05-19 10:52:47 +00:00
|
|
|
bool antialias = false;
|
|
|
|
for (int i = 0; i < np; ++i) {
|
|
|
|
points[i].setX(xp[i]);
|
|
|
|
points[i].setY(yp[i]);
|
2007-05-28 22:27:45 +00:00
|
|
|
if (i != 0)
|
2007-05-19 10:52:47 +00:00
|
|
|
antialias |= xp[i-1] != xp[i] && yp[i-1] != yp[i];
|
2007-05-04 08:52:40 +00:00
|
|
|
}
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(col), ls, lw);
|
2007-05-19 10:52:47 +00:00
|
|
|
bool const text_is_antialiased = renderHints() & TextAntialiasing;
|
|
|
|
setRenderHint(Antialiasing, antialias && text_is_antialiased);
|
2007-05-20 08:50:54 +00:00
|
|
|
drawPolyline(points.data(), np);
|
2007-05-19 10:52:47 +00:00
|
|
|
setRenderHint(Antialiasing, false);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
void GuiPainter::rectangle(int x, int y, int w, int h,
|
2009-02-09 20:10:11 +00:00
|
|
|
Color col,
|
2006-03-05 17:24:44 +00:00
|
|
|
line_style ls,
|
2010-09-05 14:46:58 +00:00
|
|
|
float lw)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(col), ls, lw);
|
2006-10-23 08:46:09 +00:00
|
|
|
drawRect(x, y, w, h);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 20:10:11 +00:00
|
|
|
void GuiPainter::fillRectangle(int x, int y, int w, int h, Color col)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-09-09 21:01:18 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2006-10-23 08:46:09 +00:00
|
|
|
fillRect(x, y, w, h, guiApp->colorCache().get(col));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
void GuiPainter::arc(int x, int y, unsigned int w, unsigned int h,
|
2009-02-09 20:10:11 +00:00
|
|
|
int a1, int a2, Color col)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
// LyX usings 1/64ths degree, Qt usings 1/16th
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(col));
|
2007-05-04 09:24:44 +00:00
|
|
|
bool const do_antialiasing = renderHints() & TextAntialiasing;
|
2007-05-04 08:52:40 +00:00
|
|
|
setRenderHint(Antialiasing, do_antialiasing);
|
2006-10-23 08:46:09 +00:00
|
|
|
drawArc(x, y, w, h, a1 / 4, a2 / 4);
|
2007-03-29 23:08:29 +00:00
|
|
|
setRenderHint(Antialiasing, false);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-08-31 22:16:11 +00:00
|
|
|
graphics::GuiImage const & qlimage =
|
|
|
|
static_cast<graphics::GuiImage const &>(i);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-10-25 12:41:02 +00:00
|
|
|
fillRectangle(x, y, w, h, Color_graphicsbg);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2008-09-19 11:27:24 +00:00
|
|
|
drawImage(x, y, qlimage.image(), 0, 0, w, h);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-28 18:51:54 +00:00
|
|
|
int GuiPainter::text(int x, int y, char_type c, FontInfo const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2014-07-20 16:54:31 +00:00
|
|
|
return text(x, y, docstring(1, c), f);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
int GuiPainter::text(int x, int y, docstring const & s,
|
2007-10-28 18:51:54 +00:00
|
|
|
FontInfo const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2014-07-20 16:54:31 +00:00
|
|
|
//LYXERR0("text: x=" << x << ", s=" << s);
|
2007-09-15 12:34:21 +00:00
|
|
|
if (s.empty())
|
|
|
|
return 0;
|
|
|
|
|
2007-02-26 16:22:54 +00:00
|
|
|
/* Caution: The following ucs4 to QString conversions work for symbol fonts
|
|
|
|
only because they are no real conversions but simple casts in reality.
|
|
|
|
When we want to draw a symbol or calculate the metrics we pass the position
|
|
|
|
of the symbol in the font (as given in lib/symbols) as a char_type to the
|
|
|
|
frontend. This is just wrong, because the symbol is no UCS4 character at
|
|
|
|
all. You can think of this number as the code point of the symbol in a
|
|
|
|
custom symbol encoding. It works because this char_type is lateron again
|
|
|
|
interpreted as a position in the font again.
|
|
|
|
The correct solution would be to have extra functions for symbols, but that
|
|
|
|
would require to duplicate a lot of frontend and mathed support code.
|
|
|
|
*/
|
2007-02-26 15:13:08 +00:00
|
|
|
QString str = toqstr(s);
|
2006-08-13 22:54:59 +00:00
|
|
|
|
|
|
|
#if 0
|
2006-03-05 17:24:44 +00:00
|
|
|
// HACK: QT3 refuses to show single compose characters
|
2006-06-14 14:23:25 +00:00
|
|
|
// Still needed with Qt4?
|
2006-03-05 17:24:44 +00:00
|
|
|
if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
|
|
|
|
str = ' ' + str;
|
2006-08-13 22:54:59 +00:00
|
|
|
#endif
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-03-20 00:19:10 +00:00
|
|
|
QFont const & ff = getFont(f);
|
|
|
|
GuiFontMetrics const & fm = getFontMetrics(f);
|
2006-10-07 16:15:06 +00:00
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
// Here we use the font width cache instead of
|
|
|
|
// textwidth = fontMetrics().width(str);
|
|
|
|
// because the above is awfully expensive on MacOSX
|
2014-05-23 16:59:53 +00:00
|
|
|
int const textwidth = fm.width(s);
|
2007-08-13 17:35:09 +00:00
|
|
|
|
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return textwidth;
|
|
|
|
|
2014-05-23 16:59:53 +00:00
|
|
|
textDecoration(f, x, y, textwidth);
|
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
// Qt4 does not display a glyph whose codepoint is the
|
|
|
|
// same as that of a soft-hyphen (0x00ad), unless it
|
|
|
|
// occurs at a line-break. As a kludge, we force Qt to
|
|
|
|
// render this glyph using a one-column line.
|
2014-03-05 11:16:57 +00:00
|
|
|
// This is needed for some math glyphs.
|
2014-03-03 09:45:40 +00:00
|
|
|
// Should the soft hyphen char be displayed at all?
|
2014-03-05 11:16:57 +00:00
|
|
|
// I don't think so (i.e., Qt is correct as far as
|
|
|
|
// texted is concerned). /spitz
|
2007-08-13 17:35:09 +00:00
|
|
|
if (s.size() == 1 && str[0].unicode() == 0x00ad) {
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(f.realColor()));
|
2007-08-13 17:35:09 +00:00
|
|
|
QTextLayout adsymbol(str);
|
2008-03-20 00:19:10 +00:00
|
|
|
adsymbol.setFont(ff);
|
2007-08-13 17:35:09 +00:00
|
|
|
adsymbol.beginLayout();
|
|
|
|
QTextLine line = adsymbol.createLine();
|
|
|
|
line.setNumColumns(1);
|
|
|
|
line.setPosition(QPointF(0, -line.ascent()));
|
|
|
|
adsymbol.endLayout();
|
|
|
|
line.draw(this, QPointF(x, y));
|
|
|
|
return textwidth;
|
|
|
|
}
|
|
|
|
|
2008-10-24 22:49:17 +00:00
|
|
|
if (use_pixmap_cache_) {
|
|
|
|
QPixmap pm;
|
|
|
|
QString key = generateStringSignature(str, f);
|
|
|
|
|
|
|
|
// Warning: Left bearing is in general negative! Only the case
|
|
|
|
// where left bearing is negative is of interest WRT the
|
|
|
|
// pixmap width and the text x-position.
|
|
|
|
// Only the left bearing of the first character is important
|
|
|
|
// as we always write from left to right, even for
|
|
|
|
// right-to-left languages.
|
|
|
|
int const lb = min(fm.lbearing(s[0]), 0);
|
|
|
|
int const mA = fm.maxAscent();
|
|
|
|
if (QPixmapCache::find(key, pm)) {
|
|
|
|
// Draw the cached pixmap.
|
|
|
|
drawPixmap(x + lb, y - mA, pm);
|
|
|
|
return textwidth;
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
// Only the right bearing of the last character is
|
|
|
|
// important as we always write from left to right,
|
|
|
|
// even for right-to-left languages.
|
2008-03-20 00:19:10 +00:00
|
|
|
int const rb = fm.rbearing(s[s.size()-1]);
|
2007-08-13 17:35:09 +00:00
|
|
|
int const w = textwidth + rb - lb;
|
2008-03-20 00:19:10 +00:00
|
|
|
int const mD = fm.maxDescent();
|
2007-08-13 17:35:09 +00:00
|
|
|
int const h = mA + mD;
|
2008-10-24 22:49:17 +00:00
|
|
|
if (w > 0 && h > 0) {
|
|
|
|
pm = QPixmap(w, h);
|
|
|
|
pm.fill(Qt::transparent);
|
|
|
|
GuiPainter p(&pm);
|
|
|
|
p.setQPainterPen(computeColor(f.realColor()));
|
|
|
|
if (p.font() != ff)
|
|
|
|
p.setFont(ff);
|
|
|
|
// We need to draw the text as LTR as we use our own bidi code.
|
|
|
|
p.setLayoutDirection(Qt::LeftToRight);
|
|
|
|
p.drawText(-lb, mA, str);
|
|
|
|
QPixmapCache::insert(key, pm);
|
|
|
|
//LYXERR(Debug::PAINTING, "h=" << h << " mA=" << mA << " mD=" << mD
|
|
|
|
// << " w=" << w << " lb=" << lb << " tw=" << textwidth
|
|
|
|
// << " rb=" << rb);
|
2008-11-03 07:40:18 +00:00
|
|
|
|
|
|
|
// Draw the new cached pixmap.
|
|
|
|
drawPixmap(x + lb, y - mA, pm);
|
|
|
|
|
2008-10-23 20:47:11 +00:00
|
|
|
return textwidth;
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2008-10-24 22:49:17 +00:00
|
|
|
// don't use the pixmap cache,
|
|
|
|
// draw directly onto the painting device
|
|
|
|
setQPainterPen(computeColor(f.realColor()));
|
|
|
|
if (font() != ff)
|
|
|
|
setFont(ff);
|
|
|
|
drawText(x, y, str);
|
|
|
|
//LYXERR(Debug::PAINTING, "draw " << string(str.toUtf8())
|
|
|
|
// << " at " << x << "," << y);
|
2006-10-10 13:24:08 +00:00
|
|
|
return textwidth;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-06-25 11:18:56 +00:00
|
|
|
|
|
|
|
|
2014-05-23 16:59:53 +00:00
|
|
|
int GuiPainter::text(int x, int y, docstring const & str, Font const & f)
|
|
|
|
{
|
|
|
|
docstring const dstr = directedString(str, f.isVisibleRightToLeft());
|
|
|
|
return text(x, y, dstr, f.fontInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiPainter::text(int x, int y, docstring const & str, Font const & f,
|
|
|
|
Color other, size_type from, size_type to)
|
|
|
|
{
|
|
|
|
GuiFontMetrics const & fm = getFontMetrics(f.fontInfo());
|
|
|
|
FontInfo fi = f.fontInfo();
|
|
|
|
bool const rtl = f.isVisibleRightToLeft();
|
|
|
|
docstring const dstr = directedString(str, rtl);
|
|
|
|
|
|
|
|
// dimensions
|
|
|
|
int const ascent = fm.maxAscent();
|
|
|
|
int const height = fm.maxAscent() + fm.maxDescent();
|
|
|
|
int xmin = fm.pos2x(str, from, rtl);
|
|
|
|
int xmax = fm.pos2x(str, to, rtl);
|
|
|
|
if (xmin > xmax)
|
|
|
|
swap(xmin, xmax);
|
|
|
|
|
|
|
|
// First the part in other color
|
|
|
|
Color const orig = fi.realColor();
|
|
|
|
fi.setPaintColor(other);
|
|
|
|
setClipRect(QRect(x + xmin, y - ascent, xmax - xmin, height));
|
|
|
|
int const textwidth = text(x, y, dstr, fi);
|
|
|
|
|
|
|
|
// Then the part in normal color
|
|
|
|
// Note that in Qt5, it is not possible to use Qt::UniteClip
|
|
|
|
fi.setPaintColor(orig);
|
|
|
|
setClipRect(QRect(x, y - ascent, xmin, height));
|
|
|
|
text(x, y, dstr, fi);
|
|
|
|
setClipRect(QRect(x + xmax, y - ascent, textwidth - xmax, height));
|
|
|
|
text(x, y, dstr, fi);
|
|
|
|
setClipping(false);
|
|
|
|
|
|
|
|
return textwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 16:02:17 +00:00
|
|
|
void GuiPainter::textDecoration(FontInfo const & f, int x, int y, int width)
|
|
|
|
{
|
|
|
|
if (f.underbar() == FONT_ON)
|
|
|
|
underline(f, x, y, width);
|
|
|
|
if (f.strikeout() == FONT_ON)
|
|
|
|
strikeoutLine(f, x, y, width);
|
|
|
|
if (f.uuline() == FONT_ON)
|
|
|
|
doubleUnderline(f, x, y, width);
|
|
|
|
if (f.uwave() == FONT_ON)
|
|
|
|
// f.color() doesn't work on some circumstances
|
|
|
|
wavyHorizontalLine(x, y, width, f.realColor().baseColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-23 21:39:51 +00:00
|
|
|
static int max(int a, int b) { return a > b ? a : b; }
|
|
|
|
|
|
|
|
|
|
|
|
void GuiPainter::button(int x, int y, int w, int h, bool mouseHover)
|
|
|
|
{
|
|
|
|
if (mouseHover)
|
|
|
|
fillRectangle(x, y, w, h, Color_buttonhoverbg);
|
|
|
|
else
|
|
|
|
fillRectangle(x, y, w, h, Color_buttonbg);
|
|
|
|
buttonFrame(x, y, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiPainter::buttonFrame(int x, int y, int w, int h)
|
|
|
|
{
|
|
|
|
line(x, y, x, y + h - 1, Color_buttonframe);
|
|
|
|
line(x - 1 + w, y, x - 1 + w, y + h - 1, Color_buttonframe);
|
|
|
|
line(x, y - 1, x - 1 + w, y - 1, Color_buttonframe);
|
|
|
|
line(x, y + h - 1, x - 1 + w, y + h - 1, Color_buttonframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiPainter::rectText(int x, int y, docstring const & str,
|
2009-02-09 20:10:11 +00:00
|
|
|
FontInfo const & font, Color back, Color frame)
|
2007-11-23 21:39:51 +00:00
|
|
|
{
|
|
|
|
int width;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
|
|
|
|
FontMetrics const & fm = theFontMetrics(font);
|
|
|
|
fm.rectText(str, width, ascent, descent);
|
|
|
|
|
|
|
|
if (back != Color_none)
|
|
|
|
fillRectangle(x + 1, y - ascent + 1, width - 1,
|
|
|
|
ascent + descent - 1, back);
|
|
|
|
|
|
|
|
if (frame != Color_none)
|
|
|
|
rectangle(x, y - ascent, width, ascent + descent, frame);
|
|
|
|
|
|
|
|
text(x + 3, y, str, font);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiPainter::buttonText(int x, int y, docstring const & str,
|
|
|
|
FontInfo const & font, bool mouseHover)
|
|
|
|
{
|
|
|
|
int width;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
|
|
|
|
FontMetrics const & fm = theFontMetrics(font);
|
|
|
|
fm.buttonText(str, width, ascent, descent);
|
|
|
|
|
2008-03-02 10:20:13 +00:00
|
|
|
static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
|
|
|
|
|
2008-02-14 08:49:45 +00:00
|
|
|
button(x + d, y - ascent, width - d, descent + ascent, mouseHover);
|
|
|
|
text(x + Inset::TEXT_TO_INSET_OFFSET, y, str, font);
|
2007-11-23 21:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiPainter::preeditText(int x, int y, char_type c,
|
|
|
|
FontInfo const & font, preedit_style style)
|
|
|
|
{
|
|
|
|
FontInfo temp_font = font;
|
|
|
|
FontMetrics const & fm = theFontMetrics(font);
|
|
|
|
int ascent = fm.maxAscent();
|
|
|
|
int descent = fm.maxDescent();
|
|
|
|
int height = ascent + descent;
|
|
|
|
int width = fm.width(c);
|
|
|
|
|
|
|
|
switch (style) {
|
|
|
|
case preedit_default:
|
|
|
|
// default unselecting mode.
|
|
|
|
fillRectangle(x, y - height + 1, width, height, Color_background);
|
|
|
|
dashedUnderline(font, x, y - descent + 1, width);
|
|
|
|
break;
|
|
|
|
case preedit_selecting:
|
|
|
|
// We are in selecting mode: white text on black background.
|
|
|
|
fillRectangle(x, y - height + 1, width, height, Color_black);
|
|
|
|
temp_font.setColor(Color_white);
|
|
|
|
break;
|
|
|
|
case preedit_cursor:
|
|
|
|
// The character comes with a cursor.
|
|
|
|
fillRectangle(x, y - height + 1, width, height, Color_background);
|
|
|
|
underline(font, x, y - descent + 1, width);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
text(x, y - descent + 1, c, temp_font);
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 09:26:28 +00:00
|
|
|
void GuiPainter::doubleUnderline(FontInfo const & f, int x, int y, int width)
|
|
|
|
{
|
|
|
|
FontMetrics const & fm = theFontMetrics(f);
|
|
|
|
|
|
|
|
int const below = max(fm.maxDescent() / 2, 2);
|
|
|
|
|
|
|
|
line(x, y + below, x + width, y + below, f.realColor());
|
|
|
|
line(x, y + below - 2, x + width, y + below - 2, f.realColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-23 21:39:51 +00:00
|
|
|
void GuiPainter::underline(FontInfo const & f, int x, int y, int width)
|
|
|
|
{
|
|
|
|
FontMetrics const & fm = theFontMetrics(f);
|
|
|
|
|
|
|
|
int const below = max(fm.maxDescent() / 2, 2);
|
|
|
|
int const height = max((fm.maxDescent() / 4) - 1, 1);
|
|
|
|
|
|
|
|
if (height < 2)
|
2008-08-29 00:31:04 +00:00
|
|
|
line(x, y + below, x + width, y + below, f.realColor());
|
2007-11-23 21:39:51 +00:00
|
|
|
else
|
2008-08-29 00:31:04 +00:00
|
|
|
fillRectangle(x, y + below, width, below + height, f.realColor());
|
2007-11-23 21:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-03 22:45:14 +00:00
|
|
|
void GuiPainter::strikeoutLine(FontInfo const & f, int x, int y, int width)
|
|
|
|
{
|
|
|
|
FontMetrics const & fm = theFontMetrics(f);
|
|
|
|
|
|
|
|
int const middle = max((fm.maxHeight() / 4), 1);
|
|
|
|
int const height = middle/3;
|
|
|
|
|
|
|
|
if (height < 2)
|
|
|
|
line(x, y - middle, x + width, y - middle, f.realColor());
|
|
|
|
else
|
|
|
|
fillRectangle(x, y - middle, width, height, f.realColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-23 21:39:51 +00:00
|
|
|
void GuiPainter::dashedUnderline(FontInfo const & f, int x, int y, int width)
|
|
|
|
{
|
|
|
|
FontMetrics const & fm = theFontMetrics(f);
|
|
|
|
|
|
|
|
int const below = max(fm.maxDescent() / 2, 2);
|
|
|
|
int height = max((fm.maxDescent() / 4) - 1, 1);
|
|
|
|
|
|
|
|
if (height >= 2)
|
|
|
|
height += below;
|
|
|
|
|
2007-11-29 22:38:53 +00:00
|
|
|
for (int n = 0; n != height; ++n)
|
2008-08-29 00:31:04 +00:00
|
|
|
line(x, y + below + n, x + width, y + below + n, f.realColor(), line_onoffdash);
|
2007-11-23 21:39:51 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:37:28 +00:00
|
|
|
|
|
|
|
void GuiPainter::wavyHorizontalLine(int x, int y, int width, ColorCode col)
|
|
|
|
{
|
|
|
|
setQPainterPen(computeColor(col));
|
2010-03-21 20:26:30 +00:00
|
|
|
int const step = 2;
|
2009-03-28 16:37:28 +00:00
|
|
|
int const xend = x + width;
|
|
|
|
int height = 1;
|
|
|
|
//FIXME: I am not sure if Antialiasing gives the best effect.
|
|
|
|
//setRenderHint(Antialiasing, true);
|
|
|
|
while (x < xend) {
|
|
|
|
height = - height;
|
|
|
|
drawLine(x, y - height, x + step, y + height);
|
|
|
|
x += step;
|
2010-03-21 20:26:30 +00:00
|
|
|
drawLine(x, y + height, x + step/2, y + height);
|
|
|
|
x += step/2;
|
2009-03-28 16:37:28 +00:00
|
|
|
}
|
|
|
|
//setRenderHint(Antialiasing, false);
|
|
|
|
}
|
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|