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