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"
31 QLPainter::QLPainter(QWorkArea & qwa)
32 : Painter(), owner_(qwa), paint_check_(0)
34 qp_.reset(new QPainter);
38 void QLPainter::start()
40 if (++paint_check_ == 1)
41 qp_->begin(owner_.getPixmap());
47 if (paint_check_ == 0) {
48 lyxerr << "ended painting whilst not painting ??" << endl;
49 } else if (--paint_check_ == 0) {
55 int QLPainter::paperWidth() const
57 return owner_.workWidth();
61 int QLPainter::paperHeight() const
63 return owner_.workHeight();
67 QPainter & QLPainter::setPen(LColor_color c,
68 Painter::line_style ls, Painter::line_width lw)
70 QPen pen = qp_->pen();
72 pen.setColor(lcolorcache.get(c));
75 case line_solid: pen.setStyle(QPen::SolidLine); break;
76 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
80 case line_thin: pen.setWidth(0); break;
81 case line_thick: pen.setWidth(3); break;
89 Painter & QLPainter::point(int x, int y, LColor_color c)
91 setPen(c).drawPoint(x, y);
96 Painter & QLPainter::line(int x1, int y1,
102 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
107 Painter & QLPainter::lines(int const * xp, int const * yp,
115 // Must use new as np is not known at compile time.
116 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
118 for (int i = 0, j = 0; i < np; ++i) {
123 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
129 Painter & QLPainter::rectangle(int x, int y,
135 setPen(col, ls, lw).drawRect(x, y, w, h);
140 Painter & QLPainter::fillRectangle(int x, int y,
144 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
149 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
150 int np, LColor_color col)
152 // Must use new as np is not known at compile time.
153 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
155 //if (1) return *this;
157 for (int i = 0, j = 0; i < np; ++i) {
163 qp_->setBrush(lcolorcache.get(col));
164 qp_->drawPolygon(QPointArray(np, points.get()));
165 qp_->setBrush(Qt::NoBrush);
171 Painter & QLPainter::arc(int x, int y,
172 unsigned int w, unsigned int h,
173 int a1, int a2, LColor_color col)
175 // LyX usings 1/64ths degree, Qt usings 1/16th
176 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
181 Painter & QLPainter::image(int x, int y,
183 lyx::graphics::Image const & i)
185 qp_->drawPixmap(x, y, static_cast<lyx::graphics::QLImage const &>(i).qpixmap(), 0, 0, w, h);
190 Painter & QLPainter::text(int x, int y,
191 string const & s, LyXFont const & f)
193 return text(x, y, s.data(), s.length(), f);
197 Painter & QLPainter::text(int x, int y,
198 char c, LyXFont const & f)
200 char s[2] = { c, '\0' };
201 return text(x, y, s, 1, f);
205 void QLPainter::smallCapsText(int x, int y,
206 QString const & s, LyXFont const & f)
208 LyXFont smallfont(f);
209 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
211 QFont const & qfont = fontloader.get(f);
212 QFont const & qsmallfont = fontloader.get(smallfont);
213 QFontMetrics const & qfontm = QFontMetrics(qfont);
214 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
217 size_t ls = s.length();
218 for (size_t i = 0; i < ls; ++i) {
219 QChar const c = s[i].upper();
221 qp_->setFont(qsmallfont);
222 qp_->drawText(tmpx, y, c);
223 tmpx += qsmallfontm.width(c);
226 qp_->drawText(tmpx, y, c);
227 tmpx += qfontm.width(c);
233 Painter & QLPainter::text(int x, int y,
234 char const * s, size_t ls,
237 setPen(f.realColor());
239 Encoding const * encoding = f.language()->encoding();
240 if (f.isSymbolFont())
241 encoding = encodings.symbol_encoding();
244 #if QT_VERSION >= 300
246 for (size_t i = 0; i < ls; ++i)
247 str[i] = QChar(encoding->ucs(s[i]));
248 // HACK: QT3 refuses to show single compose characters
249 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
252 for (size_t i = 0; i < ls; ++i)
253 str += QChar(encoding->ucs(s[i]));
256 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
257 qp_->setFont(fontloader.get(f));
258 #if QT_VERSION >= 300
259 // We need to draw the text as LTR as we use our own bidi
261 qp_->drawText(x, y, str, -1, QPainter::LTR);
263 qp_->drawText(x, y, str);
266 smallCapsText(x, y, str, f);
269 if (f.underbar() == LyXFont::ON) {
270 underline(f, x, y, font_metrics::width(s, ls, f));