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