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
|
|
|
|
2006-09-26 09:57:47 +00:00
|
|
|
#include "GuiApplication.h"
|
2006-10-27 21:27:03 +00:00
|
|
|
#include "GuiFontMetrics.h"
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiImage.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-09-22 09:47:39 +00:00
|
|
|
#include "GuiApplication.h"
|
2006-08-30 14:59:07 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-06-26 16:55:35 +00:00
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.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
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
#include "support/unicode.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;
|
2007-11-07 20:44:36 +00:00
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
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;
|
|
|
|
current_lw_ = line_thin;
|
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,
|
2006-03-05 17:24:44 +00:00
|
|
|
Painter::line_style ls, Painter::line_width lw)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lw) {
|
|
|
|
case line_thin: pen.setWidth(0); break;
|
|
|
|
case line_thick: pen.setWidth(3); break;
|
|
|
|
}
|
|
|
|
|
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())));
|
|
|
|
sig.append(QChar(static_cast<short>(f.color())));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QColor GuiPainter::computeColor(ColorCode col)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiPainter::enterMonochromeMode(ColorCode const & min, ColorCode const & max)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(!monochrome_min_.empty());
|
|
|
|
monochrome_min_.pop();
|
|
|
|
monochrome_max_.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 12:41:02 +00:00
|
|
|
void GuiPainter::point(int x, int y, ColorCode 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,
|
2007-10-25 12:41:02 +00:00
|
|
|
ColorCode col,
|
2006-03-05 17:24:44 +00:00
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
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,
|
2007-10-25 12:41:02 +00:00
|
|
|
ColorCode col,
|
2006-03-05 17:24:44 +00:00
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
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
|
|
|
|
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,
|
2007-10-25 12:41:02 +00:00
|
|
|
ColorCode col,
|
2006-03-05 17:24:44 +00:00
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 12:41:02 +00:00
|
|
|
void GuiPainter::fillRectangle(int x, int y, int w, int h, ColorCode col)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
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,
|
2007-10-25 12:41:02 +00:00
|
|
|
int a1, int a2, ColorCode 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;
|
|
|
|
|
2006-10-23 08:46:09 +00:00
|
|
|
drawImage(x, y, qlimage.qimage(), 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
|
|
|
{
|
2007-02-26 15:29:04 +00:00
|
|
|
docstring s(1, c);
|
2007-02-26 15:13:08 +00:00
|
|
|
return text(x, y, s, f);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
int GuiPainter::smallCapsText(int x, int y,
|
2007-10-28 18:51:54 +00:00
|
|
|
QString const & s, FontInfo const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-28 18:51:54 +00:00
|
|
|
FontInfo smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(UP_SHAPE);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-03 16:17:32 +00:00
|
|
|
QFont const & qfont = guiApp->guiFontLoader().get(f);
|
|
|
|
QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(f.realColor()));
|
2006-10-07 16:15:06 +00:00
|
|
|
int textwidth = 0;
|
2006-10-23 08:46:09 +00:00
|
|
|
size_t const ls = s.length();
|
2006-06-25 11:27:33 +00:00
|
|
|
for (unsigned int i = 0; i < ls; ++i) {
|
2006-08-17 08:56:53 +00:00
|
|
|
QChar const c = s[i].toUpper();
|
2006-03-05 17:24:44 +00:00
|
|
|
if (c != s.at(i)) {
|
2006-10-23 08:46:09 +00:00
|
|
|
setFont(qsmallfont);
|
2006-03-05 17:24:44 +00:00
|
|
|
} else {
|
2006-10-23 08:46:09 +00:00
|
|
|
setFont(qfont);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-10-30 10:09:59 +00:00
|
|
|
if (isDrawingEnabled())
|
|
|
|
drawText(x + textwidth, y, c);
|
2006-10-23 08:46:09 +00:00
|
|
|
textwidth += fontMetrics().width(c);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-10-07 16:15:06 +00:00
|
|
|
return textwidth;
|
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
|
|
|
{
|
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
|
|
|
|
2007-09-15 22:56:09 +00:00
|
|
|
GuiFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
|
2006-10-07 16:15:06 +00:00
|
|
|
|
|
|
|
int textwidth;
|
|
|
|
|
2007-10-28 18:51:54 +00:00
|
|
|
if (f.realShape() == SMALLCAPS_SHAPE) {
|
2007-08-13 17:35:09 +00:00
|
|
|
textwidth = smallCapsText(x, y, str, f);
|
2007-10-28 18:51:54 +00:00
|
|
|
if (f.underbar() == FONT_ON)
|
2007-08-13 17:35:09 +00:00
|
|
|
underline(f, x, y, textwidth);
|
|
|
|
return textwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we use the font width cache instead of
|
|
|
|
// textwidth = fontMetrics().width(str);
|
|
|
|
// because the above is awfully expensive on MacOSX
|
2007-11-29 22:38:53 +00:00
|
|
|
textwidth = fi.metrics.width(s);
|
2007-10-28 18:51:54 +00:00
|
|
|
if (f.underbar() == FONT_ON)
|
2007-08-13 17:35:09 +00:00
|
|
|
underline(f, x, y, textwidth);
|
|
|
|
|
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return textwidth;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
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);
|
|
|
|
adsymbol.setFont(fi.font);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-11-02 14:43:09 +00:00
|
|
|
if (!use_pixmap_cache_) {
|
2007-08-13 17:35:09 +00:00
|
|
|
// don't use the pixmap cache,
|
|
|
|
// draw directly onto the painting device
|
2007-11-01 10:52:51 +00:00
|
|
|
setQPainterPen(computeColor(f.realColor()));
|
2006-10-23 08:46:09 +00:00
|
|
|
if (font() != fi.font)
|
|
|
|
setFont(fi.font);
|
2006-06-14 14:23:25 +00:00
|
|
|
// We need to draw the text as LTR as we use our own bidi code.
|
2006-10-23 08:46:09 +00:00
|
|
|
setLayoutDirection(Qt::LeftToRight);
|
2007-08-13 17:35:09 +00:00
|
|
|
// We need to draw the text as LTR as we use our own bidi code.
|
|
|
|
setLayoutDirection(Qt::LeftToRight);
|
|
|
|
drawText(x, y, str);
|
2007-12-12 19:28:07 +00:00
|
|
|
//LYXERR(Debug::PAINTING, "draw " << string(str.toUtf8())
|
2007-11-15 20:04:51 +00:00
|
|
|
// << " at " << x << "," << y);
|
2007-08-13 17:35:09 +00:00
|
|
|
return textwidth;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
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 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.
|
2007-12-12 19:28:07 +00:00
|
|
|
int const lb = min(fi.metrics.lbearing(s[0]), 0);
|
2007-11-29 22:38:53 +00:00
|
|
|
int const mA = fi.metrics.maxAscent();
|
2007-08-13 17:35:09 +00:00
|
|
|
if (!QPixmapCache::find(key, pm)) {
|
|
|
|
// Only the right bearing of the last character is
|
|
|
|
// important as we always write from left to right,
|
|
|
|
// even for right-to-left languages.
|
2007-11-29 22:38:53 +00:00
|
|
|
int const rb = fi.metrics.rbearing(s[s.size()-1]);
|
2007-08-13 17:35:09 +00:00
|
|
|
int const w = textwidth + rb - lb;
|
2007-11-29 22:38:53 +00:00
|
|
|
int const mD = fi.metrics.maxDescent();
|
2007-08-13 17:35:09 +00:00
|
|
|
int const h = mA + mD;
|
|
|
|
pm = QPixmap(w, h);
|
|
|
|
pm.fill(Qt::transparent);
|
2007-08-31 22:16:11 +00:00
|
|
|
GuiPainter p(&pm);
|
2007-11-01 10:52:51 +00:00
|
|
|
p.setQPainterPen(computeColor(f.realColor()));
|
2007-08-13 17:35:09 +00:00
|
|
|
if (p.font() != fi.font)
|
|
|
|
p.setFont(fi.font);
|
|
|
|
// 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);
|
2007-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::PAINTING, "h=" << h << " mA=" << mA << " mD=" << mD
|
2007-09-01 09:26:37 +00:00
|
|
|
// << " w=" << w << " lb=" << lb << " tw=" << textwidth
|
2007-11-15 20:04:51 +00:00
|
|
|
// << " rb=" << rb);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2007-08-13 17:35:09 +00:00
|
|
|
// Draw the cached pixmap.
|
|
|
|
drawPixmap(x + lb, y - mA, pm);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
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,
|
|
|
|
FontInfo const & font, ColorCode back, ColorCode frame)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
button(x + 1, y - ascent, width - 2, descent + ascent, mouseHover);
|
|
|
|
text(x + 4, y - 1, str, font);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
line(x, y + below, x + width, y + below, f.color());
|
|
|
|
else
|
|
|
|
fillRectangle(x, y + below, width, below + height, f.color());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2007-11-23 21:39:51 +00:00
|
|
|
line(x, y + below + n, x + width, y + below + n, f.color(), line_onoffdash);
|
|
|
|
}
|
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|