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