2 * \file GraphicsCacheItem.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Baruch Even <baruch.even@writeme.com>
7 * \author Herbert Voss <voss@lyx.org>
8 * \author Angus Leeming <a.leeming@ic.ac.uk>
14 #pragma implementation
17 #include "graphics/GraphicsCache.h"
18 #include "graphics/GraphicsCacheItem.h"
19 #include "graphics/GraphicsImage.h"
20 #include "graphics/GraphicsParams.h"
21 #include "graphics/GraphicsConverter.h"
23 #include "insets/insetgraphics.h"
25 #include "BufferView.h"
28 #include "lyx_main.h" // for global dispatch method
30 #include "support/LAssert.h"
31 #include "support/filetools.h"
33 #include <boost/bind.hpp>
36 extern BufferView * current_view;
43 GCacheItem::GCacheItem(InsetGraphics const & inset, GParams const & params)
44 : filename_(params.filename), zipped_(false),
45 remove_loaded_file_(false), status_(WaitingToLoad)
47 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
48 modified_images.push_back(item);
54 typedef GCacheItem::ModifiedItemPtr ModifiedItemPtr;
56 class Compare_Params {
58 Compare_Params(GParams const & p) : p_(p) {}
60 bool operator()(ModifiedItemPtr const & ptr)
64 return ptr->params() == p_;
73 Find_Inset(InsetGraphics const & i) : i_(i) {}
75 bool operator()(ModifiedItemPtr const & ptr)
79 return ptr->referencedBy(i_);
83 InsetGraphics const & i_;
89 void GCacheItem::modify(InsetGraphics const & inset, GParams const & params)
91 // Does this inset currently reference an existing ModifiedItem with
93 // If so, remove the inset from the ModifiedItem's internal list
95 ListType::iterator begin = modified_images.begin();
96 ListType::iterator end = modified_images.end();
97 ListType::iterator it = begin;
99 it = std::find_if(it, end, Find_Inset(inset));
102 if ((*it)->params() != params) {
103 (*it)->remove(inset);
105 it = modified_images.erase(it);
110 // Is there an existing ModifiedItem with these params?
111 // If so, add inset to the list of insets referencing this ModifiedItem
112 begin = modified_images.begin();
113 end = modified_images.end();
114 it = std::find_if(begin, end, Compare_Params(params));
120 // If no ModifiedItem exists with these params, then create one.
121 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
122 modified_images.push_back(item);
128 void GCacheItem::remove(InsetGraphics const & inset)
130 // search the list of ModifiedItems for one referenced by this inset.
131 // If it is found, remove the reference.
132 // If the ModifiedItem is now referenced by no insets, remove it.
133 ListType::iterator begin = modified_images.begin();
134 ListType::iterator end = modified_images.end();
135 ListType::iterator it = std::find_if(begin, end, Find_Inset(inset));
140 (*it)->remove(inset);
141 if ((*it)->empty()) {
142 modified_images.clear();
147 void GCacheItem::startLoading(InsetGraphics const & inset)
149 if (status() != WaitingToLoad)
152 // Check that the image is referenced by this inset
153 ListType::const_iterator begin = modified_images.begin();
154 ListType::const_iterator end = modified_images.end();
155 ListType::const_iterator it =
156 std::find_if(begin, end, Find_Inset(inset));
161 if ((*it)->params().display == GParams::NONE)
164 convertToDisplayFormat();
168 bool GCacheItem::empty() const
170 return modified_images.empty();
174 bool GCacheItem::referencedBy(InsetGraphics const & inset) const
176 // Is one of the list of ModifiedItems referenced by this inset?
177 ListType::const_iterator begin = modified_images.begin();
178 ListType::const_iterator end = modified_images.end();
179 return std::find_if(begin, end, Find_Inset(inset)) != end;
183 string const & GCacheItem::filename() const
189 ImagePtr const GCacheItem::image(InsetGraphics const & inset) const
191 // find a ModifiedItem that is referenced by this inset.
192 ListType::const_iterator begin = modified_images.begin();
193 ListType::const_iterator end = modified_images.end();
194 ListType::const_iterator it =
195 std::find_if(begin, end, Find_Inset(inset));
197 // Someone's being daft.
201 // We are expressly requested to not render the image
202 if ((*it)->params().display == GParams::NONE)
205 // If the original image has been loaded, return what's going on
206 // in the ModifiedItem
207 if (status() == Loaded)
208 return (*it)->image();
214 ImageStatus GCacheItem::status(InsetGraphics const & inset) const
216 // find a ModifiedItem that is referenced by this inset.
217 ListType::const_iterator begin = modified_images.begin();
218 ListType::const_iterator end = modified_images.end();
219 ListType::const_iterator it =
220 std::find_if(begin, end, Find_Inset(inset));
222 // Someone's being daft.
226 if (status() == Loaded)
227 return (*it)->status();
233 // Called internally only. Use to ascertain the status of the loading of the
234 // original image. No scaling etc.
235 ImageStatus GCacheItem::status() const
241 void GCacheItem::setStatus(ImageStatus new_status)
243 status_ = new_status;
245 // Loop over all insets and tell the BufferView that it has changed.
246 typedef ModifiedItem::ListType::const_iterator inset_iterator;
248 ListType::const_iterator it = modified_images.begin();
249 ListType::const_iterator end = modified_images.end();
250 for (; it != end; ++it) {
251 inset_iterator it2 = (*it)->insets.begin();
252 inset_iterator end2 = (*it)->insets.end();
254 for (; it2 != end2; ++it2) {
255 InsetGraphics * inset =
256 const_cast<InsetGraphics *>(*it2);
258 // Use of current_view is very, very Evil!!
259 current_view->updateInset(inset, false);
265 void GCacheItem::changeDisplay(bool changed_background)
267 ListType::iterator begin = modified_images.begin();
268 ListType::iterator end = modified_images.end();
270 // The background has changed. Change all modified images.
271 if (changed_background) {
272 for (ListType::iterator it = begin; it != end; ++it) {
280 for (ListType::iterator it = begin; it != end; ++it) {
281 // ModifiedItem::changeDisplay returns a full
282 // ModifiedItemPtr if any of the insets have display=DEFAULT
283 // and if that DEFAULT value has changed
284 ModifiedItemPtr new_item = (*it)->changeDisplay();
288 temp_list.push_back(new_item);
290 // The original store may now be empty
291 if ((*it)->insets.empty()) {
292 it = modified_images.erase(it);
296 if (temp_list.empty())
299 // Recombine new_list and modified_images.
300 begin = modified_images.begin();
301 end = modified_images.end();
303 ListType::const_iterator tbegin = temp_list.begin();
304 ListType::const_iterator tend = temp_list.end();
306 ListType append_list;
308 for (ListType::const_iterator tit = tbegin; tit != tend; ++tit) {
309 GParams const & params = (*tit)->params();
310 ListType::iterator it =
311 std::find_if(begin, end, Compare_Params(params));
313 append_list.push_back(*tit);
315 (*it)->insets.merge((*tit)->insets);
318 if (append_list.empty())
321 modified_images.splice(modified_images.end(), append_list);
325 void GCacheItem::imageConverted(string const & file_to_load)
328 (!file_to_load.empty() && IsFileReadable(file_to_load));
330 string const text = success ? "succeeded" : "failed";
331 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
334 setStatus(ErrorConverting);
337 lyx::unlink(unzipped_filename_);
344 // Do the actual image loading from file to memory.
345 file_to_load_ = file_to_load;
351 // This function gets called from the callback after the image has been
352 // converted successfully.
353 void GCacheItem::loadImage()
356 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
358 // Connect a signal to this->imageLoaded and pass this signal to
359 // GImage::loadImage.
360 SignalLoadTypePtr on_finish;
361 on_finish.reset(new SignalLoadType);
362 cl_ = on_finish->connect(boost::bind(&GCacheItem::imageLoaded, this, _1));
364 image_ = GImage::newImage();
365 image_->load(file_to_load_, on_finish);
369 void GCacheItem::imageLoaded(bool success)
371 string const text = success ? "succeeded" : "failed";
372 lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
374 // Clean up after loading.
376 lyx::unlink(unzipped_filename_);
378 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
379 lyx::unlink(file_to_load_);
384 setStatus(ErrorLoading);
390 // Loop over the list of modified images and create them.
391 ListType::iterator it = modified_images.begin();
392 ListType::iterator end = modified_images.end();
393 for (; it != end; ++it) {
394 (*it)->modify(image_);
399 unsigned int GCacheItem::raw_width() const
404 return image_->getWidth();
408 unsigned int GCacheItem::raw_height() const
413 return image_->getHeight();
419 string const findTargetFormat(string const & from)
421 typedef GImage::FormatList FormatList;
422 FormatList const formats = GImage::loadableFormats();
424 // There must be a format to load from.
425 lyx::Assert(!formats.empty());
427 // First ascertain if we can load directly with no conversion
428 FormatList::const_iterator it1 = formats.begin();
429 FormatList::const_iterator end = formats.end();
430 for (; it1 != end; ++it1) {
435 // So, we have to convert to a loadable format. Can we?
436 grfx::GConverter const & graphics_converter = grfx::GConverter::get();
438 FormatList::const_iterator it2 = formats.begin();
439 for (; it2 != end; ++it2) {
440 if (graphics_converter.isReachable(from, *it2))
451 void GCacheItem::convertToDisplayFormat()
453 setStatus(Converting);
454 string filename = filename_; // Make a local copy in case we unzip it
455 string const displayed_filename = MakeDisplayPath(filename_);
457 // First, check that the file exists!
458 if (!IsFileReadable(filename)) {
459 setStatus(ErrorNoFile);
463 // maybe that other zip extensions also be useful, especially the
464 // ones that may be declared in texmf/tex/latex/config/graphics.cfg.
466 /* -----------snip-------------
467 {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
468 \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
469 \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
470 \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
471 \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
472 -----------snip-------------*/
474 lyxerr[Debug::GRAPHICS]
475 << "Attempting to convert image file: " << displayed_filename
476 << "\nwith recognised extension: " << GetExtension(filename)
479 zipped_ = zippedFile(filename);
481 filename = unzipFile(filename);
482 unzipped_filename_ = filename;
485 string const from = getExtFromContents(filename);
486 string const to = grfx::findTargetFormat(from);
488 lyxerr[Debug::GRAPHICS]
489 << "The file contains " << from << " format data." << endl;
492 setStatus(ErrorConverting);
497 // No conversion needed!
498 lyxerr[Debug::GRAPHICS] << "No conversion needed!" << endl;
499 file_to_load_ = filename;
504 lyxerr[Debug::GRAPHICS] << "Converting it to " << to << " format." << endl;
506 // Take only the filename part of the file, without path or extension.
507 string const temp = ChangeExtension(OnlyFilename(filename), string());
509 // Add some stuff to create a uniquely named temporary file.
510 // This file is deleted in loadImage after it is loaded into memory.
511 string const to_file_base = lyx::tempName(string(), temp);
512 remove_loaded_file_ = true;
514 // Remove the temp file, we only want the name...
515 lyx::unlink(to_file_base);
517 // Connect a signal to this->imageConverted and pass this signal to
518 // the graphics converter so that we can load the modified file
519 // on completion of the conversion process.
520 SignalConvertTypePtr on_finish;
521 on_finish.reset(new SignalConvertType);
522 cc_ = on_finish->connect(boost::bind(&GCacheItem::imageConverted, this, _1));
524 GConverter & graphics_converter = GConverter::get();
525 graphics_converter.convert(filename, to_file_base, from, to, on_finish);
529 ModifiedItem::ModifiedItem(InsetGraphics const & new_inset,
530 GParams const & new_params,
531 ImagePtr const & new_image)
532 : status_(ScalingEtc)
534 p_.reset(new GParams(new_params));
535 insets.push_back(&new_inset);
540 void ModifiedItem::add(InsetGraphics const & inset)
542 insets.push_back(&inset);
547 void ModifiedItem::remove(InsetGraphics const & inset)
549 ListType::iterator begin = insets.begin();
550 ListType::iterator end = insets.end();
551 ListType::iterator it = std::remove(begin, end, &inset);
552 insets.erase(it, end);
556 bool ModifiedItem::referencedBy(InsetGraphics const & inset) const
558 ListType::const_iterator begin = insets.begin();
559 ListType::const_iterator end = insets.end();
560 return std::find(begin, end, &inset) != end;
564 ImagePtr const ModifiedItem::image() const
566 if (modified_image_.get())
567 return modified_image_;
569 return original_image_;
573 void ModifiedItem::modify(ImagePtr const & new_image)
575 if (!new_image.get())
578 original_image_ = new_image;
579 modified_image_.reset(original_image_->clone());
581 if (params().display == GParams::NONE) {
586 setStatus(ScalingEtc);
587 modified_image_->clip(params());
588 modified_image_->rotate(params());
589 modified_image_->scale(params());
594 void ModifiedItem::setPixmap()
596 if (!modified_image_.get())
599 if (params().display == GParams::NONE) {
604 bool const success = modified_image_->setPixmap(params());
609 modified_image_.reset();
610 setStatus(ErrorScalingEtc);
615 void ModifiedItem::setStatus(ImageStatus new_status)
617 status_ = new_status;
619 // Tell the BufferView that the inset has changed.
621 ListType::const_iterator it = insets.begin();
622 ListType::const_iterator end = insets.end();
623 for (; it != end; ++it) {
624 InsetGraphics * inset = const_cast<InsetGraphics *>(*it);
625 current_view->updateInset(inset, false);
632 struct Params_Changed {
634 Params_Changed(GParams const & p) : p_(p) {}
636 bool operator()(InsetGraphics const * inset)
638 string const path = OnlyPath(p_.filename);
639 return inset->params().asGParams(path) != p_;
648 // changeDisplay returns an initialised ModifiedItem if any of the insets
649 // have display == DEFAULT and if that DEFAULT value has changed.
650 // If this occurs, then (this) has these insets removed.
651 ModifiedItemPtr ModifiedItem::changeDisplay()
653 // Loop over the list of insets. Compare the updated params for each
654 // with params(). If different, move into a new list.
655 ListType::iterator begin = insets.begin();
656 ListType::iterator end = insets.end();
657 ListType::iterator it =
658 std::remove_if(begin, end, Params_Changed(params()));
661 // No insets have changed params
662 return ModifiedItemPtr();
665 // it -> end have params that are changed. Move to the new list.
667 new_insets.insert(new_insets.begin(), it, end);
668 insets.erase(it, end);
670 // Create a new ModifiedItem with these new params. Note that
671 // the only params that have changed are the display ones,
672 // so we don't need to crop, rotate, scale.
673 string const path = OnlyPath(p_->filename);
675 ModifiedItemPtr new_item(new ModifiedItem(*this));
676 new_item->insets = new_insets;
677 *(new_item->p_) = (*new_insets.begin())->params().asGParams(path);
679 new_item->setPixmap();