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(QWidget * qwa)
38 //lyxerr << "QLPainter::start()" << endl;
39 QPainter::begin(qwa_);
40 setRenderHint(QPainter::TextAntialiasing);
41 // new QPainter has default QPen:
42 current_color_ = LColor::black;
43 current_ls_ = line_solid;
44 current_lw_ = line_thin;
48 QLPainter::~QLPainter()
51 //lyxerr << "QLPainter::end()" << endl;
55 int QLPainter::paperWidth() const
61 int QLPainter::paperHeight() const
63 return qwa_->height();
67 void QLPainter::setQPainterPen(LColor_color col,
68 Painter::line_style ls, Painter::line_width lw)
70 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
77 QPen pen = QPainter::pen();
79 pen.setColor(guiApp->colorCache().get(col));
82 case line_solid: pen.setStyle(Qt::SolidLine); break;
83 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
87 case line_thin: pen.setWidth(0); break;
88 case line_thick: pen.setWidth(3); break;
95 void QLPainter::point(int x, int y, LColor_color col)
102 void QLPainter::line(int x1, int y1, int x2, int y2,
107 setQPainterPen(col, ls, lw);
108 drawLine(x1, y1, x2, y2);
112 void QLPainter::lines(int const * xp, int const * yp, int np,
119 // Must use new as np is not known at compile time.
120 boost::scoped_array<QPoint> points(new QPoint[np]);
122 for (int i = 0; i < np; ++i) {
123 points[i].setX(xp[i]);
124 points[i].setY(yp[i]);
127 setQPainterPen(col, ls, lw);
128 drawPolyline(points.get(), np);
132 void QLPainter::rectangle(int x, int y, int w, int h,
137 setQPainterPen(col, ls, lw);
138 drawRect(x, y, w, h);
142 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
144 fillRect(x, y, w, h, guiApp->colorCache().get(col));
148 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
149 int a1, int a2, LColor_color col)
151 // LyX usings 1/64ths degree, Qt usings 1/16th
153 drawArc(x, y, w, h, a1 / 4, a2 / 4);
157 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
159 graphics::QLImage const & qlimage =
160 static_cast<graphics::QLImage const &>(i);
162 fillRectangle(x, y, w, h, LColor::graphicsbg);
164 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
168 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
170 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
174 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
176 char_type s[2] = { c, char_type('\0') };
177 return text(x, y, s, 1, f);
181 int QLPainter::smallCapsText(int x, int y,
182 QString const & s, LyXFont const & f)
184 LyXFont smallfont(f);
185 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
187 QFont const & qfont = guiApp->guiFontLoader().get(f);
188 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
190 setQPainterPen(f.realColor());
192 size_t const ls = s.length();
193 for (unsigned int i = 0; i < ls; ++i) {
194 QChar const c = s[i].toUpper();
200 drawText(x + textwidth, y, c);
201 textwidth += fontMetrics().width(c);
207 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
211 Encoding const * encoding = f.language()->encoding();
212 if (f.isSymbolFont())
213 encoding = encodings.symbol_encoding();
217 ucs4_to_qstring(s, ls, str);
220 // HACK: QT3 refuses to show single compose characters
221 // Still needed with Qt4?
222 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
226 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
230 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
231 setQPainterPen(f.realColor());
232 if (font() != fi.font)
234 // We need to draw the text as LTR as we use our own bidi code.
235 setLayoutDirection(Qt::LeftToRight);
237 // Here we use the font width cache instead of
238 // textwidth = fontMetrics().width(str);
239 // because the above is awfully expensive on MacOSX
240 textwidth = fi.metrics->width(str);
242 textwidth = smallCapsText(x, y, str, f);
245 if (f.underbar() == LyXFont::ON) {
246 underline(f, x, y, textwidth);
253 } // namespace frontend