3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
17 #include "GWorkArea.h"
21 #include "xftFontLoader.h"
22 #include "xformsImage.h"
23 #include "frontends/font_metrics.h"
24 #include "codeConvert.h"
26 #include "support/LAssert.h"
27 #include "support/lstrings.h"
29 #include <boost/scoped_array.hpp>
30 #include <X11/Xft/Xft.h>
34 GPainter::GPainter(GWorkArea & xwa)
35 : Painter(), owner_(xwa)
40 int GPainter::paperWidth() const
42 return owner_.workWidth();
46 int GPainter::paperHeight() const
48 return owner_.workHeight();
52 void GPainter::setForeground(Glib::RefPtr<Gdk::GC> gc, LColor::color clr)
54 Gdk::Color * gclr = owner_.getColorHandler().getGdkColor(clr);
55 gc->set_foreground(*gclr);
59 void GPainter::setLineParam(Glib::RefPtr<Gdk::GC> gc,
60 line_style ls, line_width lw)
65 case Painter::line_thin:
68 case Painter::line_thick:
74 case Painter::line_solid:
75 style = Gdk::LINE_SOLID;
77 case Painter::line_onoffdash:
78 style = Gdk::LINE_ON_OFF_DASH;
81 gc->set_line_attributes(width, style,
82 Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
86 Painter & GPainter::point(int x, int y, LColor::color c)
88 setForeground(owner_.getGC(), c);
89 owner_.getPixmap()->draw_point(owner_.getGC(), x, y);
94 Painter & GPainter::line(int x1, int y1,
100 setForeground(owner_.getGC(), col);
101 setLineParam(owner_.getGC(), ls, lw);
102 owner_.getPixmap()->draw_line(owner_.getGC(), x1, y1, x2, y2);
107 Painter & GPainter::lines(int const * xp, int const * yp,
113 setForeground(owner_.getGC(), col);
114 setLineParam(owner_.getGC(), ls, lw);
115 std::vector<Gdk::Point> points(np);
117 for (int i = 0; i < np; ++i) {
118 points[i].set_x(xp[i]);
119 points[i].set_y(yp[i]);
121 owner_.getPixmap()->draw_lines(owner_.getGC(), points);
126 Painter & GPainter::rectangle(int x, int y,
132 setForeground(owner_.getGC(), col);
133 setLineParam(owner_.getGC(), ls, lw);
134 owner_.getPixmap()->draw_rectangle(owner_.getGC(), false, x, y, w, h);
139 Painter & GPainter::fillRectangle(int x, int y,
143 setForeground(owner_.getGC(), col);
144 owner_.getPixmap()->draw_rectangle(owner_.getGC(), true, x, y, w, h);
149 Painter & GPainter::fillPolygon(int const * xp, int const * yp,
150 int np, LColor::color col)
152 setForeground(owner_.getGC(), col);
153 std::vector<Gdk::Point> points(np);
155 for (int i = 0; i < np; ++i) {
156 points[i].set_x(xp[i]);
157 points[i].set_y(yp[i]);
159 owner_.getPixmap()->draw_polygon(owner_.getGC(), true, points);
164 Painter & GPainter::arc(int x, int y,
165 unsigned int w, unsigned int h,
166 int a1, int a2, LColor::color col)
168 setForeground(owner_.getGC(), col);
169 owner_.getPixmap()->draw_arc(owner_.getGC(),
170 false, x, y, w, h, a1, a2);
175 Painter & GPainter::image(int x, int y,
177 lyx::graphics::Image const & i)
179 lyx::graphics::xformsImage const & image =
180 static_cast<lyx::graphics::xformsImage const &>(i);
181 Pixmap pixmap = GDK_PIXMAP_XID(owner_.getPixmap()->gobj());
182 GC gc = GDK_GC_XGC(owner_.getGC()->gobj());
183 XCopyArea(owner_.getDisplay(), image.getPixmap(), pixmap,
184 gc, 0, 0, w, h, x, y);
189 Painter & GPainter::text(int x, int y,
190 string const & s, LyXFont const & f)
192 size_t size = s.length() + 1;
193 wchar_t * wcs = (wchar_t *) alloca(size * sizeof(wchar_t));
194 size = mbstowcs(wcs, s.c_str(), size);
195 return text(x, y, wcs, size, f);
199 Painter & GPainter::text(int x, int y,
200 char c, LyXFont const & f)
202 char s[2] = { c, '\0' };
203 return text(x, y, s, 1, f);
207 inline XftFont * getXftFont(LyXFont const & f)
209 return fontLoader.load(f.family(), f.series(),
210 f.realShape(), f.size());
214 namespace font_metrics
217 int width(wchar_t const *s, size_t n, LyXFont const & f);
222 Painter & GPainter::text(int x, int y, wchar_t const * s, int ls,
225 XftFont * font = getXftFont(f);
226 XftColor * xftClr = owner_.getColorHandler().
227 getXftColor(f.realColor());
228 // getXftColor(f.realColor());
229 XftDraw * draw = owner_.getXftDraw();
230 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
231 XftDrawString32(draw, xftClr, font, x, y,
232 wcsToFcChar32StrFast(s), ls);
234 LyXFont smallfont(f);
235 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
236 XftFont * fontS = getXftFont(smallfont);
239 for(int i = 0; i < ls; ++i) {
240 c = lyx::support::uppercase(s[i]);
242 XftDrawString32(draw, xftClr, fontS, tmpx, y,
243 wcsToFcChar32StrFast(&c), 1);
244 tmpx += font_metrics::width(c, smallfont);
246 XftDrawString32(draw, xftClr, font, tmpx, y,
247 wcsToFcChar32StrFast(&c), 1);
248 tmpx += font_metrics::width(c, f);
252 if (f.underbar() == LyXFont::ON)
253 underline(f, x, y, font_metrics::width(s, ls, f));
258 Painter & GPainter::text(int x, int y,
259 char const * s, size_t ls,
262 boost::scoped_array<wchar_t> wcs(new wchar_t[ls + 1]);
264 if (fontLoader.isSpecial(f)) {
265 unsigned char const * us =
266 reinterpret_cast<unsigned char const *>(s);
268 std::copy(us, us + ls, wcs.get());
270 len = mbstowcs(wcs.get(), s, ls + 1);
271 return text(x, y, wcs.get(), len, f);