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" // lyx::count
80 #include "support/lyxlib.h" // lyx::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>
90 using lyx::odocstream;
92 namespace support = lyx::support;
94 using lyx::support::absolutePath;
95 using lyx::support::bformat;
96 using lyx::support::changeExtension;
97 using lyx::support::compare_timestamps;
98 using lyx::support::contains;
99 using lyx::support::FileName;
100 using lyx::support::float_equal;
101 using lyx::support::getExtension;
102 using lyx::support::isFileReadable;
103 using lyx::support::latex_path;
104 using lyx::support::onlyFilename;
105 using lyx::support::removeExtension;
106 using lyx::support::rtrim;
107 using lyx::support::subst;
108 using lyx::support::Systemcall;
109 using lyx::support::unzipFile;
110 using lyx::support::unzippedFileName;
112 namespace os = lyx::support::os;
117 using std::istringstream;
119 using std::ostringstream;
124 /// Find the most suitable image format for images in \p format
125 /// Note that \p format may be unknown (i. e. an empty string)
126 string findTargetFormat(string const & format, OutputParams const & runparams)
128 // Are we using latex or pdflatex?
129 if (runparams.flavor == OutputParams::PDFLATEX) {
130 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
131 Format const * const f = formats.getFormat(format);
132 // Convert vector graphics to pdf
133 if (f && f->vectorFormat())
135 // pdflatex can use jpeg, png and pdf directly
138 // Convert everything else to png
141 // If it's postscript, we always do eps.
142 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
144 // any other than ps is changed to eps
153 InsetGraphics::InsetGraphics()
154 : graphic_label(sgml::uniqueID("graph")),
155 graphic_(new RenderGraphic(this))
159 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
161 boost::signals::trackable(),
162 graphic_label(sgml::uniqueID("graph")),
163 graphic_(new RenderGraphic(*ig.graphic_, this))
165 setParams(ig.params());
169 auto_ptr<InsetBase> InsetGraphics::doClone() const
171 return auto_ptr<InsetBase>(new InsetGraphics(*this));
175 InsetGraphics::~InsetGraphics()
177 InsetGraphicsMailer(*this).hideDialog();
181 void InsetGraphics::doDispatch(LCursor & cur, FuncRequest & cmd)
183 switch (cmd.action) {
184 case LFUN_GRAPHICS_EDIT: {
185 Buffer const & buffer = *cur.bv().buffer();
186 InsetGraphicsParams p;
187 InsetGraphicsMailer::string2params(lyx::to_utf8(cmd.argument()), buffer, p);
188 editGraphics(p, buffer);
192 case LFUN_INSET_MODIFY: {
193 Buffer const & buffer = cur.buffer();
194 InsetGraphicsParams p;
195 InsetGraphicsMailer::string2params(lyx::to_utf8(cmd.argument()), buffer, p);
196 if (!p.filename.empty())
203 case LFUN_INSET_DIALOG_UPDATE:
204 InsetGraphicsMailer(*this).updateDialog(&cur.bv());
207 case LFUN_MOUSE_RELEASE:
208 InsetGraphicsMailer(*this).showDialog(&cur.bv());
212 InsetBase::doDispatch(cur, cmd);
218 bool InsetGraphics::getStatus(LCursor & cur, FuncRequest const & cmd,
219 FuncStatus & flag) const
221 switch (cmd.action) {
222 case LFUN_GRAPHICS_EDIT:
223 case LFUN_INSET_MODIFY:
224 case LFUN_INSET_DIALOG_UPDATE:
229 return InsetBase::getStatus(cur, cmd, flag);
234 void InsetGraphics::edit(LCursor & cur, bool)
236 InsetGraphicsMailer(*this).showDialog(&cur.bv());
240 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
242 graphic_->metrics(mi, dim);
247 void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
249 setPosCache(pi, x, y);
250 graphic_->draw(pi, x, y);
254 InsetBase::EDITABLE InsetGraphics::editable() const
260 void InsetGraphics::write(Buffer const & buf, ostream & os) const
263 params().Write(os, buf.filePath());
267 void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
269 string const token = lex.getString();
271 if (token == "Graphics")
272 readInsetGraphics(lex, buf.filePath());
274 lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
276 graphic_->update(params().as_grfxParams());
280 void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
282 bool finished = false;
284 while (lex.isOK() && !finished) {
287 string const token = lex.getString();
288 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
293 } else if (token == "\\end_inset") {
296 if (!params_.Read(lex, token, bufpath))
297 lyxerr << "Unknown token, " << token << ", skipping."
304 string const InsetGraphics::createLatexOptions() const
306 // Calculate the options part of the command, we must do it to a string
307 // stream since we might have a trailing comma that we would like to remove
308 // before writing it to the output stream.
309 ostringstream options;
310 if (!params().bb.empty())
311 options << "bb=" << rtrim(params().bb) << ',';
316 double const scl = convert<double>(params().scale);
317 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
318 if (!float_equal(scl, 100.0, 0.05))
319 options << "scale=" << scl / 100.0 << ',';
321 if (!params().width.zero())
322 options << "width=" << params().width.asLatexString() << ',';
323 if (!params().height.zero())
324 options << "height=" << params().height.asLatexString() << ',';
325 if (params().keepAspectRatio)
326 options << "keepaspectratio,";
329 // Make sure rotation angle is not very close to zero;
330 // a float can be effectively zero but not exactly zero.
331 if (!params().rotateAngle.empty()
332 && !float_equal(convert<double>(params().rotateAngle), 0.0, 0.001)) {
333 options << "angle=" << params().rotateAngle << ',';
334 if (!params().rotateOrigin.empty()) {
335 options << "origin=" << params().rotateOrigin[0];
336 if (contains(params().rotateOrigin,"Top"))
338 else if (contains(params().rotateOrigin,"Bottom"))
340 else if (contains(params().rotateOrigin,"Baseline"))
346 if (!params().special.empty())
347 options << params().special << ',';
349 string opts = options.str();
351 return opts.substr(0, opts.size() - 1);
355 string const InsetGraphics::toDocbookLength(LyXLength const & len) const
357 ostringstream result;
358 switch (len.unit()) {
359 case LyXLength::SP: // Scaled point (65536sp = 1pt) TeX's smallest unit.
360 result << len.value() * 65536.0 * 72 / 72.27 << "pt";
362 case LyXLength::PT: // Point = 1/72.27in = 0.351mm
363 result << len.value() * 72 / 72.27 << "pt";
365 case LyXLength::BP: // Big point (72bp = 1in), also PostScript point
366 result << len.value() << "pt";
368 case LyXLength::DD: // Didot point = 1/72 of a French inch, = 0.376mm
369 result << len.value() * 0.376 << "mm";
371 case LyXLength::MM: // Millimeter = 2.845pt
372 result << len.value() << "mm";
374 case LyXLength::PC: // Pica = 12pt = 4.218mm
375 result << len.value() << "pc";
377 case LyXLength::CC: // Cicero = 12dd = 4.531mm
378 result << len.value() * 4.531 << "mm";
380 case LyXLength::CM: // Centimeter = 10mm = 2.371pc
381 result << len.value() << "cm";
383 case LyXLength::IN: // Inch = 25.4mm = 72.27pt = 6.022pc
384 result << len.value() << "in";
386 case LyXLength::EX: // Height of a small "x" for the current font.
387 // Obviously we have to compromise here. Any better ratio than 1.5 ?
388 result << len.value() / 1.5 << "em";
390 case LyXLength::EM: // Width of capital "M" in current font.
391 result << len.value() << "em";
393 case LyXLength::MU: // Math unit (18mu = 1em) for positioning in math mode
394 result << len.value() * 18 << "em";
396 case LyXLength::PTW: // Percent of TextWidth
397 case LyXLength::PCW: // Percent of ColumnWidth
398 case LyXLength::PPW: // Percent of PageWidth
399 case LyXLength::PLW: // Percent of LineWidth
400 case LyXLength::PTH: // Percent of TextHeight
401 case LyXLength::PPH: // Percent of Paper
402 // Sigh, this will go wrong.
403 result << len.value() << "%";
406 result << len.asString();
412 string const InsetGraphics::createDocBookAttributes() const
414 // Calculate the options part of the command, we must do it to a string
415 // stream since we copied the code from createLatexParams() ;-)
417 // FIXME: av: need to translate spec -> Docbook XSL spec (http://www.sagehill.net/docbookxsl/ImageSizing.html)
418 // Right now it only works with my version of db2latex :-)
420 ostringstream options;
421 double const scl = convert<double>(params().scale);
422 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
423 if (!float_equal(scl, 100.0, 0.05))
424 options << " scale=\""
425 << static_cast<int>( (scl) + 0.5 )
428 if (!params().width.zero()) {
429 options << " width=\"" << toDocbookLength(params().width) << "\" ";
431 if (!params().height.zero()) {
432 options << " depth=\"" << toDocbookLength(params().height) << "\" ";
434 if (params().keepAspectRatio) {
435 // This will be irrelevant unless both width and height are set
436 options << "scalefit=\"1\" ";
441 if (!params().special.empty())
442 options << params().special << " ";
444 string opts = options.str();
445 // trailing blanks are ok ...
460 std::pair<CopyStatus, string> const
461 copyFileIfNeeded(string const & file_in, string const & file_out)
463 BOOST_ASSERT(absolutePath(file_in));
464 BOOST_ASSERT(absolutePath(file_out));
466 unsigned long const checksum_in = support::sum(file_in);
467 unsigned long const checksum_out = support::sum(file_out);
469 if (checksum_in == checksum_out)
471 return std::make_pair(IDENTICAL_CONTENTS, file_out);
473 Mover const & mover = movers(formats.getFormatFromFile(file_in));
474 bool const success = mover.copy(file_in, file_out);
477 lyxerr[Debug::GRAPHICS]
478 << lyx::to_utf8(support::bformat(_("Could not copy the file\n%1$s\n"
479 "into the temporary directory."),
480 lyx::from_utf8(file_in)))
484 CopyStatus status = success ? SUCCESS : FAILURE;
485 return std::make_pair(status, file_out);
489 std::pair<CopyStatus, string> const
490 copyToDirIfNeeded(FileName const & file, string const & dir)
492 using support::rtrim;
494 string const file_in = file.absFilename();
495 string const only_path = support::onlyPath(file_in);
496 if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/"))
497 return std::make_pair(IDENTICAL_PATHS, file_in);
499 string mangled = file.mangledFilename();
500 if (file.isZipped()) {
501 // We need to change _eps.gz to .eps.gz. The mangled name is
502 // still unique because of the counter in mangledFilename().
503 // We can't just call mangledFilename() with the zip
504 // extension removed, because base.eps and base.eps.gz may
505 // have different content but would get the same mangled
506 // name in this case.
507 string const base = removeExtension(file.unzippedFilename());
508 string::size_type const ext_len = file_in.length() - base.length();
509 mangled[mangled.length() - ext_len] = '.';
511 string const file_out = support::makeAbsPath(mangled, dir);
513 return copyFileIfNeeded(file_in, file_out);
517 string const stripExtensionIfPossible(string const & file)
519 // Remove the extension so the LaTeX will use whatever
520 // is appropriate (when there are several versions in
521 // different formats)
522 // This works only if the filename contains no dots besides
523 // the just removed one. We can fool here by replacing all
524 // dots with a macro whose definition is just a dot ;-)
525 // The automatic format selection does not work if the file
527 string const latex_name = latex_path(file,
528 lyx::support::EXCLUDE_EXTENSION);
529 if (contains(latex_name, '"'))
531 return latex_path(removeExtension(file),
532 lyx::support::PROTECT_EXTENSION,
533 lyx::support::ESCAPE_DOTS);
537 string const stripExtensionIfPossible(string const & file, string const & to)
539 // No conversion is needed. LaTeX can handle the graphic file as is.
540 // This is true even if the orig_file is compressed.
541 string const to_format = formats.getFormat(to)->extension();
542 string const file_format = getExtension(file);
543 // for latex .ps == .eps
544 if (to_format == file_format ||
545 (to_format == "eps" && file_format == "ps") ||
546 (to_format == "ps" && file_format == "eps"))
547 return stripExtensionIfPossible(file);
548 return latex_path(file, lyx::support::EXCLUDE_EXTENSION);
554 string const InsetGraphics::prepareFile(Buffer const & buf,
555 OutputParams const & runparams) const
557 // The following code depends on non-empty filenames
558 if (params().filename.empty())
561 string const orig_file = params().filename.absFilename();
562 string const rel_file = params().filename.relFilename(buf.filePath());
564 // previewing source code, no file copying or file format conversion
565 if (runparams.dryrun)
566 return stripExtensionIfPossible(rel_file);
568 // temp_file will contain the file for LaTeX to act on if, for example,
569 // we move it to a temp dir or uncompress it.
570 string temp_file = orig_file;
572 // The master buffer. This is useful when there are multiple levels
574 Buffer const * m_buffer = buf.getMasterBuffer();
576 // Return the output name if we are inside a comment or the file does
578 // We are not going to change the extension or using the name of the
579 // temporary file, the code is already complicated enough.
580 if (runparams.inComment || !isFileReadable(orig_file))
581 return params().filename.outputFilename(m_buffer->filePath());
583 // We place all temporary files in the master buffer's temp dir.
584 // This is possible because we use mangled file names.
585 // This is necessary for DVI export.
586 string const temp_path = m_buffer->temppath();
589 boost::tie(status, temp_file) =
590 copyToDirIfNeeded(params().filename, temp_path);
592 if (status == FAILURE)
595 // a relative filename should be relative to the master
597 // "nice" means that the buffer is exported to LaTeX format but not
598 // run through the LaTeX compiler.
599 string output_file = os::external_path(runparams.nice ?
600 params().filename.outputFilename(m_buffer->filePath()) :
601 onlyFilename(temp_file));
602 string source_file = runparams.nice ? orig_file : temp_file;
603 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
604 "latex" : "pdflatex";
606 // If the file is compressed and we have specified that it
607 // should not be uncompressed, then just return its name and
608 // let LaTeX do the rest!
609 if (params().filename.isZipped()) {
610 if (params().noUnzip) {
611 // We don't know whether latex can actually handle
612 // this file, but we can't check, because that would
613 // mean to unzip the file and thereby making the
614 // noUnzip parameter meaningless.
615 lyxerr[Debug::GRAPHICS]
616 << "\tpass zipped file to LaTeX.\n";
618 string const bb_orig_file = changeExtension(orig_file, "bb");
619 if (runparams.nice) {
620 runparams.exportdata->addExternalFile(tex_format,
622 changeExtension(output_file, "bb"));
624 // LaTeX needs the bounding box file in the
626 string bb_file = changeExtension(temp_file, "bb");
627 boost::tie(status, bb_file) =
628 copyFileIfNeeded(bb_orig_file, bb_file);
629 if (status == FAILURE)
631 runparams.exportdata->addExternalFile(tex_format,
634 runparams.exportdata->addExternalFile(tex_format,
635 source_file, output_file);
636 runparams.exportdata->addExternalFile("dvi",
637 source_file, output_file);
638 // We can't strip the extension, because we don't know
639 // the unzipped file format
640 return latex_path(output_file,
641 lyx::support::EXCLUDE_EXTENSION);
644 string const unzipped_temp_file = unzippedFileName(temp_file);
645 output_file = unzippedFileName(output_file);
646 source_file = unzippedFileName(source_file);
647 if (compare_timestamps(unzipped_temp_file, temp_file) > 0) {
648 // temp_file has been unzipped already and
649 // orig_file has not changed in the meantime.
650 temp_file = unzipped_temp_file;
651 lyxerr[Debug::GRAPHICS]
652 << "\twas already unzipped to " << temp_file
655 // unzipped_temp_file does not exist or is too old
656 temp_file = unzipFile(temp_file);
657 lyxerr[Debug::GRAPHICS]
658 << "\tunzipped to " << temp_file << endl;
662 string const from = formats.getFormatFromFile(temp_file);
664 lyxerr[Debug::GRAPHICS]
665 << "\tCould not get file format." << endl;
667 string const to = findTargetFormat(from, runparams);
668 string const ext = formats.extension(to);
669 lyxerr[Debug::GRAPHICS]
670 << "\t we have: from " << from << " to " << to << '\n';
672 // We're going to be running the exported buffer through the LaTeX
673 // compiler, so must ensure that LaTeX can cope with the graphics
676 lyxerr[Debug::GRAPHICS]
677 << "\tthe orig file is: " << orig_file << endl;
680 if (!runparams.nice && getExtension(temp_file) != ext) {
681 // The LaTeX compiler will not be able to determine
682 // the file format from the extension, so we must
684 string const new_file = changeExtension(temp_file, ext);
685 if (lyx::support::rename(temp_file, new_file)) {
686 temp_file = new_file;
687 output_file = changeExtension(output_file, ext);
688 source_file = changeExtension(source_file, ext);
690 lyxerr[Debug::GRAPHICS]
691 << "Could not rename file `"
692 << temp_file << "' to `" << new_file
695 // The extension of temp_file might be != ext!
696 runparams.exportdata->addExternalFile(tex_format, source_file,
698 runparams.exportdata->addExternalFile("dvi", source_file,
700 return stripExtensionIfPossible(output_file, to);
703 string const to_file = changeExtension(temp_file, ext);
704 string const output_to_file = changeExtension(output_file, ext);
706 // Do we need to perform the conversion?
707 // Yes if to_file does not exist or if temp_file is newer than to_file
708 if (compare_timestamps(temp_file, to_file) < 0) {
710 lyxerr[Debug::GRAPHICS]
711 << lyx::to_utf8(bformat(_("No conversion of %1$s is needed after all"),
712 lyx::from_utf8(rel_file)))
714 runparams.exportdata->addExternalFile(tex_format, to_file,
716 runparams.exportdata->addExternalFile("dvi", to_file,
718 return stripExtensionIfPossible(output_file);
721 lyxerr[Debug::GRAPHICS]
722 << "\tThe original file is " << orig_file << "\n"
723 << "\tA copy has been made and convert is to be called with:\n"
724 << "\tfile to convert = " << temp_file << '\n'
725 << "\t from " << from << " to " << to << '\n';
727 // FIXME (Abdel 12/08/06): Is there a need to show these errors?
729 if (converters.convert(&buf, temp_file, temp_file, from, to, el, true)) {
730 runparams.exportdata->addExternalFile(tex_format,
731 to_file, output_to_file);
732 runparams.exportdata->addExternalFile("dvi",
733 to_file, output_to_file);
736 return stripExtensionIfPossible(output_file);
740 int InsetGraphics::latex(Buffer const & buf, odocstream & os,
741 OutputParams const & runparams) const
743 // If there is no file specified or not existing,
744 // just output a message about it in the latex output.
745 lyxerr[Debug::GRAPHICS]
746 << "insetgraphics::latex: Filename = "
747 << params().filename.absFilename() << endl;
749 string const relative_file =
750 params().filename.relFilename(buf.filePath());
752 string const file_ = params().filename.absFilename();
753 bool const file_exists = !file_.empty() && isFileReadable(file_);
754 string const message = file_exists ?
755 string() : string("bb = 0 0 200 100, draft, type=eps");
756 // if !message.empty() then there was no existing file
757 // "filename" found. In this case LaTeX
758 // draws only a rectangle with the above bb and the
759 // not found filename in it.
760 lyxerr[Debug::GRAPHICS]
761 << "\tMessage = \"" << message << '\"' << endl;
763 // These variables collect all the latex code that should be before and
764 // after the actual includegraphics command.
767 // Do we want subcaptions?
768 if (params().subcaption) {
769 if (runparams.moving_arg)
770 before += "\\protect";
771 before += "\\subfigure[" + params().subcaptionText + "]{";
775 if (runparams.moving_arg)
776 before += "\\protect";
778 // We never use the starred form, we use the "clip" option instead.
779 before += "\\includegraphics";
781 // Write the options if there are any.
782 string const opts = createLatexOptions();
783 lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
785 if (!opts.empty() && !message.empty())
786 before += ('[' + opts + ',' + message + ']');
787 else if (!opts.empty() || !message.empty())
788 before += ('[' + opts + message + ']');
790 lyxerr[Debug::GRAPHICS]
791 << "\tBefore = " << before
792 << "\n\tafter = " << after << endl;
794 string latex_str = before + '{';
795 // Convert the file if necessary.
796 // Remove the extension so LaTeX will use whatever is appropriate
797 // (when there are several versions in different formats)
798 latex_str += prepareFile(buf, runparams);
799 latex_str += '}' + after;
801 os << lyx::from_utf8(latex_str);
803 lyxerr[Debug::GRAPHICS] << "InsetGraphics::latex outputting:\n"
804 << latex_str << endl;
805 // Return how many newlines we issued.
806 return int(lyx::count(latex_str.begin(), latex_str.end(),'\n'));
810 int InsetGraphics::plaintext(Buffer const &, odocstream & os,
811 OutputParams const &) const
813 // No graphics in ascii output. Possible to use gifscii to convert
814 // images to ascii approximation.
815 // 1. Convert file to ascii using gifscii
816 // 2. Read ascii output file and add it to the output stream.
817 // at least we send the filename
819 // FIXME: We have no idea what the encoding of the filename is
820 os << '<' << bformat(_("Graphics file: %1$s"),
821 lyx::from_utf8(params().filename.absFilename()))
829 int writeImageObject(char * format, ostream& os, OutputParams const & runparams,
830 string const graphic_label, string const attributes)
832 if (runparams.flavor != OutputParams::XML) {
833 os << "<![ %output.print." << format << "; [" << std::endl;
835 os <<"<imageobject><imagedata fileref=\"&"
836 << graphic_label << ";." << format << "\" " << attributes ;
837 if (runparams.flavor == OutputParams::XML) {
838 os << " role=\"" << format << "\"/>" ;
841 os << " format=\"" << format << "\">" ;
843 os << "</imageobject>";
844 if (runparams.flavor != OutputParams::XML) {
845 os << std::endl << "]]>" ;
847 return runparams.flavor == OutputParams::XML ? 0 : 2;
849 // end anonymous namespace
853 // For explanation on inserting graphics into DocBook checkout:
854 // http://en.tldp.org/LDP/LDP-Author-Guide/html/inserting-pictures.html
855 // See also the docbook guide at http://www.docbook.org/
856 int InsetGraphics::docbook(Buffer const &, ostream & os,
857 OutputParams const & runparams) const
859 // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
860 // need to switch to MediaObject. However, for now this is sufficient and
862 if (runparams.flavor == OutputParams::XML) {
863 runparams.exportdata->addExternalFile("docbook-xml",
864 params().filename.absFilename());
866 runparams.exportdata->addExternalFile("docbook",
867 params().filename.absFilename());
869 os << "<inlinemediaobject>";
872 string attributes = createDocBookAttributes();
873 r += writeImageObject("png", os, runparams, graphic_label, attributes);
874 r += writeImageObject("pdf", os, runparams, graphic_label, attributes);
875 r += writeImageObject("eps", os, runparams, graphic_label, attributes);
876 r += writeImageObject("bmp", os, runparams, graphic_label, attributes);
878 os << "</inlinemediaobject>";
883 void InsetGraphics::validate(LaTeXFeatures & features) const
885 // If we have no image, we should not require anything.
886 if (params().filename.empty())
889 features.includeFile(graphic_label,
890 removeExtension(params().filename.absFilename()));
892 features.require("graphicx");
894 if (features.runparams().nice) {
895 Buffer const * m_buffer = features.buffer().getMasterBuffer();
897 params().filename.outputFilename(m_buffer->filePath());
898 basename = removeExtension(basename);
899 if(params().filename.isZipped())
900 basename = removeExtension(basename);
901 if (contains(basename, "."))
902 features.require("lyxdot");
905 if (params().subcaption)
906 features.require("subfigure");
910 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
912 // If nothing is changed, just return and say so.
913 if (params() == p && !p.filename.empty())
916 // Copy the new parameters.
919 // Update the display using the new parameters.
920 graphic_->update(params().as_grfxParams());
922 // We have changed data, report it.
927 InsetGraphicsParams const & InsetGraphics::params() const
933 void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
934 Buffer const & buffer) const
936 string const file_with_path = p.filename.absFilename();
937 formats.edit(buffer, file_with_path,
938 formats.getFormatFromFile(file_with_path));
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";