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 "support/std_string.h"
17 #include "GWorkArea.h"
22 #include "xftFontLoader.h"
23 #include "xformsImage.h"
24 #include "frontends/font_metrics.h"
25 #include "codeConvert.h"
27 #include "support/lstrings.h"
29 #include <boost/scoped_array.hpp>
31 #include <X11/Xft/Xft.h>
36 GPainter::GPainter(GWorkArea & xwa)
37 : Painter(), owner_(xwa)
42 int GPainter::paperWidth() const
44 return owner_.workWidth();
48 int GPainter::paperHeight() const
50 return owner_.workHeight();
54 void GPainter::setForeground(Glib::RefPtr<Gdk::GC> gc, LColor_color clr)
56 Gdk::Color * gclr = owner_.getColorHandler().getGdkColor(clr);
57 gc->set_foreground(*gclr);
61 void GPainter::setLineParam(Glib::RefPtr<Gdk::GC> gc,
62 line_style ls, line_width lw)
67 case Painter::line_thin:
70 case Painter::line_thick:
76 case Painter::line_solid:
77 style = Gdk::LINE_SOLID;
79 case Painter::line_onoffdash:
80 style = Gdk::LINE_ON_OFF_DASH;
83 gc->set_line_attributes(width, style,
84 Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
88 Painter & GPainter::point(int x, int y, LColor_color c)
90 setForeground(owner_.getGC(), c);
91 owner_.getPixmap()->draw_point(owner_.getGC(), x, y);
96 Painter & GPainter::line(int x1, int y1,
102 setForeground(owner_.getGC(), col);
103 setLineParam(owner_.getGC(), ls, lw);
104 owner_.getPixmap()->draw_line(owner_.getGC(), x1, y1, x2, y2);
109 Painter & GPainter::lines(int const * xp, int const * yp,
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 Painter & GPainter::rectangle(int x, int y,
134 setForeground(owner_.getGC(), col);
135 setLineParam(owner_.getGC(), ls, lw);
136 owner_.getPixmap()->draw_rectangle(owner_.getGC(), false, x, y, w, h);
141 Painter & GPainter::fillRectangle(int x, int y,
145 setForeground(owner_.getGC(), col);
146 owner_.getPixmap()->draw_rectangle(owner_.getGC(), true, x, y, w, h);
151 Painter & GPainter::fillPolygon(int const * xp, int const * yp,
152 int np, LColor_color col)
154 setForeground(owner_.getGC(), col);
155 std::vector<Gdk::Point> points(np);
157 for (int i = 0; i < np; ++i) {
158 points[i].set_x(xp[i]);
159 points[i].set_y(yp[i]);
161 owner_.getPixmap()->draw_polygon(owner_.getGC(), true, points);
166 Painter & GPainter::arc(int x, int y,
167 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);
177 Painter & GPainter::image(int x, int y,
179 lyx::graphics::Image const & i)
181 lyx::graphics::xformsImage const & image =
182 static_cast<lyx::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);
191 Painter & GPainter::text(int x, int y,
192 string const & s, LyXFont const & f)
194 size_t size = s.length() + 1;
195 wchar_t * wcs = (wchar_t *) alloca(size * sizeof(wchar_t));
196 size = mbstowcs(wcs, s.c_str(), size);
197 return text(x, y, wcs, size, f);
201 Painter & GPainter::text(int x, int y,
202 char c, LyXFont const & f)
204 char s[2] = { c, '\0' };
205 return text(x, y, s, 1, f);
209 inline XftFont * getXftFont(LyXFont const & f)
211 return fontLoader.load(f.family(), f.series(),
212 f.realShape(), f.size());
216 namespace font_metrics
219 int width(wchar_t const *s, size_t n, LyXFont const & f);
224 Painter & GPainter::text(int x, int y, wchar_t const * s, int ls,
227 XftFont * font = getXftFont(f);
228 XftColor * xftClr = owner_.getColorHandler().
229 getXftColor(f.realColor());
230 // getXftColor(f.realColor());
231 XftDraw * draw = owner_.getXftDraw();
232 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
233 XftDrawString32(draw, xftClr, font, x, y,
234 wcsToFcChar32StrFast(s), ls);
236 LyXFont smallfont(f);
237 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
238 XftFont * fontS = getXftFont(smallfont);
241 for(int i = 0; i < ls; ++i) {
242 c = lyx::support::uppercase(s[i]);
244 XftDrawString32(draw, xftClr, fontS, tmpx, y,
245 wcsToFcChar32StrFast(&c), 1);
246 tmpx += font_metrics::width(c, smallfont);
248 XftDrawString32(draw, xftClr, font, tmpx, y,
249 wcsToFcChar32StrFast(&c), 1);
250 tmpx += font_metrics::width(c, f);
254 if (f.underbar() == LyXFont::ON)
255 underline(f, x, y, font_metrics::width(s, ls, f));
260 Painter & GPainter::text(int x, int y,
261 char const * s, size_t ls,
264 boost::scoped_array<wchar_t> wcs(new wchar_t[ls + 1]);
266 if (fontLoader.isSpecial(f)) {
267 unsigned char const * us =
268 reinterpret_cast<unsigned char const *>(s);
270 std::copy(us, us + ls, wcs.get());
272 len = mbstowcs(wcs.get(), s, ls + 1);
273 return text(x, y, wcs.get(), len, f);