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.
13 #include "QLPainter.h"
15 #include "QWorkArea.h"
17 #include "lcolorcache.h"
18 #include "qfont_loader.h"
24 #include "support/unicode.h"
26 #include "frontends/FontMetrics.h"
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(lcolorcache.get(c));
80 case line_solid: pen.setStyle(QPen::SolidLine); break;
81 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
85 case line_thin: pen.setWidth(0); break;
86 case line_thick: pen.setWidth(3); break;
94 void QLPainter::point(int x, int y, LColor_color c)
96 setPen(c).drawPoint(x, y);
100 void QLPainter::line(int x1, int y1, int x2, int y2,
105 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
109 void QLPainter::lines(int const * xp, int const * yp, int np,
116 // Must use new as np is not known at compile time.
117 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
119 for (int i = 0, j = 0; i < np; ++i) {
124 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
128 void QLPainter::rectangle(int x, int y, int w, int h,
133 setPen(col, ls, lw).drawRect(x, y, w, h);
137 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
139 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
143 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
144 int a1, int a2, LColor_color col)
146 // LyX usings 1/64ths degree, Qt usings 1/16th
147 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
151 void QLPainter::image(int x, int y, int w, int h,
152 lyx::graphics::Image const & i)
154 lyx::graphics::QLImage const & qlimage =
155 static_cast<lyx::graphics::QLImage const &>(i);
157 fillRectangle(x, y, w, h, LColor::graphicsbg);
158 bitBlt(qp_->device(), x, y, &qlimage.qpixmap(), 0, 0, w, h);
162 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
164 lyxerr << "Drawing string" << endl;
165 return text(x, y, reinterpret_cast<lyx::char_type const *>(s.data()), s.length(), f);
169 int QLPainter::text(int x, int y, lyx::char_type c, LyXFont const & f)
171 char_type s[2] = { c, L'\0' };
172 return text(x, y, s, 1, f);
176 int QLPainter::smallCapsText(int x, int y,
177 QString const & s, LyXFont const & f)
179 LyXFont smallfont(f);
180 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
182 QFont const & qfont = fontloader.get(f);
183 QFont const & qsmallfont = fontloader.get(smallfont);
184 QFontMetrics const & qfontm = QFontMetrics(qfont);
185 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
187 size_t ls = s.length();
189 for (size_t i = 0; i < ls; ++i) {
190 // Brain-dead MSVC wants at(i) rather than operator[]
191 QChar const c = s.at(i).upper();
193 qp_->setFont(qsmallfont);
194 qp_->drawText(x + textwidth, y, c);
195 textwidth += qsmallfontm.width(c);
198 qp_->drawText(x + textwidth, y, c);
199 textwidth += qfontm.width(c);
207 int QLPainter::text(int x, int y, lyx::char_type const * s, size_t ls,
210 lyxerr << "Drawing lyx::char_type const * s" << endl;
211 setPen(f.realColor());
214 Encoding const * encoding = f.language()->encoding();
215 if (f.isSymbolFont())
216 encoding = encodings.symbol_encoding();
223 for (size_t i = 0; i < ls; ++i)
224 // Brain-dead MSVC wants at(i) rather than operator[]
225 str.at(i) = QChar(encoding->ucs(s[i]));
227 //std::vector<lyx::char_type> in(s, s + ls);
228 //std::vector<unsigned short> ucs2 = ucs4_to_ucs2(in);
229 std::vector<unsigned short> ucs2 = ucs4_to_ucs2(s, ls);
231 QString str = QString::fromUcs2(&ucs2[0]);
235 // HACK: QT3 refuses to show single compose characters
236 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
242 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
243 qp_->setFont(fontloader.get(f));
244 // We need to draw the text as LTR as we use our own bidi
246 qp_->drawText(x, y, str, -1, QPainter::LTR);
247 textwidth = qp_->fontMetrics().width(str);
249 textwidth = smallCapsText(x, y, str, f);
252 if (f.underbar() == LyXFont::ON) {
253 underline(f, x, y, textwidth);
259 } // namespace frontend