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>
|
|
|
|
|
2007-02-01 03:56:23 +00:00
|
|
|
#include <QTextLayout>
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#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"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "language.h"
|
|
|
|
#include "LColor.h"
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
#include "support/unicode.h"
|
|
|
|
|
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
|
|
|
|
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:
|
2006-08-13 22:54:59 +00:00
|
|
|
current_color_ = LColor::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
void QLPainter::setQPainterPen(LColor_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
|
|
|
|
2006-06-14 14:23:25 +00:00
|
|
|
void QLPainter::point(int x, int y, LColor_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,
|
|
|
|
LColor_color col,
|
|
|
|
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-03-29 23:08:29 +00:00
|
|
|
setRenderHint(Antialiasing, x1 != x2 && y1 != y2);
|
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,
|
|
|
|
LColor_color col,
|
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
|
|
|
// FIXME ?
|
|
|
|
|
|
|
|
// Must use new as np is not known at compile time.
|
|
|
|
boost::scoped_array<QPoint> points(new QPoint[np]);
|
|
|
|
|
2007-03-30 05:30:23 +00:00
|
|
|
bool antialias = false;
|
2006-03-05 17:24:44 +00:00
|
|
|
for (int i = 0; i < np; ++i) {
|
|
|
|
points[i].setX(xp[i]);
|
|
|
|
points[i].setY(yp[i]);
|
2007-03-30 05:30:23 +00:00
|
|
|
if (i != 0)
|
2007-03-29 23:08:29 +00:00
|
|
|
antialias |= xp[i-1] != xp[i] && yp[i-1] != yp[i];
|
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, ls, lw);
|
2007-03-29 23:08:29 +00:00
|
|
|
setRenderHint(Antialiasing, antialias);
|
2006-10-23 08:46:09 +00:00
|
|
|
drawPolyline(points.get(), np);
|
2007-03-29 23:08:29 +00:00
|
|
|
setRenderHint(Antialiasing, false);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::rectangle(int x, int y, int w, int h,
|
|
|
|
LColor_color col,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
|
|
|
|
{
|
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,
|
|
|
|
int a1, int a2, LColor_color col)
|
|
|
|
{
|
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-03-29 23:08:29 +00:00
|
|
|
setRenderHint(Antialiasing, true);
|
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
|
|
|
|
|
|
|
fillRectangle(x, y, w, h, LColor::graphicsbg);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-10 13:24:08 +00:00
|
|
|
int QLPainter::text(int x, int y, char_type c, LyXFont 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,
|
2006-03-05 17:24:44 +00:00
|
|
|
QString const & s, LyXFont const & f)
|
|
|
|
{
|
|
|
|
LyXFont smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
|
|
|
|
|
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,
|
|
|
|
LyXFont 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;
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
|
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);
|
2006-11-03 08:17:26 +00:00
|
|
|
if (isDrawingEnabled()) {
|
2007-04-01 14:35:42 +00:00
|
|
|
LYXERR(Debug::PAINTING) << "draw " << std::string(str.toUtf8())
|
|
|
|
<< " at " << x << "," << y << std::endl;
|
2007-02-01 03:56:23 +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.
|
2007-02-26 15:13:08 +00:00
|
|
|
if (s.size() == 1 && str[0].unicode() == 0x00ad) {
|
2007-02-01 03:56:23 +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));
|
|
|
|
} else
|
|
|
|
drawText(x, y, str);
|
2006-11-03 08:17:26 +00:00
|
|
|
}
|
2006-10-27 13:36:23 +00:00
|
|
|
// Here we use the font width cache instead of
|
|
|
|
// textwidth = fontMetrics().width(str);
|
|
|
|
// because the above is awfully expensive on MacOSX
|
2006-10-27 21:27:03 +00:00
|
|
|
textwidth = fi.metrics->width(str);
|
2006-03-05 17:24:44 +00:00
|
|
|
} else {
|
2006-10-07 16:15:06 +00:00
|
|
|
textwidth = smallCapsText(x, y, str, f);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.underbar() == LyXFont::ON) {
|
2006-10-07 16:15:06 +00:00
|
|
|
underline(f, x, y, textwidth);
|
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
|
2006-11-07 22:17:12 +00:00
|
|
|
|