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