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>
36 namespace font_metrics {
38 int width(wchar_t const *s, size_t n, LyXFont const & f);
40 } // namespace font_metrics
46 GPainter::GPainter(GWorkArea & xwa)
47 : Painter(), owner_(xwa)
52 int GPainter::paperWidth() const
54 return owner_.workWidth();
58 int GPainter::paperHeight() const
60 return owner_.workHeight();
64 void GPainter::setForeground(Glib::RefPtr<Gdk::GC> gc, LColor_color clr)
66 Gdk::Color * gclr = owner_.getColorHandler().getGdkColor(clr);
67 gc->set_foreground(*gclr);
71 void GPainter::setLineParam(Glib::RefPtr<Gdk::GC> gc,
72 line_style ls, line_width lw)
76 case Painter::line_thin:
79 case Painter::line_thick:
84 Gdk::LineStyle style = Gdk::LINE_SOLID;
86 case Painter::line_solid:
87 style = Gdk::LINE_SOLID;
89 case Painter::line_onoffdash:
90 style = Gdk::LINE_ON_OFF_DASH;
93 gc->set_line_attributes(width, style,
94 Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
98 void GPainter::point(int x, int y, LColor_color c)
100 setForeground(owner_.getGC(), c);
101 owner_.getPixmap()->draw_point(owner_.getGC(), x, y);
105 void GPainter::line(int x1, int y1,
111 setForeground(owner_.getGC(), col);
112 setLineParam(owner_.getGC(), ls, lw);
113 owner_.getPixmap()->draw_line(owner_.getGC(), x1, y1, x2, y2);
117 void GPainter::lines(int const * xp, int const * yp, int np,
122 setForeground(owner_.getGC(), col);
123 setLineParam(owner_.getGC(), ls, lw);
124 std::vector<Gdk::Point> points(np);
126 for (int i = 0; i < np; ++i) {
127 points[i].set_x(xp[i]);
128 points[i].set_y(yp[i]);
130 owner_.getPixmap()->draw_lines(owner_.getGC(), points);
134 void GPainter::rectangle(int x, int y, int w, int h,
139 setForeground(owner_.getGC(), col);
140 setLineParam(owner_.getGC(), ls, lw);
141 owner_.getPixmap()->draw_rectangle(owner_.getGC(), false, x, y, w, h);
145 void GPainter::fillRectangle(int x, int y, int w, int h,
148 setForeground(owner_.getGC(), col);
149 owner_.getPixmap()->draw_rectangle(owner_.getGC(), true, x, y, w, h);
153 void GPainter::fillPolygon(int const * xp, int const * yp,
154 int np, LColor_color col)
156 setForeground(owner_.getGC(), col);
157 std::vector<Gdk::Point> points(np);
159 for (int i = 0; i < np; ++i) {
160 points[i].set_x(xp[i]);
161 points[i].set_y(yp[i]);
163 owner_.getPixmap()->draw_polygon(owner_.getGC(), true, points);
167 void GPainter::arc(int x, int y, unsigned int w, unsigned int h,
168 int a1, int a2, LColor_color col)
170 setForeground(owner_.getGC(), col);
171 owner_.getPixmap()->draw_arc(owner_.getGC(),
172 false, x, y, w, h, a1, a2);
176 void GPainter::image(int x, int y, int w, int h,
177 graphics::Image const & i)
179 graphics::xformsImage const & image =
180 static_cast<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);
188 void GPainter::text(int x, int y, std::string const & s, LyXFont const & f)
190 size_t size = s.length() + 1;
191 boost::scoped_array<wchar_t> wcs(new wchar_t[size]);
192 size = mbstowcs(wcs.get(), s.c_str(), size);
193 return text(x, y, wcs.get(), size, f);
197 void GPainter::text(int x, int y, char c, LyXFont const & f)
199 char s[2] = { c, '\0' };
204 inline XftFont * getXftFont(LyXFont const & f)
206 return fontLoader.load(f.family(), f.series(),
207 f.realShape(), f.size());
211 void GPainter::text(int x, int y, wchar_t const * s, int ls, LyXFont const & f)
213 XftFont * font = getXftFont(f);
214 XftColor * xftClr = owner_.getColorHandler().
215 getXftColor(f.realColor());
216 // getXftColor(f.realColor());
217 XftDraw * draw = owner_.getXftDraw();
218 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
219 XftDrawString32(draw, xftClr, font, x, y,
220 wcsToXftChar32StrFast(s), ls);
222 LyXFont smallfont(f);
223 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
224 XftFont * fontS = getXftFont(smallfont);
227 for (int i = 0; i < ls; ++i) {
228 c = lyx::support::uppercase(s[i]);
230 XftDrawString32(draw, xftClr, fontS, tmpx, y,
231 wcsToXftChar32StrFast(&c), 1);
232 tmpx += font_metrics::width(c, smallfont);
234 XftDrawString32(draw, xftClr, font, tmpx, y,
235 wcsToXftChar32StrFast(&c), 1);
236 tmpx += font_metrics::width(c, f);
240 if (f.underbar() == LyXFont::ON)
241 underline(f, x, y, font_metrics::width(s, ls, f));
245 void GPainter::text(int x, int y, char const * s, size_t ls, LyXFont const & f)
247 boost::scoped_array<wchar_t> wcs(new wchar_t[ls + 1]);
249 if (fontLoader.isSpecial(f)) {
250 unsigned char const * us =
251 reinterpret_cast<unsigned char const *>(s);
253 std::copy(us, us + ls, wcs.get());
255 len = mbstowcs(wcs.get(), s, ls + 1);
256 text(x, y, wcs.get(), len, f);
259 } // namespace frontend