X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FPainter.C;h=6b8d760bbbb0488b1a24cc5bd41ee7b3a2f7c3a0;hb=98c966c64594611e469313314abd1e59524adb4a;hp=b46b1cef75dc2a7a3583763e44dc458baf0cde75;hpb=391d71dab2c0d895c6949185e8f4f48c0fdb22e9;p=lyx.git diff --git a/src/Painter.C b/src/Painter.C index b46b1cef75..6b8d760bbb 100644 --- a/src/Painter.C +++ b/src/Painter.C @@ -1,456 +1,303 @@ -// -*- C++ -*- /* This file is part of * ====================================================== - * + * * LyX, The Document Processor - * - * Copyright 1998-2000 The LyX Team + * + * Copyright 1998-2001 The LyX Team * *======================================================*/ #include -#ifdef USE_PAINTER - #ifdef __GNUG__ #pragma implementation #endif -#include - -#ifdef USE_STL_MEMORY -#include -#endif - -#include FORMS_H_LOCATION #include "Painter.h" #include "LString.h" #include "debug.h" #include "lyxfont.h" +#include "WorkArea.h" +#include "font.h" +#include "ColorHandler.h" +#include "lyxrc.h" +#include "encoding.h" +#include "language.h" + +#include "frontends/GUIRunTime.h" +#include "graphics/GraphicsImage.h" + #include "support/LAssert.h" #include "support/lstrings.h" -Painter::Painter(WorkArea & wa) - : PainterBase(wa) +#include + +#include + + +using std::endl; +using std::max; + +namespace { + +inline +Display * display() { - colormap = fl_state[fl_get_vclass()].colormap; - // Clear the GC cache - for (int i=0; i <= LColor::ignore; ++i) { - colorGCcache[i] = 0; - } + return GUIRunTime::x11Display(); } - -Painter::~Painter() { - // Release all the registered GCs - for (int i=0; i <= LColor::ignore; ++i) { - if (colorGCcache[i] != 0) { - XFreeGC(display, colorGCcache[i]); - } - } - // Iterate over the line cache and Free the GCs - LineGCCache::iterator lit = lineGCcache.begin(); - for (; lit != lineGCcache.end(); ++lit) { - XFreeGC(display, (*lit).second); - } } -/* Basic drawing routines */ +Painter::Painter(WorkArea & wa) + : PainterBase(wa) +{} + -extern bool Lgb_bug_find_hack; +// Basic drawing routines PainterBase & Painter::point(int x, int y, LColor::color c) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "point not called from " - "workarea::workhandler\n"; - lyxerr.debug() << "Painter drawable: " << drawable << endl; - } - - XDrawPoint(display, drawable, getGCForeground(c), x, y); + XDrawPoint(display(), owner.getPixmap(), + lyxColorHandler->getGCForeground(c), x, y); return *this; } PainterBase & Painter::line(int x1, int y1, int x2, int y2, - LColor::color col, - enum line_style ls, - enum line_width lw) + LColor::color col, + enum line_style ls, + enum line_width lw) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "line not called from " - "workarea::workhandler\n"; - lyxerr.debug() << "Painter drawable: " << drawable << endl; - } - - XDrawLine(display, drawable, - getGCLinepars(ls, lw, col), x1, y1, x2, y2); + XDrawLine(display(), owner.getPixmap(), + lyxColorHandler->getGCLinepars(ls, lw, col), + x1, y1, x2, y2); return *this; } PainterBase & Painter::lines(int const * xp, int const * yp, int np, - LColor::color col, - enum line_style ls, - enum line_width lw) + LColor::color col, + enum line_style ls, + enum line_width lw) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "lines not called from " - "workarea::workhandler\n"; - lyxerr.debug() << "Painter drawable: " << drawable << endl; - } - -#ifndef HAVE_AUTO_PTR - XPoint * points = new XPoint[np]; -#else - auto_ptr points(new Xpoint[np]); -#endif - for (int i=0; i points(new XPoint[np]); + + for (int i = 0; i < np; ++i) { points[i].x = xp[i]; points[i].y = yp[i]; } - XDrawLines(display, drawable, getGCLinepars(ls, lw, col), - points, np, CoordModeOrigin); + XDrawLines(display(), owner.getPixmap(), + lyxColorHandler->getGCLinepars(ls, lw, col), + points.get(), np, CoordModeOrigin); -#ifndef HAVE_AUTO_PTR - delete[] points; -#endif return *this; -} +} PainterBase & Painter::rectangle(int x, int y, int w, int h, - LColor::color col, - enum line_style ls, - enum line_width lw) + LColor::color col, + enum line_style ls, + enum line_width lw) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "rectangle not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - - XDrawRectangle(display, drawable, getGCLinepars(ls, lw, col), + XDrawRectangle(display(), owner.getPixmap(), + lyxColorHandler->getGCLinepars(ls, lw, col), x, y, w, h); return *this; } PainterBase & Painter::fillRectangle(int x, int y, int w, int h, - LColor::color col) + LColor::color col) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "fillrectangle not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - - XFillRectangle(display, drawable, getGCForeground(col), x, y, w, h); + XFillRectangle(display(), owner.getPixmap(), + lyxColorHandler->getGCForeground(col), x, y, w, h); return *this; } PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np, - LColor::color col) + LColor::color col) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr <<"fillpolygon not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - -#ifndef HAVE_AUTO_PTR - XPoint * points = new XPoint[np]; -#else - auto_ptr points(new XPoint[np]); -#endif - for (int i=0; i points(new XPoint[np]); + + for (int i = 0; i < np; ++i) { points[i].x = xp[i]; points[i].y = yp[i]; } - XFillPolygon(display, drawable, getGCForeground(col), points, np, + XFillPolygon(display(), owner.getPixmap(), + lyxColorHandler->getGCForeground(col), points.get(), np, Nonconvex, CoordModeOrigin); -#ifndef HAVE_AUTO_PTR - delete[] points; -#endif + return *this; -} +} PainterBase & Painter::arc(int x, int y, - unsigned int w, unsigned int h, - int a1, int a2, LColor::color col) + unsigned int w, unsigned int h, + int a1, int a2, LColor::color col) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "arc not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - - XDrawArc(display, drawable, getGCForeground(col), - x, y, w, h, a1, a2); + XDrawArc(display(), owner.getPixmap(), + lyxColorHandler->getGCForeground(col), + x, y, w, h, a1, a2); return *this; -} +} /// Draw lines from x1,y1 to x2,y2. They are arrays -PainterBase & Painter::segments(int const * x1, int const * y1, - int const * x2, int const * y2, int ns, - LColor::color col, - enum line_style ls, enum line_width lw) +PainterBase & Painter::segments(int const * x1, int const * y1, + int const * x2, int const * y2, int ns, + LColor::color col, + enum line_style ls, enum line_width lw) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "segments not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - -#ifndef HAVE_AUTO_PTR - XSegment * s= new XSegment[ns]; -#else - auto_ptr s(new XSegment[ns]); -#endif - for (int i=0; i s(new XSegment[ns]); + + for (int i = 0; i < ns; ++i) { s[i].x1 = x1[i]; s[i].y1 = y1[i]; s[i].x2 = x2[i]; s[i].y2 = y2[i]; } - XDrawSegments(display, drawable, getGCLinepars(ls, lw, col), s, ns); + XDrawSegments(display(), owner.getPixmap(), + lyxColorHandler->getGCLinepars(ls, lw, col), + s.get(), ns); -#ifndef HAVE_AUTO_PTR - delete [] s; -#endif return *this; } -PainterBase & Painter::pixmap(int x, int y, Pixmap bitmap) +PainterBase & Painter::image(int x, int y, int w, int h, + grfx::GImage const & image) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "workAreaExpose not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - XGCValues val; val.function = GXcopy; - GC gc = XCreateGC(display, drawable, + GC gc = XCreateGC(display(), owner.getPixmap(), GCFunction, &val); - int w = 0, h = 0; - XCopyArea(display, bitmap, drawable, gc, + XCopyArea(display(), image.getPixmap(), owner.getPixmap(), gc, 0, 0, w, h, x, y); - XFreeGC(display, gc); + XFreeGC(display(), gc); return *this; } PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "text not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - - GC gc = getGCForeground(f.realColor()); - XSetFont(display, gc, f.getFontID()); - XDrawString(display, drawable, gc, x, y, s.c_str(), s.length()); - underline(f, x, y, this->width(s, f)); - return *this; -} - - -PainterBase & Painter::text(int x, int y, char const * s, int ls, - LyXFont const & f) -{ - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "text not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - - GC gc = getGCForeground(f.realColor()); - XSetFont(display, gc, f.getFontID()); - XDrawString(display, drawable, gc, x, y, s, ls); - underline(f, x, y, this->width(s, ls, f)); - return *this; + return text(x, y, s.data(), s.length(), f); } PainterBase & Painter::text(int x, int y, char c, LyXFont const & f) { - if (lyxerr.debugging()) { - if (!Lgb_bug_find_hack) - lyxerr << "text not called from " - "workarea::workhandler\n"; - lyxerr << "Painter drawable: " << drawable << endl; - } - - GC gc = getGCForeground(f.realColor()); - XSetFont(display, gc, f.getFontID()); - char s[2]; - s[0] = c; s[1] = '\0'; - XDrawString(display, drawable, gc, x, y, s, 1); - underline(f, x, y, this->width(c, f)); - return *this; + char s[2] = { c, '\0' }; + return text(x, y, s, 1, f); } -void Painter::underline(LyXFont const & f, int x, int y, int width) { - // What about underbars? - if (f.underbar() == LyXFont::ON && f.latex() != LyXFont::ON) { - int below = f.maxDescent() / 2; - if (below < 2) below = 2; - int height = (f.maxDescent() / 4) - 1; - if (height < 0) height = 0; - fillRectangle(x, y + below, width, below + height, f.color()); - } -} - - -// Gets GC according to color -// Uses caching -GC Painter::getGCForeground(LColor::color c) +PainterBase & Painter::text(int x, int y, char const * s, size_t ls, + LyXFont const & f) { - if (lyxerr.debugging()) { - lyxerr << "Painter drawable: " << drawable << endl; + if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) { + boost::scoped_array xs(new XChar2b[ls]); + Encoding const * encoding = f.language()->encoding(); + LyXFont font(f); + if (f.isSymbolFont()) { +#ifdef USE_UNICODE_FOR_SYMBOLS + font.setFamily(LyXFont::ROMAN_FAMILY); + font.setShape(LyXFont::UP_SHAPE); +#endif + encoding = encodings.symbol_encoding(); + } + for (size_t i = 0; i < ls; ++i) { + Uchar c = encoding->ucs(s[i]); + xs[i].byte1 = c >> 8; + xs[i].byte2 = c & 0xff; + } + text(x , y, xs.get(), ls, font); + return *this; } - - if (colorGCcache[c] != 0) return colorGCcache[c]; - XColor xcol, ccol; - string s = lcolor.getX11Name(c); - XGCValues val; - - // Look up the RGB values for the color, and an approximate - // color that we can hope to get on this display. - if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) { - lyxerr << _("LyX: Unknown X11 color ") << s - << _(" for ") << lcolor.getGUIName(c) << '\n' - << _(" Using black instead, sorry!.") << endl; - unsigned long bla = BlackPixel(display, - DefaultScreen(display)); - val.foreground = bla; - // Try the exact RGB values first, then the approximate. - } else if (XAllocColor(display, colormap, &xcol) != 0) { - lyxerr << _("LyX: X11 color ") << s - << _(" allocated for ") - << lcolor.getGUIName(c) << endl; - val.foreground = xcol.pixel; - } else if (XAllocColor(display, colormap, &ccol)) { - lyxerr << _("LyX: Using approximated X11 color ") << s - << _(" allocated for ") - << lcolor.getGUIName(c) << endl; - val.foreground = xcol.pixel; + GC gc = lyxColorHandler->getGCForeground(f.realColor()); + if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) { + lyxfont::XSetFont(display(), gc, f); + XDrawString(display(), owner.getPixmap(), gc, x, y, s, ls); } else { - // Here we are traversing the current colormap to find - // the color closest to the one we want. - Visual * vi = DefaultVisual(display, DefaultScreen(display)); - - XColor * cmap = new XColor[vi->map_entries]; - - for(int i = 0; i < vi->map_entries; ++i) { - cmap[i].pixel = i; - } - XQueryColors(display, colormap, cmap, vi->map_entries); - - // Walk through the cmap and look for close colors. - int closest_pixel = 0; - double closest_distance = 1e20; // we want to minimize this - double distance = 0; - for(int t = 0; t < vi->map_entries; ++t) { - // The Euclidean distance between two points in - // a three-dimensional space, the RGB color-cube, - // is used as the distance measurement between two - // colors. - - // Since square-root is monotonous, we don't have to - // take the square-root to find the minimum, and thus - // we use the squared distance instead to be faster. - - // If we want to get fancy, we could convert the RGB - // coordinates to a different color-cube, maybe HSV, - // but the RGB cube seems to work great. (Asger) - distance = pow(cmap[t].red - xcol.red, 2.0) + - pow(cmap[t].green - xcol.green, 2.0) + - pow(cmap[t].blue - xcol.blue, 2.0); - if (distance < closest_distance) { - closest_distance = distance; - closest_pixel = t; + LyXFont smallfont(f); + smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE); + int tmpx = x; + for (size_t i = 0; i < ls; ++i) { + char const c = uppercase(s[i]); + if (c != s[i]) { + lyxfont::XSetFont(display(), gc, smallfont); + XDrawString(display(), owner.getPixmap(), gc, + tmpx, y, &c, 1); + tmpx += lyxfont::XTextWidth(smallfont, &c, 1); + } else { + lyxfont::XSetFont(display(), gc, f); + XDrawString(display(), owner.getPixmap(), gc, + tmpx, y, &c, 1); + tmpx += lyxfont::XTextWidth(f, &c, 1); } } - lyxerr << _("LyX: Couldn't allocate '") << s - << _("' for ") << lcolor.getGUIName(c) - << _(" with (r,g,b)=(") - << xcol.red << "," << xcol.green << "," - << xcol.blue << ").\n" - << _(" Using closest allocated " - "color with (r,g,b)=(") - << cmap[closest_pixel].red << "," - << cmap[closest_pixel].green << "," - << cmap[closest_pixel].blue << ") instead.\n" - << "Pixel [" << closest_pixel << "] is used." << endl; - val.foreground = cmap[closest_pixel].pixel; - delete[] cmap; } - val.function = GXcopy; - return colorGCcache[c] = XCreateGC(display, drawable, - GCForeground | GCFunction, &val); + if (f.underbar() == LyXFont::ON) { + underline(f, x, y, lyxfont::width(s, ls, f)); + } + + return *this; } -// Gets GC for line -GC Painter::getGCLinepars(enum line_style ls, - enum line_width lw, LColor::color c) +PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls, + LyXFont const & f) { - if (lyxerr.debugging()) { - lyxerr << "Painter drawable: " << drawable << endl; + GC gc = lyxColorHandler->getGCForeground(f.realColor()); + if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) { + lyxfont::XSetFont(display(), gc, f); + XDrawString16(display(), owner.getPixmap(), gc, x, y, s, ls); + } else { + LyXFont smallfont(f); + smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE); + static XChar2b c; + int tmpx = x; + for (int i = 0; i < ls; ++i) { + if (s[i].byte1) + c = s[i]; + else { + c.byte1 = s[i].byte1; + c.byte2 = uppercase(s[i].byte2); + } + if (c.byte2 != s[i].byte2) { + lyxfont::XSetFont(display(), gc, smallfont); + XDrawString16(display(), owner.getPixmap(), gc, + tmpx, y, &c, 1); + tmpx += lyxfont::XTextWidth16(smallfont, &c, 1); + } else { + lyxfont::XSetFont(display(), gc, f); + XDrawString16(display(), owner.getPixmap(), gc, + tmpx, y, &c, 1); + tmpx += lyxfont::XTextWidth16(f, &c, 1); + } + } } - - int index = lw + (ls << 1) + (c << 3); - - if (lineGCcache.find(index) != lineGCcache.end()) - return lineGCcache[index]; - XGCValues val; - XGetGCValues(display, getGCForeground(c), GCForeground, &val); - - switch (lw) { - case line_thin: val.line_width = 0; break; - case line_thick: val.line_width = 2; break; - } - - switch (ls) { - case line_solid: val.line_style = LineSolid; break; - case line_onoffdash: val.line_style = LineOnOffDash; break; - case line_doubledash: val.line_style = LineDoubleDash; break; + if (f.underbar() == LyXFont::ON) { + underline(f, x, y, lyxfont::width(s, ls, f)); } + return *this; +} - val.cap_style = CapRound; - val.join_style = JoinRound; - val.function = GXcopy; - return lineGCcache[index] = - XCreateGC(display, drawable, - GCForeground | GCLineStyle | GCLineWidth | - GCCapStyle | GCJoinStyle | GCFunction, &val); +void Painter::underline(LyXFont const & f, int x, int y, int width) +{ + int const below = max(lyxfont::maxDescent(f) / 2, 2); + int const height = max((lyxfont::maxDescent(f) / 4) - 1, 1); + if (height < 2) + line(x, y + below, x + width, y + below, f.color()); + else + fillRectangle(x, y + below, width, below + height, + f.color()); } - -#endif