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 //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
142 setPen(col, ls, lw).drawRect(x, y, w, h);
147 Painter & QLPainter::fillRectangle(int x, int y,
151 //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
152 qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
157 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
158 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]);
163 //if (1) return *this;
166 for (int i = 0; i < np; ++i) {
172 qp_->setBrush(lcolor.getX11Name(col).c_str());
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)
185 setPen(col).drawArc(x, y, w, h, a1, a2);
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 char const * s, size_t ls,
218 LyXFont smallfont(f);
219 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
221 QFont const & qfont = fontloader.get(f);
222 QFont const & qsmallfont = fontloader.get(smallfont);
223 QFontMetrics const & qfontm = QFontMetrics(qfont);
224 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
227 for (size_t i = 0; i < ls; ++i) {
228 char const c = uppercase(s[i]);
230 qp_->setFont(qsmallfont);
231 qp_->drawText(tmpx, y, &c, 1);
232 tmpx += qsmallfontm.width(&c, 1);
235 qp_->drawText(tmpx, y, &c, 1);
236 tmpx += qfontm.width(&c, 1);
242 Painter & QLPainter::text(int x, int y,
243 char const * s, size_t ls,
248 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
249 qp_->setFont(fontloader.get(f));
250 qp_->drawText(x, y, s, ls);
252 smallCapsText(x, y, s, ls, f);
255 if (f.underbar() == LyXFont::ON) {
256 underline(f, x, y, font_metrics::width(s, ls, f));