]> git.lyx.org Git - lyx.git/blob - src/Painter.C
fix typo that put too many include paths for most people
[lyx.git] / src / Painter.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1998-2001 The LyX Team
7  *
8  *======================================================*/
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "Painter.h"
17 #include "LString.h"
18 #include "debug.h"
19 #include "lyxfont.h"
20 #include "WorkArea.h"
21 #include "font.h"
22 #include "ColorHandler.h"
23 #include "lyxrc.h"
24 #include "encoding.h"
25 #include "language.h"
26
27 #include "frontends/GUIRunTime.h"
28 #include "graphics/GraphicsImage.h"
29
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
32
33 #include <boost/smart_ptr.hpp>
34
35 #include <cmath>
36
37
38 using std::endl;
39 using std::max;
40
41 namespace {
42
43 inline
44 Display * display()
45 {
46         return GUIRunTime::x11Display();
47 }
48
49 }
50
51
52 Painter::Painter(WorkArea & wa)
53         : PainterBase(wa)
54 {}
55
56
57 // Basic drawing routines
58
59 PainterBase & Painter::point(int x, int y, LColor::color c)
60 {
61         XDrawPoint(display(), owner.getPixmap(),
62                    lyxColorHandler->getGCForeground(c), x, y);
63         return *this;
64 }
65
66
67 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
68                             LColor::color col,
69                             enum line_style ls,
70                             enum line_width lw)
71 {
72         XDrawLine(display(), owner.getPixmap(),
73                   lyxColorHandler->getGCLinepars(ls, lw, col),
74                   x1, y1, x2, y2);
75         return *this;
76 }
77
78
79 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
80                              LColor::color col,
81                              enum line_style ls,
82                              enum line_width lw)
83 {
84         boost::scoped_array<XPoint> points(new XPoint[np]);
85
86         for (int i = 0; i < np; ++i) {
87                 points[i].x = xp[i];
88                 points[i].y = yp[i];
89         }
90
91         XDrawLines(display(), owner.getPixmap(),
92                    lyxColorHandler->getGCLinepars(ls, lw, col),
93                    points.get(), np, CoordModeOrigin);
94
95         return *this;
96 }
97
98
99 PainterBase & Painter::rectangle(int x, int y, int w, int h,
100                                  LColor::color col,
101                                  enum line_style ls,
102                                  enum line_width lw)
103 {
104         XDrawRectangle(display(), owner.getPixmap(),
105                        lyxColorHandler->getGCLinepars(ls, lw, col),
106                        x, y, w, h);
107         return *this;
108 }
109
110
111 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
112                                      LColor::color col)
113 {
114         XFillRectangle(display(), owner.getPixmap(),
115                        lyxColorHandler->getGCForeground(col), x, y, w, h);
116         return *this;
117 }
118
119
120 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
121                                    LColor::color col)
122 {
123         boost::scoped_array<XPoint> points(new XPoint[np]);
124
125         for (int i = 0; i < np; ++i) {
126                 points[i].x = xp[i];
127                 points[i].y = yp[i];
128         }
129
130         XFillPolygon(display(), owner.getPixmap(),
131                      lyxColorHandler->getGCForeground(col), points.get(), np,
132                      Nonconvex, CoordModeOrigin);
133
134         return *this;
135 }
136
137
138 PainterBase & Painter::arc(int x, int y,
139                            unsigned int w, unsigned int h,
140                            int a1, int a2, LColor::color col)
141 {
142         XDrawArc(display(), owner.getPixmap(),
143                  lyxColorHandler->getGCForeground(col),
144                  x, y, w, h, a1, a2);
145         return *this;
146 }
147
148
149 /// Draw lines from x1,y1 to x2,y2. They are arrays
150 PainterBase & Painter::segments(int const * x1, int const * y1,
151                                 int const * x2, int const * y2, int ns,
152                                 LColor::color col,
153                                 enum line_style ls, enum line_width lw)
154 {
155         boost::scoped_array<XSegment> s(new XSegment[ns]);
156
157         for (int i = 0; i < ns; ++i) {
158                 s[i].x1 = x1[i];
159                 s[i].y1 = y1[i];
160                 s[i].x2 = x2[i];
161                 s[i].y2 = y2[i];
162         }
163         XDrawSegments(display(), owner.getPixmap(),
164                       lyxColorHandler->getGCLinepars(ls, lw, col),
165                       s.get(), ns);
166
167         return *this;
168 }
169
170
171 PainterBase & Painter::image(int x, int y, int w, int h,
172                              grfx::GImage const & image)
173 {
174         XGCValues val;
175         val.function = GXcopy;
176         GC gc = XCreateGC(display(), owner.getPixmap(),
177                           GCFunction, &val);
178         XCopyArea(display(), image.getPixmap(), owner.getPixmap(), gc,
179                   0, 0, w, h, x, y);
180         XFreeGC(display(), gc);
181         return *this;
182 }
183
184
185 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
186 {
187         return text(x, y, s.data(), s.length(), f);
188 }
189
190
191 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
192 {
193         char s[2] = { c, '\0' };
194         return text(x, y, s, 1, f);
195 }
196
197
198 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
199                             LyXFont const & f)
200 {
201         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
202                 boost::scoped_array<XChar2b> xs(new XChar2b[ls]);
203                 Encoding const * encoding = f.language()->encoding();
204                 LyXFont font(f);
205                 if (f.isSymbolFont()) {
206 #ifdef USE_UNICODE_FOR_SYMBOLS
207                         font.setFamily(LyXFont::ROMAN_FAMILY);
208                         font.setShape(LyXFont::UP_SHAPE);
209 #endif
210                         encoding = encodings.symbol_encoding();
211                 }
212                 for (size_t i = 0; i < ls; ++i) {
213                         Uchar c = encoding->ucs(s[i]);
214                         xs[i].byte1 = c >> 8;
215                         xs[i].byte2 = c & 0xff;
216                 }
217                 text(x , y, xs.get(), ls, font);
218                 return *this;
219         }
220
221         GC gc = lyxColorHandler->getGCForeground(f.realColor());
222         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
223                 lyxfont::XSetFont(display(), gc, f);
224                 XDrawString(display(), owner.getPixmap(), gc, x, y, s, ls);
225         } else {
226                 LyXFont smallfont(f);
227                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
228                 int tmpx = x;
229                 for (size_t i = 0; i < ls; ++i) {
230                         char const c = uppercase(s[i]);
231                         if (c != s[i]) {
232                                 lyxfont::XSetFont(display(), gc, smallfont);
233                                 XDrawString(display(), owner.getPixmap(), gc,
234                                             tmpx, y, &c, 1);
235                                 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
236                         } else {
237                                 lyxfont::XSetFont(display(), gc, f);
238                                 XDrawString(display(), owner.getPixmap(), gc,
239                                             tmpx, y, &c, 1);
240                                 tmpx += lyxfont::XTextWidth(f, &c, 1);
241                         }
242                 }
243         }
244
245         if (f.underbar() == LyXFont::ON) {
246                 underline(f, x, y, lyxfont::width(s, ls, f));
247         }
248
249         return *this;
250 }
251
252
253 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
254                             LyXFont const & f)
255 {
256         GC gc = lyxColorHandler->getGCForeground(f.realColor());
257         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
258                 lyxfont::XSetFont(display(), gc, f);
259                 XDrawString16(display(), owner.getPixmap(), gc, x, y, s, ls);
260         } else {
261                 LyXFont smallfont(f);
262                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
263                 static XChar2b c;
264                 int tmpx = x;
265                 for (int i = 0; i < ls; ++i) {
266                         if (s[i].byte1)
267                                 c = s[i];
268                         else {
269                                 c.byte1 = s[i].byte1;
270                                 c.byte2 = uppercase(s[i].byte2);
271                         }
272                         if (c.byte2 != s[i].byte2) {
273                                 lyxfont::XSetFont(display(), gc, smallfont);
274                                 XDrawString16(display(), owner.getPixmap(), gc,
275                                               tmpx, y, &c, 1);
276                                 tmpx += lyxfont::XTextWidth16(smallfont, &c, 1);
277                         } else {
278                                 lyxfont::XSetFont(display(), gc, f);
279                                 XDrawString16(display(), owner.getPixmap(), gc,
280                                               tmpx, y, &c, 1);
281                                 tmpx += lyxfont::XTextWidth16(f, &c, 1);
282                         }
283                 }
284         }
285
286         if (f.underbar() == LyXFont::ON) {
287                 underline(f, x, y, lyxfont::width(s, ls, f));
288         }
289
290         return *this;
291 }
292
293
294 void Painter::underline(LyXFont const & f, int x, int y, int width)
295 {
296         int const below = max(lyxfont::maxDescent(f) / 2, 2);
297         int const height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
298         if (height < 2)
299                 line(x, y + below, x + width, y + below, f.color());
300         else
301                 fillRectangle(x, y + below, width, below + height,
302                               f.color());
303 }