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 "GuiFontMetrics.h"
20 #include "GuiApplication.h"
21 #include "qt_helpers.h"
27 #include "support/unicode.h"
35 QLPainter::QLPainter(QPaintDevice * device)
36 : QPainter(device), Painter()
38 setRenderHint(QPainter::TextAntialiasing);
39 // new QPainter has default QPen:
40 current_color_ = LColor::black;
41 current_ls_ = line_solid;
42 current_lw_ = line_thin;
46 QLPainter::~QLPainter()
49 //lyxerr << "QLPainter::end()" << endl;
53 void QLPainter::setQPainterPen(LColor_color col,
54 Painter::line_style ls, Painter::line_width lw)
56 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
63 QPen pen = QPainter::pen();
65 pen.setColor(guiApp->colorCache().get(col));
68 case line_solid: pen.setStyle(Qt::SolidLine); break;
69 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
73 case line_thin: pen.setWidth(0); break;
74 case line_thick: pen.setWidth(3); break;
81 void QLPainter::point(int x, int y, LColor_color col)
83 if (!isDrawingEnabled())
91 void QLPainter::line(int x1, int y1, int x2, int y2,
96 if (!isDrawingEnabled())
99 setQPainterPen(col, ls, lw);
100 drawLine(x1, y1, x2, y2);
104 void QLPainter::lines(int const * xp, int const * yp, int np,
111 // Must use new as np is not known at compile time.
112 boost::scoped_array<QPoint> points(new QPoint[np]);
114 for (int i = 0; i < np; ++i) {
115 points[i].setX(xp[i]);
116 points[i].setY(yp[i]);
119 if (!isDrawingEnabled())
122 setQPainterPen(col, ls, lw);
123 drawPolyline(points.get(), np);
127 void QLPainter::rectangle(int x, int y, int w, int h,
132 if (!isDrawingEnabled())
135 setQPainterPen(col, ls, lw);
136 drawRect(x, y, w, h);
140 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
142 fillRect(x, y, w, h, guiApp->colorCache().get(col));
146 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
147 int a1, int a2, LColor_color col)
149 if (!isDrawingEnabled())
152 // LyX usings 1/64ths degree, Qt usings 1/16th
154 drawArc(x, y, w, h, a1 / 4, a2 / 4);
158 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
160 graphics::QLImage const & qlimage =
161 static_cast<graphics::QLImage const &>(i);
163 fillRectangle(x, y, w, h, LColor::graphicsbg);
165 if (!isDrawingEnabled())
168 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
172 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
174 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
178 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
180 char_type s[2] = { c, char_type('\0') };
181 return text(x, y, s, 1, f);
185 int QLPainter::smallCapsText(int x, int y,
186 QString const & s, LyXFont const & f)
188 LyXFont smallfont(f);
189 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
191 QFont const & qfont = guiApp->guiFontLoader().get(f);
192 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
194 setQPainterPen(f.realColor());
196 size_t const ls = s.length();
197 for (unsigned int i = 0; i < ls; ++i) {
198 QChar const c = s[i].toUpper();
204 if (isDrawingEnabled())
205 drawText(x + textwidth, y, c);
206 textwidth += fontMetrics().width(c);
212 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
216 Encoding const * encoding = f.language()->encoding();
217 if (f.isSymbolFont())
218 encoding = encodings.symbol_encoding();
222 ucs4_to_qstring(s, ls, str);
225 // HACK: QT3 refuses to show single compose characters
226 // Still needed with Qt4?
227 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
231 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
235 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
236 setQPainterPen(f.realColor());
237 if (font() != fi.font)
239 // We need to draw the text as LTR as we use our own bidi code.
240 setLayoutDirection(Qt::LeftToRight);
241 if (isDrawingEnabled())
243 // Here we use the font width cache instead of
244 // textwidth = fontMetrics().width(str);
245 // because the above is awfully expensive on MacOSX
246 textwidth = fi.metrics->width(str);
248 textwidth = smallCapsText(x, y, str, f);
251 if (f.underbar() == LyXFont::ON) {
252 underline(f, x, y, textwidth);
259 } // namespace frontend