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"
34 QLPainter::QLPainter(QWorkArea & qwa)
35 : Painter(), owner_(qwa), paint_check_(0)
37 qp_.reset(new QPainter);
41 void QLPainter::start()
43 if (++paint_check_ == 1)
44 qp_->begin(owner_.getPixmap());
50 if (paint_check_ == 0) {
51 lyxerr << "ended painting whilst not painting ??" << endl;
52 } else if (--paint_check_ == 0) {
58 int QLPainter::paperWidth() const
60 return owner_.workWidth();
64 int QLPainter::paperHeight() const
66 return owner_.workHeight();
70 QPainter & QLPainter::setPen(LColor_color c,
71 Painter::line_style ls, Painter::line_width lw)
73 QPen pen = qp_->pen();
75 pen.setColor(lcolorcache.get(c));
78 case line_solid: pen.setStyle(QPen::SolidLine); break;
79 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
83 case line_thin: pen.setWidth(0); break;
84 case line_thick: pen.setWidth(3); break;
92 void QLPainter::point(int x, int y, LColor_color c)
94 setPen(c).drawPoint(x, y);
98 void QLPainter::line(int x1, int y1, int x2, int y2,
103 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
107 void QLPainter::lines(int const * xp, int const * yp, int np,
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()));
126 void QLPainter::rectangle(int x, int y, int w, int h,
131 setPen(col, ls, lw).drawRect(x, y, w, h);
135 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
137 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
141 void QLPainter::fillPolygon(int const * xp, int const * yp,
142 int np, LColor_color col)
144 // Must use new as np is not known at compile time.
145 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
149 for (int i = 0, j = 0; i < np; ++i) {
155 qp_->setBrush(lcolorcache.get(col));
156 qp_->drawPolygon(QPointArray(np, points.get()));
157 qp_->setBrush(Qt::NoBrush);
161 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
162 int a1, int a2, LColor_color col)
164 // LyX usings 1/64ths degree, Qt usings 1/16th
165 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
169 void QLPainter::image(int x, int y, int w, int h,
170 lyx::graphics::Image const & i)
172 lyx::graphics::QLImage const & qlimage =
173 static_cast<lyx::graphics::QLImage const &>(i);
175 fillRectangle(x, y, w, h, LColor::graphicsbg);
176 bitBlt(qp_->device(), x, y, &qlimage.qpixmap(), 0, 0, w, h);
180 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
182 return text(x, y, s.data(), s.length(), f);
186 void QLPainter::text(int x, int y, lyx::char_type c, LyXFont const & f)
188 char s[2] = { c, '\0' };
189 return text(x, y, s, 1, f);
193 void QLPainter::smallCapsText(int x, int y,
194 QString const & s, LyXFont const & f)
196 LyXFont smallfont(f);
197 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
199 QFont const & qfont = fontloader.get(f);
200 QFont const & qsmallfont = fontloader.get(smallfont);
201 QFontMetrics const & qfontm = QFontMetrics(qfont);
202 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
205 size_t ls = s.length();
206 for (size_t i = 0; i < ls; ++i) {
207 // Brain-dead MSVC wants at(i) rather than operator[]
208 QChar const c = s.at(i).upper();
210 qp_->setFont(qsmallfont);
211 qp_->drawText(tmpx, y, c);
212 tmpx += qsmallfontm.width(c);
215 qp_->drawText(tmpx, y, c);
216 tmpx += qfontm.width(c);
222 void QLPainter::text(int x, int y, char const * s, size_t ls,
225 setPen(f.realColor());
227 Encoding const * encoding = f.language()->encoding();
228 if (f.isSymbolFont())
229 encoding = encodings.symbol_encoding();
233 for (size_t i = 0; i < ls; ++i)
234 // Brain-dead MSVC wants at(i) rather than operator[]
235 str.at(i) = QChar(encoding->ucs(s[i]));
236 // HACK: QT3 refuses to show single compose characters
237 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
240 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
241 qp_->setFont(fontloader.get(f));
242 // We need to draw the text as LTR as we use our own bidi
244 qp_->drawText(x, y, str, -1, QPainter::LTR);
246 smallCapsText(x, y, str, f);
249 if (f.underbar() == LyXFont::ON) {
250 underline(f, x, y, font_metrics::width(s, ls, f));
254 } // namespace frontend