1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team
8 *======================================================*/
13 #pragma implementation
22 #include "ColorHandler.h"
27 #include "frontends/GUIRunTime.h"
28 #include "frontends/support/LyXImage.h"
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
33 #include <boost/smart_ptr.hpp>
40 Painter::Painter(WorkArea & wa)
43 display = GUIRunTime::x11Display();
47 // Basic drawing routines
49 PainterBase & Painter::point(int x, int y, LColor::color c)
51 XDrawPoint(display, owner.getPixmap(),
52 lyxColorHandler->getGCForeground(c), x, y);
57 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
62 XDrawLine(display, owner.getPixmap(),
63 lyxColorHandler->getGCLinepars(ls, lw, col),
69 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
74 boost::scoped_array<XPoint> points(new XPoint[np]);
76 for (int i = 0; i < np; ++i) {
81 XDrawLines(display, owner.getPixmap(),
82 lyxColorHandler->getGCLinepars(ls, lw, col),
83 points.get(), np, CoordModeOrigin);
89 PainterBase & Painter::rectangle(int x, int y, int w, int h,
94 XDrawRectangle(display, owner.getPixmap(),
95 lyxColorHandler->getGCLinepars(ls, lw, col),
101 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
104 XFillRectangle(display, owner.getPixmap(),
105 lyxColorHandler->getGCForeground(col), x, y, w, h);
110 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
113 boost::scoped_array<XPoint> points(new XPoint[np]);
115 for (int i = 0; i < np; ++i) {
120 XFillPolygon(display, owner.getPixmap(),
121 lyxColorHandler->getGCForeground(col), points.get(), np,
122 Nonconvex, CoordModeOrigin);
128 PainterBase & Painter::arc(int x, int y,
129 unsigned int w, unsigned int h,
130 int a1, int a2, LColor::color col)
132 XDrawArc(display, owner.getPixmap(),
133 lyxColorHandler->getGCForeground(col),
139 /// Draw lines from x1,y1 to x2,y2. They are arrays
140 PainterBase & Painter::segments(int const * x1, int const * y1,
141 int const * x2, int const * y2, int ns,
143 enum line_style ls, enum line_width lw)
145 boost::scoped_array<XSegment> s(new XSegment[ns]);
147 for (int i = 0; i < ns; ++i) {
153 XDrawSegments(display, owner.getPixmap(),
154 lyxColorHandler->getGCLinepars(ls, lw, col),
161 PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
164 val.function = GXcopy;
165 GC gc = XCreateGC(display, owner.getPixmap(),
167 XCopyArea(display, bitmap, owner.getPixmap(), gc,
169 XFreeGC(display, gc);
174 PainterBase & Painter::image(int x, int y, int w, int h, LyXImage const * image)
176 Pixmap bitmap = image->getPixmap();
178 return pixmap(x, y, w, h, bitmap);
182 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
184 return text(x, y, s.data(), s.length(), f);
188 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
190 char s[2] = { c, '\0' };
191 return text(x, y, s, 1, f);
195 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
198 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
199 boost::scoped_array<XChar2b> xs(new XChar2b[ls]);
200 Encoding const * encoding = f.language()->encoding();
202 if (f.isSymbolFont()) {
203 #ifdef USE_UNICODE_FOR_SYMBOLS
204 font.setFamily(LyXFont::ROMAN_FAMILY);
205 font.setShape(LyXFont::UP_SHAPE);
207 encoding = encodings.symbol_encoding();
209 for (size_t i = 0; i < ls; ++i) {
210 Uchar c = encoding->ucs(s[i]);
211 xs[i].byte1 = c >> 8;
212 xs[i].byte2 = c & 0xff;
214 text(x , y, xs.get(), ls, font);
218 GC gc = lyxColorHandler->getGCForeground(f.realColor());
219 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
220 lyxfont::XSetFont(display, gc, f);
221 XDrawString(display, owner.getPixmap(), gc, x, y, s, ls);
223 LyXFont smallfont(f);
224 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
226 for (size_t i = 0; i < ls; ++i) {
228 if (islower(static_cast<unsigned char>(c))) {
230 lyxfont::XSetFont(display, gc, smallfont);
231 XDrawString(display, owner.getPixmap(),
233 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
235 lyxfont::XSetFont(display, gc, f);
236 XDrawString(display, owner.getPixmap(),
238 tmpx += lyxfont::XTextWidth(f, &c, 1);
243 if (f.underbar() == LyXFont::ON) {
244 underline(f, x, y, lyxfont::width(s, ls, f));
251 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
254 GC gc = lyxColorHandler->getGCForeground(f.realColor());
255 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
256 lyxfont::XSetFont(display, gc, f);
257 XDrawString16(display, owner.getPixmap(), gc, x, y, s, ls);
259 LyXFont smallfont(f);
260 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
263 for (int i = 0; i < ls; ++i) {
264 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
265 c.byte2 = toupper(s[i].byte2);
266 lyxfont::XSetFont(display, gc, smallfont);
267 XDrawString16(display, owner.getPixmap(),
269 tmpx += lyxfont::XTextWidth16(smallfont, &c, 1);
271 lyxfont::XSetFont(display, gc, f);
272 XDrawString16(display, owner.getPixmap(),
273 gc, tmpx, y, &s[i], 1);
274 tmpx += lyxfont::XTextWidth16(f, const_cast<XChar2b *>(&s[i]), 1);
279 if (f.underbar() == LyXFont::ON) {
280 underline(f, x, y, lyxfont::width(s, ls, f));
287 void Painter::underline(LyXFont const & f, int x, int y, int width)
289 int const below = max(lyxfont::maxDescent(f) / 2, 2);
290 int const height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
292 line(x, y + below, x + width, y + below, f.color());
294 fillRectangle(x, y + below, width, below + height,