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 #include "font_metrics.h"
15 #include "support/lstrings.h"
22 #include "QWorkArea.h"
23 #include "qfont_loader.h"
24 #include "QLPainter.h"
26 #include "qt_helpers.h"
28 #include <boost/scoped_array.hpp>
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(toqstr(lcolor.getX11Name(c)));
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]);
126 for (int i = 0, j = 0; i < np; ++i) {
131 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
137 Painter & QLPainter::rectangle(int x, int y,
143 //lyxerr << "rectangle " << x << ',' << y << ' '
144 // << w << ',' << h << endl;
145 setPen(col, ls, lw).drawRect(x, y, w, h);
150 Painter & QLPainter::fillRectangle(int x, int y,
154 //lyxerr << "fillRectangle " << x << ',' << y << ' '
155 // << w << ',' << h << endl;
156 qp_->fillRect(x, y, w, h, QColor(toqstr(lcolor.getX11Name(col))));
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;
169 for (int i = 0, j = 0; i < np; ++i) {
175 qp_->setBrush(toqstr(lcolor.getX11Name(col)));
176 qp_->drawPolygon(QPointArray(np, points.get()));
177 qp_->setBrush(Qt::NoBrush);
183 Painter & QLPainter::arc(int x, int y,
184 unsigned int w, unsigned int h,
185 int a1, int a2, LColor::color col)
187 lyxerr[Debug::GUI] << "arc: " << x << ',' << y
188 << ' ' << w << ',' << h << ", angles "
189 << a1 << " - " << a2 << endl;
190 // LyX usings 1/64ths degree, Qt usings 1/16th
191 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
196 Painter & QLPainter::image(int x, int y,
198 grfx::Image const & i)
200 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
205 Painter & QLPainter::text(int x, int y,
206 string const & s, LyXFont const & f)
208 return text(x, y, s.data(), s.length(), f);
212 Painter & QLPainter::text(int x, int y,
213 char c, LyXFont const & f)
215 char s[2] = { c, '\0' };
216 return text(x, y, s, 1, f);
220 void QLPainter::smallCapsText(int x, int y,
221 QString const & s, LyXFont const & f)
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 size_t ls = s.length();
233 for (size_t i = 0; i < ls; ++i) {
234 QChar const c = s[i].upper();
236 qp_->setFont(qsmallfont);
237 qp_->drawText(tmpx, y, c);
238 tmpx += qsmallfontm.width(c);
241 qp_->drawText(tmpx, y, c);
242 tmpx += qfontm.width(c);
248 Painter & QLPainter::text(int x, int y,
249 char const * s, size_t ls,
252 setPen(f.realColor());
254 Encoding const * encoding = f.language()->encoding();
255 if (f.isSymbolFont())
256 encoding = encodings.symbol_encoding();
259 #if QT_VERSION >= 300
261 for (size_t i = 0; i < ls; ++i)
262 str[i] = QChar(encoding->ucs(s[i]));
263 // HACK: QT3 refuses to show single compose characters
264 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
267 for (size_t i = 0; i < ls; ++i)
268 str += QChar(encoding->ucs(s[i]));
271 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
272 qp_->setFont(fontloader.get(f));
273 #if QT_VERSION >= 300
274 // We need to draw the text as LTR as we use our own bidi
276 qp_->drawText(x, y, str, -1, QPainter::LTR);
278 qp_->drawText(x, y, str);
281 smallCapsText(x, y, str, f);
284 if (f.underbar() == LyXFont::ON) {
285 underline(f, x, y, font_metrics::width(s, ls, f));