3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "QLPainter.h"
14 #include "font_metrics.h"
19 #include "QWorkArea.h"
20 #include "qfont_loader.h"
22 #include "lcolorcache.h"
30 QLPainter::QLPainter(QWorkArea & qwa)
31 : Painter(), owner_(qwa), paint_check_(0)
33 qp_.reset(new QPainter);
37 void QLPainter::start()
39 if (++paint_check_ == 1)
40 qp_->begin(owner_.getPixmap());
46 if (paint_check_ == 0) {
47 lyxerr << "ended painting whilst not painting ??" << endl;
48 } else if (--paint_check_ == 0) {
54 int QLPainter::paperWidth() const
56 return owner_.workWidth();
60 int QLPainter::paperHeight() const
62 return owner_.workHeight();
66 QPainter & QLPainter::setPen(LColor_color c,
67 Painter::line_style ls, Painter::line_width lw)
69 QPen pen = qp_->pen();
71 pen.setColor(lcolorcache.get(c));
74 case line_solid: pen.setStyle(QPen::SolidLine); break;
75 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
79 case line_thin: pen.setWidth(0); break;
80 case line_thick: pen.setWidth(3); break;
88 Painter & QLPainter::point(int x, int y, LColor_color c)
90 setPen(c).drawPoint(x, y);
95 Painter & QLPainter::line(int x1, int y1,
101 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
106 Painter & QLPainter::lines(int const * xp, int const * yp,
114 // Must use new as np is not known at compile time.
115 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
117 for (int i = 0, j = 0; i < np; ++i) {
122 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
128 Painter & QLPainter::rectangle(int x, int y,
134 setPen(col, ls, lw).drawRect(x, y, w, h);
139 Painter & QLPainter::fillRectangle(int x, int y,
143 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
148 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
149 int np, LColor_color col)
151 // Must use new as np is not known at compile time.
152 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
154 //if (1) return *this;
156 for (int i = 0, j = 0; i < np; ++i) {
162 qp_->setBrush(lcolorcache.get(col));
163 qp_->drawPolygon(QPointArray(np, points.get()));
164 qp_->setBrush(Qt::NoBrush);
170 Painter & QLPainter::arc(int x, int y,
171 unsigned int w, unsigned int h,
172 int a1, int a2, LColor_color col)
174 // LyX usings 1/64ths degree, Qt usings 1/16th
175 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
180 Painter & QLPainter::image(int x, int y,
182 lyx::graphics::Image const & i)
184 qp_->drawPixmap(x, y, static_cast<lyx::graphics::QLImage const &>(i).qpixmap(), 0, 0, w, h);
189 Painter & QLPainter::text(int x, int y,
190 string const & s, LyXFont const & f)
192 return text(x, y, s.data(), s.length(), f);
196 Painter & QLPainter::text(int x, int y,
197 char c, LyXFont const & f)
199 char s[2] = { c, '\0' };
200 return text(x, y, s, 1, f);
204 void QLPainter::smallCapsText(int x, int y,
205 QString const & s, LyXFont const & f)
207 LyXFont smallfont(f);
208 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
210 QFont const & qfont = fontloader.get(f);
211 QFont const & qsmallfont = fontloader.get(smallfont);
212 QFontMetrics const & qfontm = QFontMetrics(qfont);
213 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
216 size_t ls = s.length();
217 for (size_t i = 0; i < ls; ++i) {
218 QChar const c = s[i].upper();
220 qp_->setFont(qsmallfont);
221 qp_->drawText(tmpx, y, c);
222 tmpx += qsmallfontm.width(c);
225 qp_->drawText(tmpx, y, c);
226 tmpx += qfontm.width(c);
232 Painter & QLPainter::text(int x, int y,
233 char const * s, size_t ls,
236 setPen(f.realColor());
238 Encoding const * encoding = f.language()->encoding();
239 if (f.isSymbolFont())
240 encoding = encodings.symbol_encoding();
243 #if QT_VERSION >= 300
245 for (size_t i = 0; i < ls; ++i)
246 str[i] = QChar(encoding->ucs(s[i]));
247 // HACK: QT3 refuses to show single compose characters
248 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
251 for (size_t i = 0; i < ls; ++i)
252 str += QChar(encoding->ucs(s[i]));
255 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
256 qp_->setFont(fontloader.get(f));
257 #if QT_VERSION >= 300
258 // We need to draw the text as LTR as we use our own bidi
260 qp_->drawText(x, y, str, -1, QPainter::LTR);
262 qp_->drawText(x, y, str);
265 smallCapsText(x, y, str, f);
268 if (f.underbar() == LyXFont::ON) {
269 underline(f, x, y, font_metrics::width(s, ls, f));