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.
16 #include "GWorkArea.h"
21 #include "xftFontLoader.h"
22 #include "xformsImage.h"
23 #include "frontends/font_metrics.h"
24 #include "codeConvert.h"
26 #include "support/lstrings.h"
28 #include <boost/scoped_array.hpp>
30 #include <X11/Xft/Xft.h>
37 GPainter::GPainter(GWorkArea & xwa)
38 : Painter(), owner_(xwa)
43 int GPainter::paperWidth() const
45 return owner_.workWidth();
49 int GPainter::paperHeight() const
51 return owner_.workHeight();
55 void GPainter::setForeground(Glib::RefPtr<Gdk::GC> gc, LColor_color clr)
57 Gdk::Color * gclr = owner_.getColorHandler().getGdkColor(clr);
58 gc->set_foreground(*gclr);
62 void GPainter::setLineParam(Glib::RefPtr<Gdk::GC> gc,
63 line_style ls, line_width lw)
67 case Painter::line_thin:
70 case Painter::line_thick:
75 Gdk::LineStyle style = Gdk::LINE_SOLID;
77 case Painter::line_solid:
78 style = Gdk::LINE_SOLID;
80 case Painter::line_onoffdash:
81 style = Gdk::LINE_ON_OFF_DASH;
84 gc->set_line_attributes(width, style,
85 Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
89 void GPainter::point(int x, int y, LColor_color c)
91 setForeground(owner_.getGC(), c);
92 owner_.getPixmap()->draw_point(owner_.getGC(), x, y);
97 void GPainter::line(int x1, int y1,
103 setForeground(owner_.getGC(), col);
104 setLineParam(owner_.getGC(), ls, lw);
105 owner_.getPixmap()->draw_line(owner_.getGC(), x1, y1, x2, y2);
110 void GPainter::lines(int const * xp, int const * yp, int np,
115 setForeground(owner_.getGC(), col);
116 setLineParam(owner_.getGC(), ls, lw);
117 std::vector<Gdk::Point> points(np);
119 for (int i = 0; i < np; ++i) {
120 points[i].set_x(xp[i]);
121 points[i].set_y(yp[i]);
123 owner_.getPixmap()->draw_lines(owner_.getGC(), points);
128 void GPainter::rectangle(int x, int y, int w, int h,
133 setForeground(owner_.getGC(), col);
134 setLineParam(owner_.getGC(), ls, lw);
135 owner_.getPixmap()->draw_rectangle(owner_.getGC(), false, x, y, w, h);
140 void GPainter::fillRectangle(int x, int y, int w, int h,
143 setForeground(owner_.getGC(), col);
144 owner_.getPixmap()->draw_rectangle(owner_.getGC(), true, x, y, w, h);
149 void 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 void GPainter::arc(int x, int y, unsigned int w, unsigned int h,
165 int a1, int a2, LColor_color col)
167 setForeground(owner_.getGC(), col);
168 owner_.getPixmap()->draw_arc(owner_.getGC(),
169 false, x, y, w, h, a1, a2);
174 void GPainter::image(int x, int y, int w, int h,
175 lyx::graphics::Image const & i)
177 lyx::graphics::xformsImage const & image =
178 static_cast<lyx::graphics::xformsImage const &>(i);
179 Pixmap pixmap = GDK_PIXMAP_XID(owner_.getPixmap()->gobj());
180 GC gc = GDK_GC_XGC(owner_.getGC()->gobj());
181 XCopyArea(owner_.getDisplay(), image.getPixmap(), pixmap,
182 gc, 0, 0, w, h, x, y);
187 void GPainter::text(int x, int y, std::string const & s, LyXFont const & f)
189 size_t size = s.length() + 1;
190 wchar_t * wcs = (wchar_t *) alloca(size * sizeof(wchar_t));
191 size = mbstowcs(wcs, s.c_str(), size);
192 return text(x, y, wcs, size, f);
196 void GPainter::text(int x, int y, char c, LyXFont const & f)
198 char s[2] = { c, '\0' };
199 return text(x, y, s, 1, f);
203 inline XftFont * getXftFont(LyXFont const & f)
205 return fontLoader.load(f.family(), f.series(),
206 f.realShape(), f.size());
210 namespace font_metrics
213 int width(wchar_t const *s, size_t n, LyXFont const & f);
218 void GPainter::text(int x, int y, wchar_t const * s, int ls, LyXFont const & f)
220 XftFont * font = getXftFont(f);
221 XftColor * xftClr = owner_.getColorHandler().
222 getXftColor(f.realColor());
223 // getXftColor(f.realColor());
224 XftDraw * draw = owner_.getXftDraw();
225 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
226 XftDrawString32(draw, xftClr, font, x, y,
227 wcsToXftChar32StrFast(s), ls);
229 LyXFont smallfont(f);
230 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
231 XftFont * fontS = getXftFont(smallfont);
234 for(int i = 0; i < ls; ++i) {
235 c = lyx::support::uppercase(s[i]);
237 XftDrawString32(draw, xftClr, fontS, tmpx, y,
238 wcsToXftChar32StrFast(&c), 1);
239 tmpx += font_metrics::width(c, smallfont);
241 XftDrawString32(draw, xftClr, font, tmpx, y,
242 wcsToXftChar32StrFast(&c), 1);
243 tmpx += font_metrics::width(c, f);
247 if (f.underbar() == LyXFont::ON)
248 underline(f, x, y, font_metrics::width(s, ls, f));
253 void GPainter::text(int x, int y, char const * s, size_t ls, LyXFont const & f)
255 boost::scoped_array<wchar_t> wcs(new wchar_t[ls + 1]);
257 if (fontLoader.isSpecial(f)) {
258 unsigned char const * us =
259 reinterpret_cast<unsigned char const *>(s);
261 std::copy(us, us + ls, wcs.get());
263 len = mbstowcs(wcs.get(), s, ls + 1);
264 return text(x, y, wcs.get(), len, f);