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