1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team
8 *======================================================*/
13 #pragma implementation
26 #include "frontends/GUIRunTime.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
31 #include "ColorHandler.h"
36 #include "frontends/support/LyXImage.h"
41 Painter::Painter(WorkArea & wa)
44 display = GUIRunTime::x11Display();
48 /* Basic drawing routines */
50 PainterBase & Painter::point(int x, int y, LColor::color c)
52 XDrawPoint(display, owner.getPixmap(),
53 lyxColorHandler->getGCForeground(c), x, y);
58 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
63 XDrawLine(display, owner.getPixmap(),
64 lyxColorHandler->getGCLinepars(ls, lw, col),
70 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
76 XPoint * points = new XPoint[np];
78 auto_ptr<XPoint> points(new Xpoint[np]);
80 for (int i = 0; i < np; ++i) {
85 XDrawLines(display, owner.getPixmap(),
86 lyxColorHandler->getGCLinepars(ls, lw, col),
87 points, np, CoordModeOrigin);
96 PainterBase & Painter::rectangle(int x, int y, int w, int h,
101 XDrawRectangle(display, owner.getPixmap(),
102 lyxColorHandler->getGCLinepars(ls, lw, col),
108 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
111 XFillRectangle(display, owner.getPixmap(),
112 lyxColorHandler->getGCForeground(col), x, y, w, h);
117 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
120 #ifndef HAVE_AUTO_PTR
121 XPoint * points = new XPoint[np];
123 auto_ptr<XPoint> points(new XPoint[np]);
125 for (int i=0; i < np; ++i) {
130 XFillPolygon(display, owner.getPixmap(),
131 lyxColorHandler->getGCForeground(col), points, np,
132 Nonconvex, CoordModeOrigin);
133 #ifndef HAVE_AUTO_PTR
140 PainterBase & Painter::arc(int x, int y,
141 unsigned int w, unsigned int h,
142 int a1, int a2, LColor::color col)
144 XDrawArc(display, owner.getPixmap(),
145 lyxColorHandler->getGCForeground(col),
151 /// Draw lines from x1,y1 to x2,y2. They are arrays
152 PainterBase & Painter::segments(int const * x1, int const * y1,
153 int const * x2, int const * y2, int ns,
155 enum line_style ls, enum line_width lw)
157 #ifndef HAVE_AUTO_PTR
158 XSegment * s= new XSegment[ns];
160 auto_ptr<XSegment> s(new XSegment[ns]);
162 for (int i=0; i<ns; ++i) {
168 XDrawSegments(display, owner.getPixmap(),
169 lyxColorHandler->getGCLinepars(ls, lw, col), s, ns);
171 #ifndef HAVE_AUTO_PTR
177 PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
180 val.function = GXcopy;
181 GC gc = XCreateGC(display, owner.getPixmap(),
183 XCopyArea(display, bitmap, owner.getPixmap(), gc,
185 XFreeGC(display, gc);
189 PainterBase & Painter::image(int x, int y, int w, int h, LyXImage const * image)
191 Pixmap bitmap = image->getPixmap();
193 return pixmap(x, y, w, h, bitmap);
197 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
199 return text(x, y, s.data(), s.length(), f);
203 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
205 char s[2] = { c, '\0' };
206 return text(x, y, s, 1, f);
210 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
213 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
214 XChar2b * xs = new XChar2b[ls];
215 Encoding const * encoding = f.language()->encoding();
217 if (f.isSymbolFont()) {
218 #ifdef USE_UNICODE_FOR_SYMBOLS
219 font.setFamily(LyXFont::ROMAN_FAMILY);
220 font.setShape(LyXFont::UP_SHAPE);
222 encoding = encodings.symbol_encoding();
224 for (size_t i = 0; i < ls; ++i) {
225 Uchar c = encoding->ucs(s[i]);
226 xs[i].byte1 = c >> 8;
227 xs[i].byte2 = c & 0xff;
229 text(x , y, xs, ls, font);
234 GC gc = lyxColorHandler->getGCForeground(f.realColor());
235 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
236 lyxfont::XSetFont(display, gc, f);
237 XDrawString(display, owner.getPixmap(), gc, x, y, s, ls);
239 LyXFont smallfont(f);
240 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
243 for (size_t i = 0; i < ls; ++i) {
245 if (islower(static_cast<unsigned char>(c))) {
247 lyxfont::XSetFont(display, gc, smallfont);
248 XDrawString(display, owner.getPixmap(),
250 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
252 lyxfont::XSetFont(display, gc, f);
253 XDrawString(display, owner.getPixmap(),
255 tmpx += lyxfont::XTextWidth(f, &c, 1);
260 if (f.underbar() == LyXFont::ON) {
261 underline(f, x, y, lyxfont::width(s, ls, f));
268 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
271 GC gc = lyxColorHandler->getGCForeground(f.realColor());
272 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
273 lyxfont::XSetFont(display, gc, f);
274 XDrawString16(display, owner.getPixmap(), gc, x, y, s, ls);
276 LyXFont smallfont(f);
277 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
280 for (int i = 0; i < ls; ++i) {
281 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
282 c.byte2 = toupper(s[i].byte2);
283 lyxfont::XSetFont(display, gc, smallfont);
284 XDrawString16(display, owner.getPixmap(),
286 tmpx += lyxfont::XTextWidth16(smallfont, &c, 1);
288 lyxfont::XSetFont(display, gc, f);
289 XDrawString16(display, owner.getPixmap(),
290 gc, tmpx, y, &s[i], 1);
291 tmpx += lyxfont::XTextWidth16(f, const_cast<XChar2b *>(&s[i]), 1);
296 if (f.underbar() == LyXFont::ON) {
297 underline(f, x, y, lyxfont::width(s, ls, f));
304 void Painter::underline(LyXFont const & f, int x, int y, int width)
306 int below = max(lyxfont::maxDescent(f) / 2, 2);
307 int height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
309 line(x, y + below, x + width, y + below, f.color());
311 fillRectangle(x, y + below, width, below + height,