]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GPainter.C
Trivial compile fixes...
[lyx.git] / src / frontends / gtk / GPainter.C
1 /**
2  * \file GPainter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12 #include <gtkmm.h>
13
14 #include "GPainter.h"
15 #include "debug.h"
16 #include "GWorkArea.h"
17 #include "lyxrc.h"
18 #include "encoding.h"
19 #include "language.h"
20 #include "LColor.h"
21 #include "xftFontLoader.h"
22 #include "xformsImage.h"
23 #include "frontends/font_metrics.h"
24 #include "codeConvert.h"
25
26 #include "support/lstrings.h"
27
28 #include <boost/scoped_array.hpp>
29
30 #include <X11/Xft/Xft.h>
31
32 #include <cmath>
33
34 using std::string;
35
36
37 GPainter::GPainter(GWorkArea & xwa)
38         : Painter(), owner_(xwa)
39 {
40 }
41
42
43 int GPainter::paperWidth() const
44 {
45         return owner_.workWidth();
46 }
47
48
49 int GPainter::paperHeight() const
50 {
51         return owner_.workHeight();
52 }
53
54
55 void GPainter::setForeground(Glib::RefPtr<Gdk::GC> gc, LColor_color clr)
56 {
57         Gdk::Color * gclr = owner_.getColorHandler().getGdkColor(clr);
58         gc->set_foreground(*gclr);
59 }
60
61
62 void GPainter::setLineParam(Glib::RefPtr<Gdk::GC> gc,
63                             line_style ls, line_width lw)
64 {
65         int width = 0;
66         switch (lw) {
67         case Painter::line_thin:
68                 width = 0;
69                 break;
70         case Painter::line_thick:
71                 width = 2;
72                 break;
73         }
74
75         Gdk::LineStyle style = Gdk::LINE_SOLID;
76         switch (ls) {
77         case Painter::line_solid:
78                 style = Gdk::LINE_SOLID;
79                 break;
80         case Painter::line_onoffdash:
81                 style = Gdk::LINE_ON_OFF_DASH;
82                 break;
83         }
84         gc->set_line_attributes(width, style,
85                                 Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
86 }
87
88
89 void GPainter::point(int x, int y, LColor_color c)
90 {
91         setForeground(owner_.getGC(), c);
92         owner_.getPixmap()->draw_point(owner_.getGC(), x, y);
93 }
94
95
96 void GPainter::line(int x1, int y1,
97         int x2, int y2,
98         LColor_color col,
99         line_style ls,
100         line_width lw)
101 {
102         setForeground(owner_.getGC(), col);
103         setLineParam(owner_.getGC(), ls, lw);
104         owner_.getPixmap()->draw_line(owner_.getGC(), x1, y1, x2, y2);
105 }
106
107
108 void GPainter::lines(int const * xp, int const * yp, int np,
109         LColor_color col,
110         line_style ls,
111         line_width lw)
112 {
113         setForeground(owner_.getGC(), col);
114         setLineParam(owner_.getGC(), ls, lw);
115         std::vector<Gdk::Point> points(np);
116
117         for (int i = 0; i < np; ++i) {
118                 points[i].set_x(xp[i]);
119                 points[i].set_y(yp[i]);
120         }
121         owner_.getPixmap()->draw_lines(owner_.getGC(), points);
122 }
123
124
125 void GPainter::rectangle(int x, int y, int w, int h,
126         LColor_color col,
127         line_style ls,
128         line_width lw)
129 {
130         setForeground(owner_.getGC(), col);
131         setLineParam(owner_.getGC(), ls, lw);
132         owner_.getPixmap()->draw_rectangle(owner_.getGC(), false, x, y, w, h);
133 }
134
135
136 void GPainter::fillRectangle(int x, int y, int w, int h,
137         LColor_color col)
138 {
139         setForeground(owner_.getGC(), col);
140         owner_.getPixmap()->draw_rectangle(owner_.getGC(), true, x, y, w, h);
141 }
142
143
144 void GPainter::fillPolygon(int const * xp, int const * yp,
145         int np, LColor_color col)
146 {
147         setForeground(owner_.getGC(), col);
148         std::vector<Gdk::Point> points(np);
149
150         for (int i = 0; i < np; ++i) {
151                 points[i].set_x(xp[i]);
152                 points[i].set_y(yp[i]);
153         }
154         owner_.getPixmap()->draw_polygon(owner_.getGC(), true, points);
155 }
156
157
158 void GPainter::arc(int x, int y, unsigned int w, unsigned int h,
159         int a1, int a2, LColor_color col)
160 {
161         setForeground(owner_.getGC(), col);
162         owner_.getPixmap()->draw_arc(owner_.getGC(),
163                                      false, x, y, w, h, a1, a2);
164 }
165
166
167 void GPainter::image(int x, int y, int w, int h,
168         lyx::graphics::Image const & i)
169 {
170         lyx::graphics::xformsImage const & image =
171                 static_cast<lyx::graphics::xformsImage const &>(i);
172         Pixmap pixmap = GDK_PIXMAP_XID(owner_.getPixmap()->gobj());
173         GC gc = GDK_GC_XGC(owner_.getGC()->gobj());
174         XCopyArea(owner_.getDisplay(), image.getPixmap(), pixmap,
175                   gc, 0, 0, w, h, x, y);
176 }
177
178
179 void GPainter::text(int x, int y, std::string const & s, LyXFont const & f)
180 {
181         size_t size = s.length() + 1;
182         wchar_t * wcs = (wchar_t *) alloca(size * sizeof(wchar_t));
183         size = mbstowcs(wcs, s.c_str(), size);
184         return text(x, y, wcs, size, f);
185 }
186
187
188 void GPainter::text(int x, int y, char c, LyXFont const & f)
189 {
190         char s[2] = { c, '\0' };
191         text(x, y, s, 1, f);
192 }
193
194
195 inline XftFont * getXftFont(LyXFont const & f)
196 {
197         return fontLoader.load(f.family(), f.series(),
198                                f.realShape(), f.size());
199 }
200
201
202 namespace font_metrics
203 {
204
205 int width(wchar_t const *s, size_t n, LyXFont const & f);
206
207 }
208
209
210 void GPainter::text(int x, int y, wchar_t const * s, int ls, LyXFont const & f)
211 {
212         XftFont * font = getXftFont(f);
213         XftColor * xftClr = owner_.getColorHandler().
214                 getXftColor(f.realColor());
215 //      getXftColor(f.realColor());
216         XftDraw * draw = owner_.getXftDraw();
217         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
218                 XftDrawString32(draw, xftClr, font, x, y,
219                                 wcsToXftChar32StrFast(s), ls);
220         } else {
221                 LyXFont smallfont(f);
222                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
223                 XftFont * fontS = getXftFont(smallfont);
224                 wchar_t c;
225                 int tmpx = x;
226                 for(int i = 0; i < ls; ++i) {
227                         c = lyx::support::uppercase(s[i]);
228                         if(c != s[i]) {
229                                 XftDrawString32(draw, xftClr, fontS, tmpx, y,
230                                                 wcsToXftChar32StrFast(&c), 1);
231                                 tmpx += font_metrics::width(c, smallfont);
232                         } else {
233                                 XftDrawString32(draw, xftClr, font, tmpx, y,
234                                                 wcsToXftChar32StrFast(&c), 1);
235                                 tmpx += font_metrics::width(c, f);
236                         }
237                 }
238         }
239         if (f.underbar() == LyXFont::ON)
240                 underline(f, x, y, font_metrics::width(s, ls, f));
241 }
242
243
244 void GPainter::text(int x, int y, char const * s, size_t ls, LyXFont const & f)
245 {
246         boost::scoped_array<wchar_t> wcs(new wchar_t[ls + 1]);
247         size_t len;
248         if (fontLoader.isSpecial(f)) {
249                 unsigned char const * us =
250                         reinterpret_cast<unsigned char const *>(s);
251                 len = ls;
252                 std::copy(us, us + ls, wcs.get());
253         } else
254                 len = mbstowcs(wcs.get(), s, ls + 1);
255         text(x, y, wcs.get(), len, f);
256 }