Attached is a patch and four additional files used to load images for
inline viewing, this patch has no effective feature changes (besides a
minor resource leak in insetfig.C) but in it I refactored the Pixmap
rendering out of GraphicsCacheItem to its own class, thus easing the next
step of supporting EPS rendering using ghostscript.

Please apply this.

-- 
  Baruch Even

http://techst02.technion.ac.il/~sbaruch/   (My Site)
http://rpghost.com/jindor/                 (My brothers AD&D site)

" Learn to laugh ... it's the path to true love! " 
   - The Angel in the movie Michael

patch.gz

// -*- 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 <config.h>

#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
// -*- 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 <config.h>
#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_);
}
// -*- 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
// -*- 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 <config.h>
#include "XPM_Renderer.h"

#include FORMS_H_LOCATION
#include XPM_H_LOCATION
#include <iostream>
#include <fstream>

#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<char *>(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;
}

Reply via email to