3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "QLPainter.h"
16 #include "QWorkArea.h"
19 #include "lcolorcache.h"
20 #include "qfont_loader.h"
26 #include "frontends/font_metrics.h"
37 QLPainter::~QLPainter()
41 QLPainter::QLPainter(QWorkArea * qwa)
42 : Painter(), paint_check_(0), qwa_(qwa)
46 void QLPainter::start()
53 // if (qp_->isActive())
58 int QLPainter::paperWidth() const
60 return qwa_->viewport()->width();
64 int QLPainter::paperHeight() const
66 return qwa_->viewport()->height();
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(Qt::SolidLine); break;
79 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
83 case line_thin: pen.setWidth(0); break;
84 case line_thick: pen.setWidth(3); break;
92 QPainter & QLPainter::setQPainterPen(QPainter & qp, LColor_color c,
93 Painter::line_style ls, Painter::line_width lw)
97 pen.setColor(lcolorcache.get(c));
100 case line_solid: pen.setStyle(Qt::SolidLine); break;
101 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
105 case line_thin: pen.setWidth(0); break;
106 case line_thick: pen.setWidth(3); break;
114 void QLPainter::point(int x, int y, LColor_color c)
116 QPainter qp(qwa_->pixmap());
117 setQPainterPen(qp, c).drawPoint(x, y);
121 void QLPainter::line(int x1, int y1, int x2, int y2,
126 QPainter qp(qwa_->pixmap());
127 setQPainterPen(qp, col, ls, lw).drawLine(x1, y1, x2, y2);
131 void QLPainter::lines(int const * xp, int const * yp, int np,
138 // Must use new as np is not known at compile time.
139 boost::scoped_array<QPoint> points(new QPoint[np]);
141 for (int i = 0; i < np; ++i) {
142 points[i].setX(xp[i]);
143 points[i].setY(yp[i]);
146 QPainter qp(qwa_->pixmap());
147 setQPainterPen(qp, col, ls, lw).drawPolyline(points.get(), np);
151 void QLPainter::rectangle(int x, int y, int w, int h,
156 QPainter qp(qwa_->pixmap());
157 setQPainterPen(qp, col, ls, lw).drawRect(x, y, w, h);
161 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
163 // lyxerr[Debug::GRAPHICS] << BOOST_CURRENT_FUNCTION
164 // << "\nx=" << x << " y=" << y << " w=" << w << " h=" << h
165 // << " LColor " << col << endl;
167 QPainter qp(qwa_->pixmap());
168 qp.fillRect(x, y, w, h, lcolorcache.get(col));
172 void QLPainter::fillPolygon(int const * xp, int const * yp,
173 int np, LColor_color col)
175 // Must use new as np is not known at compile time.
176 boost::scoped_array<QPoint> points(new QPoint[np]);
178 for (int i = 0; i < np; ++i) {
179 points[i].setX(xp[i]);
180 points[i].setY(yp[i]);
183 QPainter qp(qwa_->pixmap());
184 setQPainterPen(qp, col);
185 qp.setBrush(lcolorcache.get(col));
186 qp.drawPolygon(points.get(), np);
187 qp.setBrush(Qt::NoBrush);
191 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
192 int a1, int a2, LColor_color col)
194 // LyX usings 1/64ths degree, Qt usings 1/16th
195 QPainter qp(qwa_->pixmap());
196 setQPainterPen(qp, col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
200 void QLPainter::image(int x, int y, int w, int h,
201 lyx::graphics::Image const & i)
203 lyx::graphics::QLImage const & qlimage =
204 static_cast<lyx::graphics::QLImage const &>(i);
206 fillRectangle(x, y, w, h, LColor::graphicsbg);
208 QPainter qp(qwa_->pixmap());
209 qp.drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
213 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
215 return text(x, y, s.data(), s.length(), f);
219 void QLPainter::text(int x, int y, char c, LyXFont const & f)
221 char s[2] = { c, '\0' };
222 return text(x, y, s, 1, f);
226 void QLPainter::smallCapsText(int x, int y,
227 QString const & s, LyXFont const & f)
229 LyXFont smallfont(f);
230 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
232 QFont const & qfont = fontloader.get(f);
233 QFont const & qsmallfont = fontloader.get(smallfont);
234 QFontMetrics const & qfontm = QFontMetrics(qfont);
235 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
237 QPainter qp(qwa_->pixmap());
239 size_t ls = s.length();
240 for (size_t i = 0; i < ls; ++i) {
241 // Brain-dead MSVC wants at(i) rather than operator[]
242 QChar const c = s.at(i).upper();
244 qp.setFont(qsmallfont);
245 qp.drawText(tmpx, y, c);
246 tmpx += qsmallfontm.width(c);
249 qp.drawText(tmpx, y, c);
250 tmpx += qfontm.width(c);
256 void QLPainter::text(int x, int y, char const * s, size_t ls,
259 QPainter qp(qwa_->pixmap());
260 setQPainterPen(qp, f.realColor());
262 Encoding const * encoding = f.language()->encoding();
263 if (f.isSymbolFont())
264 encoding = encodings.symbol_encoding();
268 for (int i = 0; i < ls; ++i)
269 // Brain-dead MSVC wants at(i) rather than operator[]
270 str[i] = QChar(encoding->ucs(s[i]));
271 // HACK: QT3 refuses to show single compose characters
272 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
275 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
276 qp.setFont(fontloader.get(f));
277 // We need to draw the text as LTR as we use our own bidi
279 qp.setLayoutDirection(Qt::LeftToRight);
280 qp.drawText(x, y, str, -1);
282 smallCapsText(x, y, str, f);
285 if (f.underbar() == LyXFont::ON) {
286 underline(f, x, y, font_metrics::width(s, ls, f));
290 /// draw a pixmap from the image cache
291 void QLPainter::pixmap(int x, int y, QPixmap const & pixmap)
293 QPainter qp(qwa_->pixmap());
294 qp.drawPixmap(x, y, pixmap);