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.
15 #include "GWorkArea.h"
20 #include "xftFontLoader.h"
21 #include "xformsImage.h"
22 #include "frontends/font_metrics.h"
23 #include "codeConvert.h"
25 #include "support/lstrings.h"
30 #include <X11/Xft/Xft.h>
32 #include <boost/scoped_array.hpp>
38 namespace font_metrics {
40 int width(wchar_t const * s, size_t n, LyXFont const & f);
42 } // namespace font_metrics
48 GPainter::GPainter(GWorkArea & xwa)
49 : Painter(), owner_(xwa)
54 int GPainter::paperWidth() const
56 return owner_.workWidth();
60 int GPainter::paperHeight() const
62 return owner_.workHeight();
66 void GPainter::setForeground(Glib::RefPtr<Gdk::GC> gc, LColor_color clr)
68 Gdk::Color * gclr = owner_.getColorHandler().getGdkColor(clr);
69 gc->set_foreground(*gclr);
73 void GPainter::setLineParam(Glib::RefPtr<Gdk::GC> gc,
74 line_style ls, line_width lw)
78 case Painter::line_thin:
81 case Painter::line_thick:
86 Gdk::LineStyle style = Gdk::LINE_SOLID;
88 case Painter::line_solid:
89 style = Gdk::LINE_SOLID;
91 case Painter::line_onoffdash:
92 style = Gdk::LINE_ON_OFF_DASH;
95 gc->set_line_attributes(width, style,
96 Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
100 void GPainter::point(int x, int y, LColor_color c)
102 setForeground(owner_.getGC(), c);
103 owner_.getPixmap()->draw_point(owner_.getGC(), x, y);
107 void GPainter::line(int x1, int y1,
113 setForeground(owner_.getGC(), col);
114 setLineParam(owner_.getGC(), ls, lw);
115 owner_.getPixmap()->draw_line(owner_.getGC(), x1, y1, x2, y2);
119 void GPainter::lines(int const * xp, int const * yp, int np,
124 setForeground(owner_.getGC(), col);
125 setLineParam(owner_.getGC(), ls, lw);
126 std::vector<Gdk::Point> points(np);
128 for (int i = 0; i < np; ++i) {
129 points[i].set_x(xp[i]);
130 points[i].set_y(yp[i]);
132 owner_.getPixmap()->draw_lines(owner_.getGC(), points);
136 void GPainter::rectangle(int x, int y, int w, int h,
141 setForeground(owner_.getGC(), col);
142 setLineParam(owner_.getGC(), ls, lw);
143 owner_.getPixmap()->draw_rectangle(owner_.getGC(), false, x, y, w, h);
147 void GPainter::fillRectangle(int x, int y, int w, int h,
150 setForeground(owner_.getGC(), col);
151 owner_.getPixmap()->draw_rectangle(owner_.getGC(), true, x, y, w, h);
155 void GPainter::fillPolygon(int const * xp, int const * yp,
156 int np, LColor_color col)
158 setForeground(owner_.getGC(), col);
159 std::vector<Gdk::Point> points(np);
161 for (int i = 0; i < np; ++i) {
162 points[i].set_x(xp[i]);
163 points[i].set_y(yp[i]);
165 owner_.getPixmap()->draw_polygon(owner_.getGC(), true, points);
169 void GPainter::arc(int x, int y, unsigned int w, unsigned int h,
170 int a1, int a2, LColor_color col)
172 setForeground(owner_.getGC(), col);
173 owner_.getPixmap()->draw_arc(owner_.getGC(),
174 false, x, y, w, h, a1, a2);
178 void GPainter::image(int x, int y, int w, int h,
179 graphics::Image const & i)
181 graphics::xformsImage const & image =
182 static_cast<graphics::xformsImage const &>(i);
183 Pixmap pixmap = GDK_PIXMAP_XID(owner_.getPixmap()->gobj());
184 GC gc = GDK_GC_XGC(owner_.getGC()->gobj());
185 XCopyArea(owner_.getDisplay(), image.getPixmap(), pixmap,
186 gc, 0, 0, w, h, x, y);
190 void GPainter::text(int x, int y, std::string const & s, LyXFont const & f)
192 size_t size = s.length() + 1;
193 boost::scoped_array<wchar_t> wcs(new wchar_t[size]);
194 size = mbstowcs(wcs.get(), s.c_str(), size);
195 return text(x, y, wcs.get(), size, f);
199 void GPainter::text(int x, int y, char c, LyXFont const & f)
201 char s[2] = { c, '\0' };
206 inline XftFont * getXftFont(LyXFont const & f)
208 return fontLoader.load(f.family(), f.series(),
209 f.realShape(), f.size());
213 void GPainter::text(int x, int y, wchar_t const * s, int ls, LyXFont const & f)
215 XftFont * font = getXftFont(f);
216 XftColor * xftClr = owner_.getColorHandler().
217 getXftColor(f.realColor());
218 // getXftColor(f.realColor());
219 XftDraw * draw = owner_.getXftDraw();
220 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
221 XftDrawString32(draw, xftClr, font, x, y,
222 wcsToXftChar32StrFast(s), ls);
224 LyXFont smallfont(f);
225 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
226 XftFont * fontS = getXftFont(smallfont);
229 for (int i = 0; i < ls; ++i) {
230 c = lyx::support::uppercase(s[i]);
232 XftDrawString32(draw, xftClr, fontS, tmpx, y,
233 wcsToXftChar32StrFast(&c), 1);
234 tmpx += font_metrics::width(c, smallfont);
236 XftDrawString32(draw, xftClr, font, tmpx, y,
237 wcsToXftChar32StrFast(&c), 1);
238 tmpx += font_metrics::width(c, f);
242 if (f.underbar() == LyXFont::ON)
243 underline(f, x, y, font_metrics::width(s, ls, f));
247 void GPainter::text(int x, int y, char const * s, size_t ls, LyXFont const & f)
249 boost::scoped_array<wchar_t> wcs(new wchar_t[ls + 1]);
251 if (fontLoader.isSpecial(f)) {
252 unsigned char const * us =
253 reinterpret_cast<unsigned char const *>(s);
255 std::copy(us, us + ls, wcs.get());
257 len = mbstowcs(wcs.get(), s, ls + 1);
258 text(x, y, wcs.get(), len, f);
261 } // namespace frontend