2000-07-31 12:30:10 +00:00
|
|
|
/* This file is part of
|
|
|
|
* =================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
* Copyright 1995 Matthias Ettrich.
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-01-31 14:20:09 +00:00
|
|
|
* \author Baruch Even
|
|
|
|
* \author Herbert Voss <voss@lyx.org>
|
2000-07-31 12:30:10 +00:00
|
|
|
* ================================================= */
|
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2000-08-10 13:15:05 +00:00
|
|
|
#include "graphics/GraphicsCache.h"
|
|
|
|
#include "graphics/GraphicsCacheItem.h"
|
2000-10-12 10:46:06 +00:00
|
|
|
#include "frontends/support/LyXImage.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "graphics/ImageLoaderXPM.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "converter.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lyxlib.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "lyx_gui_misc.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "debug.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "support/LAssert.h"
|
|
|
|
#include "gettext.h"
|
2002-01-31 14:20:09 +00:00
|
|
|
#include "lyxfunc.h"
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
/*
|
|
|
|
* The order of conversion:
|
|
|
|
*
|
|
|
|
* The c-tor calls convertImage()
|
|
|
|
*
|
|
|
|
* convertImage() verifies that we need to do conversion, if not it will just
|
|
|
|
* call the loadImage()
|
|
|
|
* if conversion is needed, it will initiate the conversion.
|
|
|
|
*
|
|
|
|
* When the conversion is completed imageConverted() is called, which in turn
|
|
|
|
* calls loadImage().
|
|
|
|
*
|
|
|
|
* Since we currently do everything synchronously, convertImage() calls
|
|
|
|
* imageConverted() right after it does the call to the conversion process.
|
|
|
|
*/
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
GraphicsCacheItem::GraphicsCacheItem(string const & filename)
|
|
|
|
: imageStatus_(GraphicsCacheItem::Loading)
|
2000-08-10 13:15:05 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
filename_ = filename;
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
bool success = convertImage(filename);
|
2001-07-17 00:38:04 +00:00
|
|
|
if (! success) // Conversion failed miserably (couldn't even start).
|
|
|
|
setStatus(ErrorConverting);
|
2000-08-10 13:15:05 +00:00
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
GraphicsCacheItem::~GraphicsCacheItem()
|
2001-02-22 16:53:59 +00:00
|
|
|
{}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
GraphicsCacheItem::ImageStatus
|
|
|
|
GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
void GraphicsCacheItem::setStatus(ImageStatus new_status)
|
|
|
|
{
|
|
|
|
imageStatus_ = new_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
LyXImage *
|
|
|
|
GraphicsCacheItem::getImage() const { return image_.get(); }
|
2000-08-08 09:18:39 +00:00
|
|
|
|
|
|
|
|
2002-01-31 14:20:09 +00:00
|
|
|
void GraphicsCacheItem::imageConverted(bool success)
|
2001-02-22 16:53:59 +00:00
|
|
|
{
|
2001-07-29 07:29:09 +00:00
|
|
|
// Debug output
|
|
|
|
string text = "succeeded";
|
|
|
|
if (!success)
|
|
|
|
text = "failed";
|
|
|
|
lyxerr << "imageConverted, conversion " << text << "." << endl;
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
if (! success) {
|
2001-02-22 16:53:59 +00:00
|
|
|
lyxerr << "(GraphicsCacheItem::imageConverter) "
|
|
|
|
"Error converting image." << endl;
|
2001-07-17 00:38:04 +00:00
|
|
|
setStatus(GraphicsCacheItem::ErrorConverting);
|
2001-02-22 16:53:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
// Do the actual image loading from file to memory.
|
|
|
|
loadImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2001-04-24 15:25:26 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
string const findTargetFormat(string const & from)
|
|
|
|
{
|
|
|
|
typedef ImageLoader::FormatList FormatList;
|
|
|
|
FormatList formats = ImageLoaderXPM().loadableFormats();
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(formats.size() > 0); // There must be a format to load from.
|
2001-07-23 16:07:29 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
FormatList::const_iterator iter = formats.begin();
|
|
|
|
FormatList::const_iterator end = formats.end();
|
|
|
|
|
|
|
|
for (; iter != end; ++iter) {
|
2001-07-30 11:56:00 +00:00
|
|
|
if (converters.isReachable(from, *iter))
|
2001-04-02 19:56:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (iter == end) {
|
|
|
|
// We do not know how to convert the image to something loadable.
|
|
|
|
lyxerr << "ERROR: Do not know how to convert image." << std::endl;
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
return (*iter);
|
2000-10-12 10:46:06 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
} // anon namespace
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
|
2002-01-31 14:20:09 +00:00
|
|
|
bool GraphicsCacheItem::convertImage(string const & filename)
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2002-01-31 14:20:09 +00:00
|
|
|
setStatus(GraphicsCacheItem::Converting);
|
|
|
|
string filename_ = string(filename);
|
|
|
|
lyxerr << "try to convert image file: " << filename_ << endl;
|
|
|
|
// maybe that other zip extensions also be useful, especially the
|
|
|
|
// ones that may be declared in texmf/tex/latex/config/graphics.cfg.
|
|
|
|
// for example:
|
|
|
|
/* -----------snip-------------
|
|
|
|
{\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
|
|
|
|
-----------snip-------------*/
|
|
|
|
|
|
|
|
lyxerr << "GetExtension: " << GetExtension(filename_) << endl;
|
2002-02-07 19:37:34 +00:00
|
|
|
bool const zipped = zippedFile(filename_);
|
2002-01-31 14:20:09 +00:00
|
|
|
if (zipped)
|
2002-02-07 19:37:34 +00:00
|
|
|
filename_ = unzipFile(filename_);
|
2002-01-31 14:20:09 +00:00
|
|
|
string const from = getExtFromContents(filename_); // get the type
|
|
|
|
lyxerr << "GetExtFromContents: " << from << endl;
|
2001-04-02 19:56:48 +00:00
|
|
|
string const to = findTargetFormat(from);
|
2002-01-31 14:20:09 +00:00
|
|
|
lyxerr << "from: " << from << " -> " << to << endl;
|
2001-04-02 19:56:48 +00:00
|
|
|
if (to.empty())
|
|
|
|
return false;
|
2002-01-31 14:20:09 +00:00
|
|
|
// manage zipped files. unzip them first into the tempdir
|
2001-09-20 13:16:17 +00:00
|
|
|
if (from == to) {
|
|
|
|
// No conversion needed!
|
|
|
|
// Saves more than just time: prevents the deletion of
|
|
|
|
// the "to" file after loading when it's the same as the "from"!
|
2002-01-31 14:20:09 +00:00
|
|
|
tempfile = filename_;
|
2001-09-20 13:16:17 +00:00
|
|
|
loadImage();
|
|
|
|
return true;
|
|
|
|
}
|
2001-02-22 16:53:59 +00:00
|
|
|
// Take only the filename part of the file, without path or extension.
|
2002-01-31 14:20:09 +00:00
|
|
|
string temp = OnlyFilename(filename_);
|
|
|
|
temp = ChangeExtension(filename_, string());
|
2001-02-22 16:53:59 +00:00
|
|
|
|
|
|
|
// Add some stuff to have it a unique temp file.
|
2001-04-02 19:56:48 +00:00
|
|
|
// This tempfile is deleted in loadImage after it is loaded to memory.
|
2001-02-22 16:53:59 +00:00
|
|
|
tempfile = lyx::tempName(string(), temp);
|
|
|
|
// Remove the temp file, we only want the name...
|
|
|
|
lyx::unlink(tempfile);
|
2002-01-31 14:20:09 +00:00
|
|
|
bool result = converters.convert(0, filename_, tempfile, from, to);
|
2001-07-23 16:07:29 +00:00
|
|
|
tempfile.append(".xpm");
|
2001-07-17 00:38:04 +00:00
|
|
|
// For now we are synchronous
|
2001-07-23 16:07:29 +00:00
|
|
|
imageConverted(result);
|
2001-07-17 00:38:04 +00:00
|
|
|
// Cleanup after the conversion.
|
|
|
|
lyx::unlink(tempfile);
|
2002-01-31 14:20:09 +00:00
|
|
|
if (zipped)
|
|
|
|
lyx::unlink(filename_);
|
2001-07-17 00:38:04 +00:00
|
|
|
tempfile = string();
|
2001-02-22 16:53:59 +00:00
|
|
|
return true;
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// This function gets called from the callback after the image has been
|
|
|
|
// converted successfully.
|
2000-08-08 09:18:39 +00:00
|
|
|
void
|
2001-04-02 19:56:48 +00:00
|
|
|
GraphicsCacheItem::loadImage()
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
lyxerr << "Loading XPM Image... ";
|
|
|
|
|
|
|
|
ImageLoaderXPM imageLoader;
|
|
|
|
if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
|
|
|
|
lyxerr << "Success." << endl;
|
|
|
|
image_.reset(imageLoader.getImage());
|
2001-07-17 00:38:04 +00:00
|
|
|
setStatus(GraphicsCacheItem::Loaded);
|
2001-02-22 16:53:59 +00:00
|
|
|
} else {
|
2001-03-16 12:08:14 +00:00
|
|
|
lyxerr << "Loading " << tempfile << "Failed" << endl;
|
2001-07-17 00:38:04 +00:00
|
|
|
setStatus(GraphicsCacheItem::ErrorReading);
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
2001-02-22 16:53:59 +00:00
|
|
|
}
|