]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xformsImage.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / xformsImage.C
index b4c25e08283e74d87df006af3decbec05bdeb502..bdb98f49cdfdc7537edc578bf38c9cfcd2bcaa15 100644 (file)
@@ -1,26 +1,29 @@
-/*
+/**
  * \file xformsImage.C
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming <leeming@lyx.org>
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "xformsImage.h"
-#include "graphics/GraphicsParams.h"
-#include "LColor.h"
-#include "converter.h"              // formats
+#include "Color.h"
+
 #include "debug.h"
-#include "support/LAssert.h"
+#include "format.h"
+#include "LColor.h"
+
+#include "graphics/GraphicsParams.h"
+
+#include "support/lstrings.h"
 #include "support/lyxfunctional.h"  // compare_memfun
+#include "support/lyxlib.h"
 
-#include FORMS_H_LOCATION
+#include "lyx_forms.h"
 
 #ifdef HAVE_FLIMAGE_H
 # include <flimage.h>
 
 #include <boost/tuple/tuple.hpp>
 
+
+using lyx::support::float_equal;
+using lyx::support::prefixIs;
+using lyx::support::rtrim;
+
 using std::find_if;
+using std::string;
+
 
 namespace {
 
@@ -43,7 +53,8 @@ unsigned int packedcolor(LColor::color c);
 } // namespace anon
 
 
-namespace grfx {
+namespace lyx {
+namespace graphics {
 
 /// Access to this class is through this static method.
 Image::ImagePtr xformsImage::newImage()
@@ -142,24 +153,30 @@ xformsImage::~xformsImage()
 }
 
 
-Image * xformsImage::clone() const
+Pixmap xformsImage::getPixmap() const
+{
+       if (!pixmap_status_ == PIXMAP_SUCCESS)
+               return 0;
+       return pixmap_;
+}
+
+
+Image * xformsImage::clone_impl() const
 {
        return new xformsImage(*this);
 }
 
 
-unsigned int xformsImage::getWidth() const
+unsigned int xformsImage::getWidth_impl() const
 {
        if (!image_)
                return 0;
 
-       // Why, oh why do we need such hacks?
-       // Angus 12 July 2002
-       return image_->w + 2;
+       return image_->w;
 }
 
 
-unsigned int xformsImage::getHeight() const
+unsigned int xformsImage::getHeight_impl() const
 {
        if (!image_)
                return 0;
@@ -167,21 +184,13 @@ unsigned int xformsImage::getHeight() const
 }
 
 
-bool xformsImage::isDrawable() const
-{
-       return pixmap_;
-}
-
-
-Pixmap xformsImage::getPixmap() const
+bool xformsImage::isDrawable_impl() const
 {
-       if (!pixmap_status_ == PIXMAP_SUCCESS)
-               return 0;
        return pixmap_;
 }
 
 
-void xformsImage::load(string const & filename)
+void xformsImage::load_impl(string const & filename)
 {
        if (image_) {
                lyxerr[Debug::GRAPHICS]
@@ -209,7 +218,7 @@ void xformsImage::load(string const & filename)
 }
 
 
-bool xformsImage::setPixmap(Params const & params)
+bool xformsImage::setPixmap_impl(Params const & params)
 {
        if (!image_ || params.display == NoDisplay)
                return false;
@@ -256,7 +265,7 @@ bool xformsImage::setPixmap(Params const & params)
 }
 
 
-void xformsImage::clip(Params const & params)
+void xformsImage::clip_impl(Params const & params)
 {
        if (!image_)
                return;
@@ -278,23 +287,28 @@ void xformsImage::clip(Params const & params)
                // Bounds are unchanged.
                return;
 
-       // FIXME: these values are unsigned so this makes NO sense
-       int const xoffset_l = std::max(0U, params.bb.xl);
-       int const xoffset_r = std::max(0U, image_->w - params.bb.xr);
-       int const yoffset_t = std::max(0U, image_->h - params.bb.yt);
-       int const yoffset_b = std::max(0U, params.bb.yb);
+       // flimage.h: image_ members w and h are of type int
+       // (though always >= 0)
+       // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
+       // type unsigned int.
+       // We must, therefore, be careful...
+       int const xoffset_l = params.bb.xl;
+       int const yoffset_b = params.bb.yb;
+       int const xoffset_r = image_->w > int(params.bb.xr) ?
+               image_->w - params.bb.xr : 0;
+       int const yoffset_t = image_->h > int(params.bb.yt) ?
+               image_->h - params.bb.yt : 0;
 
        flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
 }
 
 
-void xformsImage::rotate(Params const & params)
+void xformsImage::rotate_impl(Params const & params)
 {
        if (!image_)
                return ;
 
-       if (!params.angle)
+       if (float_equal(params.angle, 0.0, 0.1))
                // No rotation is necessary.
                return;
 
@@ -304,16 +318,18 @@ void xformsImage::rotate(Params const & params)
        // Work around xforms bug when params.angle == 270
        // the 'InternalError: bad special angle' error.
        // This bug fix is not needed in xforms 1.0 and greater.
-       if (params.angle == 270) {
+       if (float_equal(params.angle, 270.0, 0.1)) {
                flimage_rotate(image_,  900, FLIMAGE_SUBPIXEL);
                flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
        } else {
-               flimage_rotate(image_, params.angle * 10, FLIMAGE_SUBPIXEL);
+               flimage_rotate(image_,
+                              int(params.angle * 10),
+                              FLIMAGE_SUBPIXEL);
        }
 }
 
 
-void xformsImage::scale(Params const & params)
+void xformsImage::scale_impl(Params const & params)
 {
        if (!image_)
                return;
@@ -357,7 +373,8 @@ void xformsImage::errorCB(string const & error_message)
        finishedLoading(false);
 }
 
-} // namespace grfx
+} // namespace graphics
+} // namespace lyx
 
 
 namespace {
@@ -366,17 +383,17 @@ extern "C" {
 
 int status_report(FL_IMAGE * ob, const char *s)
 {
-       lyx::Assert(ob && ob->u_vdata);
+       BOOST_ASSERT(ob && ob->u_vdata);
 
        string const str = s ? rtrim(s) : string();
        if (str.empty())
                return 0;
 
        lyxerr[Debug::GRAPHICS]
-               << "xforms image loader. Status : " << str << std::endl;
+               << "xforms image loader. Status: " << str << std::endl;
 
-       grfx::xformsImage * ptr =
-               static_cast<grfx::xformsImage *>(ob->u_vdata);
+       lyx::graphics::xformsImage * ptr =
+               static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
        ptr->statusCB(str);
 
        return 0;
@@ -385,17 +402,17 @@ int status_report(FL_IMAGE * ob, const char *s)
 
 static void error_report(FL_IMAGE * ob, const char *s)
 {
-       lyx::Assert(ob && ob->u_vdata);
+       BOOST_ASSERT(ob && ob->u_vdata);
 
        string const str = s ? rtrim(s) : string();
        if (str.empty())
                return;
 
        lyxerr[Debug::GRAPHICS]
-               << "xforms image loader. Error : " << str << std::endl;
+               << "xforms image loader. Error: " << str << std::endl;
 
-       grfx::xformsImage * ptr =
-               static_cast<grfx::xformsImage *>(ob->u_vdata);
+       lyx::graphics::xformsImage * ptr =
+               static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
        ptr->errorCB(str);
 }
 
@@ -413,7 +430,7 @@ void init_graphics()
        flimage_enable_bmp();
        flimage_enable_fits();
        flimage_enable_gif();
-#ifdef HAVE_FLIMAGE_ENABLE_JPEG
+#ifdef USE_JPEG_IMAGE_LOADER
        flimage_enable_jpeg();
 #endif
 
@@ -423,19 +440,20 @@ void init_graphics()
 
        flimage_enable_pnm();
 
-#ifdef HAVE_FLIMAGE_ENABLE_PS
        // xforms recognises PS but not EPS
        // It dies horribly with lots of older PostScript files.
        // Easiest, therefore, to disable PS support and insist that a PS-type
        // file is converted to a bitmap format.
        // flimage_enable_ps();
-#endif
 
        flimage_enable_sgi();
        flimage_enable_tiff();
        flimage_enable_xbm();
        flimage_enable_xwd();
-       flimage_enable_xpm();
+       // xforms can load most XPM files, but will occasionally crash
+       // with some files created by ImakeMagick's convert program.
+       // Turn off xpm support for the time being.
+       // flimage_enable_xpm();
 
        // xforms stores this permanently (does not make a copy) so
        // this should never be destroyed.
@@ -446,23 +464,13 @@ void init_graphics()
 }
 
 
-unsigned int packedcolor(LColor::color c)
+unsigned int packedcolor(LColor::color col)
 {
-       string const x11color = lcolor.getX11Name(c);
-
-       Display * display = fl_get_display();
-       Colormap cmap     = fl_state[fl_get_vclass()].colormap;
-       XColor xcol;
-       XColor ccol;
-       if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
-               // Unable to parse x11color.
-               return FL_PACK(255,255,255);
-
-       // Note that X stores the RGB values in the range 0 - 65535
-       // whilst we require them in the range 0 - 255.
-       unsigned int const r = xcol.red   / 256;
-       unsigned int const g = xcol.green / 256;
-       unsigned int const b = xcol.blue  / 256;
+       unsigned int r, g, b;
+       bool const success = getRGBColor(col, r, g, b);
+       if (!success)
+               // Set to black on failure
+               return FL_PACK(255, 255, 255);
 
        return FL_PACK(r, g, b);
 }