2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file QLPainter.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>
|
|
|
|
|
|
|
|
#include "QLPainter.h"
|
|
|
|
|
2006-09-26 09:57:47 +00:00
|
|
|
#include "GuiApplication.h"
|
2006-10-27 21:27:03 +00:00
|
|
|
#include "GuiFontMetrics.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "QLImage.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
#include "debug.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Language.h"
|
2007-04-26 17:34:20 +00:00
|
|
|
#include "Color.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.
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#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
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool const usePixmapCache = USE_PIXMAP_CACHE;
|
|
|
|
|
|
|
|
QString generateStringSignature(QString const & str, Font 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())));
|
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
2006-10-30 13:16:18 +00:00
|
|
|
QLPainter::QLPainter(QPaintDevice * device)
|
|
|
|
: QPainter(device), Painter()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-06-25 11:18:56 +00:00
|
|
|
// new QPainter has default QPen:
|
2007-04-26 17:34:20 +00:00
|
|
|
current_color_ = 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-23 08:46:09 +00:00
|
|
|
QLPainter::~QLPainter()
|
2006-06-23 14:12:17 +00:00
|
|
|
{
|
2006-10-23 08:46:09 +00:00
|
|
|
QPainter::end();
|
|
|
|
//lyxerr << "QLPainter::end()" << endl;
|
2006-06-23 14:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 17:34:20 +00:00
|
|
|
void QLPainter::setQPainterPen(Color_color 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();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-09-26 09:57:47 +00:00
|
|
|
pen.setColor(guiApp->colorCache().get(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-04-26 17:34:20 +00:00
|
|
|
void QLPainter::point(int x, int y, Color_color col)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-30 10:09:59 +00:00
|
|
|
if (!isDrawingEnabled())
|
|
|
|
return;
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(col);
|
2006-10-23 08:46:09 +00:00
|
|
|
drawPoint(x, y);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::line(int x1, int y1, int x2, int y2,
|
2007-04-26 17:34:20 +00:00
|
|
|
Color_color 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;
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::lines(int const * xp, int const * yp, int np,
|
2007-04-26 17:34:20 +00:00
|
|
|
Color_color 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-05-28 22:27:45 +00:00
|
|
|
setQPainterPen(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::rectangle(int x, int y, int w, int h,
|
2007-04-26 17:34:20 +00:00
|
|
|
Color_color 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;
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(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-04-26 17:34:20 +00:00
|
|
|
void QLPainter::fillRectangle(int x, int y, int w, int h, Color_color 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
|
2007-04-26 17:34:20 +00:00
|
|
|
int a1, int a2, Color_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
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-23 08:46:09 +00:00
|
|
|
void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-23 08:46:09 +00:00
|
|
|
graphics::QLImage const & qlimage =
|
|
|
|
static_cast<graphics::QLImage const &>(i);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-04-26 17:34:20 +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-04-29 18:17:15 +00:00
|
|
|
int QLPainter::text(int x, int y, char_type c, Font 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
int QLPainter::smallCapsText(int x, int y,
|
2007-04-29 18:17:15 +00:00
|
|
|
QString const & s, Font const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-04-29 18:17:15 +00:00
|
|
|
Font smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(Font::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
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(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-02-26 15:13:08 +00:00
|
|
|
int QLPainter::text(int x, int y, docstring const & s,
|
2007-04-29 18:17:15 +00:00
|
|
|
Font const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
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
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
|
|
|
|
|
|
|
|
int textwidth;
|
|
|
|
|
2007-08-13 17:35:09 +00:00
|
|
|
if (f.realShape() == Font::SMALLCAPS_SHAPE) {
|
|
|
|
textwidth = smallCapsText(x, y, str, f);
|
|
|
|
if (f.underbar() == Font::ON)
|
|
|
|
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
|
|
|
|
textwidth = fi.metrics->width(s);
|
|
|
|
if (f.underbar() == Font::ON)
|
|
|
|
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-08-16 16:35:38 +00:00
|
|
|
setQPainterPen(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usePixmapCache) {
|
|
|
|
// don't use the pixmap cache,
|
|
|
|
// draw directly onto the painting device
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(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);
|
|
|
|
LYXERR(Debug::PAINTING) << "draw " << std::string(str.toUtf8())
|
|
|
|
<< " at " << x << "," << y << std::endl;
|
|
|
|
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.
|
|
|
|
int const lb = std::min(fi.metrics->lbearing(s[0]), 0);
|
|
|
|
int const mA = fi.metrics->maxAscent();
|
|
|
|
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.
|
|
|
|
int const rb = fi.metrics->rbearing(s[s.size()-1]);
|
|
|
|
int const w = textwidth + rb - lb;
|
|
|
|
int const mD = fi.metrics->maxDescent();
|
|
|
|
int const h = mA + mD;
|
|
|
|
pm = QPixmap(w, h);
|
|
|
|
pm.fill(Qt::transparent);
|
|
|
|
QLPainter p(&pm);
|
|
|
|
p.setQPainterPen(f.realColor());
|
|
|
|
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);
|
|
|
|
LYXERR(Debug::PAINTING) << "h=" << h << " mA=" << mA << " mD=" << mD
|
|
|
|
<< " w=" << w << " lb=" << lb << " tw=" << textwidth
|
|
|
|
<< " rb=" << rb << endl;
|
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
|
|
|
|
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|