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 "GuiWorkArea.h"
19 #include "ColorCache.h"
20 #include "FontLoader.h"
26 #include "frontends/font_metrics.h"
39 QLPainter::~QLPainter()
43 QLPainter::QLPainter(GuiWorkArea * qwa)
44 : Painter(), qwa_(qwa)
49 int QLPainter::paperWidth() const
51 return qwa_->viewport()->width();
55 int QLPainter::paperHeight() const
57 return qwa_->viewport()->height();
60 void QLPainter::setQPainterPen(QPainter & qp, LColor_color col,
61 Painter::line_style ls, Painter::line_width lw)
63 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
72 pen.setColor(lcolorcache.get(col));
75 case line_solid: pen.setStyle(Qt::SolidLine); break;
76 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
80 case line_thin: pen.setWidth(0); break;
81 case line_thick: pen.setWidth(3); break;
87 void QLPainter::point(int x, int y, LColor_color col)
89 QPainter qp(qwa_->paintDevice());
90 setQPainterPen(qp, col);
95 void QLPainter::line(int x1, int y1, int x2, int y2,
100 QPainter qp(qwa_->paintDevice());
101 setQPainterPen(qp, col, ls, lw);
102 qp.drawLine(x1, y1, x2, y2);
106 void QLPainter::lines(int const * xp, int const * yp, int np,
113 // Must use new as np is not known at compile time.
114 boost::scoped_array<QPoint> points(new QPoint[np]);
116 for (int i = 0; i < np; ++i) {
117 points[i].setX(xp[i]);
118 points[i].setY(yp[i]);
121 QPainter qp(qwa_->paintDevice());
122 setQPainterPen(qp, col, ls, lw);
123 qp.drawPolyline(points.get(), np);
127 void QLPainter::rectangle(int x, int y, int w, int h,
132 QPainter qp(qwa_->paintDevice());
133 setQPainterPen(qp, col, ls, lw);
134 qp.drawRect(x, y, w, h);
138 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
140 QPainter qp(qwa_->paintDevice());
141 qp.fillRect(x, y, w, h, lcolorcache.get(col));
145 void QLPainter::fillPolygon(int const * xp, int const * yp,
146 int np, LColor_color col)
148 // Must use new as np is not known at compile time.
149 boost::scoped_array<QPoint> points(new QPoint[np]);
151 for (int i = 0; i < np; ++i) {
152 points[i].setX(xp[i]);
153 points[i].setY(yp[i]);
156 QPainter qp(qwa_->paintDevice());
157 setQPainterPen(qp, col);
158 qp.setBrush(lcolorcache.get(col));
159 qp.drawPolygon(points.get(), np);
160 qp.setBrush(Qt::NoBrush);
164 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
165 int a1, int a2, LColor_color col)
167 // LyX usings 1/64ths degree, Qt usings 1/16th
168 QPainter qp(qwa_->paintDevice());
169 setQPainterPen(qp, col);
170 qp.drawArc(x, y, w, h, a1 / 4, a2 / 4);
174 void QLPainter::image(int x, int y, int w, int h,
175 lyx::graphics::Image const & i)
177 lyx::graphics::QLImage const & qlimage =
178 static_cast<lyx::graphics::QLImage const &>(i);
180 fillRectangle(x, y, w, h, LColor::graphicsbg);
182 QPainter qp(qwa_->paintDevice());
183 qp.drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
187 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
189 return text(x, y, s.data(), s.length(), f);
193 void QLPainter::text(int x, int y, char c, LyXFont const & f)
195 char s[2] = { c, '\0' };
196 return text(x, y, s, 1, f);
200 void QLPainter::smallCapsText(int x, int y,
201 QString const & s, LyXFont const & f)
203 LyXFont smallfont(f);
204 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
206 QFont const & qfont = fontloader.get(f);
207 QFont const & qsmallfont = fontloader.get(smallfont);
208 QFontMetrics const & qfontm = QFontMetrics(qfont);
209 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
211 QPainter qp(qwa_->paintDevice());
212 setQPainterPen(qp, f.realColor());
214 size_t ls = s.length();
215 for (size_t i = 0; i < ls; ++i) {
216 QChar const c = s[i].upper();
218 qp.setFont(qsmallfont);
219 qp.drawText(tmpx, y, c);
220 tmpx += qsmallfontm.width(c);
223 qp.drawText(tmpx, y, c);
224 tmpx += qfontm.width(c);
230 void QLPainter::text(int x, int y, char const * s, size_t ls,
233 Encoding const * encoding = f.language()->encoding();
234 if (f.isSymbolFont())
235 encoding = encodings.symbol_encoding();
239 for (size_t i = 0; i < ls; ++i)
240 str[i] = QChar(encoding->ucs(s[i]));
242 // HACK: QT3 refuses to show single compose characters
243 // Still needed with Qt4?
244 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
247 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
248 QPainter qp(qwa_->paintDevice());
249 setQPainterPen(qp, f.realColor());
250 qp.setFont(fontloader.get(f));
251 // We need to draw the text as LTR as we use our own bidi code.
252 qp.setLayoutDirection(Qt::LeftToRight);
253 qp.drawText(x, y, str);
255 smallCapsText(x, y, str, f);
258 if (f.underbar() == LyXFont::ON) {
259 underline(f, x, y, font_metrics::width(s, ls, f));
263 /// draw a pixmap from the image cache
264 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
266 QPainter qp(qwa_->paintDevice());
267 qp.drawPixmap(x, y, pixmap);
270 void QLPainter::drawImage(int x, int y, QImage const & image)
272 QPainter qp(qwa_->paintDevice());
273 qp.drawImage(x, y, image);
276 } // namespace frontend