The graphics inset has currently three problems concerning zipped files and
"not nice" latex output:

1. If the file is zipped and the noUnzip parameter is true, lyx presents an
absolute filename to latex to ensure that the corresponding .bb file can be
found. This will be a problem for dvi export when it works again (yes, I
did not forget that).
Solution: Copy both the zipped file and the .bb file to the temp dir. The
filename is still absolute, but this can be changed later.

2. If the file is zipped and the noUnzip parameter is false, the file will
only be unzipped before the first latex run. This is a problem if the
original file has changed before a subsequent latex run.
Solution: Explicitly check wether the original file has changed.

3. The mangled filename of the zipped file is constructed from the filename
with the .gz extension removed. This ensures that the mangled name becomes
*.eps.gz instead of *_eps.gz. Problem: The files a.eps and a.eps.gz get the
same mangled name, although they may be completely different.
Solution: construct the mangled name from the full name and change *_eps.gz
to *.eps.gz afterwards.

The attached archive contains a test file and images for problem 3.
The attached patch fixes these problems, mainly by rearranging the code. A
side effect is that the variable "orig_file" is now always the orig file
name and not sometimes a temp file name. I'll apply it if nobody complains.


Georg
Index: src/insets/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.1011
diff -u -r1.1011 ChangeLog
--- src/insets/ChangeLog	29 Apr 2004 11:33:31 -0000	1.1011
+++ src/insets/ChangeLog	5 May 2004 08:23:59 -0000
@@ -1,3 +1,12 @@
+2004-05-05  Georg Baum  <[EMAIL PROTECTED]>
+
+	* insetgraphics.C (prepareFile): copy zipped file and corresponding
+	.bb file to the tmp dir if we don't unzip it (needed for dvi export)
+	* insetgraphics.C (prepareFile): unzip zipped file also if it has
+	changed since the last run
+	* insetgraphics.C (copyToDirIfNeeded): split into copyToDirIfNeeded
+	and copyFileIfNeeded.
+
 2004-04-29  Angus Leeming  <[EMAIL PROTECTED]>
 
 	* ExternalTemplate.[Ch]: remove redundant #includes.
Index: src/insets/insetgraphics.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphics.C,v
retrieving revision 1.246
diff -u -r1.246 insetgraphics.C
--- src/insets/insetgraphics.C	29 Apr 2004 09:24:29 -0000	1.246
+++ src/insets/insetgraphics.C	5 May 2004 08:24:00 -0000
@@ -85,6 +85,7 @@
 using lyx::support::AbsolutePath;
 using lyx::support::bformat;
 using lyx::support::ChangeExtension;
+using lyx::support::compare_timestamps;
 using lyx::support::contains;
 using lyx::support::FileName;
 using lyx::support::float_equal;
@@ -343,26 +344,10 @@
 
 
 std::pair<CopyStatus, string> const
-copyToDirIfNeeded(string const & file_in, string const & dir)
+copyFileIfNeeded(string const & file_in, string const & file_out)
 {
-	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;
-	if (support::zippedFile(file_in)) {
-		string const ext = GetExtension(file_in);
-		string const unzipped = support::unzippedFileName(file_in);
-		mangled = FileName(unzipped).mangledFilename();
-		mangled += "." + ext;
-	} else
-		mangled = FileName(file_in).mangledFilename();
-
-	string const file_out = support::MakeAbsPath(mangled, dir);
+	BOOST_ASSERT(AbsolutePath(file_out));
 
 	unsigned long const checksum_in  = support::sum(file_in);
 	unsigned long const checksum_out = support::sum(file_out);
@@ -385,6 +370,35 @@
 }
 
 
+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, string const & to)
 {
 	// No conversion is needed. LaTeX can handle the graphic file as is.
@@ -417,14 +431,6 @@
 	// let LaTeX do the rest!
 	bool const zipped = params().filename.isZipped();
 
-	if (zipped && params().noUnzip) {
-		lyxerr[Debug::GRAPHICS]
-			<< "\tpass zipped file to LaTeX but with full path.\n";
-		// LaTeX needs an absolute path, otherwise the
-		// coresponding *.eps.bb file isn't found
-		return orig_file;
-	}
-
 	// 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;
@@ -434,23 +440,48 @@
 	// This is necessary for DVI export.
 	string const temp_path = buf.getMasterBuffer()->temppath();
 
+	bool conversion_needed = true;
+
+	CopyStatus status;
+	boost::tie(status, temp_file) =
+			copyToDirIfNeeded(orig_file, temp_path, zipped);
+
+	if (status == FAILURE)
+		return orig_file;
+	else if (status == IDENTICAL_CONTENTS)
+		conversion_needed = false;
+
 	if (zipped) {
-		CopyStatus status;
-		boost::tie(status, temp_file) =
-			copyToDirIfNeeded(orig_file, temp_path);
-
-		if (status == FAILURE)
-			return orig_file;
-
-		orig_file = unzippedFileName(temp_file);
-		if (!IsFileReadable(orig_file)) {
-			unzipFile(temp_file);
+		if (params().noUnzip) {
 			lyxerr[Debug::GRAPHICS]
-				<< "\tunzipped to " << orig_file << endl;
+				<< "\tpass zipped file to LaTeX.\n";
+			// LaTeX needs the bounding box file in the tmp dir
+			string bb_file;
+			boost::tie(status, bb_file) =
+				copyFileIfNeeded(ChangeExtension(orig_file, "bb"),
+				                 ChangeExtension(temp_file, "bb"));
+			if (status == FAILURE)
+				return orig_file;
+			return temp_file;
+		}
+
+		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 = getExtFromContents(orig_file);
+	string const from = getExtFromContents(temp_file);
 	string const to   = findTargetFormat(from, runparams);
 	lyxerr[Debug::GRAPHICS]
 		<< "\t we have: from " << from << " to " << to << '\n';
@@ -462,16 +493,6 @@
 	lyxerr[Debug::GRAPHICS]
 		<< "\tthe orig file is: " << orig_file << endl;
 
-	bool conversion_needed = true;
-	CopyStatus status;
-	boost::tie(status, temp_file) =
-			copyToDirIfNeeded(orig_file, temp_path);
-
-	if (status == FAILURE)
-		return orig_file;
-	else if (status == IDENTICAL_CONTENTS)
-		conversion_needed = false;
-
 	if (from == to)
 		return stripExtensionIfPossible(temp_file, to);
 
@@ -481,7 +502,7 @@
 	// Do we need to perform the conversion?
 	// Yes if to_file does not exist or if temp_file is newer than to_file
 	if (!conversion_needed ||
-	    support::compare_timestamps(temp_file, to_file) < 0) {
+	    compare_timestamps(temp_file, to_file) < 0) {
 		lyxerr[Debug::GRAPHICS]
 			<< bformat(_("No conversion of %1$s is needed after all"),
 				   rel_file)

Attachment: graphics-zipped-test.tar.bz2
Description: BZip2 compressed data

Reply via email to