3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "QLPainter.h"
16 #include "GuiApplication.h"
17 #include "GuiWorkArea.h"
20 #include "GuiApplication.h"
21 #include "qt_helpers.h"
27 #include "frontends/FontMetrics.h"
29 #include "support/unicode.h"
43 QLPainter::QLPainter(QWidget * qwa)
46 //lyxerr << "QLPainter::start()" << endl;
47 QPainter::begin(qwa_);
48 setRenderHint(QPainter::TextAntialiasing);
49 // new QPainter has default QPen:
50 current_color_ = LColor::black;
51 current_ls_ = line_solid;
52 current_lw_ = line_thin;
56 QLPainter::~QLPainter()
59 //lyxerr << "QLPainter::end()" << endl;
63 int QLPainter::paperWidth() const
69 int QLPainter::paperHeight() const
71 return qwa_->height();
75 void QLPainter::setQPainterPen(LColor_color col,
76 Painter::line_style ls, Painter::line_width lw)
78 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
85 QPen pen = QPainter::pen();
87 pen.setColor(guiApp->colorCache().get(col));
90 case line_solid: pen.setStyle(Qt::SolidLine); break;
91 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
95 case line_thin: pen.setWidth(0); break;
96 case line_thick: pen.setWidth(3); break;
103 void QLPainter::point(int x, int y, LColor_color col)
110 void QLPainter::line(int x1, int y1, int x2, int y2,
115 setQPainterPen(col, ls, lw);
116 drawLine(x1, y1, x2, y2);
120 void QLPainter::lines(int const * xp, int const * yp, int np,
127 // Must use new as np is not known at compile time.
128 boost::scoped_array<QPoint> points(new QPoint[np]);
130 for (int i = 0; i < np; ++i) {
131 points[i].setX(xp[i]);
132 points[i].setY(yp[i]);
135 setQPainterPen(col, ls, lw);
136 drawPolyline(points.get(), np);
140 void QLPainter::rectangle(int x, int y, int w, int h,
145 setQPainterPen(col, ls, lw);
146 drawRect(x, y, w, h);
150 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
152 fillRect(x, y, w, h, guiApp->colorCache().get(col));
156 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
157 int a1, int a2, LColor_color col)
159 // LyX usings 1/64ths degree, Qt usings 1/16th
161 drawArc(x, y, w, h, a1 / 4, a2 / 4);
165 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
167 graphics::QLImage const & qlimage =
168 static_cast<graphics::QLImage const &>(i);
170 fillRectangle(x, y, w, h, LColor::graphicsbg);
172 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
176 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
178 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
182 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
184 char_type s[2] = { c, char_type('\0') };
185 return text(x, y, s, 1, f);
189 int QLPainter::smallCapsText(int x, int y,
190 QString const & s, LyXFont const & f)
192 LyXFont smallfont(f);
193 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
195 QFont const & qfont = guiApp->guiFontLoader().get(f);
196 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
198 setQPainterPen(f.realColor());
200 size_t const ls = s.length();
201 for (unsigned int i = 0; i < ls; ++i) {
202 QChar const c = s[i].toUpper();
208 drawText(x + textwidth, y, c);
209 textwidth += fontMetrics().width(c);
215 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
219 Encoding const * encoding = f.language()->encoding();
220 if (f.isSymbolFont())
221 encoding = encodings.symbol_encoding();
225 ucs4_to_qstring(s, ls, str);
228 // HACK: QT3 refuses to show single compose characters
229 // Still needed with Qt4?
230 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
234 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
238 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
239 setQPainterPen(f.realColor());
240 if (font() != fi.font)
242 // We need to draw the text as LTR as we use our own bidi code.
243 setLayoutDirection(Qt::LeftToRight);
245 // Here we use the font width cache instead of
246 // textwidth = fontMetrics().width(str);
247 // because the above is awfully expensive on MacOSX
248 textwidth = guiApp->guiFontLoader().metrics(f).width(s, ls);
250 textwidth = smallCapsText(x, y, str, f);
253 if (f.underbar() == LyXFont::ON) {
254 underline(f, x, y, textwidth);
261 } // namespace frontend