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"
15 #include "QWorkArea.h"
17 #include "lcolorcache.h"
18 #include "qfont_loader.h"
24 #include "frontends/font_metrics.h"
32 QLPainter::QLPainter(QWorkArea & qwa)
33 : Painter(), owner_(qwa), paint_check_(0)
35 qp_.reset(new QPainter);
39 void QLPainter::start()
41 if (++paint_check_ == 1)
42 qp_->begin(owner_.getPixmap());
48 if (paint_check_ == 0) {
49 lyxerr << "ended painting whilst not painting ??" << endl;
50 } else if (--paint_check_ == 0) {
56 int QLPainter::paperWidth() const
58 return owner_.workWidth();
62 int QLPainter::paperHeight() const
64 return owner_.workHeight();
68 QPainter & QLPainter::setPen(LColor_color c,
69 Painter::line_style ls, Painter::line_width lw)
71 QPen pen = qp_->pen();
73 pen.setColor(lcolorcache.get(c));
76 case line_solid: pen.setStyle(QPen::SolidLine); break;
77 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
81 case line_thin: pen.setWidth(0); break;
82 case line_thick: pen.setWidth(3); break;
90 void QLPainter::point(int x, int y, LColor_color c)
92 setPen(c).drawPoint(x, y);
96 void QLPainter::line(int x1, int y1, int x2, int y2,
101 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
105 void QLPainter::lines(int const * xp, int const * yp, int np,
112 // Must use new as np is not known at compile time.
113 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
115 for (int i = 0, j = 0; i < np; ++i) {
120 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
124 void QLPainter::rectangle(int x, int y, int w, int h,
129 setPen(col, ls, lw).drawRect(x, y, w, h);
133 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
135 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
139 void QLPainter::fillPolygon(int const * xp, int const * yp,
140 int np, LColor_color col)
142 // Must use new as np is not known at compile time.
143 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
147 for (int i = 0, j = 0; i < np; ++i) {
153 qp_->setBrush(lcolorcache.get(col));
154 qp_->drawPolygon(QPointArray(np, points.get()));
155 qp_->setBrush(Qt::NoBrush);
159 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
160 int a1, int a2, LColor_color col)
162 // LyX usings 1/64ths degree, Qt usings 1/16th
163 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
167 void QLPainter::image(int x, int y, int w, int h,
168 lyx::graphics::Image const & i)
170 lyx::graphics::QLImage const & qlimage =
171 static_cast<lyx::graphics::QLImage const &>(i);
173 fillRectangle(x, y, w, h, LColor::graphicsbg);
174 qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
178 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
180 return text(x, y, s.data(), s.length(), f);
184 void QLPainter::text(int x, int y, char c, LyXFont const & f)
186 char s[2] = { c, '\0' };
187 return text(x, y, s, 1, f);
191 void QLPainter::smallCapsText(int x, int y,
192 QString const & s, LyXFont const & f)
194 LyXFont smallfont(f);
195 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
197 QFont const & qfont = fontloader.get(f);
198 QFont const & qsmallfont = fontloader.get(smallfont);
199 QFontMetrics const & qfontm = QFontMetrics(qfont);
200 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
203 size_t ls = s.length();
204 for (size_t i = 0; i < ls; ++i) {
205 // Brain-dead MSVC wants at(i) rather than operator[]
206 QChar const c = s.at(i).upper();
208 qp_->setFont(qsmallfont);
209 qp_->drawText(tmpx, y, c);
210 tmpx += qsmallfontm.width(c);
213 qp_->drawText(tmpx, y, c);
214 tmpx += qfontm.width(c);
220 void QLPainter::text(int x, int y, char const * s, size_t ls,
223 setPen(f.realColor());
225 Encoding const * encoding = f.language()->encoding();
226 if (f.isSymbolFont())
227 encoding = encodings.symbol_encoding();
230 #if QT_VERSION >= 300
232 for (size_t i = 0; i < ls; ++i)
233 // Brain-dead MSVC wants at(i) rather than operator[]
234 str.at(i) = QChar(encoding->ucs(s[i]));
235 // HACK: QT3 refuses to show single compose characters
236 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
239 for (size_t i = 0; i < ls; ++i)
240 str += QChar(encoding->ucs(s[i]));
243 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
244 qp_->setFont(fontloader.get(f));
245 #if QT_VERSION >= 300
246 // We need to draw the text as LTR as we use our own bidi
248 qp_->drawText(x, y, str, -1, QPainter::LTR);
250 qp_->drawText(x, y, str);
253 smallCapsText(x, y, str, f);
256 if (f.underbar() == LyXFont::ON) {
257 underline(f, x, y, font_metrics::width(s, ls, f));