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