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 "frontends/GUIRunTime.h"
28 #include "graphics/GraphicsImage.h"
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
33 #include <boost/scoped_array.hpp>
46 return GUIRunTime::x11Display();
52 Painter::Painter(WorkArea & wa)
57 // Basic drawing routines
59 PainterBase & Painter::point(int x, int y, LColor::color c)
61 XDrawPoint(display(), owner.getPixmap(),
62 lyxColorHandler->getGCForeground(c), x, y);
67 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
72 XDrawLine(display(), owner.getPixmap(),
73 lyxColorHandler->getGCLinepars(ls, lw, col),
79 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
84 boost::scoped_array<XPoint> points(new XPoint[np]);
86 for (int i = 0; i < np; ++i) {
91 XDrawLines(display(), owner.getPixmap(),
92 lyxColorHandler->getGCLinepars(ls, lw, col),
93 points.get(), np, CoordModeOrigin);
99 PainterBase & Painter::rectangle(int x, int y, int w, int h,
104 XDrawRectangle(display(), owner.getPixmap(),
105 lyxColorHandler->getGCLinepars(ls, lw, col),
111 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
114 XFillRectangle(display(), owner.getPixmap(),
115 lyxColorHandler->getGCForeground(col), x, y, w, h);
120 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
123 boost::scoped_array<XPoint> points(new XPoint[np]);
125 for (int i = 0; i < np; ++i) {
130 XFillPolygon(display(), owner.getPixmap(),
131 lyxColorHandler->getGCForeground(col), points.get(), np,
132 Nonconvex, CoordModeOrigin);
138 PainterBase & Painter::arc(int x, int y,
139 unsigned int w, unsigned int h,
140 int a1, int a2, LColor::color col)
142 XDrawArc(display(), owner.getPixmap(),
143 lyxColorHandler->getGCForeground(col),
149 /// Draw lines from x1,y1 to x2,y2. They are arrays
150 PainterBase & Painter::segments(int const * x1, int const * y1,
151 int const * x2, int const * y2, int ns,
153 enum line_style ls, enum line_width lw)
155 boost::scoped_array<XSegment> s(new XSegment[ns]);
157 for (int i = 0; i < ns; ++i) {
163 XDrawSegments(display(), owner.getPixmap(),
164 lyxColorHandler->getGCLinepars(ls, lw, col),
171 PainterBase & Painter::image(int x, int y, int w, int h,
172 grfx::GImage const & image)
175 val.function = GXcopy;
176 GC gc = XCreateGC(display(), owner.getPixmap(),
178 XCopyArea(display(), image.getPixmap(), owner.getPixmap(), gc,
180 XFreeGC(display(), gc);
185 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
187 return text(x, y, s.data(), s.length(), f);
191 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
193 char s[2] = { c, '\0' };
194 return text(x, y, s, 1, f);
198 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
201 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
202 boost::scoped_array<XChar2b> xs(new XChar2b[ls]);
203 Encoding const * encoding = f.language()->encoding();
205 if (f.isSymbolFont()) {
206 #ifdef USE_UNICODE_FOR_SYMBOLS
207 font.setFamily(LyXFont::ROMAN_FAMILY);
208 font.setShape(LyXFont::UP_SHAPE);
210 encoding = encodings.symbol_encoding();
212 for (size_t i = 0; i < ls; ++i) {
213 Uchar c = encoding->ucs(s[i]);
214 xs[i].byte1 = c >> 8;
215 xs[i].byte2 = c & 0xff;
217 text(x , y, xs.get(), ls, font);
221 GC gc = lyxColorHandler->getGCForeground(f.realColor());
222 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
223 xfont_metrics::XSetFont(display(), gc, f);
224 XDrawString(display(), owner.getPixmap(), gc, x, y, s, ls);
226 LyXFont smallfont(f);
227 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
229 for (size_t i = 0; i < ls; ++i) {
230 char const c = uppercase(s[i]);
232 xfont_metrics::XSetFont(display(), gc, smallfont);
233 XDrawString(display(), owner.getPixmap(), gc,
235 tmpx += xfont_metrics::XTextWidth(smallfont, &c, 1);
237 xfont_metrics::XSetFont(display(), gc, f);
238 XDrawString(display(), owner.getPixmap(), gc,
240 tmpx += xfont_metrics::XTextWidth(f, &c, 1);
245 if (f.underbar() == LyXFont::ON) {
246 underline(f, x, y, font_metrics::width(s, ls, f));
253 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
256 GC gc = lyxColorHandler->getGCForeground(f.realColor());
257 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
258 xfont_metrics::XSetFont(display(), gc, f);
259 XDrawString16(display(), owner.getPixmap(), gc, x, y, s, ls);
261 LyXFont smallfont(f);
262 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
265 for (int i = 0; i < ls; ++i) {
269 c.byte1 = s[i].byte1;
270 c.byte2 = uppercase(s[i].byte2);
272 if (c.byte2 != s[i].byte2) {
273 xfont_metrics::XSetFont(display(), gc, smallfont);
274 XDrawString16(display(), owner.getPixmap(), gc,
276 tmpx += xfont_metrics::XTextWidth16(smallfont, &c, 1);
278 xfont_metrics::XSetFont(display(), gc, f);
279 XDrawString16(display(), owner.getPixmap(), gc,
281 tmpx += xfont_metrics::XTextWidth16(f, &c, 1);
286 if (f.underbar() == LyXFont::ON) {
287 underline(f, x, y, xfont_metrics::width(s, ls, f));
294 void Painter::underline(LyXFont const & f, int x, int y, int width)
296 int const below = max(font_metrics::maxDescent(f) / 2, 2);
297 int const height = max((font_metrics::maxDescent(f) / 4) - 1, 1);
299 line(x, y + below, x + width, y + below, f.color());
301 fillRectangle(x, y + below, width, below + height,