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 "graphics/GraphicsImage.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,
175 grfx::GImage const & image)
177 return pixmap(x, y, w, h, image.getPixmap());
181 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
183 return text(x, y, s.data(), s.length(), f);
187 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
189 char s[2] = { c, '\0' };
190 return text(x, y, s, 1, f);
194 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
197 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
198 boost::scoped_array<XChar2b> xs(new XChar2b[ls]);
199 Encoding const * encoding = f.language()->encoding();
201 if (f.isSymbolFont()) {
202 #ifdef USE_UNICODE_FOR_SYMBOLS
203 font.setFamily(LyXFont::ROMAN_FAMILY);
204 font.setShape(LyXFont::UP_SHAPE);
206 encoding = encodings.symbol_encoding();
208 for (size_t i = 0; i < ls; ++i) {
209 Uchar c = encoding->ucs(s[i]);
210 xs[i].byte1 = c >> 8;
211 xs[i].byte2 = c & 0xff;
213 text(x , y, xs.get(), ls, font);
217 GC gc = lyxColorHandler->getGCForeground(f.realColor());
218 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
219 lyxfont::XSetFont(display, gc, f);
220 XDrawString(display, owner.getPixmap(), gc, x, y, s, ls);
222 LyXFont smallfont(f);
223 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
225 for (size_t i = 0; i < ls; ++i) {
226 char const c = uppercase(s[i]);
228 lyxfont::XSetFont(display, gc, smallfont);
229 XDrawString(display, owner.getPixmap(), gc,
231 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
233 lyxfont::XSetFont(display, gc, f);
234 XDrawString(display, owner.getPixmap(), gc,
236 tmpx += lyxfont::XTextWidth(f, &c, 1);
241 if (f.underbar() == LyXFont::ON) {
242 underline(f, x, y, lyxfont::width(s, ls, f));
249 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
252 GC gc = lyxColorHandler->getGCForeground(f.realColor());
253 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
254 lyxfont::XSetFont(display, gc, f);
255 XDrawString16(display, owner.getPixmap(), gc, x, y, s, ls);
257 LyXFont smallfont(f);
258 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
261 for (int i = 0; i < ls; ++i) {
265 c.byte1 = s[i].byte1;
266 c.byte2 = uppercase(s[i].byte2);
268 if (c.byte2 != s[i].byte2) {
269 lyxfont::XSetFont(display, gc, smallfont);
270 XDrawString16(display, owner.getPixmap(), gc,
272 tmpx += lyxfont::XTextWidth16(smallfont, &c, 1);
274 lyxfont::XSetFont(display, gc, f);
275 XDrawString16(display, owner.getPixmap(), gc,
277 tmpx += lyxfont::XTextWidth16(f, &c, 1);
282 if (f.underbar() == LyXFont::ON) {
283 underline(f, x, y, lyxfont::width(s, ls, f));
290 void Painter::underline(LyXFont const & f, int x, int y, int width)
292 int const below = max(lyxfont::maxDescent(f) / 2, 2);
293 int const height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
295 line(x, y + below, x + width, y + below, f.color());
297 fillRectangle(x, y + below, width, below + height,