]> git.lyx.org Git - features.git/commitdiff
Herbert's unzip et al patch.
authorAngus Leeming <leeming@lyx.org>
Thu, 7 Feb 2002 19:37:34 +0000 (19:37 +0000)
committerAngus Leeming <leeming@lyx.org>
Thu, 7 Feb 2002 19:37:34 +0000 (19:37 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3502 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/controllers/ChangeLog
src/frontends/controllers/ControlGraphics.C
src/graphics/ChangeLog
src/graphics/GraphicsCacheItem.C
src/insets/ChangeLog
src/insets/insetgraphics.C
src/support/ChangeLog
src/support/filetools.C
src/support/filetools.h

index 2c9d1004ff7895471c14cf8c5b63144bf4e9c9b1..bc13a121d20da93e14b9ac5a857acfbc8e6ba67a 100644 (file)
@@ -1,3 +1,10 @@
+2002-02-06  Herbert Voss  <voss@lyx.org>
+
+       * ControlGraphics.[C] (readBB): search also in
+       g'zipped files. If it is useful than PK and compress
+       should also be supported. use unzipFile() from
+       support/filetools
+
 2002-02-06  Jean-Marc Lasgouttes  <lasgouttes@freesurf.fr>
 
        * ControlRef.C (getBufferName): new method to get a real buffer
index 63aa81561b8d3d3c2b6c1af063d50d902b8e6956..5b94c7b66bc9d7e4d84a36b6f025442e86676784 100644 (file)
@@ -36,6 +36,7 @@
 #include "helper_funcs.h"      // for browseFile
 #include "support/lstrings.h"
 #include "support/filetools.h" // for AddName
+#include "support/syscall.h"   // for zippedFile()
 #include "BufferView.h"
 
 using std::pair;
@@ -108,8 +109,11 @@ string const ControlGraphics::readBB(string const & file)
 // end of the file. Than we have in the header a
 //     %%BoundingBox: (atend)
 // In this case we must check until the end.
-       std::ifstream is(file.c_str());
-       if (!contains(getExtFromContents(file),"ps"))   // bb exists? 
+       string file_ = file;
+       if (zippedFile(file_))
+           file_ = unzipFile(file_);
+       std::ifstream is(file_.c_str());
+       if (!contains(getExtFromContents(file_),"ps"))  // bb exists? 
            return string();
        while (is) {
                string s;
index 2fb11db6374448fa5424794ed981e03e1c048790..ab5dfdf2e477a2c4cda3b5f1daa0c7a0ea7f0910 100644 (file)
@@ -1,3 +1,7 @@
+2002-02-07  Herbert Voss  <voss@lyx.org>
+
+       * GraphicsCacheItem.C: use unzipFile() from support/filetools
+
 2002-01-31  Herbert Voss  <voss@lyx.org>
 
        * GraphicsCacheItem.h: handle filetype without the file extension;
index 8c171714fb43addef26ee3ec3074179407ef3722..918dfc2c67b8f38526df2ea9a59fd4f7bca016a2 100644 (file)
@@ -26,7 +26,6 @@
 #include "debug.h"
 #include "support/LAssert.h"
 #include "gettext.h"
-#include "support/syscall.h"
 #include "lyxfunc.h"
 
 using std::endl;
@@ -139,9 +138,9 @@ bool GraphicsCacheItem::convertImage(string const & filename)
    -----------snip-------------*/
 
        lyxerr << "GetExtension: " << GetExtension(filename_) << endl;
-       bool zipped = GetExtension(filename_).compare("gz") == 0;
+       bool const zipped = zippedFile(filename_);
        if (zipped)
-           filename_ = ChangeExtension(filename_, string());   // snip the ".gz"
+           filename_ = unzipFile(filename_);
        string const from = getExtFromContents(filename_);      // get the type
        lyxerr << "GetExtFromContents: " << from << endl;
        string const to = findTargetFormat(from);
@@ -149,13 +148,6 @@ bool GraphicsCacheItem::convertImage(string const & filename)
        if (to.empty()) 
                return false;
        // manage zipped files. unzip them first into the tempdir
-       if (zipped) {
-           tempfile = lyx::tempName(string(), filename_);
-           // Run gunzip
-           string const command = "gunzip -c "+filename+" > "+tempfile;
-           Systemcalls one(Systemcalls::System, command); 
-           filename_ = tempfile;
-       }
        if (from == to) {
                // No conversion needed!
                // Saves more than just time: prevents the deletion of
index a5ce8165ccba1b45f7d0959df0e4aaa16a91b7e1..3448c7d2b036d65e0571dbc6aa53476c63f63a04 100644 (file)
@@ -1,3 +1,9 @@
+2002-02-06  Herbert Voss  <voss@lyx.org>
+
+       * insetgraphic.C: prepareFile(): don't convert the file, when
+       it's not viewed in LyX. Saves a lot of time when opening a
+       document!
+
 2002-02-05  Herbert Voss  <voss@lyx.org>
 
        * insetgraphic.C: 
index 819ba4b88f9700e862d15e38e5214b67acf01569..f82aca731497f79b44944ee8269b00a68ba2c075 100644 (file)
@@ -37,10 +37,6 @@ Known BUGS:
                its original size and color, resizing is done in the final output,
                but not in the LyX window.
 
-       * EPS figures are not fully detected, they may have a lot of possible
-               suffixes so we need to read the file and detect if it's EPS or not.
-               [Implemented, need testing]
-               
 TODO Before initial production release:
     * Replace insetfig everywhere
         * Search for comments of the form
@@ -461,26 +457,26 @@ string const InsetGraphics::createLatexOptions() const
        // before writing it to the output stream.
        ostringstream options;
        if (!params.bb.empty())
-           options << "  bb=" << strip(params.bb) << ",%\n";
+           options << "  bb=" << strip(params.bb) << ",\n";
        if (params.draft)
-           options << "  draft,%\n";
+           options << "  draft,\n";
        if (params.clip)
-           options << "  clip,%\n";
+           options << "  clip,\n";
        if (params.size_type == InsetGraphicsParams::WH) {
            if (!params.width.zero())
-               options << "  width=" << params.width.asLatexString() << ",%\n";
+               options << "  width=" << params.width.asLatexString() << ",\n";
            if (!params.height.zero())
-               options << "  height=" << params.height.asLatexString() << ",%\n";
+               options << "  height=" << params.height.asLatexString() << ",\n";
        } else if (params.size_type == InsetGraphicsParams::SCALE) {
            if (params.scale > 0)
-               options << "  scale=" << double(params.scale)/100.0 << ",%\n";
+               options << "  scale=" << double(params.scale)/100.0 << ",\n";
        }
        if (params.keepAspectRatio)
-           options << "  keepaspectratio,%\n";
+           options << "  keepaspectratio,\n";
        // Make sure it's not very close to zero, a float can be effectively
        // zero but not exactly zero.
        if (!lyx::float_equal(params.rotateAngle, 0, 0.001) && params.rotate) {
-           options << "  angle=" << params.rotateAngle << ",%\n";
+           options << "  angle=" << params.rotateAngle << ",\n";
            if (!params.rotateOrigin.empty()) {
                options << "  origin=" << params.rotateOrigin[0];
                if (contains(params.rotateOrigin,"Top"))
@@ -489,14 +485,13 @@ string const InsetGraphics::createLatexOptions() const
                    options << 'b';
                else if (contains(params.rotateOrigin,"Baseline"))
                    options << 'B';
-               options << ",%\n";
+               options << ",\n";
            }
        }
        if (!params.special.empty())
-           options << params.special << ",%\n";
+           options << params.special << ",\n";
        string opts = options.str().c_str();
-       opts = opts.substr(0,opts.size()-3);    // delete last ",%\n"
-       return opts;
+       return opts.substr(0,opts.size()-2);    // delete last ",\n"
 }
 
 namespace {
@@ -538,13 +533,24 @@ string const InsetGraphics::prepareFile(Buffer const *buf) const
        //   convert_place = original file directory
        //   return original filename without the extension
        //
-       // Get the extension (format) of the original file.
-       // we handle it like a virtual one, so we can have
-       // different extensions with the same type.
+       // first check if file is viewed in LyX. First local
+       // than global
+       if ((params.display == InsetGraphicsParams::NONE) || 
+           ((params.display == InsetGraphicsParams::DEFAULT) &&
+           (lyxrc.display_graphics == "no"))) {
+               lyxerr << "InsetGraphics::no converting of: " << params.filename << "\n";
+               return params.filename;
+       }
        // if it's a zipped one, than let LaTeX do the rest!!!
-       if (zippedFile(params.filename))        
+       if (zippedFile(params.filename)) {
+           lyxerr << "InsetGraphics::prepareFilter(zippedFile): return " 
+                   << params.filename << endl;
            return params.filename;
+       }
        // now we have unzipped files
+       // Get the extension (format) of the original file.
+       // we handle it like a virtual one, so we can have
+       // different extensions with the same type.
        string const extension = getExtFromContents(params.filename);
        // Are we creating a PDF or a PS file?
        // (Should actually mean, are we usind latex or pdflatex).
index 595209e1c8166183141f9bb5c396d6de345c75f4..9f3ce4b56dd4c11616cd964d39ca526d80db330f 100644 (file)
@@ -1,3 +1,8 @@
+2002-02-06  Herbert Voss  <voss@perce.de>
+
+       * filetools.[Ch]: fix sume bugs for detecting zipped files 
+       adding unzipFile()
+
 2002-02-04  Herbert Voss  <voss@perce.de>
 
        * filetools.[Ch]: add ifZippedFile() for zipped
index 644e98f809d440604f41a11746145e949978e14a..8b7b9d9c17ed6540dcac4e9a6dd916e6d7d14b1d 100644 (file)
@@ -33,6 +33,7 @@
 #include <cerrno>
 #include "debug.h"
 #include "support/lstrings.h"
+#include "support/syscall.h"
 
 #include "filetools.h"
 #include "LSubstring.h"
@@ -970,13 +971,13 @@ string const GetExtension(string const & name)
 // JPG JFIF
 // PDF %PDF-...
 // PNG .PNG...
-// PS  %!PS-Adobe-2.0
+// PS  %!PS-Adobe-2.0 or 1.0,  no "EPSF"!
 // XBM ... static char ...
 // XPM /* XPM */
 //
-// GZIP        \213\037\008\008...     http://www.ietf.org/rfc/rfc1952.txt
+// GZIP        \037\213\010\010...     http://www.ietf.org/rfc/rfc1952.txt
 // ZIP PK...                   http://www.halyava.ru/document/ind_arch.htm
-// Z   \177\037                UNIX compress
+// Z   \037\177                UNIX compress
 /// return the "extension" which belongs to the contents
 string const getExtFromContents(string const & filename) {
        if (filename.empty() || !IsFileReadable(filename)) 
@@ -984,16 +985,16 @@ string const getExtFromContents(string const & filename) {
        ifstream ifs(filename.c_str());
        if (!ifs) 
            return string();    // Couldn't open file...
-       string const gzipStamp = "\213\037\008\008";    // gnuzip
+       string const gzipStamp = "\037\213\010\010";    // gnuzip
        string const zipStamp = "PK";                   // PKZIP
-       string const compressStamp = "\177\037";        // compress
-       int const max_count = 50; // Maximum strings to read to attempt recognition
-       int count = 0; // Counter of attempts.
+       string const compressStamp = "\037\177";        // compress
+       int const max_count = 50;       // Maximum strings to read
+       int count = 0;                  // Counter of attempts.
        string str;
        bool zipChecked = false;
        for (; count < max_count; ++count) {
                if (ifs.eof()) {
-                       lyxerr[Debug::INFO] << "InsetGraphics (classifyFiletype)"
+                       lyxerr[Debug::INFO] << "filetools(getExtFromContents)"
                                " End of file reached and it wasn't found to be a known Type!" << endl;
                        break;
                }
@@ -1009,8 +1010,8 @@ string const getExtFromContents(string const & filename) {
                        return "compress";
                    zipChecked = true;
                }
-               if (contains(str,"EPSF"))
-                   return "eps";
+               if (contains(str,"EPSF")) // dummy, if we have wrong file
+                   return "eps";         // description like "%!PS-Adobe-2.0EPSF"
                else if (contains(str,"GIF"))
                    return "gif";
                else if (contains(str,"JFIF"))
@@ -1019,14 +1020,19 @@ string const getExtFromContents(string const & filename) {
                    return "pdf";
                else if (contains(str,"PNG"))
                    return "png";
-               else if (contains(str,"%!PS-Adobe-"))
-                   return "ps";                // eps here no more possible
-               else if (contains(str,"static char"))
+               else if (contains(str,"%!PS-Adobe")) { // eps or ps
+                   // test if it's ps or eps
+                   ifs >> str;
+                   if (contains(str,"EPSF"))
+                       return "eps";
+                   else 
+                       return "ps";
+               } else if (contains(str,"static char"))
                    return "xbm";
                else if (contains(str,"XPM"))
                    return "xpm";
        }
-       lyxerr[Debug::INFO] << "InsetGraphics (classifyFiletype)"
+       lyxerr[Debug::INFO] << "filetools(getExtFromContents)"
                " Couldn't find a known Type!" << endl;
        return string();
 }
@@ -1035,11 +1041,23 @@ string const getExtFromContents(string const & filename) {
 /// check for zipped file
 bool zippedFile(string const & name) {
        string const type = getExtFromContents(name);
-       if (contains("gzip zip",type) && !type.empty())
+       if (contains("gzip zip compress",type) && !type.empty())
            return true;
        return false;
 }
 
+string const unzipFile(string const & zipped_file)
+{
+    string const file = ChangeExtension(zipped_file, string());
+    string  const tempfile = lyx::tempName(string(), file);
+    // Run gunzip
+    string const command = "gunzip -c "+file+" > "+tempfile;
+    Systemcalls one(Systemcalls::System, command);
+    // test that command was executed successfully
+    return tempfile;
+}
+
+
 // Creates a nice compact path for displaying
 string const
 MakeDisplayPath (string const & path, unsigned int threshold)
index 5299d8ad619d19e5a49bedb46f3a3adc560c9a05..1c667a1414da7b44d9859c9f00b8deb05c45d47c 100644 (file)
@@ -138,6 +138,9 @@ string const getExtFromContents(string const & name);
 /// check for zipped file
 bool zippedFile(string const & name);
 
+/// unzip a file
+string const unzipFile(string const & zipped_file);
+
 /// Returns true is path is absolute
 bool AbsolutePath(string const & path);