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