+
+ CopyStatus status = success ? SUCCESS : FAILURE;
+ return std::make_pair(status, file_out);
+}
+
+
+std::pair<CopyStatus, string> const
+copyToDirIfNeeded(string const & file_in, string const & dir, bool zipped)
+{
+ using support::rtrim;
+
+ BOOST_ASSERT(AbsolutePath(file_in));
+
+ string const only_path = support::OnlyPath(file_in);
+ if (rtrim(support::OnlyPath(file_in) , "/") == rtrim(dir, "/"))
+ return std::make_pair(IDENTICAL_PATHS, file_in);
+
+ string mangled = FileName(file_in).mangledFilename();
+ if (zipped) {
+ // We need to change _eps.gz to .eps.gz. The mangled name is
+ // still unique because of the counter in mangledFilename().
+ // We can't just call mangledFilename() with the zip
+ // extension removed, because base.eps and base.eps.gz may
+ // have different content but would get the same mangled
+ // name in this case.
+ string const base = RemoveExtension(unzippedFileName(file_in));
+ string::size_type const ext_len = file_in.length() - base.length();
+ mangled[mangled.length() - ext_len] = '.';
+ }
+ string const file_out = support::MakeAbsPath(mangled, dir);
+
+ return copyFileIfNeeded(file_in, file_out);
+}
+
+
+string const stripExtensionIfPossible(string const & file)
+{
+ // Remove the extension so the LaTeX will use whatever
+ // is appropriate (when there are several versions in
+ // different formats)
+ // This works only if the filename contains no dots besides
+ // the just removed one. We can fool here by replacing all
+ // dots with a macro whose definition is just a dot ;-)
+ // The automatic format selection does not work if the file
+ // name is escaped.
+ string const latex_name = latex_path(file,
+ lyx::support::EXCLUDE_EXTENSION);
+ if (contains(latex_name, '"'))
+ return latex_name;
+ return latex_path(RemoveExtension(file),
+ lyx::support::PROTECT_EXTENSION,
+ lyx::support::ESCAPE_DOTS);
+}
+
+
+string const stripExtensionIfPossible(string const & file, string const & to)
+{
+ // No conversion is needed. LaTeX can handle the graphic file as is.
+ // This is true even if the orig_file is compressed.
+ string const to_format = formats.getFormat(to)->extension();
+ string const file_format = GetExtension(file);
+ // for latex .ps == .eps
+ if (to_format == file_format ||
+ (to_format == "eps" && file_format == "ps") ||
+ (to_format == "ps" && file_format == "eps"))
+ return stripExtensionIfPossible(file);
+ return latex_path(file, lyx::support::EXCLUDE_EXTENSION);
+}
+
+} // namespace anon
+
+
+string const InsetGraphics::prepareFile(Buffer const & buf,
+ OutputParams const & runparams) const
+{
+ // The following code depends on non-empty filenames
+ if (params().filename.empty())
+ return string();
+
+ string const orig_file = params().filename.absFilename();
+ string const rel_file = params().filename.relFilename(buf.filePath());
+
+ // If the file is compressed and we have specified that it
+ // should not be uncompressed, then just return its name and
+ // let LaTeX do the rest!
+ bool const zipped = params().filename.isZipped();
+
+ // temp_file will contain the file for LaTeX to act on if, for example,
+ // we move it to a temp dir or uncompress it.
+ string temp_file = orig_file;
+
+ // The master buffer. This is useful when there are multiple levels
+ // of include files
+ Buffer const * m_buffer = buf.getMasterBuffer();
+
+ // Return the output name if the file does not exist.
+ // We are not going to change the extension or using the name of the
+ // temporary file, the code is already complicated enough.
+ if (!IsFileReadable(orig_file))
+ return params().filename.outputFilename(m_buffer->filePath());
+
+ // We place all temporary files in the master buffer's temp dir.
+ // This is possible because we use mangled file names.
+ // This is necessary for DVI export.
+ string const temp_path = m_buffer->temppath();
+
+ CopyStatus status;
+ boost::tie(status, temp_file) =
+ copyToDirIfNeeded(orig_file, temp_path, zipped);
+
+ if (status == FAILURE)
+ return orig_file;
+
+ // a relative filename should be relative to the master
+ // buffer.
+ // "nice" means that the buffer is exported to LaTeX format but not
+ // run through the LaTeX compiler.
+ string const output_file = os::external_path(runparams.nice ?
+ params().filename.outputFilename(m_buffer->filePath()) :
+ OnlyFilename(temp_file));
+ string const source_file = runparams.nice ? orig_file : temp_file;
+
+ if (zipped) {
+ if (params().noUnzip) {
+ // We don't know whether latex can actually handle
+ // this file, but we can't check, because that would
+ // mean to unzip the file and thereby making the
+ // noUnzip parameter meaningless.
+ lyxerr[Debug::GRAPHICS]
+ << "\tpass zipped file to LaTeX.\n";
+
+ string const bb_orig_file = ChangeExtension(orig_file, "bb");
+ if (runparams.nice) {
+ runparams.exportdata->addExternalFile("latex",
+ bb_orig_file,
+ ChangeExtension(output_file, "bb"));
+ } else {
+ // LaTeX needs the bounding box file in the
+ // tmp dir
+ string bb_file = ChangeExtension(temp_file, "bb");
+ boost::tie(status, bb_file) =
+ copyFileIfNeeded(bb_orig_file, bb_file);
+ if (status == FAILURE)
+ return orig_file;
+ runparams.exportdata->addExternalFile("latex",
+ bb_file);
+ }
+ runparams.exportdata->addExternalFile("latex",
+ source_file, output_file);
+ runparams.exportdata->addExternalFile("dvi",
+ source_file, output_file);
+ // We can't strip the extension, because we don't know
+ // the unzipped file format
+ return latex_path(output_file,
+ lyx::support::EXCLUDE_EXTENSION);
+ }
+
+ string const unzipped_temp_file = unzippedFileName(temp_file);
+ if (compare_timestamps(unzipped_temp_file, temp_file) > 0) {
+ // temp_file has been unzipped already and
+ // orig_file has not changed in the meantime.
+ temp_file = unzipped_temp_file;
+ lyxerr[Debug::GRAPHICS]
+ << "\twas already unzipped to " << temp_file
+ << endl;
+ } else {
+ // unzipped_temp_file does not exist or is too old
+ temp_file = unzipFile(temp_file);
+ lyxerr[Debug::GRAPHICS]
+ << "\tunzipped to " << temp_file << endl;
+ }
+ }
+
+ string const from = formats.getFormatFromFile(temp_file);
+ if (from.empty()) {
+ lyxerr[Debug::GRAPHICS]
+ << "\tCould not get file format." << endl;
+ return orig_file;
+ }
+ string const to = findTargetFormat(from, runparams);
+ string const ext = formats.extension(to);
+ lyxerr[Debug::GRAPHICS]
+ << "\t we have: from " << from << " to " << to << '\n';
+
+ // We're going to be running the exported buffer through the LaTeX
+ // compiler, so must ensure that LaTeX can cope with the graphics
+ // file format.
+
+ lyxerr[Debug::GRAPHICS]
+ << "\tthe orig file is: " << orig_file << endl;
+
+ if (from == to) {
+ // The extension of temp_file might be != ext!
+ runparams.exportdata->addExternalFile("latex", source_file,
+ output_file);
+ runparams.exportdata->addExternalFile("dvi", source_file,
+ output_file);
+ return stripExtensionIfPossible(output_file, to);
+ }
+
+ string const to_file = ChangeExtension(temp_file, ext);
+ string const output_to_file = ChangeExtension(output_file, ext);
+
+ // Do we need to perform the conversion?
+ // Yes if to_file does not exist or if temp_file is newer than to_file
+ if (compare_timestamps(temp_file, to_file) < 0) {
+ lyxerr[Debug::GRAPHICS]
+ << bformat(_("No conversion of %1$s is needed after all"),
+ rel_file)
+ << std::endl;
+ runparams.exportdata->addExternalFile("latex", to_file,
+ output_to_file);
+ runparams.exportdata->addExternalFile("dvi", to_file,
+ output_to_file);
+ return stripExtensionIfPossible(output_file);