1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team
8 *======================================================*/
13 #pragma implementation
16 #include "frontends/Painter.h"
20 #include "frontends/xforms/XWorkArea.h"
21 #include "xfont_metrics.h"
22 #include "ColorHandler.h"
27 #include "graphics/GraphicsImage.h"
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
32 #include <boost/scoped_array.hpp>
45 return fl_get_display();
51 Painter::Painter(WorkArea & wa)
56 // Basic drawing routines
58 PainterBase & Painter::point(int x, int y, LColor::color c)
60 XDrawPoint(display(), owner.getPixmap(),
61 lyxColorHandler->getGCForeground(c), x, y);
66 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
71 XDrawLine(display(), owner.getPixmap(),
72 lyxColorHandler->getGCLinepars(ls, lw, col),
78 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
83 boost::scoped_array<XPoint> points(new XPoint[np]);
85 for (int i = 0; i < np; ++i) {
90 XDrawLines(display(), owner.getPixmap(),
91 lyxColorHandler->getGCLinepars(ls, lw, col),
92 points.get(), np, CoordModeOrigin);
98 PainterBase & Painter::rectangle(int x, int y, int w, int h,
103 XDrawRectangle(display(), owner.getPixmap(),
104 lyxColorHandler->getGCLinepars(ls, lw, col),
110 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
113 XFillRectangle(display(), owner.getPixmap(),
114 lyxColorHandler->getGCForeground(col), x, y, w, h);
119 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
122 boost::scoped_array<XPoint> points(new XPoint[np]);
124 for (int i = 0; i < np; ++i) {
129 XFillPolygon(display(), owner.getPixmap(),
130 lyxColorHandler->getGCForeground(col), points.get(), np,
131 Nonconvex, CoordModeOrigin);
137 PainterBase & Painter::arc(int x, int y,
138 unsigned int w, unsigned int h,
139 int a1, int a2, LColor::color col)
141 XDrawArc(display(), owner.getPixmap(),
142 lyxColorHandler->getGCForeground(col),
148 /// Draw lines from x1,y1 to x2,y2. They are arrays
149 PainterBase & Painter::segments(int const * x1, int const * y1,
150 int const * x2, int const * y2, int ns,
152 enum line_style ls, enum line_width lw)
154 boost::scoped_array<XSegment> s(new XSegment[ns]);
156 for (int i = 0; i < ns; ++i) {
162 XDrawSegments(display(), owner.getPixmap(),
163 lyxColorHandler->getGCLinepars(ls, lw, col),
170 PainterBase & Painter::image(int x, int y, int w, int h,
171 grfx::GImage const & image)
174 val.function = GXcopy;
175 GC gc = XCreateGC(display(), owner.getPixmap(),
177 XCopyArea(display(), image.getPixmap(), owner.getPixmap(), gc,
179 XFreeGC(display(), gc);
184 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
186 return text(x, y, s.data(), s.length(), f);
190 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
192 char s[2] = { c, '\0' };
193 return text(x, y, s, 1, f);
197 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
200 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
201 boost::scoped_array<XChar2b> xs(new XChar2b[ls]);
202 Encoding const * encoding = f.language()->encoding();
204 if (f.isSymbolFont()) {
205 #ifdef USE_UNICODE_FOR_SYMBOLS
206 font.setFamily(LyXFont::ROMAN_FAMILY);
207 font.setShape(LyXFont::UP_SHAPE);
209 encoding = encodings.symbol_encoding();
211 for (size_t i = 0; i < ls; ++i) {
212 Uchar c = encoding->ucs(s[i]);
213 xs[i].byte1 = c >> 8;
214 xs[i].byte2 = c & 0xff;
216 text(x , y, xs.get(), ls, font);
220 GC gc = lyxColorHandler->getGCForeground(f.realColor());
221 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
222 xfont_metrics::XSetFont(display(), gc, f);
223 XDrawString(display(), owner.getPixmap(), gc, x, y, s, ls);
225 LyXFont smallfont(f);
226 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
228 for (size_t i = 0; i < ls; ++i) {
229 char const c = uppercase(s[i]);
231 xfont_metrics::XSetFont(display(), gc, smallfont);
232 XDrawString(display(), owner.getPixmap(), gc,
234 tmpx += xfont_metrics::XTextWidth(smallfont, &c, 1);
236 xfont_metrics::XSetFont(display(), gc, f);
237 XDrawString(display(), owner.getPixmap(), gc,
239 tmpx += xfont_metrics::XTextWidth(f, &c, 1);
244 if (f.underbar() == LyXFont::ON) {
245 underline(f, x, y, font_metrics::width(s, ls, f));
252 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
255 GC gc = lyxColorHandler->getGCForeground(f.realColor());
256 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
257 xfont_metrics::XSetFont(display(), gc, f);
258 XDrawString16(display(), owner.getPixmap(), gc, x, y, s, ls);
260 LyXFont smallfont(f);
261 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
264 for (int i = 0; i < ls; ++i) {
268 c.byte1 = s[i].byte1;
269 c.byte2 = uppercase(s[i].byte2);
271 if (c.byte2 != s[i].byte2) {
272 xfont_metrics::XSetFont(display(), gc, smallfont);
273 XDrawString16(display(), owner.getPixmap(), gc,
275 tmpx += xfont_metrics::XTextWidth16(smallfont, &c, 1);
277 xfont_metrics::XSetFont(display(), gc, f);
278 XDrawString16(display(), owner.getPixmap(), gc,
280 tmpx += xfont_metrics::XTextWidth16(f, &c, 1);
285 if (f.underbar() == LyXFont::ON) {
286 underline(f, x, y, xfont_metrics::width(s, ls, f));
293 void Painter::underline(LyXFont const & f, int x, int y, int width)
295 int const below = max(font_metrics::maxDescent(f) / 2, 2);
296 int const height = max((font_metrics::maxDescent(f) / 4) - 1, 1);
298 line(x, y + below, x + width, y + below, f.color());
300 fillRectangle(x, y + below, width, below + height,