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(), qwa_(qwa)
47 int QLPainter::paperWidth() const
49 return qwa_->viewport()->width();
53 int QLPainter::paperHeight() const
55 return qwa_->viewport()->height();
58 void QLPainter::setQPainterPen(QPainter & qp, LColor_color col,
59 Painter::line_style ls, Painter::line_width lw)
61 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
70 pen.setColor(lcolorcache.get(col));
73 case line_solid: pen.setStyle(Qt::SolidLine); break;
74 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
78 case line_thin: pen.setWidth(0); break;
79 case line_thick: pen.setWidth(3); break;
85 void QLPainter::point(int x, int y, LColor_color col)
87 QPainter qp(qwa_->paintDevice());
88 setQPainterPen(qp, col);
93 void QLPainter::line(int x1, int y1, int x2, int y2,
98 QPainter qp(qwa_->paintDevice());
99 setQPainterPen(qp, col, ls, lw);
100 qp.drawLine(x1, y1, x2, y2);
104 void QLPainter::lines(int const * xp, int const * yp, int np,
111 // Must use new as np is not known at compile time.
112 boost::scoped_array<QPoint> points(new QPoint[np]);
114 for (int i = 0; i < np; ++i) {
115 points[i].setX(xp[i]);
116 points[i].setY(yp[i]);
119 QPainter qp(qwa_->paintDevice());
120 setQPainterPen(qp, col, ls, lw);
121 qp.drawPolyline(points.get(), np);
125 void QLPainter::rectangle(int x, int y, int w, int h,
130 QPainter qp(qwa_->paintDevice());
131 setQPainterPen(qp, col, ls, lw);
132 qp.drawRect(x, y, w, h);
136 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
138 QPainter qp(qwa_->paintDevice());
139 qp.fillRect(x, y, w, h, lcolorcache.get(col));
143 void QLPainter::fillPolygon(int const * xp, int const * yp,
144 int np, LColor_color col)
146 // Must use new as np is not known at compile time.
147 boost::scoped_array<QPoint> points(new QPoint[np]);
149 for (int i = 0; i < np; ++i) {
150 points[i].setX(xp[i]);
151 points[i].setY(yp[i]);
154 QPainter qp(qwa_->paintDevice());
155 setQPainterPen(qp, col);
156 qp.setBrush(lcolorcache.get(col));
157 qp.drawPolygon(points.get(), np);
158 qp.setBrush(Qt::NoBrush);
162 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
163 int a1, int a2, LColor_color col)
165 // LyX usings 1/64ths degree, Qt usings 1/16th
166 QPainter qp(qwa_->paintDevice());
167 setQPainterPen(qp, col);
168 qp.drawArc(x, y, w, h, a1 / 4, a2 / 4);
172 void QLPainter::image(int x, int y, int w, int h,
173 lyx::graphics::Image const & i)
175 lyx::graphics::QLImage const & qlimage =
176 static_cast<lyx::graphics::QLImage const &>(i);
178 fillRectangle(x, y, w, h, LColor::graphicsbg);
180 QPainter qp(qwa_->paintDevice());
181 qp.drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
185 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
187 return text(x, y, s.data(), s.length(), f);
191 void QLPainter::text(int x, int y, char c, LyXFont const & f)
193 char s[2] = { c, '\0' };
194 return text(x, y, s, 1, f);
198 void QLPainter::smallCapsText(int x, int y,
199 QString const & s, LyXFont const & f)
201 LyXFont smallfont(f);
202 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
204 QFont const & qfont = fontloader.get(f);
205 QFont const & qsmallfont = fontloader.get(smallfont);
206 QFontMetrics const & qfontm = QFontMetrics(qfont);
207 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
209 QPainter qp(qwa_->paintDevice());
210 setQPainterPen(qp, f.realColor());
212 size_t ls = s.length();
213 for (size_t i = 0; i < ls; ++i) {
214 QChar const c = s[i].upper();
216 qp.setFont(qsmallfont);
217 qp.drawText(tmpx, y, c);
218 tmpx += qsmallfontm.width(c);
221 qp.drawText(tmpx, y, c);
222 tmpx += qfontm.width(c);
228 void QLPainter::text(int x, int y, char const * s, size_t ls,
231 Encoding const * encoding = f.language()->encoding();
232 if (f.isSymbolFont())
233 encoding = encodings.symbol_encoding();
237 for (int i = 0; i < ls; ++i)
238 str[i] = QChar(encoding->ucs(s[i]));
240 // HACK: QT3 refuses to show single compose characters
241 // Still needed with Qt4?
242 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
245 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
246 QPainter qp(qwa_->paintDevice());
247 setQPainterPen(qp, f.realColor());
248 qp.setFont(fontloader.get(f));
249 // We need to draw the text as LTR as we use our own bidi code.
250 qp.setLayoutDirection(Qt::LeftToRight);
251 qp.drawText(x, y, str, -1);
253 smallCapsText(x, y, str, f);
256 if (f.underbar() == LyXFont::ON) {
257 underline(f, x, y, font_metrics::width(s, ls, f));
261 /// draw a pixmap from the image cache
262 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
264 QPainter qp(qwa_->paintDevice());
265 qp.drawPixmap(x, y, pixmap);
268 void QLPainter::drawImage(int x, int y, QImage const & image)
270 QPainter qp(qwa_->paintDevice());
271 qp.drawImage(x, y, image);