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::odocstream;
96 using lyx::support::absolutePath;
97 using lyx::support::bformat;
98 using lyx::support::changeExtension;
99 using lyx::support::compare_timestamps;
100 using lyx::support::contains;
101 using lyx::support::FileName;
102 using lyx::support::float_equal;
103 using lyx::support::getExtension;
104 using lyx::support::isFileReadable;
105 using lyx::support::latex_path;
106 using lyx::support::onlyFilename;
107 using lyx::support::removeExtension;
108 using lyx::support::rtrim;
109 using lyx::support::subst;
110 using lyx::support::Systemcall;
111 using lyx::support::unzipFile;
112 using lyx::support::unzippedFileName;
114 namespace os = lyx::support::os;
119 using std::istringstream;
121 using std::ostringstream;
126 /// Find the most suitable image format for images in \p format
127 /// Note that \p format may be unknown (i. e. an empty string)
128 string findTargetFormat(string const & format, OutputParams const & runparams)
130 // Are we using latex or pdflatex?
131 if (runparams.flavor == OutputParams::PDFLATEX) {
132 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
133 Format const * const f = formats.getFormat(format);
134 // Convert vector graphics to pdf
135 if (f && f->vectorFormat())
137 // pdflatex can use jpeg, png and pdf directly
140 // Convert everything else to png
143 // If it's postscript, we always do eps.
144 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
146 // any other than ps is changed to eps
155 InsetGraphics::InsetGraphics()
156 : graphic_label(sgml::uniqueID("graph")),
157 graphic_(new RenderGraphic(this))
161 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
163 boost::signals::trackable(),
164 graphic_label(sgml::uniqueID("graph")),
165 graphic_(new RenderGraphic(*ig.graphic_, this))
167 setParams(ig.params());
171 auto_ptr<InsetBase> InsetGraphics::doClone() const
173 return auto_ptr<InsetBase>(new InsetGraphics(*this));
177 InsetGraphics::~InsetGraphics()
179 InsetGraphicsMailer(*this).hideDialog();
183 void InsetGraphics::doDispatch(LCursor & cur, FuncRequest & cmd)
185 switch (cmd.action) {
186 case LFUN_GRAPHICS_EDIT: {
187 Buffer const & buffer = *cur.bv().buffer();
188 InsetGraphicsParams p;
189 InsetGraphicsMailer::string2params(lyx::to_utf8(cmd.argument()), buffer, p);
190 editGraphics(p, buffer);
194 case LFUN_INSET_MODIFY: {
195 Buffer const & buffer = cur.buffer();
196 InsetGraphicsParams p;
197 InsetGraphicsMailer::string2params(lyx::to_utf8(cmd.argument()), buffer, p);
198 if (!p.filename.empty())
205 case LFUN_INSET_DIALOG_UPDATE:
206 InsetGraphicsMailer(*this).updateDialog(&cur.bv());
209 case LFUN_MOUSE_RELEASE:
210 InsetGraphicsMailer(*this).showDialog(&cur.bv());
214 InsetBase::doDispatch(cur, cmd);
220 bool InsetGraphics::getStatus(LCursor & cur, FuncRequest const & cmd,
221 FuncStatus & flag) const
223 switch (cmd.action) {
224 case LFUN_GRAPHICS_EDIT:
225 case LFUN_INSET_MODIFY:
226 case LFUN_INSET_DIALOG_UPDATE:
231 return InsetBase::getStatus(cur, cmd, flag);
236 void InsetGraphics::edit(LCursor & cur, bool)
238 InsetGraphicsMailer(*this).showDialog(&cur.bv());
242 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
244 graphic_->metrics(mi, dim);
249 void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
251 setPosCache(pi, x, y);
252 graphic_->draw(pi, x, y);
256 InsetBase::EDITABLE InsetGraphics::editable() const
262 void InsetGraphics::write(Buffer const & buf, ostream & os) const
265 params().Write(os, buf.filePath());
269 void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
271 string const token = lex.getString();
273 if (token == "Graphics")
274 readInsetGraphics(lex, buf.filePath());
276 lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
278 graphic_->update(params().as_grfxParams());
282 void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
284 bool finished = false;
286 while (lex.isOK() && !finished) {
289 string const token = lex.getString();
290 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
295 } else if (token == "\\end_inset") {
298 if (!params_.Read(lex, token, bufpath))
299 lyxerr << "Unknown token, " << token << ", skipping."
306 string const InsetGraphics::createLatexOptions() const
308 // Calculate the options part of the command, we must do it to a string
309 // stream since we might have a trailing comma that we would like to remove
310 // before writing it to the output stream.
311 ostringstream options;
312 if (!params().bb.empty())
313 options << "bb=" << rtrim(params().bb) << ',';
318 double const scl = convert<double>(params().scale);
319 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
320 if (!float_equal(scl, 100.0, 0.05))
321 options << "scale=" << scl / 100.0 << ',';
323 if (!params().width.zero())
324 options << "width=" << params().width.asLatexString() << ',';
325 if (!params().height.zero())
326 options << "height=" << params().height.asLatexString() << ',';
327 if (params().keepAspectRatio)
328 options << "keepaspectratio,";
331 // Make sure rotation angle is not very close to zero;
332 // a float can be effectively zero but not exactly zero.
333 if (!params().rotateAngle.empty()
334 && !float_equal(convert<double>(params().rotateAngle), 0.0, 0.001)) {
335 options << "angle=" << params().rotateAngle << ',';
336 if (!params().rotateOrigin.empty()) {
337 options << "origin=" << params().rotateOrigin[0];
338 if (contains(params().rotateOrigin,"Top"))
340 else if (contains(params().rotateOrigin,"Bottom"))
342 else if (contains(params().rotateOrigin,"Baseline"))
348 if (!params().special.empty())
349 options << params().special << ',';
351 string opts = options.str();
353 return opts.substr(0, opts.size() - 1);
357 string const InsetGraphics::toDocbookLength(LyXLength const & len) const
359 ostringstream result;
360 switch (len.unit()) {
361 case LyXLength::SP: // Scaled point (65536sp = 1pt) TeX's smallest unit.
362 result << len.value() * 65536.0 * 72 / 72.27 << "pt";
364 case LyXLength::PT: // Point = 1/72.27in = 0.351mm
365 result << len.value() * 72 / 72.27 << "pt";
367 case LyXLength::BP: // Big point (72bp = 1in), also PostScript point
368 result << len.value() << "pt";
370 case LyXLength::DD: // Didot point = 1/72 of a French inch, = 0.376mm
371 result << len.value() * 0.376 << "mm";
373 case LyXLength::MM: // Millimeter = 2.845pt
374 result << len.value() << "mm";
376 case LyXLength::PC: // Pica = 12pt = 4.218mm
377 result << len.value() << "pc";
379 case LyXLength::CC: // Cicero = 12dd = 4.531mm
380 result << len.value() * 4.531 << "mm";
382 case LyXLength::CM: // Centimeter = 10mm = 2.371pc
383 result << len.value() << "cm";
385 case LyXLength::IN: // Inch = 25.4mm = 72.27pt = 6.022pc
386 result << len.value() << "in";
388 case LyXLength::EX: // Height of a small "x" for the current font.
389 // Obviously we have to compromise here. Any better ratio than 1.5 ?
390 result << len.value() / 1.5 << "em";
392 case LyXLength::EM: // Width of capital "M" in current font.
393 result << len.value() << "em";
395 case LyXLength::MU: // Math unit (18mu = 1em) for positioning in math mode
396 result << len.value() * 18 << "em";
398 case LyXLength::PTW: // Percent of TextWidth
399 case LyXLength::PCW: // Percent of ColumnWidth
400 case LyXLength::PPW: // Percent of PageWidth
401 case LyXLength::PLW: // Percent of LineWidth
402 case LyXLength::PTH: // Percent of TextHeight
403 case LyXLength::PPH: // Percent of Paper
404 // Sigh, this will go wrong.
405 result << len.value() << "%";
408 result << len.asString();
414 string const InsetGraphics::createDocBookAttributes() const
416 // Calculate the options part of the command, we must do it to a string
417 // stream since we copied the code from createLatexParams() ;-)
419 // FIXME: av: need to translate spec -> Docbook XSL spec (http://www.sagehill.net/docbookxsl/ImageSizing.html)
420 // Right now it only works with my version of db2latex :-)
422 ostringstream options;
423 double const scl = convert<double>(params().scale);
424 if (!params().scale.empty() && !float_equal(scl, 0.0, 0.05)) {
425 if (!float_equal(scl, 100.0, 0.05))
426 options << " scale=\""
427 << static_cast<int>( (scl) + 0.5 )
430 if (!params().width.zero()) {
431 options << " width=\"" << toDocbookLength(params().width) << "\" ";
433 if (!params().height.zero()) {
434 options << " depth=\"" << toDocbookLength(params().height) << "\" ";
436 if (params().keepAspectRatio) {
437 // This will be irrelevant unless both width and height are set
438 options << "scalefit=\"1\" ";
443 if (!params().special.empty())
444 options << params().special << " ";
446 string opts = options.str();
447 // trailing blanks are ok ...
462 std::pair<CopyStatus, string> const
463 copyFileIfNeeded(string const & file_in, string const & file_out)
465 BOOST_ASSERT(absolutePath(file_in));
466 BOOST_ASSERT(absolutePath(file_out));
468 unsigned long const checksum_in = support::sum(file_in);
469 unsigned long const checksum_out = support::sum(file_out);
471 if (checksum_in == checksum_out)
473 return std::make_pair(IDENTICAL_CONTENTS, file_out);
475 Mover const & mover = movers(formats.getFormatFromFile(file_in));
476 bool const success = mover.copy(file_in, file_out);
479 lyxerr[Debug::GRAPHICS]
480 << lyx::to_utf8(support::bformat(_("Could not copy the file\n%1$s\n"
481 "into the temporary directory."),
482 lyx::from_utf8(file_in)))
486 CopyStatus status = success ? SUCCESS : FAILURE;
487 return std::make_pair(status, file_out);
491 std::pair<CopyStatus, string> const
492 copyToDirIfNeeded(FileName const & file, string const & dir)
494 using support::rtrim;
496 string const file_in = file.absFilename();
497 string const only_path = support::onlyPath(file_in);
498 if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/"))
499 return std::make_pair(IDENTICAL_PATHS, file_in);
501 string mangled = file.mangledFilename();
502 if (file.isZipped()) {
503 // We need to change _eps.gz to .eps.gz. The mangled name is
504 // still unique because of the counter in mangledFilename().
505 // We can't just call mangledFilename() with the zip
506 // extension removed, because base.eps and base.eps.gz may
507 // have different content but would get the same mangled
508 // name in this case.
509 string const base = removeExtension(file.unzippedFilename());
510 string::size_type const ext_len = file_in.length() - base.length();
511 mangled[mangled.length() - ext_len] = '.';
513 string const file_out = support::makeAbsPath(mangled, dir);
515 return copyFileIfNeeded(file_in, file_out);
519 string const stripExtensionIfPossible(string const & file)
521 // Remove the extension so the LaTeX will use whatever
522 // is appropriate (when there are several versions in
523 // different formats)
524 // This works only if the filename contains no dots besides
525 // the just removed one. We can fool here by replacing all
526 // dots with a macro whose definition is just a dot ;-)
527 // The automatic format selection does not work if the file
529 string const latex_name = latex_path(file,
530 lyx::support::EXCLUDE_EXTENSION);
531 if (contains(latex_name, '"'))
533 return latex_path(removeExtension(file),
534 lyx::support::PROTECT_EXTENSION,
535 lyx::support::ESCAPE_DOTS);
539 string const stripExtensionIfPossible(string const & file, string const & to)
541 // No conversion is needed. LaTeX can handle the graphic file as is.
542 // This is true even if the orig_file is compressed.
543 string const to_format = formats.getFormat(to)->extension();
544 string const file_format = getExtension(file);
545 // for latex .ps == .eps
546 if (to_format == file_format ||
547 (to_format == "eps" && file_format == "ps") ||
548 (to_format == "ps" && file_format == "eps"))
549 return stripExtensionIfPossible(file);
550 return latex_path(file, lyx::support::EXCLUDE_EXTENSION);
556 string const InsetGraphics::prepareFile(Buffer const & buf,
557 OutputParams const & runparams) const
559 // The following code depends on non-empty filenames
560 if (params().filename.empty())
563 string const orig_file = params().filename.absFilename();
564 string const rel_file = params().filename.relFilename(buf.filePath());
566 // previewing source code, no file copying or file format conversion
567 if (runparams.dryrun)
568 return stripExtensionIfPossible(rel_file);
570 // temp_file will contain the file for LaTeX to act on if, for example,
571 // we move it to a temp dir or uncompress it.
572 string temp_file = orig_file;
574 // The master buffer. This is useful when there are multiple levels
576 Buffer const * m_buffer = buf.getMasterBuffer();
578 // Return the output name if we are inside a comment or the file does
580 // We are not going to change the extension or using the name of the
581 // temporary file, the code is already complicated enough.
582 if (runparams.inComment || !isFileReadable(orig_file))
583 return params().filename.outputFilename(m_buffer->filePath());
585 // We place all temporary files in the master buffer's temp dir.
586 // This is possible because we use mangled file names.
587 // This is necessary for DVI export.
588 string const temp_path = m_buffer->temppath();
591 boost::tie(status, temp_file) =
592 copyToDirIfNeeded(params().filename, temp_path);
594 if (status == FAILURE)
597 // a relative filename should be relative to the master
599 // "nice" means that the buffer is exported to LaTeX format but not
600 // run through the LaTeX compiler.
601 string output_file = os::external_path(runparams.nice ?
602 params().filename.outputFilename(m_buffer->filePath()) :
603 onlyFilename(temp_file));
604 string source_file = runparams.nice ? orig_file : temp_file;
605 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
606 "latex" : "pdflatex";
608 // If the file is compressed and we have specified that it
609 // should not be uncompressed, then just return its name and
610 // let LaTeX do the rest!
611 if (params().filename.isZipped()) {
612 if (params().noUnzip) {
613 // We don't know whether latex can actually handle
614 // this file, but we can't check, because that would
615 // mean to unzip the file and thereby making the
616 // noUnzip parameter meaningless.
617 lyxerr[Debug::GRAPHICS]
618 << "\tpass zipped file to LaTeX.\n";
620 string const bb_orig_file = changeExtension(orig_file, "bb");
621 if (runparams.nice) {
622 runparams.exportdata->addExternalFile(tex_format,
624 changeExtension(output_file, "bb"));
626 // LaTeX needs the bounding box file in the
628 string bb_file = changeExtension(temp_file, "bb");
629 boost::tie(status, bb_file) =
630 copyFileIfNeeded(bb_orig_file, bb_file);
631 if (status == FAILURE)
633 runparams.exportdata->addExternalFile(tex_format,
636 runparams.exportdata->addExternalFile(tex_format,
637 source_file, output_file);
638 runparams.exportdata->addExternalFile("dvi",
639 source_file, output_file);
640 // We can't strip the extension, because we don't know
641 // the unzipped file format
642 return latex_path(output_file,
643 lyx::support::EXCLUDE_EXTENSION);
646 string const unzipped_temp_file = unzippedFileName(temp_file);
647 output_file = unzippedFileName(output_file);
648 source_file = unzippedFileName(source_file);
649 if (compare_timestamps(unzipped_temp_file, temp_file) > 0) {
650 // temp_file has been unzipped already and
651 // orig_file has not changed in the meantime.
652 temp_file = unzipped_temp_file;
653 lyxerr[Debug::GRAPHICS]
654 << "\twas already unzipped to " << temp_file
657 // unzipped_temp_file does not exist or is too old
658 temp_file = unzipFile(temp_file);
659 lyxerr[Debug::GRAPHICS]
660 << "\tunzipped to " << temp_file << endl;
664 string const from = formats.getFormatFromFile(temp_file);
666 lyxerr[Debug::GRAPHICS]
667 << "\tCould not get file format." << endl;
669 string const to = findTargetFormat(from, runparams);
670 string const ext = formats.extension(to);
671 lyxerr[Debug::GRAPHICS]
672 << "\t we have: from " << from << " to " << to << '\n';
674 // We're going to be running the exported buffer through the LaTeX
675 // compiler, so must ensure that LaTeX can cope with the graphics
678 lyxerr[Debug::GRAPHICS]
679 << "\tthe orig file is: " << orig_file << endl;
682 if (!runparams.nice && getExtension(temp_file) != ext) {
683 // The LaTeX compiler will not be able to determine
684 // the file format from the extension, so we must
686 string const new_file = changeExtension(temp_file, ext);
687 if (lyx::support::rename(temp_file, new_file)) {
688 temp_file = new_file;
689 output_file = changeExtension(output_file, ext);
690 source_file = changeExtension(source_file, ext);
692 lyxerr[Debug::GRAPHICS]
693 << "Could not rename file `"
694 << temp_file << "' to `" << new_file
697 // The extension of temp_file might be != ext!
698 runparams.exportdata->addExternalFile(tex_format, source_file,
700 runparams.exportdata->addExternalFile("dvi", source_file,
702 return stripExtensionIfPossible(output_file, to);
705 string const to_file = changeExtension(temp_file, ext);
706 string const output_to_file = changeExtension(output_file, ext);
708 // Do we need to perform the conversion?
709 // Yes if to_file does not exist or if temp_file is newer than to_file
710 if (compare_timestamps(temp_file, to_file) < 0) {
712 lyxerr[Debug::GRAPHICS]
713 << lyx::to_utf8(bformat(_("No conversion of %1$s is needed after all"),
714 lyx::from_utf8(rel_file)))
716 runparams.exportdata->addExternalFile(tex_format, to_file,
718 runparams.exportdata->addExternalFile("dvi", to_file,
720 return stripExtensionIfPossible(output_file);
723 lyxerr[Debug::GRAPHICS]
724 << "\tThe original file is " << orig_file << "\n"
725 << "\tA copy has been made and convert is to be called with:\n"
726 << "\tfile to convert = " << temp_file << '\n'
727 << "\t from " << from << " to " << to << '\n';
729 // FIXME (Abdel 12/08/06): Is there a need to show these errors?
731 if (converters.convert(&buf, temp_file, temp_file, from, to, el, true)) {
732 runparams.exportdata->addExternalFile(tex_format,
733 to_file, output_to_file);
734 runparams.exportdata->addExternalFile("dvi",
735 to_file, output_to_file);
738 return stripExtensionIfPossible(output_file);
742 int InsetGraphics::latex(Buffer const & buf, odocstream & os,
743 OutputParams const & runparams) const
745 // If there is no file specified or not existing,
746 // just output a message about it in the latex output.
747 lyxerr[Debug::GRAPHICS]
748 << "insetgraphics::latex: Filename = "
749 << params().filename.absFilename() << endl;
751 string const relative_file =
752 params().filename.relFilename(buf.filePath());
754 string const file_ = params().filename.absFilename();
755 bool const file_exists = !file_.empty() && isFileReadable(file_);
756 string const message = file_exists ?
757 string() : string("bb = 0 0 200 100, draft, type=eps");
758 // if !message.empty() then there was no existing file
759 // "filename" found. In this case LaTeX
760 // draws only a rectangle with the above bb and the
761 // not found filename in it.
762 lyxerr[Debug::GRAPHICS]
763 << "\tMessage = \"" << message << '\"' << endl;
765 // These variables collect all the latex code that should be before and
766 // after the actual includegraphics command.
769 // Do we want subcaptions?
770 if (params().subcaption) {
771 if (runparams.moving_arg)
772 before += "\\protect";
773 before += "\\subfigure[" + params().subcaptionText + "]{";
777 if (runparams.moving_arg)
778 before += "\\protect";
780 // We never use the starred form, we use the "clip" option instead.
781 before += "\\includegraphics";
783 // Write the options if there are any.
784 string const opts = createLatexOptions();
785 lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
787 if (!opts.empty() && !message.empty())
788 before += ('[' + opts + ',' + message + ']');
789 else if (!opts.empty() || !message.empty())
790 before += ('[' + opts + message + ']');
792 lyxerr[Debug::GRAPHICS]
793 << "\tBefore = " << before
794 << "\n\tafter = " << after << endl;
796 string latex_str = before + '{';
797 // Convert the file if necessary.
798 // Remove the extension so LaTeX will use whatever is appropriate
799 // (when there are several versions in different formats)
800 latex_str += prepareFile(buf, runparams);
801 latex_str += '}' + after;
803 os << lyx::from_utf8(latex_str);
805 lyxerr[Debug::GRAPHICS] << "InsetGraphics::latex outputting:\n"
806 << latex_str << endl;
807 // Return how many newlines we issued.
808 return int(lyx::count(latex_str.begin(), latex_str.end(),'\n'));
812 int InsetGraphics::plaintext(Buffer const &, odocstream & os,
813 OutputParams const &) const
815 // No graphics in ascii output. Possible to use gifscii to convert
816 // images to ascii approximation.
817 // 1. Convert file to ascii using gifscii
818 // 2. Read ascii output file and add it to the output stream.
819 // at least we send the filename
821 // FIXME: We have no idea what the encoding of the filename is
822 os << '<' << bformat(_("Graphics file: %1$s"),
823 lyx::from_utf8(params().filename.absFilename()))
831 int writeImageObject(char * format, odocstream & os, OutputParams const & runparams,
832 string const graphic_label, string const attributes)
834 if (runparams.flavor != OutputParams::XML) {
835 os << "<![ %output.print."
841 os <<"<imageobject><imagedata fileref=\"&"
842 << lyx::from_ascii(graphic_label)
846 << lyx::from_ascii(attributes);
847 if (runparams.flavor == OutputParams::XML) {
848 os << " role=\"" << format << "\"/>" ;
851 os << " format=\"" << format << "\">" ;
853 os << "</imageobject>";
854 if (runparams.flavor != OutputParams::XML) {
855 os << std::endl << "]]>" ;
857 return runparams.flavor == OutputParams::XML ? 0 : 2;
859 // end anonymous namespace
863 // For explanation on inserting graphics into DocBook checkout:
864 // http://en.tldp.org/LDP/LDP-Author-Guide/html/inserting-pictures.html
865 // See also the docbook guide at http://www.docbook.org/
866 int InsetGraphics::docbook(Buffer const &, odocstream & os,
867 OutputParams const & runparams) const
869 // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
870 // need to switch to MediaObject. However, for now this is sufficient and
872 if (runparams.flavor == OutputParams::XML) {
873 runparams.exportdata->addExternalFile("docbook-xml",
874 params().filename.absFilename());
876 runparams.exportdata->addExternalFile("docbook",
877 params().filename.absFilename());
879 os << "<inlinemediaobject>";
882 string attributes = createDocBookAttributes();
883 r += writeImageObject("png", os, runparams, graphic_label, attributes);
884 r += writeImageObject("pdf", os, runparams, graphic_label, attributes);
885 r += writeImageObject("eps", os, runparams, graphic_label, attributes);
886 r += writeImageObject("bmp", os, runparams, graphic_label, attributes);
888 os << "</inlinemediaobject>";
893 void InsetGraphics::validate(LaTeXFeatures & features) const
895 // If we have no image, we should not require anything.
896 if (params().filename.empty())
899 features.includeFile(graphic_label,
900 removeExtension(params().filename.absFilename()));
902 features.require("graphicx");
904 if (features.runparams().nice) {
905 Buffer const * m_buffer = features.buffer().getMasterBuffer();
907 params().filename.outputFilename(m_buffer->filePath());
908 basename = removeExtension(basename);
909 if(params().filename.isZipped())
910 basename = removeExtension(basename);
911 if (contains(basename, "."))
912 features.require("lyxdot");
915 if (params().subcaption)
916 features.require("subfigure");
920 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
922 // If nothing is changed, just return and say so.
923 if (params() == p && !p.filename.empty())
926 // Copy the new parameters.
929 // Update the display using the new parameters.
930 graphic_->update(params().as_grfxParams());
932 // We have changed data, report it.
937 InsetGraphicsParams const & InsetGraphics::params() const
943 void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
944 Buffer const & buffer) const
946 string const file_with_path = p.filename.absFilename();
947 formats.edit(buffer, file_with_path,
948 formats.getFormatFromFile(file_with_path));
952 string const InsetGraphicsMailer::name_("graphics");
954 InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)
959 string const InsetGraphicsMailer::inset2string(Buffer const & buffer) const
961 return params2string(inset_.params(), buffer);
965 void InsetGraphicsMailer::string2params(string const & in,
966 Buffer const & buffer,
967 InsetGraphicsParams & params)
969 params = InsetGraphicsParams();
973 istringstream data(in);
979 if (!lex || name != name_)
980 return print_mailer_error("InsetGraphicsMailer", in, 1, name_);
983 inset.readInsetGraphics(lex, buffer.filePath());
984 params = inset.params();
989 InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
990 Buffer const & buffer)
993 data << name_ << ' ';
994 params.Write(data, buffer.filePath());
995 data << "\\end_inset\n";