2 * \file insetgraphics.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 * What advanced features the users want to do?
16 Implement them in a non latex dependent way, but a logical way.
17 LyX should translate it to latex or any other fitting format.
18 * Add a way to roll the image file into the file format.
19 * When loading, if the image is not found in the expected place, try
20 to find it in the clipart, or in the same directory with the image.
21 * The image choosing dialog could show thumbnails of the image formats
22 it knows of, thus selection based on the image instead of based on
24 * Add support for the 'picins' package.
25 * Add support for the 'picinpar' package.
26 * Improve support for 'subfigure' - Allow to set the various options
32 * The filename is kept in the lyx file in a relative way, so as to allow
33 * moving the document file and its images with no problem.
37 * Postscript output means EPS figures.
39 * PDF output is best done with PDF figures if it's a direct conversion
40 * or PNG figures otherwise.
52 #include "insets/insetgraphics.h"
53 #include "insets/render_graphic.h"
56 #include "BufferView.h"
57 #include "converter.h"
60 #include "dispatchresult.h"
63 #include "funcrequest.h"
65 #include "LaTeXFeatures.h"
67 #include "lyxlength.h"
69 #include "metricsinfo.h"
71 #include "outputparams.h"
74 #include "frontends/Alert.h"
75 #include "frontends/LyXView.h"
77 #include "support/filetools.h"
78 #include "support/lyxalgo.h" // lyx::count
79 #include "support/lyxlib.h" // lyx::sum
80 #include "support/lstrings.h"
81 #include "support/os.h"
82 #include "support/systemcall.h"
84 #include <boost/bind.hpp>
85 #include <boost/tuple/tuple.hpp>
89 namespace support = lyx::support;
91 using lyx::support::AbsolutePath;
92 using lyx::support::bformat;
93 using lyx::support::ChangeExtension;
94 using lyx::support::compare_timestamps;
95 using lyx::support::contains;
96 using lyx::support::FileName;
97 using lyx::support::float_equal;
98 using lyx::support::GetExtension;
99 using lyx::support::IsFileReadable;
100 using lyx::support::LibFileSearch;
101 using lyx::support::OnlyFilename;
102 using lyx::support::rtrim;
103 using lyx::support::strToDbl;
104 using lyx::support::subst;
105 using lyx::support::Systemcall;
106 using lyx::support::unzipFile;
107 using lyx::support::unzippedFileName;
109 namespace os = lyx::support::os;
114 using std::istringstream;
116 using std::ostringstream;
121 // This function is a utility function
122 // ... that should be with ChangeExtension ...
124 string const RemoveExtension(string const & filename)
126 return ChangeExtension(filename, string());
130 string findTargetFormat(string const & format, OutputParams const & runparams)
132 // Are we using latex or pdflatex?
133 if (runparams.flavor == OutputParams::PDFLATEX) {
134 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
135 // Convert postscript to pdf
136 if (format == "eps" || format == "ps")
138 // pdflatex can use jpeg, png and pdf directly
139 if (format == "jpg" || format == "pdf")
141 // Convert everything else to png
144 // If it's postscript, we always do eps.
145 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
147 // any other than ps is changed to eps
156 InsetGraphics::InsetGraphics()
157 : graphic_label(sgml::uniqueID("graph")),
158 graphic_(new RenderGraphic(this))
162 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
164 boost::signals::trackable(),
165 graphic_label(sgml::uniqueID("graph")),
166 graphic_(new RenderGraphic(*ig.graphic_, this))
168 setParams(ig.params());
172 auto_ptr<InsetBase> InsetGraphics::doClone() const
174 return auto_ptr<InsetBase>(new InsetGraphics(*this));
178 InsetGraphics::~InsetGraphics()
180 InsetGraphicsMailer(*this).hideDialog();
184 void InsetGraphics::doDispatch(LCursor & cur, FuncRequest & cmd)
186 switch (cmd.action) {
187 case LFUN_GRAPHICS_EDIT: {
188 Buffer const & buffer = *cur.bv().buffer();
189 InsetGraphicsParams p;
190 InsetGraphicsMailer::string2params(cmd.argument, buffer, p);
191 editGraphics(p, buffer);
195 case LFUN_INSET_MODIFY: {
196 Buffer const & buffer = cur.buffer();
197 InsetGraphicsParams p;
198 InsetGraphicsMailer::string2params(cmd.argument, buffer, p);
199 if (!p.filename.empty()) {
206 case LFUN_INSET_DIALOG_UPDATE:
207 InsetGraphicsMailer(*this).updateDialog(&cur.bv());
210 case LFUN_MOUSE_RELEASE:
211 InsetGraphicsMailer(*this).showDialog(&cur.bv());
215 InsetBase::doDispatch(cur, cmd);
221 void InsetGraphics::edit(LCursor & cur, bool)
223 InsetGraphicsMailer(*this).showDialog(&cur.bv());
227 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
229 graphic_->metrics(mi, dim);
234 void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
236 setPosCache(pi, x, y);
237 graphic_->draw(pi, x, y);
241 InsetBase::EDITABLE InsetGraphics::editable() const
247 void InsetGraphics::write(Buffer const & buf, ostream & os) const
250 params().Write(os, buf.filePath());
254 void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
256 string const token = lex.getString();
258 if (token == "Graphics")
259 readInsetGraphics(lex, buf.filePath());
261 lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
263 graphic_->update(params().as_grfxParams());
267 void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
269 bool finished = false;
271 while (lex.isOK() && !finished) {
274 string const token = lex.getString();
275 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
280 } else if (token == "\\end_inset") {
283 if (!params_.Read(lex, token, bufpath))
284 lyxerr << "Unknown token, " << token << ", skipping."
291 string const InsetGraphics::createLatexOptions() const
293 // Calculate the options part of the command, we must do it to a string
294 // stream since we might have a trailing comma that we would like to remove
295 // before writing it to the output stream.
296 ostringstream options;
297 if (!params().bb.empty())
298 options << " bb=" << rtrim(params().bb) << ",\n";
300 options << " draft,\n";
302 options << " clip,\n";
303 double const scl = strToDbl(params().scale);
304 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
305 if (!float_equal(scl, 100.0, 0.05))
306 options << " scale=" << scl / 100.0
309 if (!params().width.zero())
310 options << " width=" << params().width.asLatexString() << ",\n";
311 if (!params().height.zero())
312 options << " height=" << params().height.asLatexString() << ",\n";
313 if (params().keepAspectRatio)
314 options << " keepaspectratio,\n";
317 // Make sure rotation angle is not very close to zero;
318 // a float can be effectively zero but not exactly zero.
319 if (!params().rotateAngle.empty()
320 && !float_equal(strToDbl(params().rotateAngle), 0.0, 0.001)) {
321 options << " angle=" << params().rotateAngle << ",\n";
322 if (!params().rotateOrigin.empty()) {
323 options << " origin=" << params().rotateOrigin[0];
324 if (contains(params().rotateOrigin,"Top"))
326 else if (contains(params().rotateOrigin,"Bottom"))
328 else if (contains(params().rotateOrigin,"Baseline"))
334 if (!params().special.empty())
335 options << params().special << ",\n";
337 string opts = options.str();
339 return opts.substr(0, opts.size() - 2);
343 string const InsetGraphics::toDocbookLength(LyXLength const & len) const
345 ostringstream result;
346 switch (len.unit()) {
347 case LyXLength::SP: // Scaled point (65536sp = 1pt) TeX's smallest unit.
348 result << len.value() * 65536.0 * 72 / 72.27 << "pt";
350 case LyXLength::PT: // Point = 1/72.27in = 0.351mm
351 result << len.value() * 72 / 72.27 << "pt";
353 case LyXLength::BP: // Big point (72bp = 1in), also PostScript point
354 result << len.value() << "pt";
356 case LyXLength::DD: // Didot point = 1/72 of a French inch, = 0.376mm
357 result << len.value() * 0.376 << "mm";
359 case LyXLength::MM: // Millimeter = 2.845pt
360 result << len.value() << "mm";
362 case LyXLength::PC: // Pica = 12pt = 4.218mm
363 result << len.value() << "pc";
365 case LyXLength::CC: // Cicero = 12dd = 4.531mm
366 result << len.value() * 4.531 << "mm";
368 case LyXLength::CM: // Centimeter = 10mm = 2.371pc
369 result << len.value() << "cm";
371 case LyXLength::IN: // Inch = 25.4mm = 72.27pt = 6.022pc
372 result << len.value() << "in";
374 case LyXLength::EX: // Height of a small "x" for the current font.
375 // Obviously we have to compromise here. Any better ratio than 1.5 ?
376 result << len.value() / 1.5 << "em";
378 case LyXLength::EM: // Width of capital "M" in current font.
379 result << len.value() << "em";
381 case LyXLength::MU: // Math unit (18mu = 1em) for positioning in math mode
382 result << len.value() * 18 << "em";
384 case LyXLength::PTW: // Percent of TextWidth
385 case LyXLength::PCW: // Percent of ColumnWidth
386 case LyXLength::PPW: // Percent of PageWidth
387 case LyXLength::PLW: // Percent of LineWidth
388 case LyXLength::PTH: // Percent of TextHeight
389 case LyXLength::PPH: // Percent of Paper
390 // Sigh, this will go wrong.
391 result << len.value() << "%";
394 result << len.asString();
400 string const InsetGraphics::createDocBookAttributes() const
402 // Calculate the options part of the command, we must do it to a string
403 // stream since we copied the code from createLatexParams() ;-)
405 // FIXME: av: need to translate spec -> Docbook XSL spec (http://www.sagehill.net/docbookxsl/ImageSizing.html)
406 // Right now it only works with my version of db2latex :-)
408 ostringstream options;
409 double const scl = strToDbl(params().scale);
410 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
411 if (!float_equal(scl, 100.0, 0.05))
412 options << " scale=\""
413 << static_cast<int>( (scl) + 0.5 )
416 if (!params().width.zero()) {
417 options << " width=\"" << toDocbookLength(params().width) << "\" ";
419 if (!params().height.zero()) {
420 options << " depth=\"" << toDocbookLength(params().height) << "\" ";
422 if (params().keepAspectRatio) {
423 // This will be irrelevant unless both width and height are set
424 options << "scalefit=\"1\" ";
429 if (!params().special.empty())
430 options << params().special << " ";
432 string opts = options.str();
433 // trailing blanks are ok ...
448 std::pair<CopyStatus, string> const
449 copyFileIfNeeded(string const & file_in, string const & file_out)
451 BOOST_ASSERT(AbsolutePath(file_in));
452 BOOST_ASSERT(AbsolutePath(file_out));
454 unsigned long const checksum_in = support::sum(file_in);
455 unsigned long const checksum_out = support::sum(file_out);
457 if (checksum_in == checksum_out)
459 return std::make_pair(IDENTICAL_CONTENTS, file_out);
461 Mover const & mover = movers(formats.getFormatFromFile(file_in));
462 bool const success = mover.copy(file_in, file_out);
464 lyxerr[Debug::GRAPHICS]
465 << support::bformat(_("Could not copy the file\n%1$s\n"
466 "into the temporary directory."),
471 CopyStatus status = success ? SUCCESS : FAILURE;
472 return std::make_pair(status, file_out);
476 std::pair<CopyStatus, string> const
477 copyToDirIfNeeded(string const & file_in, string const & dir, bool zipped)
479 using support::rtrim;
481 BOOST_ASSERT(AbsolutePath(file_in));
483 string const only_path = support::OnlyPath(file_in);
484 if (rtrim(support::OnlyPath(file_in) , "/") == rtrim(dir, "/"))
485 return std::make_pair(IDENTICAL_PATHS, file_in);
487 string mangled = FileName(file_in).mangledFilename();
489 // We need to change _eps.gz to .eps.gz. The mangled name is
490 // still unique because of the counter in mangledFilename().
491 // We can't just call mangledFilename() with the zip
492 // extension removed, because base.eps and base.eps.gz may
493 // have different content but would get the same mangled
494 // name in this case.
495 string const base = RemoveExtension(unzippedFileName(file_in));
496 string::size_type const ext_len = file_in.length() - base.length();
497 mangled[mangled.length() - ext_len] = '.';
499 string const file_out = support::MakeAbsPath(mangled, dir);
501 return copyFileIfNeeded(file_in, file_out);
505 string const stripExtension(string const & file)
507 // Remove the extension so the LaTeX will use whatever
508 // is appropriate (when there are several versions in
509 // different formats)
510 // This works only if the filename contains no dots besides
511 // the just removed one. We can fool here by replacing all
512 // dots with a macro whose definition is just a dot ;-)
513 return subst(RemoveExtension(file), ".", "\\lyxdot ");
517 string const stripExtensionIfPossible(string const & file, string const & to)
519 // No conversion is needed. LaTeX can handle the graphic file as is.
520 // This is true even if the orig_file is compressed.
521 string const to_format = formats.getFormat(to)->extension();
522 string const file_format = GetExtension(file);
523 // for latex .ps == .eps
524 if (to_format == file_format ||
525 (to_format == "eps" && file_format == "ps") ||
526 (to_format == "ps" && file_format == "eps"))
527 return stripExtension(file);
534 string const InsetGraphics::prepareFile(Buffer const & buf,
535 OutputParams const & runparams) const
537 // We assume that the file exists (the caller checks this)
538 string const orig_file = params().filename.absFilename();
539 string const rel_file = params().filename.relFilename(buf.filePath());
541 // If the file is compressed and we have specified that it
542 // should not be uncompressed, then just return its name and
543 // let LaTeX do the rest!
544 bool const zipped = params().filename.isZipped();
546 // temp_file will contain the file for LaTeX to act on if, for example,
547 // we move it to a temp dir or uncompress it.
548 string temp_file = orig_file;
550 // The master buffer. This is useful when there are multiple levels
552 Buffer const * m_buffer = buf.getMasterBuffer();
554 // We place all temporary files in the master buffer's temp dir.
555 // This is possible because we use mangled file names.
556 // This is necessary for DVI export.
557 string const temp_path = m_buffer->temppath();
560 boost::tie(status, temp_file) =
561 copyToDirIfNeeded(orig_file, temp_path, zipped);
563 if (status == FAILURE)
566 // a relative filename should be relative to the master
568 // "nice" means that the buffer is exported to LaTeX format but not
569 // run through the LaTeX compiler.
570 string const output_file = os::external_path(runparams.nice ?
571 params().filename.outputFilename(m_buffer->filePath()) :
572 OnlyFilename(temp_file));
573 string const source_file = runparams.nice ? orig_file : temp_file;
576 if (params().noUnzip) {
577 // We don't know whether latex can actually handle
578 // this file, but we can't check, because that would
579 // mean to unzip the file and thereby making the
580 // noUnzip parameter meaningless.
581 lyxerr[Debug::GRAPHICS]
582 << "\tpass zipped file to LaTeX.\n";
584 string const bb_orig_file = ChangeExtension(orig_file, "bb");
585 if (runparams.nice) {
586 runparams.exportdata->addExternalFile("latex",
588 ChangeExtension(output_file, "bb"));
590 // LaTeX needs the bounding box file in the
592 string bb_file = ChangeExtension(temp_file, "bb");
593 boost::tie(status, bb_file) =
594 copyFileIfNeeded(bb_orig_file, bb_file);
595 if (status == FAILURE)
597 runparams.exportdata->addExternalFile("latex",
600 runparams.exportdata->addExternalFile("latex",
601 source_file, output_file);
602 runparams.exportdata->addExternalFile("dvi",
603 source_file, output_file);
604 // We can't strip the extension, because we don't know
605 // the unzipped file format
609 string const unzipped_temp_file = unzippedFileName(temp_file);
610 if (compare_timestamps(unzipped_temp_file, temp_file) > 0) {
611 // temp_file has been unzipped already and
612 // orig_file has not changed in the meantime.
613 temp_file = unzipped_temp_file;
614 lyxerr[Debug::GRAPHICS]
615 << "\twas already unzipped to " << temp_file
618 // unzipped_temp_file does not exist or is too old
619 temp_file = unzipFile(temp_file);
620 lyxerr[Debug::GRAPHICS]
621 << "\tunzipped to " << temp_file << endl;
625 string const from = formats.getFormatFromFile(temp_file);
627 lyxerr[Debug::GRAPHICS]
628 << "\tCould not get file format." << endl;
631 string const to = findTargetFormat(from, runparams);
632 string const ext = formats.extension(to);
633 lyxerr[Debug::GRAPHICS]
634 << "\t we have: from " << from << " to " << to << '\n';
636 // We're going to be running the exported buffer through the LaTeX
637 // compiler, so must ensure that LaTeX can cope with the graphics
640 lyxerr[Debug::GRAPHICS]
641 << "\tthe orig file is: " << orig_file << endl;
644 // The extension of temp_file might be != ext!
645 runparams.exportdata->addExternalFile("latex", source_file,
647 runparams.exportdata->addExternalFile("dvi", source_file,
649 return stripExtensionIfPossible(output_file, to);
652 string const to_file = ChangeExtension(temp_file, ext);
653 string const output_to_file = ChangeExtension(output_file, ext);
655 // Do we need to perform the conversion?
656 // Yes if to_file does not exist or if temp_file is newer than to_file
657 if (compare_timestamps(temp_file, to_file) < 0) {
658 lyxerr[Debug::GRAPHICS]
659 << bformat(_("No conversion of %1$s is needed after all"),
662 runparams.exportdata->addExternalFile("latex", to_file,
664 runparams.exportdata->addExternalFile("dvi", to_file,
666 return stripExtension(output_file);
669 lyxerr[Debug::GRAPHICS]
670 << "\tThe original file is " << orig_file << "\n"
671 << "\tA copy has been made and convert is to be called with:\n"
672 << "\tfile to convert = " << temp_file << '\n'
673 << "\t from " << from << " to " << to << '\n';
675 // if no special converter defined, then we take the default one
676 // from ImageMagic: convert from:inname.from to:outname.to
677 if (converters.convert(&buf, temp_file, temp_file, from, to)) {
678 runparams.exportdata->addExternalFile("latex",
679 to_file, output_to_file);
680 runparams.exportdata->addExternalFile("dvi",
681 to_file, output_to_file);
683 string const command =
684 "sh " + LibFileSearch("scripts", "convertDefault.sh") +
685 ' ' + formats.extension(from) + ':' + temp_file +
686 ' ' + ext + ':' + to_file;
687 lyxerr[Debug::GRAPHICS]
688 << "No converter defined! I use convertDefault.sh:\n\t"
691 one.startscript(Systemcall::Wait, command);
692 if (IsFileReadable(to_file)) {
693 runparams.exportdata->addExternalFile("latex",
694 to_file, output_to_file);
695 runparams.exportdata->addExternalFile("dvi",
696 to_file, output_to_file);
698 string str = bformat(_("No information for converting %1$s "
699 "format files to %2$s.\n"
700 "Try defining a convertor in the preferences."), from, to);
701 Alert::error(_("Could not convert image"), str);
705 return stripExtension(output_file);
709 int InsetGraphics::latex(Buffer const & buf, ostream & os,
710 OutputParams const & runparams) const
712 // If there is no file specified or not existing,
713 // just output a message about it in the latex output.
714 lyxerr[Debug::GRAPHICS]
715 << "insetgraphics::latex: Filename = "
716 << params().filename.absFilename() << endl;
718 string const relative_file =
719 params().filename.relFilename(buf.filePath());
721 string const file_ = params().filename.absFilename();
722 bool const file_exists = !file_.empty() && IsFileReadable(file_);
723 string const message = file_exists ?
724 string() : string("bb = 0 0 200 100, draft, type=eps");
725 // if !message.empty() then there was no existing file
726 // "filename" found. In this case LaTeX
727 // draws only a rectangle with the above bb and the
728 // not found filename in it.
729 lyxerr[Debug::GRAPHICS]
730 << "\tMessage = \"" << message << '\"' << endl;
732 // These variables collect all the latex code that should be before and
733 // after the actual includegraphics command.
736 // Do we want subcaptions?
737 if (params().subcaption) {
738 before += "\\subfigure[" + params().subcaptionText + "]{";
741 // We never use the starred form, we use the "clip" option instead.
742 before += "\\includegraphics";
744 // Write the options if there are any.
745 string const opts = createLatexOptions();
746 lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
748 if (!opts.empty() && !message.empty())
749 before += ("[%\n" + opts + ',' + message + ']');
750 else if (!opts.empty() || !message.empty())
751 before += ("[%\n" + opts + message + ']');
753 lyxerr[Debug::GRAPHICS]
754 << "\tBefore = " << before
755 << "\n\tafter = " << after << endl;
758 string latex_str = before + '{';
760 // Convert the file if necessary.
761 // Remove the extension so the LaTeX will use whatever
762 // is appropriate (when there are several versions in
763 // different formats)
764 latex_str += prepareFile(buf, runparams);
766 latex_str += relative_file + " not found!";
768 latex_str += '}' + after;
771 lyxerr[Debug::GRAPHICS] << "InsetGraphics::latex outputting:\n"
772 << latex_str << endl;
773 // Return how many newlines we issued.
774 return int(lyx::count(latex_str.begin(), latex_str.end(),'\n') + 1);
778 int InsetGraphics::plaintext(Buffer const &, ostream & os,
779 OutputParams const &) const
781 // No graphics in ascii output. Possible to use gifscii to convert
782 // images to ascii approximation.
783 // 1. Convert file to ascii using gifscii
784 // 2. Read ascii output file and add it to the output stream.
785 // at least we send the filename
786 os << '<' << bformat(_("Graphics file: %1$s"),
787 params().filename.absFilename()) << ">\n";
792 int InsetGraphics::linuxdoc(Buffer const & buf, ostream & os,
793 OutputParams const & runparams) const
795 string const file_name = runparams.nice ?
796 params().filename.relFilename(buf.filePath()):
797 params().filename.absFilename();
799 runparams.exportdata->addExternalFile("linuxdoc",
800 params().filename.absFilename());
801 os << "<eps file=\"" << file_name << "\">\n";
802 os << "<img src=\"" << file_name << "\">";
809 int writeImageObject(char * format, ostream& os, OutputParams const & runparams,
810 string const graphic_label, string const attributes)
812 if (runparams.flavor != OutputParams::XML) {
813 os << "<![ %output.print." << format << "; [" << std::endl;
815 os <<"<imageobject><imagedata fileref=\"&"
816 << graphic_label << ";." << format << "\" " << attributes ;
817 if (runparams.flavor == OutputParams::XML) {
818 os << " role=\"" << format << "\"/>" ;
821 os << " format=\"" << format << "\">" ;
823 os << "</imageobject>";
824 if (runparams.flavor != OutputParams::XML) {
825 os << std::endl << "]]>" ;
827 return runparams.flavor == OutputParams::XML ? 0 : 2;
829 // end anonymous namespace
833 // For explanation on inserting graphics into DocBook checkout:
834 // http://en.tldp.org/LDP/LDP-Author-Guide/html/inserting-pictures.html
835 // See also the docbook guide at http://www.docbook.org/
836 int InsetGraphics::docbook(Buffer const &, ostream & os,
837 OutputParams const & runparams) const
839 // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
840 // need to switch to MediaObject. However, for now this is sufficient and
842 if (runparams.flavor == OutputParams::XML) {
843 runparams.exportdata->addExternalFile("docbook-xml",
844 params().filename.absFilename());
846 runparams.exportdata->addExternalFile("docbook",
847 params().filename.absFilename());
849 os << "<inlinemediaobject>";
852 string attributes = createDocBookAttributes();
853 r += writeImageObject("png", os, runparams, graphic_label, attributes);
854 r += writeImageObject("pdf", os, runparams, graphic_label, attributes);
855 r += writeImageObject("eps", os, runparams, graphic_label, attributes);
856 r += writeImageObject("bmp", os, runparams, graphic_label, attributes);
858 os << "</inlinemediaobject>";
863 void InsetGraphics::validate(LaTeXFeatures & features) const
865 // If we have no image, we should not require anything.
866 if (params().filename.empty())
869 features.includeFile(graphic_label,
870 RemoveExtension(params().filename.absFilename()));
872 features.require("graphicx");
874 if (features.nice()) {
875 Buffer const * m_buffer = features.buffer().getMasterBuffer();
877 params().filename.outputFilename(m_buffer->filePath());
878 basename = RemoveExtension(basename);
879 if(params().filename.isZipped())
880 basename = RemoveExtension(basename);
881 if (contains(basename, "."))
882 features.require("lyxdot");
885 if (params().subcaption)
886 features.require("subfigure");
890 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
892 // If nothing is changed, just return and say so.
893 if (params() == p && !p.filename.empty())
896 // Copy the new parameters.
899 // Update the display using the new parameters.
900 graphic_->update(params().as_grfxParams());
902 // We have changed data, report it.
907 InsetGraphicsParams const & InsetGraphics::params() const
913 void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
914 Buffer const & buffer) const
916 string const file_with_path = p.filename.absFilename();
917 formats.edit(buffer, file_with_path,
918 formats.getFormatFromFile(file_with_path));
922 string const InsetGraphicsMailer::name_("graphics");
924 InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)
929 string const InsetGraphicsMailer::inset2string(Buffer const & buffer) const
931 return params2string(inset_.params(), buffer);
935 void InsetGraphicsMailer::string2params(string const & in,
936 Buffer const & buffer,
937 InsetGraphicsParams & params)
939 params = InsetGraphicsParams();
943 istringstream data(in);
949 if (!lex || name != name_)
950 return print_mailer_error("InsetGraphicsMailer", in, 1, name_);
953 inset.readInsetGraphics(lex, buffer.filePath());
954 params = inset.params();
959 InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
960 Buffer const & buffer)
963 data << name_ << ' ';
964 params.Write(data, buffer.filePath());
965 data << "\\end_inset\n";