]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPainter.C
Unicode support
[lyx.git] / src / frontends / qt2 / 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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <iostream>
18 #include <boost/scoped_array.hpp>
19  
20 #include "font_metrics.h"
21 #include "support/lstrings.h" 
22 #include "lyxrc.h"
23 #include "debug.h"
24 #include "LyXView.h"
25 #include "encoding.h"
26 #include "language.h"
27
28 #include "QWorkArea.h"
29 #include "qfont_loader.h"
30 #include "QLPainter.h"
31 #include "QLImage.h"
32  
33 #include <qpainter.h>
34 #include <qbrush.h> 
35 #include <qcolor.h>
36
37 using std::endl;
38
39 QLPainter::QLPainter(QWorkArea & qwa)
40         : Painter(), owner_(qwa), paint_check_(0)
41 {
42         qp_.reset(new QPainter());
43 }
44
45
46 void QLPainter::start()
47 {
48         if (++paint_check_ == 1)
49                 qp_->begin(owner_.getPixmap());
50 }
51
52  
53 void QLPainter::end()
54 {
55         if (paint_check_ == 0) {
56                 lyxerr << "ended painting whilst not painting ??" << endl;
57         } else if (--paint_check_ == 0) {
58                 qp_->end();
59         }
60 }
61
62  
63 int QLPainter::paperWidth() const
64 {
65         return owner_.workWidth();
66 }
67
68
69 int QLPainter::paperHeight() const
70 {
71         return owner_.workHeight();
72 }
73
74  
75 QPainter & QLPainter::setPen(LColor::color c, 
76         Painter::line_style ls, Painter::line_width lw)
77 {
78         QPen pen = qp_->pen();
79  
80         pen.setColor(lcolor.getX11Name(c).c_str());
81  
82         switch (ls) {
83                 case line_solid: pen.setStyle(QPen::SolidLine); break;
84                 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
85         }
86  
87         switch (lw) {
88                 case line_thin: pen.setWidth(0); break;
89                 case line_thick: pen.setWidth(3); break;
90         }
91  
92         qp_->setPen(pen);
93         return *qp_;
94 }
95  
96  
97 Painter & QLPainter::point(int x, int y, LColor::color c)
98 {
99         setPen(c).drawPoint(x, y);
100         return *this;
101 }
102
103
104 Painter & QLPainter::line(int x1, int y1, 
105         int x2, int y2,
106         LColor::color col,
107         line_style ls,
108         line_width lw)
109 {
110         setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
111         return *this;
112 }
113
114
115 Painter & QLPainter::lines(int const * xp, int const * yp, 
116         int np,
117         LColor::color col,
118         line_style ls,
119         line_width lw)
120 {
121         // FIXME ?
122
123         // Must use new as np is not known at compile time.
124         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
125
126         int j = 0;
127  
128         for (int i = 0; i < np; ++i) {
129                 points[j++] = xp[i];
130                 points[j++] = yp[i];
131         }
132
133         setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
134
135         return *this;
136 }
137
138
139 Painter & QLPainter::rectangle(int x, int y, 
140         int w, int h,
141         LColor::color col,
142         line_style ls,
143         line_width lw)
144 {
145         //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl; 
146         setPen(col, ls, lw).drawRect(x, y, w, h);
147         return *this;
148 }
149
150
151 Painter & QLPainter::fillRectangle(int x, int y, 
152         int w, int h,
153         LColor::color col)
154 {
155         //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl; 
156         qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
157         return *this;
158 }
159
160
161 Painter & QLPainter::fillPolygon(int const * xp, int const * yp, 
162         int np, LColor::color col)
163 {
164         // Must use new as np is not known at compile time.
165         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
166
167         //if (1) return *this; 
168         int j = 0;
169  
170         for (int i = 0; i < np; ++i) {
171                 points[j++] = xp[i];
172                 points[j++] = yp[i];
173         }
174
175         setPen(col);
176         qp_->setBrush(lcolor.getX11Name(col).c_str()); 
177         qp_->drawPolygon(QPointArray(np, points.get()));
178         qp_->setBrush(Qt::NoBrush);
179
180         return *this;
181 }
182
183  
184 Painter & QLPainter::arc(int x, int y,
185         unsigned int w, unsigned int h,
186         int a1, int a2, LColor::color col)
187 {
188         lyxerr[Debug::GUI] << "arc: " << x<<","<<y
189                 << " " << w<<","<<h << ", angles "
190                 << a1 << " - " << a2 << endl;
191         // LyX usings 1/64ths degree, Qt usings 1/16th 
192         setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
193         return *this;
194 }
195
196  
197 Painter & QLPainter::image(int x, int y, 
198         int w, int h,
199         grfx::Image const & i)
200 {
201         qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
202         return *this;
203 }
204
205
206 Painter & QLPainter::text(int x, int y, 
207         string const & s, LyXFont const & f)
208 {
209         return text(x, y, s.data(), s.length(), f);
210 }
211
212
213 Painter & QLPainter::text(int x, int y, 
214         char c, LyXFont const & f)
215 {
216         char s[2] = { c, '\0' };
217         return text(x, y, s, 1, f);
218 }
219
220
221 void QLPainter::smallCapsText(int x, int y,
222         QString const & s, LyXFont const & f)
223 {
224         LyXFont smallfont(f);
225         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
226
227         QFont const & qfont = fontloader.get(f);
228         QFont const & qsmallfont = fontloader.get(smallfont);
229         QFontMetrics const & qfontm = QFontMetrics(qfont);
230         QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
231  
232         int tmpx = x;
233         size_t ls = s.length();
234         for (size_t i = 0; i < ls; ++i) {
235                 QChar const c = s[i].upper();
236                 if (c != s[i]) {
237                         qp_->setFont(qsmallfont);
238                         qp_->drawText(tmpx, y, c);
239                         tmpx += qsmallfontm.width(c);
240                 } else {
241                         qp_->setFont(qfont);
242                         qp_->drawText(tmpx, y, c);
243                         tmpx += qfontm.width(c);
244                 }
245         }
246 }
247
248  
249 Painter & QLPainter::text(int x, int y, 
250         char const * s, size_t ls,
251         LyXFont const & f)
252 {
253         setPen(f.color());
254
255         Encoding const * encoding = f.language()->encoding();
256         if (f.isSymbolFont())
257                 encoding = encodings.symbol_encoding();
258
259         QString str;
260         str.setLength(ls);
261         for (size_t i = 0; i < ls; ++i)
262                 str[i] = QChar(encoding->ucs(s[i]));
263
264 #if QT_VERSION >= 0x030000
265         // HACK: QT3 refuses to show single compose characters
266         if (ls = 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
267                 str = ' '+str;
268 #endif
269
270         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
271                 qp_->setFont(fontloader.get(f));
272 #if QT_VERSION >= 0x030000
273                 // We need to draw the text as LTR as we use our own bidi
274                 // code.
275                 qp_->drawText(x, y, str, -1, QPainter::LTR);
276 #else
277                 qp_->drawText(x, y, str);
278 #endif
279         } else {
280                 smallCapsText(x, y, str, f);
281         }
282
283         if (f.underbar() == LyXFont::ON) {
284                 underline(f, x, y, font_metrics::width(s, ls, f));
285         }
286         
287         return *this;
288 }