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 QPainter & QLPainter::setQPainterPen(QPainter & qp, LColor_color c,
59 Painter::line_style ls, Painter::line_width lw)
63 pen.setColor(lcolorcache.get(c));
66 case line_solid: pen.setStyle(Qt::SolidLine); break;
67 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
71 case line_thin: pen.setWidth(0); break;
72 case line_thick: pen.setWidth(3); break;
80 void QLPainter::point(int x, int y, LColor_color c)
82 QPainter qp(qwa_->paintDevice());
83 setQPainterPen(qp, c).drawPoint(x, y);
87 void QLPainter::line(int x1, int y1, int x2, int y2,
92 QPainter qp(qwa_->paintDevice());
93 setQPainterPen(qp, col, ls, lw).drawLine(x1, y1, x2, y2);
97 void QLPainter::lines(int const * xp, int const * yp, int np,
104 // Must use new as np is not known at compile time.
105 boost::scoped_array<QPoint> points(new QPoint[np]);
107 for (int i = 0; i < np; ++i) {
108 points[i].setX(xp[i]);
109 points[i].setY(yp[i]);
112 QPainter qp(qwa_->paintDevice());
113 setQPainterPen(qp, col, ls, lw).drawPolyline(points.get(), np);
117 void QLPainter::rectangle(int x, int y, int w, int h,
122 QPainter qp(qwa_->paintDevice());
123 setQPainterPen(qp, col, ls, lw).drawRect(x, y, w, h);
127 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
129 QPainter qp(qwa_->paintDevice());
130 qp.fillRect(x, y, w, h, lcolorcache.get(col));
134 void QLPainter::fillPolygon(int const * xp, int const * yp,
135 int np, LColor_color col)
137 // Must use new as np is not known at compile time.
138 boost::scoped_array<QPoint> points(new QPoint[np]);
140 for (int i = 0; i < np; ++i) {
141 points[i].setX(xp[i]);
142 points[i].setY(yp[i]);
145 QPainter qp(qwa_->paintDevice());
146 setQPainterPen(qp, col);
147 qp.setBrush(lcolorcache.get(col));
148 qp.drawPolygon(points.get(), np);
149 qp.setBrush(Qt::NoBrush);
153 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
154 int a1, int a2, LColor_color col)
156 // LyX usings 1/64ths degree, Qt usings 1/16th
157 QPainter qp(qwa_->paintDevice());
158 setQPainterPen(qp, col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
162 void QLPainter::image(int x, int y, int w, int h,
163 lyx::graphics::Image const & i)
165 lyx::graphics::QLImage const & qlimage =
166 static_cast<lyx::graphics::QLImage const &>(i);
168 fillRectangle(x, y, w, h, LColor::graphicsbg);
170 QPainter qp(qwa_->paintDevice());
171 qp.drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
175 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
177 return text(x, y, s.data(), s.length(), f);
181 void QLPainter::text(int x, int y, char c, LyXFont const & f)
183 char s[2] = { c, '\0' };
184 return text(x, y, s, 1, f);
188 void QLPainter::smallCapsText(int x, int y,
189 QString const & s, LyXFont const & f)
191 LyXFont smallfont(f);
192 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
194 QFont const & qfont = fontloader.get(f);
195 QFont const & qsmallfont = fontloader.get(smallfont);
196 QFontMetrics const & qfontm = QFontMetrics(qfont);
197 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
199 QPainter qp(qwa_->paintDevice());
201 size_t ls = s.length();
202 for (size_t i = 0; i < ls; ++i) {
203 // Brain-dead MSVC wants at(i) rather than operator[]
204 QChar const c = s.at(i).upper();
206 qp.setFont(qsmallfont);
207 qp.drawText(tmpx, y, c);
208 tmpx += qsmallfontm.width(c);
211 qp.drawText(tmpx, y, c);
212 tmpx += qfontm.width(c);
218 void QLPainter::text(int x, int y, char const * s, size_t ls,
221 QPainter qp(qwa_->paintDevice());
222 setQPainterPen(qp, f.realColor());
224 Encoding const * encoding = f.language()->encoding();
225 if (f.isSymbolFont())
226 encoding = encodings.symbol_encoding();
230 for (int i = 0; i < ls; ++i)
231 // Brain-dead MSVC wants at(i) rather than operator[]
232 str[i] = QChar(encoding->ucs(s[i]));
233 // HACK: QT3 refuses to show single compose characters
234 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
237 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
238 qp.setFont(fontloader.get(f));
239 // We need to draw the text as LTR as we use our own bidi
241 qp.setLayoutDirection(Qt::LeftToRight);
242 qp.drawText(x, y, str, -1);
244 smallCapsText(x, y, str, f);
247 if (f.underbar() == LyXFont::ON) {
248 underline(f, x, y, font_metrics::width(s, ls, f));
252 /// draw a pixmap from the image cache
253 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
255 QPainter qp(qwa_->paintDevice());
256 qp.drawPixmap(x, y, pixmap);
259 void QLPainter::drawImage(int x, int y, QImage const & image)
261 QPainter qp(qwa_->paintDevice());
262 qp.drawImage(x, y, image);