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