]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLPainter.C
- LyX is dead slow, so the least we can do is use anti-alised text
[lyx.git] / src / frontends / qt4 / QLPainter.C
1 /**
2  * \file QLPainter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QLPainter.h"
15
16 #include "GuiApplication.h"
17 #include "GuiWorkArea.h"
18 #include "QLImage.h"
19
20 #include "GuiApplication.h"
21 #include "qt_helpers.h"
22
23 #include "debug.h"
24 #include "language.h"
25 #include "LColor.h"
26
27 #include "frontends/FontMetrics.h"
28
29 #include "support/unicode.h"
30
31 #include <QPainter>
32 #include <QPicture>
33 #include <QPixmap>
34 #include <QImage>
35
36 using lyx::char_type;
37 using lyx::docstring;
38
39 using std::endl;
40 using std::string;
41
42 namespace lyx {
43 namespace frontend {
44
45 QLPainter::~QLPainter()
46 {
47 }
48
49
50 QLPainter::QLPainter(GuiWorkArea * qwa)
51         : Painter(), qwa_(qwa)
52 {
53 }
54
55
56 void QLPainter::start()
57 {
58         lyxerr << "QLPainter::start()" << endl;
59         qp_.reset(new QPainter(qwa_->paintDevice()));
60         qp_->setRenderHint(QPainter::TextAntialiasing);
61         // new QPainter has default QPen:
62         current_color_ = LColor::black;
63         current_ls_ = line_solid;
64         current_lw_ = line_thin;
65 }
66
67
68 void QLPainter::end()
69 {
70         qp_->end();
71         lyxerr << "QLPainter::end()" << endl;
72 }
73
74
75 int QLPainter::paperWidth() const
76 {
77         return qwa_->viewport()->width();
78 }
79
80
81 int QLPainter::paperHeight() const
82 {
83         return qwa_->viewport()->height();
84 }
85
86 void QLPainter::setQPainterPen(LColor_color col,
87         Painter::line_style ls, Painter::line_width lw)
88 {
89         if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
90                 return;
91
92         current_color_ = col;
93         current_ls_ = ls;
94         current_lw_ = lw;
95
96         QPen pen = qp_.get()->pen();
97
98         pen.setColor(guiApp->colorCache().get(col));
99
100         switch (ls) {
101                 case line_solid: pen.setStyle(Qt::SolidLine); break;
102                 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
103         }
104
105         switch (lw) {
106                 case line_thin: pen.setWidth(0); break;
107                 case line_thick: pen.setWidth(3); break;
108         }
109
110         qp_.get()->setPen(pen);
111 }
112
113
114 void QLPainter::point(int x, int y, LColor_color col)
115 {
116         setQPainterPen(col);
117         qp_->drawPoint(x, y);
118 }
119
120
121 void QLPainter::line(int x1, int y1, int x2, int y2,
122         LColor_color col,
123         line_style ls,
124         line_width lw)
125 {
126         setQPainterPen(col, ls, lw);
127         qp_->drawLine(x1, y1, x2, y2);
128 }
129
130
131 void QLPainter::lines(int const * xp, int const * yp, int np,
132         LColor_color col,
133         line_style ls,
134         line_width lw)
135 {
136         // FIXME ?
137
138         // Must use new as np is not known at compile time.
139         boost::scoped_array<QPoint> points(new QPoint[np]);
140
141         for (int i = 0; i < np; ++i) {
142                 points[i].setX(xp[i]);
143                 points[i].setY(yp[i]);
144         }
145
146         setQPainterPen(col, ls, lw);
147         qp_->drawPolyline(points.get(), np);
148 }
149
150
151 void QLPainter::rectangle(int x, int y, int w, int h,
152         LColor_color col,
153         line_style ls,
154         line_width lw)
155 {
156         setQPainterPen(col, ls, lw);
157         qp_->drawRect(x, y, w, h);
158 }
159
160
161 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
162 {
163         qp_->fillRect(x, y, w, h, guiApp->colorCache().get(col));
164 }
165
166
167 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
168         int a1, int a2, LColor_color col)
169 {
170         // LyX usings 1/64ths degree, Qt usings 1/16th
171         setQPainterPen(col);
172         qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
173 }
174
175
176 void QLPainter::image(int x, int y, int w, int h,
177         lyx::graphics::Image const & i)
178 {
179         lyx::graphics::QLImage const & qlimage =
180                 static_cast<lyx::graphics::QLImage const &>(i);
181
182         fillRectangle(x, y, w, h, LColor::graphicsbg);
183
184         qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
185 }
186
187
188 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
189 {
190     return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
191 }
192
193
194 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
195 {
196         char_type s[2] = { c, char_type('\0') };
197         return text(x, y, s, 1, f);
198 }
199
200
201 int QLPainter::smallCapsText(int x, int y,
202         QString const & s, LyXFont const & f)
203 {
204         LyXFont smallfont(f);
205         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
206
207         QFont const & qfont = guiApp->guiFontLoader().get(f);
208         QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
209
210         setQPainterPen(f.realColor());
211         int textwidth = 0;
212         size_t ls = s.length();
213         for (unsigned int i = 0; i < ls; ++i) {
214                 QChar const c = s[i].toUpper();
215                 if (c != s.at(i)) {
216                         qp_->setFont(qsmallfont);
217                 } else {
218                         qp_->setFont(qfont);
219                 }
220                 qp_->drawText(x + textwidth, y, c);
221                 textwidth += qp_->fontMetrics().width(c);
222         }
223         return textwidth;
224 }
225
226
227 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
228         LyXFont const & f)
229 {
230 #if 0
231         Encoding const * encoding = f.language()->encoding();
232         if (f.isSymbolFont())
233                 encoding = encodings.symbol_encoding();
234 #endif
235
236         QString str;
237         ucs4_to_qstring(s, ls, str);
238
239 #if 0
240         // HACK: QT3 refuses to show single compose characters
241         //       Still needed with Qt4?
242         if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
243                 str = ' ' + str;
244 #endif
245
246         QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
247
248         int textwidth;
249
250         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
251                 setQPainterPen(f.realColor());
252                 if (qp_->font() != fi.font) {
253                         qp_->setFont(fi.font);
254                 }
255                 // We need to draw the text as LTR as we use our own bidi code.
256                 qp_->setLayoutDirection(Qt::LeftToRight);
257                 qp_->drawText(x, y, str);
258                 textwidth = qp_->fontMetrics().width(str);
259         } else {
260                 textwidth = smallCapsText(x, y, str, f);
261         }
262
263         if (f.underbar() == LyXFont::ON) {
264                 underline(f, x, y, textwidth);
265         }
266
267         return textwidth;
268 }
269
270
271 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
272 {
273         qp_->drawPixmap(x, y, pixmap);
274 }
275
276
277 void QLPainter::drawImage(int x, int y, QImage const & image)
278 {
279         qp_->drawImage(x, y, image);
280 }
281
282 } // namespace frontend
283 } // namespace lyx