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::bformat;
94 using support::changeExtension;
95 using support::compare_timestamps;
96 using support::contains;
97 using support::DocFileName;
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 bool InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
239 graphic_->metrics(mi, dim);
240 bool const changed = dim_ != dim;
246 void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
248 setPosCache(pi, x, y);
249 graphic_->draw(pi, x, y);
253 InsetBase::EDITABLE InsetGraphics::editable() const
259 void InsetGraphics::write(Buffer const & buf, ostream & os) const
262 params().Write(os, buf.filePath());
266 void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
268 string const token = lex.getString();
270 if (token == "Graphics")
271 readInsetGraphics(lex, buf.filePath());
273 lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
275 graphic_->update(params().as_grfxParams());
279 void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
281 bool finished = false;
283 while (lex.isOK() && !finished) {
286 string const token = lex.getString();
287 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
292 } else if (token == "\\end_inset") {
295 if (!params_.Read(lex, token, bufpath))
296 lyxerr << "Unknown token, " << token << ", skipping."
303 string const InsetGraphics::createLatexOptions() const
305 // Calculate the options part of the command, we must do it to a string
306 // stream since we might have a trailing comma that we would like to remove
307 // before writing it to the output stream.
308 ostringstream options;
309 if (!params().bb.empty())
310 options << "bb=" << rtrim(params().bb) << ',';
315 double const scl = convert<double>(params().scale);
316 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
317 if (!float_equal(scl, 100.0, 0.05))
318 options << "scale=" << scl / 100.0 << ',';
320 if (!params().width.zero())
321 options << "width=" << params().width.asLatexString() << ',';
322 if (!params().height.zero())
323 options << "height=" << params().height.asLatexString() << ',';
324 if (params().keepAspectRatio)
325 options << "keepaspectratio,";
328 // Make sure rotation angle is not very close to zero;
329 // a float can be effectively zero but not exactly zero.
330 if (!params().rotateAngle.empty()
331 && !float_equal(convert<double>(params().rotateAngle), 0.0, 0.001)) {
332 options << "angle=" << params().rotateAngle << ',';
333 if (!params().rotateOrigin.empty()) {
334 options << "origin=" << params().rotateOrigin[0];
335 if (contains(params().rotateOrigin,"Top"))
337 else if (contains(params().rotateOrigin,"Bottom"))
339 else if (contains(params().rotateOrigin,"Baseline"))
345 if (!params().special.empty())
346 options << params().special << ',';
348 string opts = options.str();
350 return opts.substr(0, opts.size() - 1);
354 docstring const InsetGraphics::toDocbookLength(LyXLength const & len) const
356 odocstringstream result;
357 switch (len.unit()) {
358 case LyXLength::SP: // Scaled point (65536sp = 1pt) TeX's smallest unit.
359 result << len.value() * 65536.0 * 72 / 72.27 << "pt";
361 case LyXLength::PT: // Point = 1/72.27in = 0.351mm
362 result << len.value() * 72 / 72.27 << "pt";
364 case LyXLength::BP: // Big point (72bp = 1in), also PostScript point
365 result << len.value() << "pt";
367 case LyXLength::DD: // Didot point = 1/72 of a French inch, = 0.376mm
368 result << len.value() * 0.376 << "mm";
370 case LyXLength::MM: // Millimeter = 2.845pt
371 result << len.value() << "mm";
373 case LyXLength::PC: // Pica = 12pt = 4.218mm
374 result << len.value() << "pc";
376 case LyXLength::CC: // Cicero = 12dd = 4.531mm
377 result << len.value() * 4.531 << "mm";
379 case LyXLength::CM: // Centimeter = 10mm = 2.371pc
380 result << len.value() << "cm";
382 case LyXLength::IN: // Inch = 25.4mm = 72.27pt = 6.022pc
383 result << len.value() << "in";
385 case LyXLength::EX: // Height of a small "x" for the current font.
386 // Obviously we have to compromise here. Any better ratio than 1.5 ?
387 result << len.value() / 1.5 << "em";
389 case LyXLength::EM: // Width of capital "M" in current font.
390 result << len.value() << "em";
392 case LyXLength::MU: // Math unit (18mu = 1em) for positioning in math mode
393 result << len.value() * 18 << "em";
395 case LyXLength::PTW: // Percent of TextWidth
396 case LyXLength::PCW: // Percent of ColumnWidth
397 case LyXLength::PPW: // Percent of PageWidth
398 case LyXLength::PLW: // Percent of LineWidth
399 case LyXLength::PTH: // Percent of TextHeight
400 case LyXLength::PPH: // Percent of Paper
401 // Sigh, this will go wrong.
402 result << len.value() << "%";
405 result << len.asDocstring();
411 docstring const InsetGraphics::createDocBookAttributes() const
413 // Calculate the options part of the command, we must do it to a string
414 // stream since we copied the code from createLatexParams() ;-)
416 // FIXME: av: need to translate spec -> Docbook XSL spec (http://www.sagehill.net/docbookxsl/ImageSizing.html)
417 // Right now it only works with my version of db2latex :-)
419 odocstringstream options;
420 double const scl = convert<double>(params().scale);
421 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
422 if (!float_equal(scl, 100.0, 0.05))
423 options << " scale=\""
424 << static_cast<int>( (scl) + 0.5 )
427 if (!params().width.zero()) {
428 options << " width=\"" << toDocbookLength(params().width) << "\" ";
430 if (!params().height.zero()) {
431 options << " depth=\"" << toDocbookLength(params().height) << "\" ";
433 if (params().keepAspectRatio) {
434 // This will be irrelevant unless both width and height are set
435 options << "scalefit=\"1\" ";
440 if (!params().special.empty())
441 options << from_ascii(params().special) << " ";
443 // trailing blanks are ok ...
444 return options.str();
458 std::pair<CopyStatus, FileName> const
459 copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
461 unsigned long const checksum_in = support::sum(file_in);
462 unsigned long const checksum_out = support::sum(file_out);
464 if (checksum_in == checksum_out)
466 return std::make_pair(IDENTICAL_CONTENTS, file_out);
468 Mover const & mover = movers(formats.getFormatFromFile(file_in));
469 bool const success = mover.copy(file_in, file_out);
472 lyxerr[Debug::GRAPHICS]
473 << to_utf8(support::bformat(_("Could not copy the file\n%1$s\n"
474 "into the temporary directory."),
475 from_utf8(file_in.absFilename())))
479 CopyStatus status = success ? SUCCESS : FAILURE;
480 return std::make_pair(status, file_out);
484 std::pair<CopyStatus, FileName> const
485 copyToDirIfNeeded(DocFileName const & file, string const & dir)
487 using support::rtrim;
489 string const file_in = file.absFilename();
490 string const only_path = support::onlyPath(file_in);
491 if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/"))
492 return std::make_pair(IDENTICAL_PATHS, file_in);
494 string mangled = file.mangledFilename();
495 if (file.isZipped()) {
496 // We need to change _eps.gz to .eps.gz. The mangled name is
497 // still unique because of the counter in mangledFilename().
498 // We can't just call mangledFilename() with the zip
499 // extension removed, because base.eps and base.eps.gz may
500 // have different content but would get the same mangled
501 // name in this case.
502 string const base = removeExtension(file.unzippedFilename());
503 string::size_type const ext_len = file_in.length() - base.length();
504 mangled[mangled.length() - ext_len] = '.';
506 string const file_out = support::makeAbsPath(mangled, dir);
508 return copyFileIfNeeded(file, FileName(file_out));
512 string const stripExtensionIfPossible(string const & file)
514 // Remove the extension so the LaTeX will use whatever
515 // is appropriate (when there are several versions in
516 // different formats)
517 // This works only if the filename contains no dots besides
518 // the just removed one. We can fool here by replacing all
519 // dots with a macro whose definition is just a dot ;-)
520 // The automatic format selection does not work if the file
522 string const latex_name = latex_path(file,
523 support::EXCLUDE_EXTENSION);
524 if (contains(latex_name, '"'))
526 return latex_path(removeExtension(file),
527 support::PROTECT_EXTENSION,
528 support::ESCAPE_DOTS);
532 string const stripExtensionIfPossible(string const & file, string const & to)
534 // No conversion is needed. LaTeX can handle the graphic file as is.
535 // This is true even if the orig_file is compressed.
536 string const to_format = formats.getFormat(to)->extension();
537 string const file_format = getExtension(file);
538 // for latex .ps == .eps
539 if (to_format == file_format ||
540 (to_format == "eps" && file_format == "ps") ||
541 (to_format == "ps" && file_format == "eps"))
542 return stripExtensionIfPossible(file);
543 return latex_path(file, support::EXCLUDE_EXTENSION);
549 string const InsetGraphics::prepareFile(Buffer const & buf,
550 OutputParams const & runparams) const
552 // The following code depends on non-empty filenames
553 if (params().filename.empty())
556 string const orig_file = params().filename.absFilename();
557 string const rel_file = params().filename.relFilename(buf.filePath());
559 // previewing source code, no file copying or file format conversion
560 if (runparams.dryrun)
561 return stripExtensionIfPossible(rel_file);
563 // temp_file will contain the file for LaTeX to act on if, for example,
564 // we move it to a temp dir or uncompress it.
565 FileName temp_file = params().filename;
567 // The master buffer. This is useful when there are multiple levels
569 Buffer const * m_buffer = buf.getMasterBuffer();
571 // Return the output name if we are inside a comment or the file does
573 // We are not going to change the extension or using the name of the
574 // temporary file, the code is already complicated enough.
575 if (runparams.inComment || !isFileReadable(params().filename))
576 return params().filename.outputFilename(m_buffer->filePath());
578 // We place all temporary files in the master buffer's temp dir.
579 // This is possible because we use mangled file names.
580 // This is necessary for DVI export.
581 string const temp_path = m_buffer->temppath();
584 boost::tie(status, temp_file) =
585 copyToDirIfNeeded(params().filename, temp_path);
587 if (status == FAILURE)
590 // a relative filename should be relative to the master
592 // "nice" means that the buffer is exported to LaTeX format but not
593 // run through the LaTeX compiler.
594 string output_file = support::os::external_path(runparams.nice ?
595 params().filename.outputFilename(m_buffer->filePath()) :
596 onlyFilename(temp_file.absFilename()));
597 FileName source_file = runparams.nice ? FileName(params().filename) : temp_file;
598 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
599 "latex" : "pdflatex";
601 // If the file is compressed and we have specified that it
602 // should not be uncompressed, then just return its name and
603 // let LaTeX do the rest!
604 if (params().filename.isZipped()) {
605 if (params().noUnzip) {
606 // We don't know whether latex can actually handle
607 // this file, but we can't check, because that would
608 // mean to unzip the file and thereby making the
609 // noUnzip parameter meaningless.
610 lyxerr[Debug::GRAPHICS]
611 << "\tpass zipped file to LaTeX.\n";
613 FileName const bb_orig_file = FileName(changeExtension(orig_file, "bb"));
614 if (runparams.nice) {
615 runparams.exportdata->addExternalFile(tex_format,
617 changeExtension(output_file, "bb"));
619 // LaTeX needs the bounding box file in the
621 FileName bb_file = FileName(changeExtension(temp_file.absFilename(), "bb"));
622 boost::tie(status, bb_file) =
623 copyFileIfNeeded(bb_orig_file, bb_file);
624 if (status == FAILURE)
626 runparams.exportdata->addExternalFile(tex_format,
629 runparams.exportdata->addExternalFile(tex_format,
630 source_file, output_file);
631 runparams.exportdata->addExternalFile("dvi",
632 source_file, output_file);
633 // We can't strip the extension, because we don't know
634 // the unzipped file format
635 return latex_path(output_file,
636 support::EXCLUDE_EXTENSION);
639 FileName const unzipped_temp_file =
640 FileName(unzippedFileName(temp_file.absFilename()));
641 output_file = unzippedFileName(output_file);
642 source_file = FileName(unzippedFileName(source_file.absFilename()));
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.absFilename()) != ext) {
677 // The LaTeX compiler will not be able to determine
678 // the file format from the extension, so we must
680 FileName const new_file = FileName(changeExtension(temp_file.absFilename(), ext));
681 if (support::rename(temp_file, new_file)) {
682 temp_file = new_file;
683 output_file = changeExtension(output_file, ext);
684 source_file = FileName(changeExtension(source_file.absFilename(), 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 FileName const to_file = FileName(changeExtension(temp_file.absFilename(), 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, to_file, params().filename,
727 Converters::try_default | Converters::try_cache)) {
728 runparams.exportdata->addExternalFile(tex_format,
729 to_file, output_to_file);
730 runparams.exportdata->addExternalFile("dvi",
731 to_file, output_to_file);
734 return stripExtensionIfPossible(output_file);
738 int InsetGraphics::latex(Buffer const & buf, odocstream & os,
739 OutputParams const & runparams) const
741 // If there is no file specified or not existing,
742 // just output a message about it in the latex output.
743 lyxerr[Debug::GRAPHICS]
744 << "insetgraphics::latex: Filename = "
745 << params().filename.absFilename() << endl;
747 string const relative_file =
748 params().filename.relFilename(buf.filePath());
750 bool const file_exists = !params().filename.empty() &&
751 isFileReadable(params().filename);
752 string const message = file_exists ?
753 string() : string("bb = 0 0 200 100, draft, type=eps");
754 // if !message.empty() then there was no existing file
755 // "filename" found. In this case LaTeX
756 // draws only a rectangle with the above bb and the
757 // not found filename in it.
758 lyxerr[Debug::GRAPHICS]
759 << "\tMessage = \"" << message << '\"' << endl;
761 // These variables collect all the latex code that should be before and
762 // after the actual includegraphics command.
765 // Do we want subcaptions?
766 if (params().subcaption) {
767 if (runparams.moving_arg)
768 before += "\\protect";
769 before += "\\subfigure[" + params().subcaptionText + "]{";
773 if (runparams.moving_arg)
774 before += "\\protect";
776 // We never use the starred form, we use the "clip" option instead.
777 before += "\\includegraphics";
779 // Write the options if there are any.
780 string const opts = createLatexOptions();
781 lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
783 if (!opts.empty() && !message.empty())
784 before += ('[' + opts + ',' + message + ']');
785 else if (!opts.empty() || !message.empty())
786 before += ('[' + opts + message + ']');
788 lyxerr[Debug::GRAPHICS]
789 << "\tBefore = " << before
790 << "\n\tafter = " << after << endl;
792 string latex_str = before + '{';
793 // Convert the file if necessary.
794 // Remove the extension so LaTeX will use whatever is appropriate
795 // (when there are several versions in different formats)
796 latex_str += prepareFile(buf, runparams);
797 latex_str += '}' + after;
799 os << from_utf8(latex_str);
801 lyxerr[Debug::GRAPHICS] << "InsetGraphics::latex outputting:\n"
802 << latex_str << endl;
803 // Return how many newlines we issued.
804 return int(lyx::count(latex_str.begin(), latex_str.end(),'\n'));
808 int InsetGraphics::plaintext(Buffer const &, odocstream & os,
809 OutputParams const &) const
811 // No graphics in ascii output. Possible to use gifscii to convert
812 // images to ascii approximation.
813 // 1. Convert file to ascii using gifscii
814 // 2. Read ascii output file and add it to the output stream.
815 // at least we send the filename
817 // FIXME: We have no idea what the encoding of the filename is
818 os << '<' << bformat(_("Graphics file: %1$s"),
819 from_utf8(params().filename.absFilename()))
827 int writeImageObject(char * format, odocstream & os, OutputParams const & runparams,
828 docstring const graphic_label, docstring const attributes)
830 if (runparams.flavor != OutputParams::XML) {
831 os << "<![ %output.print."
836 os <<"<imageobject><imagedata fileref=\"&"
842 if (runparams.flavor == OutputParams::XML) {
843 os << " role=\"" << format << "\"/>" ;
846 os << " format=\"" << format << "\">" ;
848 os << "</imageobject>";
849 if (runparams.flavor != OutputParams::XML) {
850 os << std::endl << "]]>" ;
852 return runparams.flavor == OutputParams::XML ? 0 : 2;
854 // end anonymous namespace
858 // For explanation on inserting graphics into DocBook checkout:
859 // http://en.tldp.org/LDP/LDP-Author-Guide/html/inserting-pictures.html
860 // See also the docbook guide at http://www.docbook.org/
861 int InsetGraphics::docbook(Buffer const &, odocstream & os,
862 OutputParams const & runparams) const
864 // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
865 // need to switch to MediaObject. However, for now this is sufficient and
867 if (runparams.flavor == OutputParams::XML) {
868 runparams.exportdata->addExternalFile("docbook-xml",
871 runparams.exportdata->addExternalFile("docbook",
874 os << "<inlinemediaobject>";
877 docstring attributes = createDocBookAttributes();
878 r += writeImageObject("png", os, runparams, graphic_label, attributes);
879 r += writeImageObject("pdf", os, runparams, graphic_label, attributes);
880 r += writeImageObject("eps", os, runparams, graphic_label, attributes);
881 r += writeImageObject("bmp", os, runparams, graphic_label, attributes);
883 os << "</inlinemediaobject>";
888 void InsetGraphics::validate(LaTeXFeatures & features) const
890 // If we have no image, we should not require anything.
891 if (params().filename.empty())
894 features.includeFile(graphic_label,
895 removeExtension(params().filename.absFilename()));
897 features.require("graphicx");
899 if (features.runparams().nice) {
900 Buffer const * m_buffer = features.buffer().getMasterBuffer();
901 string const rel_file = removeExtension(params().filename.relFilename(m_buffer->filePath()));
902 if (contains(rel_file, "."))
903 features.require("lyxdot");
906 if (params().subcaption)
907 features.require("subfigure");
911 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
913 // If nothing is changed, just return and say so.
914 if (params() == p && !p.filename.empty())
917 // Copy the new parameters.
920 // Update the display using the new parameters.
921 graphic_->update(params().as_grfxParams());
923 // We have changed data, report it.
928 InsetGraphicsParams const & InsetGraphics::params() const
934 void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
935 Buffer const & buffer) const
937 formats.edit(buffer, p.filename,
938 formats.getFormatFromFile(p.filename));
942 string const InsetGraphicsMailer::name_("graphics");
944 InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)
949 string const InsetGraphicsMailer::inset2string(Buffer const & buffer) const
951 return params2string(inset_.params(), buffer);
955 void InsetGraphicsMailer::string2params(string const & in,
956 Buffer const & buffer,
957 InsetGraphicsParams & params)
959 params = InsetGraphicsParams();
963 istringstream data(in);
969 if (!lex || name != name_)
970 return print_mailer_error("InsetGraphicsMailer", in, 1, name_);
973 inset.readInsetGraphics(lex, buffer.filePath());
974 params = inset.params();
979 InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
980 Buffer const & buffer)
983 data << name_ << ' ';
984 params.Write(data, buffer.filePath());
985 data << "\\end_inset\n";