From c39bf82c2fad694cd12222ce26aa19ab64397c30 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrgen=20Vigna?= Date: Tue, 8 Aug 2000 10:15:18 +0000 Subject: [PATCH] Forgot to add this files. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@958 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/graphics/Renderer.C | 84 ++++++++++++++++++++++++++++++++++ src/graphics/Renderer.h | 78 ++++++++++++++++++++++++++++++++ src/graphics/XPM_Renderer.C | 89 +++++++++++++++++++++++++++++++++++++ src/graphics/XPM_Renderer.h | 36 +++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 src/graphics/Renderer.C create mode 100644 src/graphics/Renderer.h create mode 100644 src/graphics/XPM_Renderer.C create mode 100644 src/graphics/XPM_Renderer.h diff --git a/src/graphics/Renderer.C b/src/graphics/Renderer.C new file mode 100644 index 0000000000..405f5a1d15 --- /dev/null +++ b/src/graphics/Renderer.C @@ -0,0 +1,84 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include +#include "Renderer.h" + +#include FORMS_H_LOCATION +#include "support/filetools.h" + + +Renderer::Renderer() + : width_(0), height_(0), pixmapLoaded_(false) +{} + +Renderer::~Renderer() +{ + freePixmap(); +} + +bool Renderer::setFilename(string const & filename) +{ + // Make sure file exists and is readable. + if (! IsFileReadable(filename)) { + return false; + } + + // Verify that the file format is correct. + if (! isImageFormatOK(filename)) { + return false; + } + + filename_ = filename; + return true; +} + +bool Renderer::renderImage() +{ + return false; +} + +bool Renderer::isImageFormatOK(string const & /*filename*/) const +{ + return false; +} + +void Renderer::setPixmap(Pixmap pixmap, unsigned int width, unsigned int height) +{ + freePixmap(); + + pixmap_ = pixmap; + width_ = width; + height_ = height; + pixmapLoaded_ = true; +} + +Pixmap Renderer::getPixmap() const +{ return pixmap_; } + +unsigned int Renderer::getWidth() const +{ return width_; } + +unsigned int Renderer::getHeight() const +{ return height_; } + +string const & Renderer::getFilename() const +{ return filename_; } + +void Renderer::freePixmap() +{ + if (pixmapLoaded_) + XFreePixmap(fl_display, pixmap_); +} diff --git a/src/graphics/Renderer.h b/src/graphics/Renderer.h new file mode 100644 index 0000000000..ee2101b156 --- /dev/null +++ b/src/graphics/Renderer.h @@ -0,0 +1,78 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#ifndef RENDERER_H +#define RENDERER_H + +#include + +#ifdef __GNUG__ +#pragma interface +#endif + +#include "LString.h" +#include "X11/Xlib.h" +#include "support/utility.hpp" + +/** Renderer is a base class that is used to take an image format, and render + * it into a Pixmap in order to be able to display it later on in LyX. + * Essentially it's job is to load an image format and create a Pixmap from it. + * It also needs to do various transforms on the image, like Rotation, + * Resize and color reduction. + */ +class Renderer : public noncopyable { +public: + /// c-tor. + Renderer(); + /// d-tor. + virtual ~Renderer(); + + /// Set the filename that we will render + bool setFilename(string const & filename); + + /// Render the image, doing the necessary transforms. + virtual bool renderImage() = 0; + + /// Get the last rendered pixmap. + Pixmap getPixmap() const; + + /// Get the width of the pixmap. + unsigned int getWidth() const; + + /// Get the height of the pixmap. + unsigned int getHeight() const; + +protected: + /// Verify that the file is one that we can handle. + virtual bool isImageFormatOK(string const & filename) const = 0; + + /// Set the pixmap. + void setPixmap(Pixmap pixmap, unsigned int width, unsigned int height); + + string const & getFilename() const; + +private: + /// Free the loaded pixmap + void freePixmap(); + + /// The filename of the image file that we are responsible for. + string filename_; + /// The last rendered pixmap. + Pixmap pixmap_; + /// The width of the rendered pixmap. + unsigned int width_; + /// The height of the rendered pixmap. + unsigned int height_; + /// is Pixmap ready? + bool pixmapLoaded_; +}; + +#endif diff --git a/src/graphics/XPM_Renderer.C b/src/graphics/XPM_Renderer.C new file mode 100644 index 0000000000..1f3f9729a8 --- /dev/null +++ b/src/graphics/XPM_Renderer.C @@ -0,0 +1,89 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include +#include "XPM_Renderer.h" + +#include FORMS_H_LOCATION +#include XPM_H_LOCATION +#include +#include + +#include "support/LAssert.h" +#include "debug.h" + +XPM_Renderer::XPM_Renderer() + : Renderer() +{} + +XPM_Renderer::~XPM_Renderer() +{} + +bool XPM_Renderer::renderImage() +{ + Pixmap pixmap; + Pixmap mask; + XpmAttributes attrib; + attrib.valuemask = 0; + + Display * display = fl_get_display(); + +//(BE 2000-08-05) +#warning This might be a dirty thing, but I dont know any other solution. + Screen * screen = DefaultScreenOfDisplay(display); + + int status = XpmReadFileToPixmap( + display, + XRootWindowOfScreen(screen), + const_cast(getFilename().c_str()), + &pixmap, &mask, &attrib); + + if (status != XpmSuccess) { + lyxerr << "Error reading XPM file '" + << XpmGetErrorString(status) + << endl; + return false; + } + + // This should have been set by the XpmReadFileToPixmap call! + Assert(attrib.valuemask & XpmSize); + + setPixmap(pixmap, attrib.width, attrib.height); + + XpmFreeAttributes(&attrib); + + return true; +} + +bool XPM_Renderer::isImageFormatOK(string const & filename) const +{ + std::ifstream is(filename.c_str(), ios::in | ios::nocreate); + + // The signature of the file without the spaces. + static const char str[] = "/*XPM*/"; + const char * ptr = str; + + do { + char c; + is >> c; + + if (c != *ptr) + return false; + + ++ptr; + } while (*ptr != '\0'); + + return true; +} diff --git a/src/graphics/XPM_Renderer.h b/src/graphics/XPM_Renderer.h new file mode 100644 index 0000000000..2b294267fc --- /dev/null +++ b/src/graphics/XPM_Renderer.h @@ -0,0 +1,36 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#ifndef XPM_RENDERER_H +#define XPM_RENDERER_H + +#ifdef __GNUG__ +#pragma interface +#endif + +#include "graphics/Renderer.h" + +class XPM_Renderer : public Renderer { +public: + /// c-tor. + XPM_Renderer(); + /// d-tor. + virtual ~XPM_Renderer(); + + /// Load the XPM image and create a pixmap out of it. + virtual bool renderImage(); + +private: + /// Verify that filename is really an XPM file. + virtual bool isImageFormatOK(string const & filename) const; +}; + +#endif -- 2.39.2