1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2002 the LyX Team.
9 * \author Herbert Voss <voss@lyx.org>
10 * ====================================================== */
15 * If the image is from the clipart, and the document is moved to another
16 directory, the user is screwed. Need a way to handle it.
17 This amounts to a problem of when to use relative or absolute file paths
18 We should probably use what the user asks to use... but when he chooses
19 by the file dialog we normally get an absolute path and this may not be
22 Note that browseRelFile in helper_funcs.* provides a file name
23 which is relative if it is at reference path (here puffer path)
24 level or below, and an absolute path if the file name is not a
25 `natural' relative file name. In any case,
26 MakeAbsPath(filename, buf->filePath())
27 is guaranteed to provide the correct absolute path. This is what is
28 done know for include insets. Feel free to ask me -- JMarc
31 TODO Before initial production release:
33 * What advanced features the users want to do?
34 Implement them in a non latex dependent way, but a logical way.
35 LyX should translate it to latex or any other fitting format.
36 * Add a way to roll the image file into the file format.
37 * When loading, if the image is not found in the expected place, try
38 to find it in the clipart, or in the same directory with the image.
39 * Keep a tab on the image file, if it changes, update the lyx view.
40 * The image choosing dialog could show thumbnails of the image formats
41 it knows of, thus selection based on the image instead of based on
43 * Add support for the 'picins' package.
44 * Add support for the 'picinpar' package.
45 * Improve support for 'subfigure' - Allow to set the various options
51 * Current version is 1 (inset file format version), when changing it
52 * it should be changed in the Write() function when writing in one place
53 * and when reading one should change the version check and the error message.
54 * The filename is kept in the lyx file in a relative way, so as to allow
55 * moving the document file and its images with no problem.
59 * Postscript output means EPS figures.
61 * PDF output is best done with PDF figures if it's a direct conversion
62 * or PNG figures otherwise.
75 #pragma implementation
78 #include "insets/insetgraphics.h"
79 #include "insets/insetgraphicsParams.h"
81 #include "graphics/GraphicsCache.h"
82 #include "graphics/GraphicsCacheItem.h"
83 #include "graphics/GraphicsImage.h"
85 #include "frontends/LyXView.h"
88 #include "BufferView.h"
89 #include "converter.h"
90 #include "frontends/Painter.h"
92 #include "frontends/font_metrics.h"
95 #include "LaTeXFeatures.h"
97 #include "frontends/Dialogs.h"
98 #include "frontends/Alert.h"
99 #include "frontends/controllers/helper_funcs.h" // getVectorFromString
101 #include "support/LAssert.h"
102 #include "support/filetools.h"
103 #include "support/lyxalgo.h" // lyx::count
104 #include "support/path.h"
105 #include "support/systemcall.h"
106 #include "support/os.h"
108 #include <boost/bind.hpp>
110 #include <algorithm> // For the std::max
113 extern BufferView * current_view;
115 extern string system_tempdir;
120 ///////////////////////////////////////////////////////////////////////////
121 int const VersionNumber = 1;
122 ///////////////////////////////////////////////////////////////////////////
126 // This function is a utility function
127 // ... that should be with ChangeExtension ...
129 string const RemoveExtension(string const & filename)
131 return ChangeExtension(filename, string());
139 string const uniqueID()
141 static unsigned int seed = 1000;
144 ost << "graph" << ++seed;
146 // Needed if we use lyxstring.
147 return ost.str().c_str();
153 struct InsetGraphics::Cache
156 Cache(InsetGraphics &);
160 void reset(grfx::GraphicPtr const &);
162 bool empty() const { return !graphic_.get(); }
164 grfx::ImageStatus status() const;
166 void setStatus(grfx::ImageStatus);
168 grfx::GImage * image() const;
170 string const filename() const;
172 void update(string const & file_with_path);
179 grfx::GraphicPtr graphic_;
182 /// The connection to cache_->statusChanged.
183 boost::signals::connection cc_;
185 grfx::ImageStatus status_;
187 grfx::GParams params_;
189 grfx::ImagePtr modified_image_;
191 InsetGraphics & parent_;
195 InsetGraphics::Cache::Cache(InsetGraphics & p)
196 : old_ascent(0), status_(grfx::ErrorUnknown), parent_(p)
200 InsetGraphics::Cache::~Cache()
202 string const old_file = filename();
205 if (!old_file.empty()) {
206 grfx::GCache & gc = grfx::GCache::get();
212 void InsetGraphics::Cache::reset(grfx::GraphicPtr const & graphic)
214 string const old_file = filename();
215 string const new_file = graphic.get() ? graphic->filename() : string();
216 if (old_file == new_file)
221 if (!old_file.empty()) {
222 grfx::GCache & gc = grfx::GCache::get();
228 grfx::ImageStatus InsetGraphics::Cache::status() const
234 void InsetGraphics::Cache::setStatus(grfx::ImageStatus new_status)
236 status_ = new_status;
240 grfx::GImage * InsetGraphics::Cache::image() const
242 return modified_image_.get();
246 string const InsetGraphics::Cache::filename() const
248 return empty() ? string() : graphic_->filename();
252 void InsetGraphics::Cache::update(string const & file_with_path)
254 lyx::Assert(!file_with_path.empty());
256 // Check whether the file has changed.
257 string current_file = filename();
259 if (current_file == file_with_path) {
265 // Remove the connection to any previous grfx::CacheItems
266 grfx::GCache & gc = grfx::GCache::get();
267 if (!current_file.empty() && gc.inCache(current_file)) {
269 gc.remove(current_file);
273 // Update the cache to point to the new file
274 if (!gc.inCache(file_with_path))
275 gc.add(file_with_path);
277 graphic_ = gc.graphic(file_with_path);
278 cc_ = graphic_->statusChanged.connect(
279 boost::bind(&InsetGraphics::statusChanged, &parent_));
281 setStatus(graphic_->status());
282 if (status() == grfx::Loaded)
287 void InsetGraphics::Cache::modify()
289 // The image has not been loaded from file
290 if (!graphic_->image().get())
293 string const path = OnlyPath(filename());
294 grfx::GParams params = parent_.params().asGParams(path);
296 if (params == params_)
300 setStatus(grfx::ScalingEtc);
301 modified_image_.reset(graphic_->image()->clone());
302 modified_image_->clip(params);
303 modified_image_->rotate(params);
304 modified_image_->scale(params);
306 bool const success = modified_image_->setPixmap(params);
309 setStatus(grfx::Loaded);
311 modified_image_.reset();
312 setStatus(grfx::ErrorScalingEtc);
317 InsetGraphics::InsetGraphics()
318 : graphic_label(uniqueID()),
319 cache_(new Cache(*this))
323 InsetGraphics::InsetGraphics(InsetGraphics const & ig,
324 string const & filepath,
326 : Inset(ig, same_id),
327 graphic_label(uniqueID()),
328 cache_(new Cache(*this))
330 setParams(ig.params(), filepath);
334 InsetGraphics::~InsetGraphics()
336 cache_->reset(grfx::GraphicPtr());
337 // Emits the hide signal to the dialog connected (if any)
342 string const InsetGraphics::statusMessage() const
346 switch (cache_->status()) {
347 case grfx::WaitingToLoad:
348 msg = _("Waiting for draw request to start loading...");
351 msg = _("Loading...");
353 case grfx::Converting:
354 msg = _("Converting to loadable format...");
356 case grfx::ScalingEtc:
357 msg = _("Scaling etc...");
362 case grfx::ErrorNoFile:
363 msg = _("No file found!");
365 case grfx::ErrorLoading:
366 msg = _("Error loading file into memory");
368 case grfx::ErrorConverting:
369 msg = _("Error converting to loadable format");
371 case grfx::ErrorScalingEtc:
372 msg = _("Error scaling etc");
374 case grfx::ErrorUnknown:
383 bool InsetGraphics::imageIsDrawable() const
385 if (!cache_->image() || cache_->status() != grfx::Loaded)
388 return cache_->image()->getPixmap() != 0;
392 int InsetGraphics::ascent(BufferView *, LyXFont const &) const
394 cache_->old_ascent = 50;
395 if (imageIsDrawable())
396 cache_->old_ascent = cache_->image()->getHeight();
397 return cache_->old_ascent;
401 int InsetGraphics::descent(BufferView *, LyXFont const &) const
407 int InsetGraphics::width(BufferView *, LyXFont const & font) const
409 if (imageIsDrawable())
410 return cache_->image()->getWidth();
414 LyXFont msgFont(font);
415 msgFont.setFamily(LyXFont::SANS_FAMILY);
417 string const justname = OnlyFilename (params().filename);
418 if (!justname.empty()) {
419 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
420 font_width = font_metrics::width(justname, msgFont);
423 string const msg = statusMessage();
425 msgFont.setSize(LyXFont::SIZE_TINY);
426 int const msg_width = font_metrics::width(msg, msgFont);
427 font_width = std::max(font_width, msg_width);
430 return std::max(50, font_width + 15);
435 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
436 int baseline, float & x, bool) const
438 int oasc = cache_->old_ascent;
440 int ldescent = descent(bv, font);
441 int lascent = ascent(bv, font);
442 int lwidth = width(bv, font);
444 // we may have changed while someone other was drawing us so better
445 // to not draw anything as we surely call to redraw ourself soon.
446 // This is not a nice thing to do and should be fixed properly somehow.
447 // But I still don't know the best way to go. So let's do this like this
448 // for now (Jug 20020311)
449 if (lascent != oasc) {
453 // Make sure now that x is updated upon exit from this routine
457 if (cache_->status() == grfx::WaitingToLoad) {
458 cache_->graphic_->startLoading();
461 // This will draw the graphics. If the graphics has not been loaded yet,
462 // we draw just a rectangle.
463 Painter & paint = bv->painter();
465 if (imageIsDrawable()) {
466 paint.image(old_x + 2, baseline - lascent,
467 lwidth - 4, lascent + ldescent,
472 paint.rectangle(old_x + 2, baseline - lascent,
476 // Print the file name.
477 LyXFont msgFont(font);
478 msgFont.setFamily(LyXFont::SANS_FAMILY);
479 string const justname = OnlyFilename (params().filename);
480 if (!justname.empty()) {
481 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
482 paint.text(old_x + 8,
483 baseline - font_metrics::maxAscent(msgFont) - 4,
487 // Print the message.
488 string const msg = statusMessage();
490 msgFont.setSize(LyXFont::SIZE_TINY);
491 paint.text(old_x + 8, baseline - 4, msg, msgFont);
495 // the status message may mean we changed size, so indicate
496 // we need a row redraw
498 if (old_status_ != grfx::ErrorUnknown && old_status_ != cached_status_) {
499 bv->getLyXText()->status(bv, LyXText::CHANGED_IN_DRAW);
503 // Reset the cache, ready for the next draw request
505 cached_status_ = grfx::ErrorUnknown;
506 cached_image_.reset();
507 cache_filled_ = false;
512 void InsetGraphics::edit(BufferView *bv, int, int, mouse_button::state)
514 bv->owner()->getDialogs()->showGraphics(this);
518 void InsetGraphics::edit(BufferView * bv, bool)
520 edit(bv, 0, 0, mouse_button::none);
524 Inset::EDITABLE InsetGraphics::editable() const
530 void InsetGraphics::write(Buffer const *, ostream & os) const
532 os << "Graphics FormatVersion " << VersionNumber << '\n';
537 void InsetGraphics::read(Buffer const * buf, LyXLex & lex)
539 string const token = lex.getString();
541 if (token == "Graphics")
542 readInsetGraphics(lex);
543 else if (token == "Figure") // Compatibility reading of FigInset figures.
546 lyxerr[Debug::GRAPHICS] << "Not a Graphics or Figure inset!\n";
548 cache_->update(MakeAbsPath(params().filename, buf->filePath()));
552 void InsetGraphics::readInsetGraphics(LyXLex & lex)
554 bool finished = false;
556 while (lex.isOK() && !finished) {
559 string const token = lex.getString();
560 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
565 } else if (token == "\\end_inset") {
567 } else if (token == "FormatVersion") {
569 int version = lex.getInteger();
570 if (version > VersionNumber)
572 << "This document was created with a newer Graphics widget"
573 ", You should use a newer version of LyX to read this"
576 // TODO: Possibly open up a dialog?
579 if (! params_.Read(lex, token))
580 lyxerr << "Unknown token, " << token << ", skipping."
586 // FormatVersion < 1.0 (LyX < 1.2)
587 void InsetGraphics::readFigInset(LyXLex & lex)
589 std::vector<string> const oldUnits =
590 getVectorFromString("pt,cm,in,p%,c%");
591 bool finished = false;
592 // set the display default
593 if (lyxrc.display_graphics == "mono")
594 params_.display = InsetGraphicsParams::MONOCHROME;
595 else if (lyxrc.display_graphics == "gray")
596 params_.display = InsetGraphicsParams::GRAYSCALE;
597 else if (lyxrc.display_graphics == "color")
598 params_.display = InsetGraphicsParams::COLOR;
600 params_.display = InsetGraphicsParams::NONE;
601 while (lex.isOK() && !finished) {
604 string const token = lex.getString();
605 lyxerr[Debug::GRAPHICS] << "Token: " << token << endl;
609 else if (token == "\\end_inset") {
611 } else if (token == "file") {
613 params_.filename = lex.getString();
615 } else if (token == "extra") {
617 // kept for backwards compability. Delete in 0.13.x
618 } else if (token == "subcaption") {
620 params_.subcaptionText = lex.getString();
621 } else if (token == "label") {
623 // kept for backwards compability. Delete in 0.13.x
624 } else if (token == "angle") {
626 params_.rotate = true;
627 params_.rotateAngle = lex.getFloat();
629 } else if (token == "size") {
631 params_.lyxwidth = LyXLength(lex.getString()+"pt");
633 params_.lyxheight = LyXLength(lex.getString()+"pt");
634 params_.lyxsize_type = InsetGraphicsParams::WH;
635 } else if (token == "flags") {
637 switch (lex.getInteger()) {
638 case 1: params_.display = InsetGraphicsParams::MONOCHROME;
640 case 2: params_.display = InsetGraphicsParams::GRAYSCALE;
642 case 3: params_.display = InsetGraphicsParams::COLOR;
645 } else if (token == "subfigure") {
646 params_.subcaption = true;
647 } else if (token == "width") {
649 int i = lex.getInteger();
652 params_.scale = lex.getInteger();
653 params_.size_type = InsetGraphicsParams::SCALE;
655 params_.width = LyXLength(lex.getString()+oldUnits[i]);
656 params_.size_type = InsetGraphicsParams::WH;
660 } else if (token == "height") {
662 int i = lex.getInteger();
664 params_.height = LyXLength(lex.getString()+oldUnits[i]);
665 params_.size_type = InsetGraphicsParams::WH;
672 string const InsetGraphics::createLatexOptions() const
674 // Calculate the options part of the command, we must do it to a string
675 // stream since we might have a trailing comma that we would like to remove
676 // before writing it to the output stream.
677 ostringstream options;
678 if (!params().bb.empty())
679 options << " bb=" << strip(params().bb) << ",\n";
681 options << " draft,\n";
683 options << " clip,\n";
684 if (params().size_type == InsetGraphicsParams::WH) {
685 if (!params().width.zero())
686 options << " width=" << params().width.asLatexString() << ",\n";
687 if (!params().height.zero())
688 options << " height=" << params().height.asLatexString() << ",\n";
689 } else if (params().size_type == InsetGraphicsParams::SCALE) {
690 if (params().scale > 0)
691 options << " scale=" << double(params().scale)/100.0 << ",\n";
693 if (params().keepAspectRatio)
694 options << " keepaspectratio,\n";
695 // Make sure it's not very close to zero, a float can be effectively
696 // zero but not exactly zero.
697 if (!lyx::float_equal(params().rotateAngle, 0, 0.001) && params().rotate) {
698 options << " angle=" << params().rotateAngle << ",\n";
699 if (!params().rotateOrigin.empty()) {
700 options << " origin=" << params().rotateOrigin[0];
701 if (contains(params().rotateOrigin,"Top"))
703 else if (contains(params().rotateOrigin,"Bottom"))
705 else if (contains(params().rotateOrigin,"Baseline"))
710 if (!params().special.empty())
711 options << params().special << ",\n";
712 string opts = options.str().c_str();
713 return opts.substr(0,opts.size()-2); // delete last ",\n"
717 string findTargetFormat(string const & suffix)
719 // lyxrc.pdf_mode means:
720 // Are we creating a PDF or a PS file?
721 // (Should actually mean, are we using latex or pdflatex).
722 if (lyxrc.pdf_mode) {
723 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode\n";
724 if (contains(suffix,"ps") || suffix == "pdf")
726 else if (suffix == "jpg") // pdflatex can use jpeg
729 return "png"; // and also png
731 // If it's postscript, we always do eps.
732 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode\n";
733 if (suffix != "ps") // any other than ps
734 return "eps"; // is changed to eps
736 return suffix; // let ps untouched
742 string const InsetGraphics::prepareFile(Buffer const *buf) const
744 // LaTeX can cope if the graphics file doesn't exist, so just return the
746 string const orig_file = params().filename;
747 string orig_file_with_path =
748 MakeAbsPath(orig_file, buf->filePath());
749 lyxerr[Debug::GRAPHICS] << "[InsetGraphics::prepareFile] orig_file = "
750 << orig_file << "\n\twith path: "
751 << orig_file_with_path << endl;
753 if (!IsFileReadable(orig_file_with_path))
756 // If the file is compressed and we have specified that it should not be
757 // uncompressed, then just return its name and let LaTeX do the rest!
759 // maybe that other zip extensions also be useful, especially the
760 // ones that may be declared in texmf/tex/latex/config/graphics.cfg.
762 /* -----------snip-------------
763 {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
764 \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
765 \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
766 \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
767 \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
768 -----------snip-------------*/
770 bool const zipped = zippedFile(orig_file_with_path);
772 lyxerr[Debug::GRAPHICS] << "\twe have a zipped file ("
773 << getExtFromContents(orig_file_with_path) << ")\n";
774 if (params().noUnzip && zipped) {
775 lyxerr[Debug::GRAPHICS]
776 << "\tpass file unzipped to LaTeX but with full path.\n";
777 // latex needs an absolue path, otherwise the coresponding
778 // *.eps.bb file isn't found
779 return orig_file_with_path;
782 string temp_file(orig_file);
783 // Uncompress the file if necessary. If it has been uncompressed in
784 // a previous call to prepareFile, do nothing.
786 temp_file = MakeAbsPath(OnlyFilename(temp_file), buf->tmppath);
787 lyxerr[Debug::GRAPHICS]
788 << "\ttemp_file: " << temp_file << endl;
789 if (!IsFileReadable(temp_file)) {
790 bool const success = lyx::copy(orig_file_with_path, temp_file);
791 lyxerr[Debug::GRAPHICS]
792 << "\tCopying zipped file from "
793 << orig_file_with_path << " to " << temp_file
794 << (success ? " succeeded\n" : " failed\n");
796 lyxerr[Debug::GRAPHICS]
797 << "\tzipped file " << temp_file
798 << " exists! Maybe no tempdir ...\n";
799 orig_file_with_path = unzipFile(temp_file);
800 lyxerr[Debug::GRAPHICS]
801 << "\tunzipped to " << orig_file_with_path << endl;
803 string const from = getExtFromContents(orig_file_with_path);
805 // "nice" means that the buffer is exported to LaTeX format but not
806 // run through the LaTeX compiler.
808 // No conversion of the graphics file is needed.
809 // Return the original filename without any extension.
811 return RemoveExtension(orig_file);
813 // We're going to be running the exported buffer through the LaTeX
814 // compiler, so must ensure that LaTeX can cope with the graphics
817 // Perform all these manipulations on a temporary file if possible.
818 // If we are not using a temp dir, then temp_file contains the
820 // to allow files with the same name in different dirs
821 // we manipulate the original file "any.dir/file.ext"
822 // to "any_dir_file.ext"! changing the dots in the
823 // dirname is important for the use of ChangeExtension
824 lyxerr[Debug::GRAPHICS]
825 << "\tthe orig file is: " << orig_file_with_path << endl;
827 if (lyxrc.use_tempdir) {
828 string const ext_tmp = GetExtension(orig_file_with_path);
831 ChangeExtension(orig_file_with_path, string()), "/", "_");
832 // without dots and again with ext
833 temp_file = ChangeExtension(
834 subst(temp_file, ".", "_"), ext_tmp);
835 // now we have any_dir_file.ext
836 temp_file = MakeAbsPath(temp_file, buf->tmppath);
837 lyxerr[Debug::GRAPHICS]
838 << "\tchanged to: " << temp_file << endl;
840 // if the file doen't exists, copy it into the tempdi
841 if (!IsFileReadable(temp_file)) {
842 bool const success = lyx::copy(orig_file_with_path, temp_file);
843 lyxerr[Debug::GRAPHICS]
844 << "\tcopying from " << orig_file_with_path << " to "
846 << (success ? " succeeded\n" : " failed\n");
848 Alert::alert(_("Cannot copy file"), orig_file_with_path,
855 string const to = findTargetFormat(from);
856 lyxerr[Debug::GRAPHICS]
857 << "\t we have: from " << from << " to " << to << '\n';
859 // No conversion is needed. LaTeX can handle the graphic file as is.
860 // This is true even if the orig_file is compressed. We have to return
861 // the orig_file_with_path, maybe it is a zipped one
862 return lyxrc.use_tempdir ? temp_file : orig_file_with_path;
865 string const outfile_base = RemoveExtension(temp_file);
866 lyxerr[Debug::GRAPHICS]
867 << "\tThe original file is " << orig_file << "\n"
868 << "\tA copy has been made and convert is to be called with:\n"
869 << "\tfile to convert = " << temp_file << '\n'
870 << "\toutfile_base = " << outfile_base << '\n'
871 << "\t from " << from << " to " << to << '\n';
873 // if no special converter defined, than we take the default one
874 // from ImageMagic: convert from:inname.from to:outname.to
875 if (!converters.convert(buf, temp_file, outfile_base, from, to)) {
876 string const command =
878 from + ':' + temp_file + ' ' +
879 to + ':' + outfile_base + '.' + to;
880 lyxerr[Debug::GRAPHICS]
881 << "No converter defined! I use convert from ImageMagic:\n\t"
884 one.startscript(Systemcall::Wait, command);
885 if (!IsFileReadable(ChangeExtension(outfile_base, to)))
886 Alert::alert(_("Cannot convert Image (not existing file?)"),
887 _("No information for converting from ")
888 + from + _(" to ") + to);
891 return RemoveExtension(temp_file);
895 int InsetGraphics::latex(Buffer const *buf, ostream & os,
896 bool /*fragile*/, bool/*fs*/) const
898 // If there is no file specified or not existing,
899 // just output a message about it in the latex output.
900 lyxerr[Debug::GRAPHICS]
901 << "insetgraphics::latex: Filename = "
902 << params().filename << endl;
904 // A missing (e)ps-extension is no problem for LaTeX, so
905 // we have to test three different cases
906 string const file_(MakeAbsPath(params().filename, buf->filePath()));
907 bool const file_exists =
909 (IsFileReadable(file_) || // original
910 IsFileReadable(file_ + ".eps") || // original.eps
911 IsFileReadable(file_ + ".ps")); // original.ps
912 string const message = file_exists ?
913 string() : string("bb = 0 0 200 100, draft, type=eps]");
914 // if !message.empty() than there was no existing file
915 // "filename(.(e)ps)" found. In this case LaTeX
916 // draws only a rectangle with the above bb and the
917 // not found filename in it.
918 lyxerr[Debug::GRAPHICS]
919 << "\tMessage = \"" << message << '\"' << endl;
921 // These variables collect all the latex code that should be before and
922 // after the actual includegraphics command.
925 // Do we want subcaptions?
926 if (params().subcaption) {
927 before += "\\subfigure[" + params().subcaptionText + "]{";
930 // We never use the starred form, we use the "clip" option instead.
931 before += "\\includegraphics";
933 // Write the options if there are any.
934 string const opts = createLatexOptions();
935 lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
937 if (!opts.empty() && !message.empty())
938 before += ("[%\n" + opts + ',' + message);
939 else if (!message.empty())
940 before += ("[%\n" + message);
941 else if (!opts.empty())
942 before += ("[%\n" + opts + ']');
944 lyxerr[Debug::GRAPHICS]
945 << "\tBefore = " << before
946 << "\n\tafter = " << after << endl;
948 // Make the filename relative to the lyx file
949 // and remove the extension so the LaTeX will use whatever is
950 // appropriate (when there are several versions in different formats)
951 string const latex_str = message.empty() ?
952 (before + '{' + os::external_path(prepareFile(buf)) + '}' + after) :
953 (before + '{' + params().filename + " not found!}" + after);
956 // Return how many newlines we issued.
958 int(lyx::count(latex_str.begin(), latex_str.end(),'\n') + 1);
964 int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
966 // No graphics in ascii output. Possible to use gifscii to convert
967 // images to ascii approximation.
968 // 1. Convert file to ascii using gifscii
969 // 2. Read ascii output file and add it to the output stream.
970 // at least we send the filename
971 os << '<' << _("Graphic file:") << params().filename << ">\n";
976 int InsetGraphics::linuxdoc(Buffer const *, ostream &) const
978 // No graphics in LinuxDoc output. Should check how/what to add.
983 // For explanation on inserting graphics into DocBook checkout:
984 // http://linuxdoc.org/LDP/LDP-Author-Guide/inserting-pictures.html
985 // See also the docbook guide at http://www.docbook.org/
986 int InsetGraphics::docbook(Buffer const *, ostream & os,
987 bool /*mixcont*/) const
989 // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
990 // need to switch to MediaObject. However, for now this is sufficient and
992 os << "<graphic fileref=\"&" << graphic_label << ";\">";
997 void InsetGraphics::validate(LaTeXFeatures & features) const
999 // If we have no image, we should not require anything.
1000 if (params().filename.empty())
1003 features.includeFile(graphic_label, RemoveExtension(params().filename));
1005 features.require("graphicx");
1007 if (params().subcaption)
1008 features.require("subfigure");
1012 void InsetGraphics::statusChanged()
1014 cache_->setStatus(cache_->graphic_->status());
1015 if (cache_->status() == grfx::Loaded)
1018 current_view->updateInset(this, false);
1022 bool InsetGraphics::setParams(InsetGraphicsParams const & p,
1023 string const & filepath)
1025 // If nothing is changed, just return and say so.
1026 if (params() == p && !p.filename.empty()) {
1030 // Copy the new parameters.
1033 // Update the inset with the new parameters.
1034 cache_->update(MakeAbsPath(params().filename, filepath));
1036 // We have changed data, report it.
1041 InsetGraphicsParams const & InsetGraphics::params() const
1047 Inset * InsetGraphics::clone(Buffer const & buffer, bool same_id) const
1049 return new InsetGraphics(*this, buffer.filePath(), same_id);