]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphics.C
3f9f3465a3448c239c6e6fe04fdd1609fbf6f58b
[lyx.git] / src / insets / insetgraphics.C
1 /**
2  * \file insetgraphics.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Herbert Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 /*
13 TODO
14
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
23       filename.
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
27       that are possible.
28 */
29
30 /* NOTES:
31  * Fileformat:
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.
34  *
35  *
36  * Conversions:
37  *   Postscript output means EPS figures.
38  *
39  *   PDF output is best done with PDF figures if it's a direct conversion
40  *   or PNG figures otherwise.
41  *      Image format
42  *      from        to
43  *      EPS         epstopdf
44  *      PS          ps2pdf
45  *      JPG/PNG     direct
46  *      PDF         direct
47  *      others      PNG
48  */
49
50 #include <config.h>
51
52 #include "insets/insetgraphics.h"
53 #include "insets/render_graphic.h"
54
55 #include "buffer.h"
56 #include "BufferView.h"
57 #include "converter.h"
58 #include "cursor.h"
59 #include "debug.h"
60 #include "dispatchresult.h"
61 #include "format.h"
62 #include "funcrequest.h"
63 #include "gettext.h"
64 #include "LaTeXFeatures.h"
65 #include "lyx_main.h"
66 #include "lyxlex.h"
67 #include "metricsinfo.h"
68 #include "outputparams.h"
69
70 #include "frontends/Alert.h"
71 #include "frontends/LyXView.h"
72
73 #include "support/filetools.h"
74 #include "support/lyxalgo.h" // lyx::count
75 #include "support/lyxlib.h" // float_equal
76 #include "support/os.h"
77 #include "support/systemcall.h"
78 #include "support/tostr.h"
79 #include "support/std_sstream.h"
80
81 #include <boost/bind.hpp>
82 #include <boost/tuple/tuple.hpp>
83
84 namespace support = lyx::support;
85 using lyx::support::AbsolutePath;
86 using lyx::support::bformat;
87 using lyx::support::ChangeExtension;
88 using lyx::support::contains;
89 using lyx::support::FileName;
90 using lyx::support::float_equal;
91 using lyx::support::GetExtension;
92 using lyx::support::getExtFromContents;
93 using lyx::support::IsFileReadable;
94 using lyx::support::LibFileSearch;
95 using lyx::support::rtrim;
96 using lyx::support::subst;
97 using lyx::support::Systemcall;
98 using lyx::support::unzipFile;
99 using lyx::support::unzippedFileName;
100
101 namespace os = lyx::support::os;
102
103 using std::endl;
104 using std::string;
105 using std::auto_ptr;
106 using std::istringstream;
107 using std::ostream;
108 using std::ostringstream;
109
110
111 namespace {
112
113 ///////////////////////////////////////////////////////////////////////////
114 int const VersionNumber = 1;
115 ///////////////////////////////////////////////////////////////////////////
116
117 // This function is a utility function
118 // ... that should be with ChangeExtension ...
119 inline
120 string const RemoveExtension(string const & filename)
121 {
122         return ChangeExtension(filename, string());
123 }
124
125
126 string const uniqueID()
127 {
128         static unsigned int seed = 1000;
129         return "graph" + tostr(++seed);
130 }
131
132
133 string findTargetFormat(string const & suffix, OutputParams const & runparams)
134 {
135         // Are we using latex or pdflatex).
136         if (runparams.flavor == OutputParams::PDFLATEX) {
137                 lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
138                 if (contains(suffix, "ps") || suffix == "pdf")
139                         return "pdf";
140                 if (suffix == "jpg")    // pdflatex can use jpeg
141                         return suffix;
142                 return "png";         // and also png
143         }
144         // If it's postscript, we always do eps.
145         lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
146         if (suffix != "ps")     // any other than ps
147                 return "eps";         // is changed to eps
148         return suffix;          // let ps untouched
149 }
150
151 } // namespace anon
152
153
154 InsetGraphics::InsetGraphics()
155         : graphic_label(uniqueID()),
156           graphic_(new RenderGraphic(this))
157 {}
158
159
160 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
161         : InsetOld(ig),
162           boost::signals::trackable(),
163           graphic_label(uniqueID()),
164           graphic_(new RenderGraphic(*ig.graphic_, this))
165 {
166         setParams(ig.params());
167 }
168
169
170 auto_ptr<InsetBase> InsetGraphics::clone() const
171 {
172         return auto_ptr<InsetBase>(new InsetGraphics(*this));
173 }
174
175
176 InsetGraphics::~InsetGraphics()
177 {
178         InsetGraphicsMailer(*this).hideDialog();
179 }
180
181
182 void InsetGraphics::priv_dispatch(LCursor & cur, FuncRequest & cmd)
183 {
184         switch (cmd.action) {
185         case LFUN_GRAPHICS_EDIT: {
186                 Buffer const & buffer = *cur.bv().buffer();
187                 InsetGraphicsParams p;
188                 InsetGraphicsMailer::string2params(cmd.argument, buffer, p);
189                 editGraphics(p, buffer);
190                 break;
191         }
192
193         case LFUN_INSET_MODIFY: {
194                 Buffer const & buffer = cur.buffer();
195                 InsetGraphicsParams p;
196                 InsetGraphicsMailer::string2params(cmd.argument, buffer, p);
197                 if (!p.filename.empty()) {
198                         setParams(p);
199                         cur.bv().update();
200                 }
201                 break;
202         }
203
204         case LFUN_INSET_DIALOG_UPDATE:
205                 InsetGraphicsMailer(*this).updateDialog(&cur.bv());
206                 break;
207
208         case LFUN_MOUSE_RELEASE:
209                 InsetGraphicsMailer(*this).showDialog(&cur.bv());
210                 break;
211
212         default:
213                 InsetOld::priv_dispatch(cur, cmd);
214                 break;
215         }
216 }
217
218
219 void InsetGraphics::edit(LCursor & cur, bool)
220 {
221         InsetGraphicsMailer(*this).showDialog(&cur.bv());
222 }
223
224
225 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
226 {
227         graphic_->metrics(mi, dim);
228         dim_ = dim;
229 }
230
231
232 void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
233 {
234         setPosCache(pi, x, y);
235         graphic_->draw(pi, x, y);
236 }
237
238
239 InsetOld::EDITABLE InsetGraphics::editable() const
240 {
241         return IS_EDITABLE;
242 }
243
244
245 void InsetGraphics::write(Buffer const & buf, ostream & os) const
246 {
247         os << "Graphics\n";
248         params().Write(os, buf.filePath());
249 }
250
251
252 void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
253 {
254         string const token = lex.getString();
255
256         if (token == "Graphics")
257                 readInsetGraphics(lex, buf.filePath());
258         else
259                 lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
260
261         graphic_->update(params().as_grfxParams());
262 }
263
264
265 void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
266 {
267         bool finished = false;
268
269         while (lex.isOK() && !finished) {
270                 lex.next();
271
272                 string const token = lex.getString();
273                 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
274                                     << endl;
275
276                 if (token.empty()) {
277                         continue;
278                 } else if (token == "\\end_inset") {
279                         finished = true;
280                 } else {
281                         if (!params_.Read(lex, token, bufpath))
282                                 lyxerr << "Unknown token, " << token << ", skipping."
283                                         << std::endl;
284                 }
285         }
286 }
287
288
289 string const InsetGraphics::createLatexOptions() const
290 {
291         // Calculate the options part of the command, we must do it to a string
292         // stream since we might have a trailing comma that we would like to remove
293         // before writing it to the output stream.
294         ostringstream options;
295         if (!params().bb.empty())
296             options << "  bb=" << rtrim(params().bb) << ",\n";
297         if (params().draft)
298             options << "  draft,\n";
299         if (params().clip)
300             options << "  clip,\n";
301         if (!float_equal(params().scale, 0.0, 0.05)) {
302                 if (!float_equal(params().scale, 100.0, 0.05))
303                         options << "  scale=" << params().scale / 100.0
304                                 << ",\n";
305         } else {
306                 if (!params().width.zero())
307                         options << "  width=" << params().width.asLatexString() << ",\n";
308                 if (!params().height.zero())
309                         options << "  height=" << params().height.asLatexString() << ",\n";
310                 if (params().keepAspectRatio)
311                         options << "  keepaspectratio,\n";
312         }
313
314         // Make sure rotation angle is not very close to zero;
315         // a float can be effectively zero but not exactly zero.
316         if (!float_equal(params().rotateAngle, 0, 0.001)) {
317             options << "  angle=" << params().rotateAngle << ",\n";
318             if (!params().rotateOrigin.empty()) {
319                 options << "  origin=" << params().rotateOrigin[0];
320                 if (contains(params().rotateOrigin,"Top"))
321                     options << 't';
322                 else if (contains(params().rotateOrigin,"Bottom"))
323                     options << 'b';
324                 else if (contains(params().rotateOrigin,"Baseline"))
325                     options << 'B';
326                 options << ",\n";
327             }
328         }
329
330         if (!params().special.empty())
331             options << params().special << ",\n";
332
333         string opts = options.str();
334         // delete last ",\n"
335         return opts.substr(0, opts.size() - 2);
336 }
337
338
339 namespace {
340
341 enum CopyStatus {
342         SUCCESS,
343         FAILURE,
344         IDENTICAL_PATHS,
345         IDENTICAL_CONTENTS
346 };
347
348
349 std::pair<CopyStatus, string> const
350 copyToDirIfNeeded(string const & file_in, string const & dir)
351 {
352         using support::rtrim;
353
354         BOOST_ASSERT(AbsolutePath(file_in));
355
356         string const only_path = support::OnlyPath(file_in);
357         if (rtrim(support::OnlyPath(file_in) , "/") == rtrim(dir, "/"))
358                 return std::make_pair(IDENTICAL_PATHS, file_in);
359
360         string mangled;
361         if (support::zippedFile(file_in)) {
362                 string const ext = GetExtension(file_in);
363                 string const unzipped = support::unzippedFileName(file_in);
364                 mangled = FileName(unzipped).mangledFilename();
365                 mangled += "." + ext;
366         } else
367                 mangled = FileName(file_in).mangledFilename();
368
369         string const file_out = support::MakeAbsPath(mangled, dir);
370
371         unsigned long const checksum_in  = support::sum(file_in);
372         unsigned long const checksum_out = support::sum(file_out);
373
374         if (checksum_in == checksum_out)
375                 // Nothing to do...
376                 return std::make_pair(IDENTICAL_CONTENTS, file_out);
377
378         bool const success = support::copy(file_in, file_out);
379         if (!success) {
380                 lyxerr[Debug::GRAPHICS]
381                         << support::bformat(_("Could not copy the file\n%1$s\n"
382                                               "into the temporary directory."),
383                                             file_in)
384                         << std::endl;
385         }
386
387         CopyStatus status = success ? SUCCESS : FAILURE;
388         return std::make_pair(status, file_out);
389 }
390
391
392 string const stripExtensionIfPossible(string const & file, string const & to)
393 {
394         // No conversion is needed. LaTeX can handle the graphic file as is.
395         // This is true even if the orig_file is compressed.
396         if (formats.getFormat(to)->extension() == GetExtension(file))
397                 return RemoveExtension(file);
398         return file;
399 }
400
401 } // namespace anon
402
403
404 string const InsetGraphics::prepareFile(Buffer const & buf,
405                                         OutputParams const & runparams) const
406 {
407         string orig_file = params().filename.absFilename();
408         string const rel_file = params().filename.relFilename(buf.filePath());
409
410         // LaTeX can cope if the graphics file doesn't exist, so just return the
411         // filename.
412         if (!IsFileReadable(orig_file)) {
413                 lyxerr[Debug::GRAPHICS]
414                         << "InsetGraphics::prepareFile\n"
415                         << "No file '" << orig_file << "' can be found!" << endl;
416                 return rel_file;
417         }
418
419         // If the file is compressed and we have specified that it
420         // should not be uncompressed, then just return its name and
421         // let LaTeX do the rest!
422         bool const zipped = params().filename.isZipped();
423
424         if (zipped && params().noUnzip) {
425                 lyxerr[Debug::GRAPHICS]
426                         << "\tpass zipped file to LaTeX but with full path.\n";
427                 // LaTeX needs an absolute path, otherwise the
428                 // coresponding *.eps.bb file isn't found
429                 return orig_file;
430         }
431
432         // temp_file will contain the file for LaTeX to act on if, for example,
433         // we move it to a temp dir or uncompress it.
434         string temp_file = orig_file;
435
436         // We place all temporary files in the master buffer's temp dir.
437         // This is possible because we use mangled file names.
438         // This is necessary for DVI export.
439         string const temp_path = buf.getMasterBuffer()->temppath();
440
441         if (zipped) {
442                 CopyStatus status;
443                 boost::tie(status, temp_file) =
444                         copyToDirIfNeeded(orig_file, temp_path);
445
446                 if (status == FAILURE)
447                         return orig_file;
448
449                 orig_file = unzippedFileName(temp_file);
450                 if (!IsFileReadable(orig_file)) {
451                         unzipFile(temp_file);
452                         lyxerr[Debug::GRAPHICS]
453                                 << "\tunzipped to " << orig_file << endl;
454                 }
455         }
456
457         string const from = getExtFromContents(orig_file);
458         string const to   = findTargetFormat(from, runparams);
459         lyxerr[Debug::GRAPHICS]
460                 << "\t we have: from " << from << " to " << to << '\n';
461
462         // We're going to be running the exported buffer through the LaTeX
463         // compiler, so must ensure that LaTeX can cope with the graphics
464         // file format.
465
466         lyxerr[Debug::GRAPHICS]
467                 << "\tthe orig file is: " << orig_file << endl;
468
469         bool conversion_needed = true;
470         CopyStatus status;
471         boost::tie(status, temp_file) =
472                         copyToDirIfNeeded(orig_file, temp_path);
473
474         if (status == FAILURE)
475                 return orig_file;
476         else if (status == IDENTICAL_CONTENTS)
477                 conversion_needed = false;
478
479         if (from == to)
480                 return stripExtensionIfPossible(temp_file, to);
481
482         string const to_file_base = RemoveExtension(temp_file);
483         string const to_file = ChangeExtension(to_file_base, to);
484
485         // Do we need to perform the conversion?
486         // Yes if to_file does not exist or if temp_file is newer than to_file
487         if (!conversion_needed ||
488             support::compare_timestamps(temp_file, to_file) < 0) {
489                 lyxerr[Debug::GRAPHICS]
490                         << bformat(_("No conversion of %1$s is needed after all"),
491                                    rel_file)
492                         << std::endl;
493                 return to_file_base;
494         }
495
496         lyxerr[Debug::GRAPHICS]
497                 << "\tThe original file is " << orig_file << "\n"
498                 << "\tA copy has been made and convert is to be called with:\n"
499                 << "\tfile to convert = " << temp_file << '\n'
500                 << "\tto_file_base = " << to_file_base << '\n'
501                 << "\t from " << from << " to " << to << '\n';
502
503         // if no special converter defined, then we take the default one
504         // from ImageMagic: convert from:inname.from to:outname.to
505         if (!converters.convert(&buf, temp_file, to_file_base, from, to)) {
506                 string const command =
507                         "sh " + LibFileSearch("scripts", "convertDefault.sh") +
508                                 ' ' + from + ':' + temp_file + ' ' +
509                                 to + ':' + to_file_base + '.' + to;
510                 lyxerr[Debug::GRAPHICS]
511                         << "No converter defined! I use convertDefault.sh:\n\t"
512                         << command << endl;
513                 Systemcall one;
514                 one.startscript(Systemcall::Wait, command);
515                 if (!IsFileReadable(ChangeExtension(to_file_base, to))) {
516                         string str = bformat(_("No information for converting %1$s "
517                                 "format files to %2$s.\n"
518                                 "Try defining a convertor in the preferences."), from, to);
519                         Alert::error(_("Could not convert image"), str);
520                 }
521         }
522
523         return to_file_base;
524 }
525
526
527 int InsetGraphics::latex(Buffer const & buf, ostream & os,
528                          OutputParams const & runparams) const
529 {
530         // The master buffer. This is useful when there are multiple levels
531         // of include files
532         Buffer const * m_buffer = buf.getMasterBuffer();
533
534         // If there is no file specified or not existing,
535         // just output a message about it in the latex output.
536         lyxerr[Debug::GRAPHICS]
537                 << "insetgraphics::latex: Filename = "
538                 << params().filename.absFilename() << endl;
539
540         string const relative_file =
541                 params().filename.relFilename(buf.filePath());
542
543         // A missing (e)ps-extension is no problem for LaTeX, so
544         // we have to test three different cases
545 #ifdef WITH_WARNINGS
546 #warning uh, but can our cache handle it ? no.
547 #endif
548         string const file_ = params().filename.absFilename();
549         bool const file_exists =
550                 !file_.empty() &&
551                 (IsFileReadable(file_) ||               // original
552                  IsFileReadable(file_ + ".eps") ||      // original.eps
553                  IsFileReadable(file_ + ".ps"));        // original.ps
554         string const message = file_exists ?
555                 string() : string("bb = 0 0 200 100, draft, type=eps");
556         // if !message.empty() than there was no existing file
557         // "filename(.(e)ps)" found. In this case LaTeX
558         // draws only a rectangle with the above bb and the
559         // not found filename in it.
560         lyxerr[Debug::GRAPHICS]
561                 << "\tMessage = \"" << message << '\"' << endl;
562
563         // These variables collect all the latex code that should be before and
564         // after the actual includegraphics command.
565         string before;
566         string after;
567         // Do we want subcaptions?
568         if (params().subcaption) {
569                 before += "\\subfigure[" + params().subcaptionText + "]{";
570                 after = '}';
571         }
572         // We never use the starred form, we use the "clip" option instead.
573         before += "\\includegraphics";
574
575         // Write the options if there are any.
576         string const opts = createLatexOptions();
577         lyxerr[Debug::GRAPHICS] << "\tOpts = " << opts << endl;
578
579         if (!opts.empty() && !message.empty())
580                 before += ("[%\n" + opts + ',' + message + ']');
581         else if (!opts.empty() || !message.empty())
582                 before += ("[%\n" + opts + message + ']');
583
584         lyxerr[Debug::GRAPHICS]
585                 << "\tBefore = " << before
586                 << "\n\tafter = " << after << endl;
587
588
589         string latex_str = before + '{';
590         // "nice" means that the buffer is exported to LaTeX format but not
591         //        run through the LaTeX compiler.
592         if (runparams.nice) {
593                 // a relative filename should be relative to the master
594                 // buffer.
595                 string basename = params().filename.outputFilename(m_buffer->filePath());
596                 // Remove the extension so the LaTeX will use whatever
597                 // is appropriate (when there are several versions in
598                 // different formats)
599                 if (!(IsFileReadable(file_ + ".eps") || IsFileReadable(file_ + ".ps")))
600                         basename = RemoveExtension(basename);
601                 // This works only if the filename contains no dots besides
602                 // the just removed one. We can fool here by replacing all
603                 // dots with a macro whose definition is just a dot ;-)
604                 latex_str += subst(basename, ".", "\\lyxdot ");
605         } else if (file_exists) {
606                 // Make the filename relative to the lyx file
607                 // and remove the extension so the LaTeX will use whatever
608                 // is appropriate (when there are several versions in
609                 // different formats)
610                 latex_str += os::external_path(prepareFile(buf, runparams));
611         } else
612                 latex_str += relative_file + " not found!";
613
614         latex_str += '}' + after;
615         os << latex_str;
616
617         lyxerr[Debug::GRAPHICS] << "InsetGraphics::latex outputting:\n"
618                                 << latex_str << endl;
619         // Return how many newlines we issued.
620         return int(lyx::count(latex_str.begin(), latex_str.end(),'\n') + 1);
621 }
622
623
624 int InsetGraphics::plaintext(Buffer const &, ostream & os,
625                          OutputParams const &) const
626 {
627         // No graphics in ascii output. Possible to use gifscii to convert
628         // images to ascii approximation.
629         // 1. Convert file to ascii using gifscii
630         // 2. Read ascii output file and add it to the output stream.
631         // at least we send the filename
632         os << '<' << bformat(_("Graphics file: %1$s"),
633                              params().filename.absFilename()) << ">\n";
634         return 0;
635 }
636
637
638 int InsetGraphics::linuxdoc(Buffer const & buf, ostream & os,
639                             OutputParams const & runparams) const
640 {
641         string const file_name = runparams.nice ?
642                                 params().filename.relFilename(buf.filePath()):
643                                 params().filename.absFilename();
644
645         os << "<eps file=\"" << file_name << "\">\n";
646         os << "<img src=\"" << file_name << "\">";
647         return 0;
648 }
649
650
651 // For explanation on inserting graphics into DocBook checkout:
652 // http://en.tldp.org/LDP/LDP-Author-Guide/inserting-pictures.html
653 // See also the docbook guide at http://www.docbook.org/
654 int InsetGraphics::docbook(Buffer const &, ostream & os,
655                            OutputParams const &) const
656 {
657         // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
658         // need to switch to MediaObject. However, for now this is sufficient and
659         // easier to use.
660         os << "<graphic fileref=\"&" << graphic_label << ";\">";
661         return 0;
662 }
663
664
665 void InsetGraphics::validate(LaTeXFeatures & features) const
666 {
667         // If we have no image, we should not require anything.
668         if (params().filename.empty())
669                 return;
670
671         features.includeFile(graphic_label,
672                              RemoveExtension(params().filename.absFilename()));
673
674         features.require("graphicx");
675
676         if (features.nice()) {
677                 Buffer const * m_buffer = features.buffer().getMasterBuffer();
678                 string basename =
679                         params().filename.outputFilename(m_buffer->filePath());
680                 string const file_ = params().filename.absFilename();
681                 if (!(IsFileReadable(file_ + ".eps") || IsFileReadable(file_ + ".ps")))
682                         basename = RemoveExtension(basename);
683                 if (contains(basename, "."))
684                         features.require("lyxdot");
685         }
686
687         if (params().subcaption)
688                 features.require("subfigure");
689 }
690
691
692 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
693 {
694         // If nothing is changed, just return and say so.
695         if (params() == p && !p.filename.empty())
696                 return false;
697
698         // Copy the new parameters.
699         params_ = p;
700
701         // Update the display using the new parameters.
702         graphic_->update(params().as_grfxParams());
703
704         // We have changed data, report it.
705         return true;
706 }
707
708
709 InsetGraphicsParams const & InsetGraphics::params() const
710 {
711         return params_;
712 }
713
714
715 void InsetGraphics::editGraphics(InsetGraphicsParams const & p, Buffer const & buffer) const
716 {
717         string const file_with_path = p.filename.absFilename();
718         formats.edit(buffer, file_with_path, getExtFromContents(file_with_path));
719 }
720
721
722 string const InsetGraphicsMailer::name_("graphics");
723
724 InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)
725         : inset_(inset)
726 {}
727
728
729 string const InsetGraphicsMailer::inset2string(Buffer const & buffer) const
730 {
731         return params2string(inset_.params(), buffer);
732 }
733
734
735 void InsetGraphicsMailer::string2params(string const & in,
736                                         Buffer const & buffer,
737                                         InsetGraphicsParams & params)
738 {
739         params = InsetGraphicsParams();
740         if (in.empty())
741                 return;
742
743         istringstream data(in);
744         LyXLex lex(0,0);
745         lex.setStream(data);
746
747         string name;
748         lex >> name;
749         if (!lex || name != name_)
750                 return print_mailer_error("InsetGraphicsMailer", in, 1, name_);
751
752         InsetGraphics inset;
753         inset.readInsetGraphics(lex, buffer.filePath());
754         params = inset.params();
755 }
756
757
758 string const
759 InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
760                                    Buffer const & buffer)
761 {
762         ostringstream data;
763         data << name_ << ' ';
764         params.Write(data, buffer.filePath());
765         data << "\\end_inset\n";
766         return data.str();
767 }