3 * Copyright 1998-2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
12 #pragma implementation
16 #include <boost/scoped_array.hpp>
18 #include "font_metrics.h"
19 #include "support/lstrings.h"
24 #include "QWorkArea.h"
25 #include "qfont_loader.h"
26 #include "QLPainter.h"
35 QLPainter::QLPainter(QWorkArea & qwa)
36 : Painter(), owner_(qwa), paint_check_(0)
38 qp_.reset(new QPainter());
42 void QLPainter::start()
44 if (++paint_check_ == 1)
45 qp_->begin(owner_.getPixmap());
51 if (paint_check_ == 0) {
52 lyxerr << "ended painting whilst not painting ??" << endl;
53 } else if (--paint_check_ == 0) {
59 int QLPainter::paperWidth() const
61 return owner_.workWidth();
65 int QLPainter::paperHeight() const
67 return owner_.workHeight();
71 QPainter & QLPainter::setPen(LColor::color c,
72 Painter::line_style ls, Painter::line_width lw)
74 QPen pen = qp_->pen();
76 pen.setColor(lcolor.getX11Name(c).c_str());
79 case line_solid: pen.setStyle(QPen::SolidLine); break;
80 case line_onoffdash: pen.setStyle(QPen::DashLine); break;
84 case line_thin: pen.setWidth(0); break;
85 case line_thick: pen.setWidth(3); break;
93 Painter & QLPainter::point(int x, int y, LColor::color c)
95 setPen(c).drawPoint(x, y);
100 Painter & QLPainter::line(int x1, int y1,
106 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
111 Painter & QLPainter::lines(int const * xp, int const * yp,
119 // Must use new as np is not known at compile time.
120 boost::scoped_array<QCOORD> points(new QCOORD [np * 2]);
124 for (int i = 0; i < np; ++i) {
129 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
135 Painter & QLPainter::rectangle(int x, int y,
141 setPen(col, ls, lw).drawRect(x, y, w, h);
146 Painter & QLPainter::fillRectangle(int x, int y,
150 qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
155 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
156 int np, LColor::color col)
160 // Must use new as np is not known at compile time.
161 boost::scoped_array<QCOORD> points(new QCOORD [np * 2]);
165 for (int i = 0; i < np; ++i) {
170 setPen(col).drawPolygon(QPointArray(np, points.get()));
176 Painter & QLPainter::arc(int x, int y,
177 unsigned int w, unsigned int h,
178 int a1, int a2, LColor::color col)
181 setPen(col).drawArc(x, y, w, h, a1, a2);
186 Painter & QLPainter::image(int x, int y,
188 grfx::Image const & i)
190 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
195 Painter & QLPainter::text(int x, int y,
196 string const & s, LyXFont const & f)
198 return text(x, y, s.data(), s.length(), f);
202 Painter & QLPainter::text(int x, int y,
203 char c, LyXFont const & f)
205 char s[2] = { c, '\0' };
206 return text(x, y, s, 1, f);
210 void QLPainter::smallCapsText(int x, int y,
211 char const * s, size_t ls,
214 LyXFont smallfont(f);
215 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
217 QFont const & qfont = fontloader.get(f);
218 QFont const & qsmallfont = fontloader.get(smallfont);
219 QFontMetrics const & qfontm = QFontMetrics(qfont);
220 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
223 for (size_t i = 0; i < ls; ++i) {
224 char const c = uppercase(s[i]);
226 qp_->setFont(qsmallfont);
227 qp_->drawText(tmpx, y, &c, 1);
228 tmpx += qsmallfontm.width(&c, 1);
231 qp_->drawText(tmpx, y, &c, 1);
232 tmpx += qfontm.width(&c, 1);
238 Painter & QLPainter::text(int x, int y,
239 char const * s, size_t ls,
244 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
245 qp_->setFont(fontloader.get(f));
246 qp_->drawText(x, y, s, ls);
248 smallCapsText(x, y, s, ls, f);
251 if (f.underbar() == LyXFont::ON) {
252 underline(f, x, y, font_metrics::width(s, ls, f));