3 * Copyright 1998-2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
12 #pragma implementation
17 //#include "graphics/GraphicsImage.h"
18 #include "font_metrics.h"
19 #include "support/lstrings.h"
24 #include "QWorkArea.h"
25 #include "qfont_loader.h"
26 #include "QLPainter.h"
32 #include <boost/scoped_array.hpp>
36 QLPainter::QLPainter(QWorkArea & qwa)
37 : Painter(), owner_(qwa), paint_check_(0)
39 qp_.reset(new QPainter());
43 void QLPainter::start()
45 if (++paint_check_ == 1)
46 qp_->begin(owner_.getPixmap());
52 if (paint_check_ == 0) {
53 lyxerr << "ended painting whilst not painting ??" << endl;
54 } else if (--paint_check_ == 0) {
60 int QLPainter::paperWidth() const
62 return owner_.workWidth();
66 int QLPainter::paperHeight() const
68 return owner_.workHeight();
72 QPainter & QLPainter::setPen(LColor::color c,
73 Painter::line_style ls, Painter::line_width lw)
75 QPen pen = qp_->pen();
77 pen.setColor(lcolor.getX11Name(c).c_str());
80 case line_solid: pen.setStyle(QPen::SolidLine); break;
81 case line_onoffdash: pen.setStyle(QPen::DashLine); break;
85 case line_thin: pen.setWidth(0); break;
86 case line_thick: pen.setWidth(3); break;
94 Painter & QLPainter::point(int x, int y, LColor::color c)
96 setPen(c).drawPoint(x, y);
101 Painter & QLPainter::line(int x1, int y1,
107 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
112 Painter & QLPainter::lines(int const * xp, int const * yp,
120 // Must use new as np is not known at compile time.
121 boost::scoped_array<QCOORD> points(new QCOORD [np * 2]);
125 for (int i = 0; i < np; ++i) {
130 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
136 Painter & QLPainter::rectangle(int x, int y,
142 setPen(col, ls, lw).drawRect(x, y, w, h);
147 Painter & QLPainter::fillRectangle(int x, int y,
151 qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
156 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
157 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]);
166 for (int i = 0; i < np; ++i) {
171 setPen(col).drawPolygon(QPointArray(np, points.get()));
177 Painter & QLPainter::arc(int x, int y,
178 unsigned int w, unsigned int h,
179 int a1, int a2, LColor::color col)
182 setPen(col).drawArc(x, y, w, h, a1, a2);
187 Painter & QLPainter::image(int , int ,
189 grfx::GImage const & )
193 val.function = GXcopy;
194 GC gc = XCreateGC(display(), owner_.getPixmap(),
196 XCopyArea(display(), image.getPixmap(), owner_.getPixmap(),
197 gc, 0, 0, w, h, x, y);
198 XFreeGC(display(), gc);
204 Painter & QLPainter::text(int x, int y,
205 string const & s, LyXFont const & f)
207 return text(x, y, s.data(), s.length(), f);
211 Painter & QLPainter::text(int x, int y,
212 char c, LyXFont const & f)
214 char s[2] = { c, '\0' };
215 return text(x, y, s, 1, f);
219 void QLPainter::smallCapsText(int x, int y,
220 char const * s, size_t ls,
223 LyXFont smallfont(f);
224 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
226 QFont const & qfont = fontloader.get(f);
227 QFont const & qsmallfont = fontloader.get(smallfont);
228 QFontMetrics const & qfontm = QFontMetrics(qfont);
229 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
232 for (size_t i = 0; i < ls; ++i) {
233 char const c = uppercase(s[i]);
235 qp_->setFont(qsmallfont);
236 qp_->drawText(tmpx, y, &c, 1);
237 tmpx += qsmallfontm.width(&c, 1);
240 qp_->drawText(tmpx, y, &c, 1);
241 tmpx += qfontm.width(&c, 1);
247 Painter & QLPainter::text(int x, int y,
248 char const * s, size_t ls,
253 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
254 qp_->setFont(fontloader.get(f));
255 qp_->drawText(x, y, s, ls);
257 smallCapsText(x, y, s, ls, f);
260 if (f.underbar() == LyXFont::ON) {
261 underline(f, x, y, font_metrics::width(s, ls, f));