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
18 #include <boost/scoped_array.hpp>
20 #include "font_metrics.h"
21 #include "support/lstrings.h"
28 #include "QWorkArea.h"
29 #include "qfont_loader.h"
30 #include "QLPainter.h"
39 QLPainter::QLPainter(QWorkArea & qwa)
40 : Painter(), owner_(qwa), paint_check_(0)
42 qp_.reset(new QPainter());
46 void QLPainter::start()
48 if (++paint_check_ == 1)
49 qp_->begin(owner_.getPixmap());
55 if (paint_check_ == 0) {
56 lyxerr << "ended painting whilst not painting ??" << endl;
57 } else if (--paint_check_ == 0) {
63 int QLPainter::paperWidth() const
65 return owner_.workWidth();
69 int QLPainter::paperHeight() const
71 return owner_.workHeight();
75 QPainter & QLPainter::setPen(LColor::color c,
76 Painter::line_style ls, Painter::line_width lw)
78 QPen pen = qp_->pen();
80 pen.setColor(lcolor.getX11Name(c).c_str());
83 case line_solid: pen.setStyle(QPen::SolidLine); break;
84 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
88 case line_thin: pen.setWidth(0); break;
89 case line_thick: pen.setWidth(3); break;
97 Painter & QLPainter::point(int x, int y, LColor::color c)
99 setPen(c).drawPoint(x, y);
104 Painter & QLPainter::line(int x1, int y1,
110 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
115 Painter & QLPainter::lines(int const * xp, int const * yp,
123 // Must use new as np is not known at compile time.
124 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
128 for (int i = 0; i < np; ++i) {
133 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
139 Painter & QLPainter::rectangle(int x, int y,
145 //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
146 setPen(col, ls, lw).drawRect(x, y, w, h);
151 Painter & QLPainter::fillRectangle(int x, int y,
155 //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
156 qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
161 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
162 int np, LColor::color col)
164 // Must use new as np is not known at compile time.
165 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
167 //if (1) return *this;
170 for (int i = 0; i < np; ++i) {
176 qp_->setBrush(lcolor.getX11Name(col).c_str());
177 qp_->drawPolygon(QPointArray(np, points.get()));
178 qp_->setBrush(Qt::NoBrush);
184 Painter & QLPainter::arc(int x, int y,
185 unsigned int w, unsigned int h,
186 int a1, int a2, LColor::color col)
188 lyxerr[Debug::GUI] << "arc: " << x<<","<<y
189 << " " << w<<","<<h << ", angles "
190 << a1 << " - " << a2 << endl;
191 // LyX usings 1/64ths degree, Qt usings 1/16th
192 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
197 Painter & QLPainter::image(int x, int y,
199 grfx::Image const & i)
201 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
206 Painter & QLPainter::text(int x, int y,
207 string const & s, LyXFont const & f)
209 return text(x, y, s.data(), s.length(), f);
213 Painter & QLPainter::text(int x, int y,
214 char c, LyXFont const & f)
216 char s[2] = { c, '\0' };
217 return text(x, y, s, 1, f);
221 void QLPainter::smallCapsText(int x, int y,
222 QString const & s, LyXFont const & f)
224 LyXFont smallfont(f);
225 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
227 QFont const & qfont = fontloader.get(f);
228 QFont const & qsmallfont = fontloader.get(smallfont);
229 QFontMetrics const & qfontm = QFontMetrics(qfont);
230 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
233 size_t ls = s.length();
234 for (size_t i = 0; i < ls; ++i) {
235 QChar const c = s[i].upper();
237 qp_->setFont(qsmallfont);
238 qp_->drawText(tmpx, y, c);
239 tmpx += qsmallfontm.width(c);
242 qp_->drawText(tmpx, y, c);
243 tmpx += qfontm.width(c);
249 Painter & QLPainter::text(int x, int y,
250 char const * s, size_t ls,
255 Encoding const * encoding = f.language()->encoding();
256 if (f.isSymbolFont())
257 encoding = encodings.symbol_encoding();
261 for (size_t i = 0; i < ls; ++i)
262 str[i] = QChar(encoding->ucs(s[i]));
264 #if QT_VERSION >= 0x030000
265 // HACK: QT3 refuses to show single compose characters
266 if (ls = 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
270 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
271 qp_->setFont(fontloader.get(f));
272 #if QT_VERSION >= 0x030000
273 // We need to draw the text as LTR as we use our own bidi
275 qp_->drawText(x, y, str, -1, QPainter::LTR);
277 qp_->drawText(x, y, str);
280 smallCapsText(x, y, str, f);
283 if (f.underbar() == LyXFont::ON) {
284 underline(f, x, y, font_metrics::width(s, ls, f));