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"
64 #include "FuncStatus.h"
66 #include "LaTeXFeatures.h"
68 #include "lyxlength.h"
70 #include "metricsinfo.h"
72 #include "outputparams.h"
75 #include "frontends/Alert.h"
77 #include "support/convert.h"
78 #include "support/filetools.h"
79 #include "support/lyxalgo.h" // count
80 #include "support/lyxlib.h" // sum
81 #include "support/lstrings.h"
82 #include "support/os.h"
83 #include "support/systemcall.h"
85 #include <boost/bind.hpp>
86 #include <boost/tuple/tuple.hpp>
93 using support::absolutePath;
94 using support::bformat;
95 using support::changeExtension;
96 using support::compare_timestamps;
97 using support::contains;
98 using support::FileName;
99 using support::float_equal;
100 using support::getExtension;
101 using support::isFileReadable;
102 using support::latex_path;
103 using support::onlyFilename;
104 using support::removeExtension;
105 using support::rtrim;
106 using support::subst;
107 using support::Systemcall;
108 using support::unzipFile;
109 using support::unzippedFileName;
114 using std::istringstream;
116 using std::ostringstream;
121 /// Find the most suitable image format for images in \p format
122 /// Note that \p format may be unknown (i. e. an empty string)
123 string findTargetFormat(string const & format, OutputParams const & runparams)
125 // Are we using latex or pdflatex?
126 if (runparams.flavor == OutputParams::PDFLATEX) {
127 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
128 Format const * const f = formats.getFormat(format);
129 // Convert vector graphics to pdf
130 if (f && f->vectorFormat())
132 // pdflatex can use jpeg, png and pdf directly
135 // Convert everything else to png
138 // If it's postscript, we always do eps.
139 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
141 // any other than ps is changed to eps
150 InsetGraphics::InsetGraphics()
151 : graphic_label(sgml::uniqueID(from_ascii("graph"))),
152 graphic_(new RenderGraphic(this))
156 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
158 boost::signals::trackable(),
159 graphic_label(sgml::uniqueID(from_ascii("graph"))),
160 graphic_(new RenderGraphic(*ig.graphic_, this))
162 setParams(ig.params());
166 auto_ptr<InsetBase> InsetGraphics::doClone() const
168 return auto_ptr<InsetBase>(new InsetGraphics(*this));
172 InsetGraphics::~InsetGraphics()
174 InsetGraphicsMailer(*this).hideDialog();
178 void InsetGraphics::doDispatch(LCursor & cur, FuncRequest & cmd)
180 switch (cmd.action) {
181 case LFUN_GRAPHICS_EDIT: {
182 Buffer const & buffer = *cur.bv().buffer();
183 InsetGraphicsParams p;
184 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, p);
185 editGraphics(p, buffer);
189 case LFUN_INSET_MODIFY: {
190 Buffer const & buffer = cur.buffer();
191 InsetGraphicsParams p;
192 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, p);
193 if (!p.filename.empty())
200 case LFUN_INSET_DIALOG_UPDATE:
201 InsetGraphicsMailer(*this).updateDialog(&cur.bv());
204 case LFUN_MOUSE_RELEASE:
205 InsetGraphicsMailer(*this).showDialog(&cur.bv());
209 InsetBase::doDispatch(cur, cmd);
215 bool InsetGraphics::getStatus(LCursor & cur, FuncRequest const & cmd,
216 FuncStatus & flag) const
218 switch (cmd.action) {
219 case LFUN_GRAPHICS_EDIT:
220 case LFUN_INSET_MODIFY:
221 case LFUN_INSET_DIALOG_UPDATE:
226 return InsetBase::getStatus(cur, cmd, flag);
231 void InsetGraphics::edit(LCursor & cur, bool)
233 InsetGraphicsMailer(*this).showDialog(&cur.bv());
237 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
239 graphic_->metrics(mi, dim);
244 void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
246 setPosCache(pi, x, y);
247 graphic_->draw(pi, x, y);
251 InsetBase::EDITABLE InsetGraphics::editable() const
257 void InsetGraphics::write(Buffer const & buf, ostream & os) const
260 params().Write(os, buf.filePath());
264 void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
266 string const token = lex.getString();
268 if (token == "Graphics")
269 readInsetGraphics(lex, buf.filePath());
271 lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
273 graphic_->update(params().as_grfxParams());
277 void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
279 bool finished = false;
281 while (lex.isOK() && !finished) {
284 string const token = lex.getString();
285 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
290 } else if (token == "\\end_inset") {
293 if (!params_.Read(lex, token, bufpath))
294 lyxerr << "Unknown token, " << token << ", skipping."
301 string const InsetGraphics::createLatexOptions() const
303 // Calculate the options part of the command, we must do it to a string
304 // stream since we might have a trailing comma that we would like to remove
305 // before writing it to the output stream.
306 ostringstream options;
307 if (!params().bb.empty())
308 options << "bb=" << rtrim(params().bb) << ',';
313 double const scl = convert<double>(params().scale);
314 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
315 if (!float_equal(scl, 100.0, 0.05))
316 options << "scale=" << scl / 100.0 << ',';
318 if (!params().width.zero())
319 options << "width=" << params().width.asLatexString() << ',';
320 if (!params().height.zero())
321 options << "height=" << params().height.asLatexString() << ',';
322 if (params().keepAspectRatio)
323 options << "keepaspectratio,";
326 // Make sure rotation angle is not very close to zero;
327 // a float can be effectively zero but not exactly zero.
328 if (!params().rotateAngle.empty()
329 && !float_equal(convert<double>(params().rotateAngle), 0.0, 0.001)) {
330 options << "angle=" << params().rotateAngle << ',';
331 if (!params().rotateOrigin.empty()) {
332 options << "origin=" << params().rotateOrigin[0];
333 if (contains(params().rotateOrigin,"Top"))
335 else if (contains(params().rotateOrigin,"Bottom"))
337 else if (contains(params().rotateOrigin,"Baseline"))
343 if (!params().special.empty())
344 options << params().special << ',';
346 string opts = options.str();
348 return opts.substr(0, opts.size() - 1);
352 docstring const InsetGraphics::toDocbookLength(LyXLength const & len) const
354 odocstringstream result;
355 switch (len.unit()) {
356 case LyXLength::SP: // Scaled point (65536sp = 1pt) TeX's smallest unit.
357 result << len.value() * 65536.0 * 72 / 72.27 << "pt";
359 case LyXLength::PT: // Point = 1/72.27in = 0.351mm
360 result << len.value() * 72 / 72.27 << "pt";
362 case LyXLength::BP: // Big point (72bp = 1in), also PostScript point
363 result << len.value() << "pt";
365 case LyXLength::DD: // Didot point = 1/72 of a French inch, = 0.376mm
366 result << len.value() * 0.376 << "mm";
368 case LyXLength::MM: // Millimeter = 2.845pt
369 result << len.value() << "mm";
371 case LyXLength::PC: // Pica = 12pt = 4.218mm
372 result << len.value() << "pc";
374 case LyXLength::CC: // Cicero = 12dd = 4.531mm
375 result << len.value() * 4.531 << "mm";
377 case LyXLength::CM: // Centimeter = 10mm = 2.371pc
378 result << len.value() << "cm";
380 case LyXLength::IN: // Inch = 25.4mm = 72.27pt = 6.022pc
381 result << len.value() << "in";
383 case LyXLength::EX: // Height of a small "x" for the current font.
384 // Obviously we have to compromise here. Any better ratio than 1.5 ?
385 result << len.value() / 1.5 << "em";
387 case LyXLength::EM: // Width of capital "M" in current font.
388 result << len.value() << "em";
390 case LyXLength::MU: // Math unit (18mu = 1em) for positioning in math mode
391 result << len.value() * 18 << "em";
393 case LyXLength::PTW: // Percent of TextWidth
394 case LyXLength::PCW: // Percent of ColumnWidth
395 case LyXLength::PPW: // Percent of PageWidth
396 case LyXLength::PLW: // Percent of LineWidth
397 case LyXLength::PTH: // Percent of TextHeight
398 case LyXLength::PPH: // Percent of Paper
399 // Sigh, this will go wrong.
400 result << len.value() << "%";
403 result << len.asDocstring();
409 docstring const InsetGraphics::createDocBookAttributes() const
411 // Calculate the options part of the command, we must do it to a string
412 // stream since we copied the code from createLatexParams() ;-)
414 // FIXME: av: need to translate spec -> Docbook XSL spec (http://www.sagehill.net/docbookxsl/ImageSizing.html)
415 // Right now it only works with my version of db2latex :-)
417 odocstringstream options;
418 double const scl = convert<double>(params().scale);
419 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
420 if (!float_equal(scl, 100.0, 0.05))
421 options << " scale=\""
422 << static_cast<int>( (scl) + 0.5 )
425 if (!params().width.zero()) {
426 options << " width=\"" << toDocbookLength(params().width) << "\" ";
428 if (!params().height.zero()) {
429 options << " depth=\"" << toDocbookLength(params().height) << "\" ";
431 if (params().keepAspectRatio) {
432 // This will be irrelevant unless both width and height are set
433 options << "scalefit=\"1\" ";
438 if (!params().special.empty())
439 options << from_ascii(params().special) << " ";
441 // trailing blanks are ok ...
442 return options.str();
456 std::pair<CopyStatus, string> const
457 copyFileIfNeeded(string const & file_in, string const & file_out)
459 BOOST_ASSERT(absolutePath(file_in));
460 BOOST_ASSERT(absolutePath(file_out));
462 unsigned long const checksum_in = support::sum(file_in);
463 unsigned long const checksum_out = support::sum(file_out);
465 if (checksum_in == checksum_out)
467 return std::make_pair(IDENTICAL_CONTENTS, file_out);
469 Mover const & mover = movers(formats.getFormatFromFile(file_in));
470 bool const success = mover.copy(file_in, file_out);
473 lyxerr[Debug::GRAPHICS]
474 << to_utf8(support::bformat(_("Could not copy the file\n%1$s\n"
475 "into the temporary directory."),
480 CopyStatus status = success ? SUCCESS : FAILURE;
481 return std::make_pair(status, file_out);
485 std::pair<CopyStatus, string> const
486 copyToDirIfNeeded(FileName const & file, string const & dir)
488 using support::rtrim;
490 string const file_in = file.absFilename();
491 string const only_path = support::onlyPath(file_in);
492 if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/"))
493 return std::make_pair(IDENTICAL_PATHS, file_in);
495 string mangled = file.mangledFilename();
496 if (file.isZipped()) {
497 // We need to change _eps.gz to .eps.gz. The mangled name is
498 // still unique because of the counter in mangledFilename().
499 // We can't just call mangledFilename() with the zip
500 // extension removed, because base.eps and base.eps.gz may
501 // have different content but would get the same mangled
502 // name in this case.
503 string const base = removeExtension(file.unzippedFilename());
504 string::size_type const ext_len = file_in.length() - base.length();
505 mangled[mangled.length() - ext_len] = '.';
507 string const file_out = support::makeAbsPath(mangled, dir);
509 return copyFileIfNeeded(file_in, file_out);
513 string const stripExtensionIfPossible(string const & file)
515 // Remove the extension so the LaTeX will use whatever
516 // is appropriate (when there are several versions in
517 // different formats)
518 // This works only if the filename contains no dots besides
519 // the just removed one. We can fool here by replacing all
520 // dots with a macro whose definition is just a dot ;-)
521 // The automatic format selection does not work if the file
523 string const latex_name = latex_path(file,
524 support::EXCLUDE_EXTENSION);
525 if (contains(latex_name, '"'))
527 return latex_path(removeExtension(file),
528 support::PROTECT_EXTENSION,
529 support::ESCAPE_DOTS);
533 string const stripExtensionIfPossible(string const & file, string const & to)
535 // No conversion is needed. LaTeX can handle the graphic file as is.
536 // This is true even if the orig_file is compressed.
537 string const to_format = formats.getFormat(to)->extension();
538 string const file_format = getExtension(file);
539 // for latex .ps == .eps
540 if (to_format == file_format ||
541 (to_format == "eps" && file_format == "ps") ||
542 (to_format == "ps" && file_format == "eps"))
543 return stripExtensionIfPossible(file);
544 return latex_path(file, support::EXCLUDE_EXTENSION);
550 string const InsetGraphics::prepareFile(Buffer const & buf,
551 OutputParams const & runparams) const
553 // The following code depends on non-empty filenames
554 if (params().filename.empty())
557 string const orig_file = params().filename.absFilename();
558 string const rel_file = params().filename.relFilename(buf.filePath());
560 // previewing source code, no file copying or file format conversion
561 if (runparams.dryrun)
562 return stripExtensionIfPossible(rel_file);
564 // temp_file will contain the file for LaTeX to act on if, for example,
565 // we move it to a temp dir or uncompress it.
566 string temp_file = orig_file;
568 // The master buffer. This is useful when there are multiple levels
570 Buffer const * m_buffer = buf.getMasterBuffer();
572 // Return the output name if we are inside a comment or the file does
574 // We are not going to change the extension or using the name of the
575 // temporary file, the code is already complicated enough.
576 if (runparams.inComment || !isFileReadable(orig_file))
577 return params().filename.outputFilename(m_buffer->filePath());
579 // We place all temporary files in the master buffer's temp dir.
580 // This is possible because we use mangled file names.
581 // This is necessary for DVI export.
582 string const temp_path = m_buffer->temppath();
585 boost::tie(status, temp_file) =
586 copyToDirIfNeeded(params().filename, temp_path);
588 if (status == FAILURE)
591 // a relative filename should be relative to the master
593 // "nice" means that the buffer is exported to LaTeX format but not
594 // run through the LaTeX compiler.
595 string output_file = support::os::external_path(runparams.nice ?
596 params().filename.outputFilename(m_buffer->filePath()) :
597 onlyFilename(temp_file));
598 string source_file = runparams.nice ? orig_file : temp_file;
599 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
600 "latex" : "pdflatex";
602 // If the file is compressed and we have specified that it
603 // should not be uncompressed, then just return its name and
604 // let LaTeX do the rest!
605 if (params().filename.isZipped()) {
606 if (params().noUnzip) {
607 // We don't know whether latex can actually handle
608 // this file, but we can't check, because that would
609 // mean to unzip the file and thereby making the
610 // noUnzip parameter meaningless.
611 lyxerr[Debug::GRAPHICS]
612 << "\tpass zipped file to LaTeX.\n";
614 string const bb_orig_file = changeExtension(orig_file, "bb");
615 if (runparams.nice) {
616 runparams.exportdata->addExternalFile(tex_format,
618 changeExtension(output_file, "bb"));
620 // LaTeX needs the bounding box file in the
622 string bb_file = changeExtension(temp_file, "bb");
623 boost::tie(status, bb_file) =
624 copyFileIfNeeded(bb_orig_file, bb_file);
625 if (status == FAILURE)
627 runparams.exportdata->addExternalFile(tex_format,
630 runparams.exportdata->addExternalFile(tex_format,
631 source_file, output_file);
632 runparams.exportdata->addExternalFile("dvi",
633 source_file, output_file);
634 // We can't strip the extension, because we don't know
635 // the unzipped file format
636 return latex_path(output_file,
637 support::EXCLUDE_EXTENSION);
640 string const unzipped_temp_file = unzippedFileName(temp_file);
641 output_file = unzippedFileName(output_file);
642 source_file = unzippedFileName(source_file);
643 if (compare_timestamps(unzipped_temp_file, temp_file) > 0) {
644 // temp_file has been unzipped already and
645 // orig_file has not changed in the meantime.
646 temp_file = unzipped_temp_file;
647 lyxerr[Debug::GRAPHICS]
648 << "\twas already unzipped to " << temp_file
651 // unzipped_temp_file does not exist or is too old
652 temp_file = unzipFile(temp_file);
653 lyxerr[Debug::GRAPHICS]
654 << "\tunzipped to " << temp_file << endl;
658 string const from = formats.getFormatFromFile(temp_file);
660 lyxerr[Debug::GRAPHICS]
661 << "\tCould not get file format." << endl;
663 string const to = findTargetFormat(from, runparams);
664 string const ext = formats.extension(to);
665 lyxerr[Debug::GRAPHICS]
666 << "\t we have: from " << from << " to " << to << '\n';
668 // We're going to be running the exported buffer through the LaTeX
669 // compiler, so must ensure that LaTeX can cope with the graphics
672 lyxerr[Debug::GRAPHICS]
673 << "\tthe orig file is: " << orig_file << endl;
676 if (!runparams.nice && getExtension(temp_file) != ext) {
677 // The LaTeX compiler will not be able to determine
678 // the file format from the extension, so we must
680 string const new_file = changeExtension(temp_file, ext);
681 if (support::rename(temp_file, new_file)) {
682 temp_file = new_file;
683 output_file = changeExtension(output_file, ext);
684 source_file = changeExtension(source_file, ext);
686 lyxerr[Debug::GRAPHICS]
687 << "Could not rename file `"
688 << temp_file << "' to `" << new_file
691 // The extension of temp_file might be != ext!
692 runparams.exportdata->addExternalFile(tex_format, source_file,
694 runparams.exportdata->addExternalFile("dvi", source_file,
696 return stripExtensionIfPossible(output_file, to);
699 string const to_file = changeExtension(temp_file, ext);
700 string const output_to_file = changeExtension(output_file, ext);
702 // Do we need to perform the conversion?
703 // Yes if to_file does not exist or if temp_file is newer than to_file
704 if (compare_timestamps(temp_file, to_file) < 0) {
706 lyxerr[Debug::GRAPHICS]
707 << to_utf8(bformat(_("No conversion of %1$s is needed after all"),
708 from_utf8(rel_file)))
710 runparams.exportdata->addExternalFile(tex_format, to_file,
712 runparams.exportdata->addExternalFile("dvi", to_file,
714 return stripExtensionIfPossible(output_file);
717 lyxerr[Debug::GRAPHICS]
718 << "\tThe original file is " << orig_file << "\n"
719 << "\tA copy has been made and convert is to be called with:\n"
720 << "\tfile to convert = " << temp_file << '\n'
721 << "\t from " << from << " to " << to << '\n';
723 // FIXME (Abdel 12/08/06): Is there a need to show these errors?
725 if (converters.convert(&buf, temp_file, temp_file, from, to, el, true)) {
726 runparams.exportdata->addExternalFile(tex_format,
727 to_file, output_to_file);
728 runparams.exportdata->addExternalFile("dvi",
729 to_file, output_to_file);
732 return stripExtensionIfPossible(output_file);
736 int InsetGraphics::latex(Buffer const & buf, odocstream & os,
737 OutputParams const & runparams) const
739 // If there is no file specified or not existing,
740 // just output a message about it in the latex output.
741 lyxerr[Debug::GRAPHICS]
742 << "insetgraphics::latex: Filename = "
743 << params().filename.absFilename() << endl;
745 string const relative_file =
746 params().filename.relFilename(buf.filePath());
748 string const file_ = params().filename.absFilename();
749 bool const file_exists = !file_.empty() && isFileReadable(file_);
750 string const message = file_exists ?
751 string() : string("bb = 0 0 200 100, draft, type=eps");
752 // if !message.empty() then there was no existing file
753 // "filename" found. In this case LaTeX
754 // draws only a rectangle with the above bb and the
755 // not found filename in it.
756 lyxerr[Debug::GRAPHICS]
757 << "\tMessage = \"" << message << '\"' << endl;
759 // These variables collect all the latex code that should be before and
760 // after the actual includegraphics command.
763 // Do we want subcaptions?
764 if (params().subcaption) {
765 if (runparams.moving_arg)
766 before += "\\protect";
767 before += "\\subfigure[" + params().subcaptionText + "]{";
771 if (runparams.moving_arg)
772 before += "\\protect";
774 // We never use the starred form, we use the "clip" option instead.
775 before += "\\includegraphics";
777 // Write the options if there are any.
778 string const opts = createLatexOptions();
779 lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
781 if (!opts.empty() && !message.empty())
782 before += ('[' + opts + ',' + message + ']');
783 else if (!opts.empty() || !message.empty())
784 before += ('[' + opts + message + ']');
786 lyxerr[Debug::GRAPHICS]
787 << "\tBefore = " << before
788 << "\n\tafter = " << after << endl;
790 string latex_str = before + '{';
791 // Convert the file if necessary.
792 // Remove the extension so LaTeX will use whatever is appropriate
793 // (when there are several versions in different formats)
794 latex_str += prepareFile(buf, runparams);
795 latex_str += '}' + after;
797 os << from_utf8(latex_str);
799 lyxerr[Debug::GRAPHICS] << "InsetGraphics::latex outputting:\n"
800 << latex_str << endl;
801 // Return how many newlines we issued.
802 return int(lyx::count(latex_str.begin(), latex_str.end(),'\n'));
806 int InsetGraphics::plaintext(Buffer const &, odocstream & os,
807 OutputParams const &) const
809 // No graphics in ascii output. Possible to use gifscii to convert
810 // images to ascii approximation.
811 // 1. Convert file to ascii using gifscii
812 // 2. Read ascii output file and add it to the output stream.
813 // at least we send the filename
815 // FIXME: We have no idea what the encoding of the filename is
816 os << '<' << bformat(_("Graphics file: %1$s"),
817 from_utf8(params().filename.absFilename()))
825 int writeImageObject(char * format, odocstream & os, OutputParams const & runparams,
826 docstring const graphic_label, docstring const attributes)
828 if (runparams.flavor != OutputParams::XML) {
829 os << "<![ %output.print."
834 os <<"<imageobject><imagedata fileref=\"&"
840 if (runparams.flavor == OutputParams::XML) {
841 os << " role=\"" << format << "\"/>" ;
844 os << " format=\"" << format << "\">" ;
846 os << "</imageobject>";
847 if (runparams.flavor != OutputParams::XML) {
848 os << std::endl << "]]>" ;
850 return runparams.flavor == OutputParams::XML ? 0 : 2;
852 // end anonymous namespace
856 // For explanation on inserting graphics into DocBook checkout:
857 // http://en.tldp.org/LDP/LDP-Author-Guide/html/inserting-pictures.html
858 // See also the docbook guide at http://www.docbook.org/
859 int InsetGraphics::docbook(Buffer const &, odocstream & os,
860 OutputParams const & runparams) const
862 // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
863 // need to switch to MediaObject. However, for now this is sufficient and
865 if (runparams.flavor == OutputParams::XML) {
866 runparams.exportdata->addExternalFile("docbook-xml",
867 params().filename.absFilename());
869 runparams.exportdata->addExternalFile("docbook",
870 params().filename.absFilename());
872 os << "<inlinemediaobject>";
875 docstring attributes = createDocBookAttributes();
876 r += writeImageObject("png", os, runparams, graphic_label, attributes);
877 r += writeImageObject("pdf", os, runparams, graphic_label, attributes);
878 r += writeImageObject("eps", os, runparams, graphic_label, attributes);
879 r += writeImageObject("bmp", os, runparams, graphic_label, attributes);
881 os << "</inlinemediaobject>";
886 void InsetGraphics::validate(LaTeXFeatures & features) const
888 // If we have no image, we should not require anything.
889 if (params().filename.empty())
892 features.includeFile(graphic_label,
893 removeExtension(params().filename.absFilename()));
895 features.require("graphicx");
897 if (features.runparams().nice) {
898 Buffer const * m_buffer = features.buffer().getMasterBuffer();
899 string const rel_file = removeExtension(params().filename.relFilename(m_buffer->filePath()));
900 if (contains(rel_file, "."))
901 features.require("lyxdot");
904 if (params().subcaption)
905 features.require("subfigure");
909 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
911 // If nothing is changed, just return and say so.
912 if (params() == p && !p.filename.empty())
915 // Copy the new parameters.
918 // Update the display using the new parameters.
919 graphic_->update(params().as_grfxParams());
921 // We have changed data, report it.
926 InsetGraphicsParams const & InsetGraphics::params() const
932 void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
933 Buffer const & buffer) const
935 string const file_with_path = p.filename.absFilename();
936 formats.edit(buffer, file_with_path,
937 formats.getFormatFromFile(file_with_path));
941 string const InsetGraphicsMailer::name_("graphics");
943 InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)
948 string const InsetGraphicsMailer::inset2string(Buffer const & buffer) const
950 return params2string(inset_.params(), buffer);
954 void InsetGraphicsMailer::string2params(string const & in,
955 Buffer const & buffer,
956 InsetGraphicsParams & params)
958 params = InsetGraphicsParams();
962 istringstream data(in);
968 if (!lex || name != name_)
969 return print_mailer_error("InsetGraphicsMailer", in, 1, name_);
972 inset.readInsetGraphics(lex, buffer.filePath());
973 params = inset.params();
978 InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
979 Buffer const & buffer)
982 data << name_ << ' ';
983 params.Write(data, buffer.filePath());
984 data << "\\end_inset\n";