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
14 #include "font_metrics.h"
15 #include "support/lstrings.h"
22 #include "QWorkArea.h"
23 #include "qfont_loader.h"
24 #include "QLPainter.h"
26 #include "qt_helpers.h"
27 #include "lcolorcache.h"
29 #include <boost/scoped_array.hpp>
40 QLPainter::QLPainter(QWorkArea & qwa)
41 : Painter(), owner_(qwa), paint_check_(0)
43 qp_.reset(new QPainter);
47 void QLPainter::start()
49 if (++paint_check_ == 1)
50 qp_->begin(owner_.getPixmap());
56 if (paint_check_ == 0) {
57 lyxerr << "ended painting whilst not painting ??" << endl;
58 } else if (--paint_check_ == 0) {
64 int QLPainter::paperWidth() const
66 return owner_.workWidth();
70 int QLPainter::paperHeight() const
72 return owner_.workHeight();
76 QPainter & QLPainter::setPen(LColor::color c,
77 Painter::line_style ls, Painter::line_width lw)
79 QPen pen = qp_->pen();
81 pen.setColor(lcolorcache.get(c));
84 case line_solid: pen.setStyle(QPen::SolidLine); break;
85 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
89 case line_thin: pen.setWidth(0); break;
90 case line_thick: pen.setWidth(3); break;
98 Painter & QLPainter::point(int x, int y, LColor::color c)
100 setPen(c).drawPoint(x, y);
105 Painter & QLPainter::line(int x1, int y1,
111 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
116 Painter & QLPainter::lines(int const * xp, int const * yp,
124 // Must use new as np is not known at compile time.
125 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
127 for (int i = 0, j = 0; i < np; ++i) {
132 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
138 Painter & QLPainter::rectangle(int x, int y,
144 setPen(col, ls, lw).drawRect(x, y, w, h);
149 Painter & QLPainter::fillRectangle(int x, int y,
153 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
158 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
159 int np, LColor::color col)
161 // Must use new as np is not known at compile time.
162 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
164 //if (1) return *this;
166 for (int i = 0, j = 0; i < np; ++i) {
172 qp_->setBrush(lcolorcache.get(col));
173 qp_->drawPolygon(QPointArray(np, points.get()));
174 qp_->setBrush(Qt::NoBrush);
180 Painter & QLPainter::arc(int x, int y,
181 unsigned int w, unsigned int h,
182 int a1, int a2, LColor::color col)
184 // LyX usings 1/64ths degree, Qt usings 1/16th
185 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
190 Painter & QLPainter::image(int x, int y,
192 grfx::Image const & i)
194 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
199 Painter & QLPainter::text(int x, int y,
200 string const & s, LyXFont const & f)
202 return text(x, y, s.data(), s.length(), f);
206 Painter & QLPainter::text(int x, int y,
207 char c, LyXFont const & f)
209 char s[2] = { c, '\0' };
210 return text(x, y, s, 1, f);
214 void QLPainter::smallCapsText(int x, int y,
215 QString const & s, LyXFont const & f)
217 LyXFont smallfont(f);
218 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
220 QFont const & qfont = fontloader.get(f);
221 QFont const & qsmallfont = fontloader.get(smallfont);
222 QFontMetrics const & qfontm = QFontMetrics(qfont);
223 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
226 size_t ls = s.length();
227 for (size_t i = 0; i < ls; ++i) {
228 QChar const c = s[i].upper();
230 qp_->setFont(qsmallfont);
231 qp_->drawText(tmpx, y, c);
232 tmpx += qsmallfontm.width(c);
235 qp_->drawText(tmpx, y, c);
236 tmpx += qfontm.width(c);
242 Painter & QLPainter::text(int x, int y,
243 char const * s, size_t ls,
246 setPen(f.realColor());
248 Encoding const * encoding = f.language()->encoding();
249 if (f.isSymbolFont())
250 encoding = encodings.symbol_encoding();
253 #if QT_VERSION >= 300
255 for (size_t i = 0; i < ls; ++i)
256 str[i] = QChar(encoding->ucs(s[i]));
257 // HACK: QT3 refuses to show single compose characters
258 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
261 for (size_t i = 0; i < ls; ++i)
262 str += QChar(encoding->ucs(s[i]));
265 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
266 qp_->setFont(fontloader.get(f));
267 #if QT_VERSION >= 300
268 // We need to draw the text as LTR as we use our own bidi
270 qp_->drawText(x, y, str, -1, QPainter::LTR);
272 qp_->drawText(x, y, str);
275 smallCapsText(x, y, str, f);
278 if (f.underbar() == LyXFont::ON) {
279 underline(f, x, y, font_metrics::width(s, ls, f));