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 #pragma implementation
17 #include "font_metrics.h"
18 #include "support/lstrings.h"
25 #include "QWorkArea.h"
26 #include "qfont_loader.h"
27 #include "QLPainter.h"
29 #include "qt_helpers.h"
31 #include <boost/scoped_array.hpp>
42 QLPainter::QLPainter(QWorkArea & qwa)
43 : Painter(), owner_(qwa), paint_check_(0)
45 qp_.reset(new QPainter);
49 void QLPainter::start()
51 if (++paint_check_ == 1)
52 qp_->begin(owner_.getPixmap());
58 if (paint_check_ == 0) {
59 lyxerr << "ended painting whilst not painting ??" << endl;
60 } else if (--paint_check_ == 0) {
66 int QLPainter::paperWidth() const
68 return owner_.workWidth();
72 int QLPainter::paperHeight() const
74 return owner_.workHeight();
78 QPainter & QLPainter::setPen(LColor::color c,
79 Painter::line_style ls, Painter::line_width lw)
81 QPen pen = qp_->pen();
83 pen.setColor(toqstr(lcolor.getX11Name(c)));
86 case line_solid: pen.setStyle(QPen::SolidLine); break;
87 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
91 case line_thin: pen.setWidth(0); break;
92 case line_thick: pen.setWidth(3); break;
100 Painter & QLPainter::point(int x, int y, LColor::color c)
102 setPen(c).drawPoint(x, y);
107 Painter & QLPainter::line(int x1, int y1,
113 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
118 Painter & QLPainter::lines(int const * xp, int const * yp,
126 // Must use new as np is not known at compile time.
127 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
129 for (int i = 0, j = 0; i < np; ++i) {
134 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
140 Painter & QLPainter::rectangle(int x, int y,
146 //lyxerr << "rectangle " << x << ',' << y << ' '
147 // << w << ',' << h << endl;
148 setPen(col, ls, lw).drawRect(x, y, w, h);
153 Painter & QLPainter::fillRectangle(int x, int y,
157 //lyxerr << "fillRectangle " << x << ',' << y << ' '
158 // << w << ',' << h << endl;
159 qp_->fillRect(x, y, w, h, QColor(toqstr(lcolor.getX11Name(col))));
164 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
165 int np, LColor::color col)
167 // Must use new as np is not known at compile time.
168 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
170 //if (1) return *this;
172 for (int i = 0, j = 0; i < np; ++i) {
178 qp_->setBrush(toqstr(lcolor.getX11Name(col)));
179 qp_->drawPolygon(QPointArray(np, points.get()));
180 qp_->setBrush(Qt::NoBrush);
186 Painter & QLPainter::arc(int x, int y,
187 unsigned int w, unsigned int h,
188 int a1, int a2, LColor::color col)
190 lyxerr[Debug::GUI] << "arc: " << x << ',' << y
191 << ' ' << w << ',' << h << ", angles "
192 << a1 << " - " << a2 << endl;
193 // LyX usings 1/64ths degree, Qt usings 1/16th
194 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
199 Painter & QLPainter::image(int x, int y,
201 grfx::Image const & i)
203 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
208 Painter & QLPainter::text(int x, int y,
209 string const & s, LyXFont const & f)
211 return text(x, y, s.data(), s.length(), f);
215 Painter & QLPainter::text(int x, int y,
216 char c, LyXFont const & f)
218 char s[2] = { c, '\0' };
219 return text(x, y, s, 1, f);
223 void QLPainter::smallCapsText(int x, int y,
224 QString const & s, LyXFont const & f)
226 LyXFont smallfont(f);
227 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
229 QFont const & qfont = fontloader.get(f);
230 QFont const & qsmallfont = fontloader.get(smallfont);
231 QFontMetrics const & qfontm = QFontMetrics(qfont);
232 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
235 size_t ls = s.length();
236 for (size_t i = 0; i < ls; ++i) {
237 QChar const c = s[i].upper();
239 qp_->setFont(qsmallfont);
240 qp_->drawText(tmpx, y, c);
241 tmpx += qsmallfontm.width(c);
244 qp_->drawText(tmpx, y, c);
245 tmpx += qfontm.width(c);
251 Painter & QLPainter::text(int x, int y,
252 char const * s, size_t ls,
255 setPen(f.realColor());
257 Encoding const * encoding = f.language()->encoding();
258 if (f.isSymbolFont())
259 encoding = encodings.symbol_encoding();
262 #if QT_VERSION >= 300
264 for (size_t i = 0; i < ls; ++i)
265 str[i] = QChar(encoding->ucs(s[i]));
266 // HACK: QT3 refuses to show single compose characters
267 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
270 for (size_t i = 0; i < ls; ++i)
271 str += QChar(encoding->ucs(s[i]));
274 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
275 qp_->setFont(fontloader.get(f));
276 #if QT_VERSION >= 300
277 // We need to draw the text as LTR as we use our own bidi
279 qp_->drawText(x, y, str, -1, QPainter::LTR);
281 qp_->drawText(x, y, str);
284 smallCapsText(x, y, str, f);
287 if (f.underbar() == LyXFont::ON) {
288 underline(f, x, y, font_metrics::width(s, ls, f));