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