Herbert Voss wrote:

> the next patch to get the file type from it's contents.
> Especially to Garst, please try.


please commit the attached graphics2.diff instead of the one

from the previous mail.

this patch does:
- read properly scale
- sets display_screen dependet to the one in pref
- get the type of the graphic only from it's contents
   so that the file-extension is now not important. supported
   are gif, jpg, eps, ps, xbm, xpm, pdf
   let me know, if you miss a format! In this case send me
   a demofile of that type.
- support ps-graphic files
- the graphic files may be zipped. At this time only
   *.gz is supported.

Herbert



-- 
http://www.lyx.org/help/
Index: lib/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ChangeLog,v
retrieving revision 1.157
diff -u -r1.157 ChangeLog
--- lib/ChangeLog       2002/01/30 19:18:40     1.157
+++ lib/ChangeLog       2002/01/31 11:03:21
@@ -1,3 +1,7 @@
+2002-01-30  Herbert Voss  <[EMAIL PROTECTED]>
+
+       * configure.m4: add convert-support for ps-files
+
 2002-01-29  Herbert Voss  <[EMAIL PROTECTED]>
 
        * ui/default.ui: delete old figure entries
Index: lib/configure.m4
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/configure.m4,v
retrieving revision 1.28
diff -u -r1.28 configure.m4
--- lib/configure.m4    2002/01/13 13:07:24     1.28
+++ lib/configure.m4    2002/01/31 11:03:21
@@ -371,6 +371,7 @@
        eps_to_xpm="convert EPS:\$\$i XPM:\$\$o" 
        jpg_to_xpm="convert JPG:\$\$i XPM:\$\$o"
        png_to_xpm="convert PNG:\$\$i XPM:\$\$o"
+       ps_to_xpm="convert PS:\$\$i XPM:\$\$o" 
 fi
 
 SEARCH_PROG([For an EPS -> PDF converter], EPSTOPDF, epstopdf)
Index: src/frontends/controllers/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ChangeLog,v
retrieving revision 1.128
diff -u -r1.128 ChangeLog
--- src/frontends/controllers/ChangeLog 2002/01/30 19:14:09     1.128
+++ src/frontends/controllers/ChangeLog 2002/01/31 11:03:22
@@ -1,3 +1,8 @@
+2002-01-30  Herbert Voss  <[EMAIL PROTECTED]>
+
+       * ControlGraphic.[C]: do not search the whole file, when
+       getting the bb
+
 2002-01-29  Herbert Voss  <[EMAIL PROTECTED]>
 
        * ControlGraphic.[C]: added a button for document path
Index: src/frontends/controllers/ControlGraphics.C
===================================================================
RCS file: 
/usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ControlGraphics.C,v
retrieving revision 1.16
diff -u -r1.16 ControlGraphics.C
--- src/frontends/controllers/ControlGraphics.C 2002/01/30 19:14:09     1.16
+++ src/frontends/controllers/ControlGraphics.C 2002/01/31 11:03:22
@@ -40,7 +40,6 @@
 
 using std::pair;
 using std::make_pair;
-
 using std::ifstream;
 
 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
@@ -107,7 +106,9 @@
 // to check a bit more. 
 //     ControlGraphics::bbChanged = false;
        std::ifstream is(file.c_str());
-       while (is) {
+       int count = 0;
+       int const max_count = 50;       // don't search the whole file
+       while (is && (++count < max_count)) {
                string s;
                is >> s;
                if (contains(s,"%%BoundingBox:")) {
Index: src/graphics/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/graphics/ChangeLog,v
retrieving revision 1.25
diff -u -r1.25 ChangeLog
--- src/graphics/ChangeLog      2002/01/29 09:26:24     1.25
+++ src/graphics/ChangeLog      2002/01/31 11:03:22
@@ -1,3 +1,8 @@
+2002-01-31  Herbert Voss  <[EMAIL PROTECTED]>
+
+       * GraphicsCacheItem.h: handle filetype without the file extension;
+       getting type from contents; handle zipped files
+
 2002-01-27  Herbert Voss  <[EMAIL PROTECTED]>
 
        * GraphicsCacheItem.h: added Converting to the ImageStatus enum.
Index: src/graphics/GraphicsCacheItem.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/graphics/GraphicsCacheItem.C,v
retrieving revision 1.21
diff -u -r1.21 GraphicsCacheItem.C
--- src/graphics/GraphicsCacheItem.C    2001/09/20 13:16:17     1.21
+++ src/graphics/GraphicsCacheItem.C    2002/01/31 11:03:22
@@ -5,7 +5,8 @@
  *          Copyright 1995 Matthias Ettrich.
  *          Copyright 1995-2001 The LyX Team.
  *
- *          This file Copyright 2000 Baruch Even
+ * \author Baruch Even
+ * \author Herbert Voss <[EMAIL PROTECTED]>
  * ================================================= */
 
 #include <config.h>
@@ -25,6 +26,8 @@
 #include "debug.h"
 #include "support/LAssert.h"
 #include "gettext.h"
+#include "support/syscall.h"
+#include "lyxfunc.h"
 
 using std::endl;
 
@@ -73,8 +76,7 @@
 GraphicsCacheItem::getImage() const { return image_.get(); }
 
 
-void
-GraphicsCacheItem::imageConverted(bool success)
+void GraphicsCacheItem::imageConverted(bool success)
 {
        // Debug output
        string text = "succeeded";
@@ -109,56 +111,77 @@
                if (converters.isReachable(from, *iter))
                        break;
        }
-       
        if (iter == end) {
                // We do not know how to convert the image to something loadable.
                lyxerr << "ERROR: Do not know how to convert image." << std::endl;
                return string();
        }
-
        return (*iter);
 }
 
 } // anon namespace
 
        
-bool
-GraphicsCacheItem::convertImage(string const & filename)
+bool GraphicsCacheItem::convertImage(string const & filename)
 {
-       string const from = GetExtension(filename);
+       setStatus(GraphicsCacheItem::Converting);
+       string filename_ = string(filename);
+       lyxerr << "try to convert image file: " << filename_ << endl;
+// maybe that other zip extensions also be useful, especially the
+// ones that may be declared in texmf/tex/latex/config/graphics.cfg.
+// for example:
+/* -----------snip-------------
+          {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
+           \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
+           \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
+           \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
+           \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
+   -----------snip-------------*/
+
+       lyxerr << "GetExtension: " << GetExtension(filename_) << endl;
+       bool zipped = GetExtension(filename_).compare("gz") == 0;
+       if (zipped)
+           filename_ = ChangeExtension(filename_, string());   // snip the ".gz"
+       string const from = getExtFromContents(filename_);      // get the type
+       lyxerr << "GetExtFromContents: " << from << endl;
        string const to = findTargetFormat(from);
+       lyxerr << "from: " << from << " -> " << to << endl;
        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
                // the "to" file after loading when it's the same as the "from"!
-               tempfile = filename;
+               tempfile = filename_;
                loadImage();    
                return true;
        }
-
        // Take only the filename part of the file, without path or extension.
-       string temp = OnlyFilename(filename);
-       temp = ChangeExtension(filename, string());
+       string temp = OnlyFilename(filename_);
+       temp = ChangeExtension(filename_, string());
        
        // Add some stuff to have it a unique temp file.
        // This tempfile is deleted in loadImage after it is loaded to memory.
        tempfile = lyx::tempName(string(), temp);
        // Remove the temp file, we only want the name...
        lyx::unlink(tempfile);
-
-       bool result = converters.convert(0, filename, tempfile, from, to);
+       bool result = converters.convert(0, filename_, tempfile, from, to);
        tempfile.append(".xpm");
-
        // For now we are synchronous
        imageConverted(result);
-
        // Cleanup after the conversion.
        lyx::unlink(tempfile);
+       if (zipped)
+           lyx::unlink(filename_);
        tempfile = string();
-
        return true;
 }
 
Index: src/insets/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.299
diff -u -r1.299 ChangeLog
--- src/insets/ChangeLog        2002/01/30 18:16:12     1.299
+++ src/insets/ChangeLog        2002/01/31 11:03:23
@@ -1,3 +1,9 @@
+2002-01-30  Herbert Voss  <[EMAIL PROTECTED]>
+
+       * insetgraphic.C: get the filetyp from it's contents
+       * insetgraphicparams.C: add token scale and lyxrc.display when
+       creating a new inset
+
 2002-01-30  Angus Leeming  <[EMAIL PROTECTED]>
 
        * figinset.C: added using std::ios directive.
Index: src/insets/insetgraphics.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphics.C,v
retrieving revision 1.68
diff -u -r1.68 insetgraphics.C
--- src/insets/insetgraphics.C  2002/01/29 09:26:24     1.68
+++ src/insets/insetgraphics.C  2002/01/31 11:03:23
@@ -81,6 +81,7 @@
  *     Image format
  *     from        to
  *     EPS         epstopdf
+ *     PS          ps2pdf
  *     JPG/PNG     direct
  *     PDF         direct
  *     others      PNG
@@ -161,8 +162,7 @@
 }
 
 
-string const
-InsetGraphics::statusMessage() const
+string const InsetGraphics::statusMessage() const
 {
        string msg;
        if (cacheHandle.get()) {
@@ -258,32 +258,25 @@
                paint.image(old_x + 2, baseline - lascent,
                            lwidth - 4, lascent + ldescent,
                            cacheHandle->getImage());
-       } else {
-               
+       } else {        
                // Get the image status, default to unknown error.
                GraphicsCacheItem::ImageStatus status = 
GraphicsCacheItem::UnknownError;
-               if (lyxrc.display_graphics != "no" && lyxrc.use_gui
-                   && params.display != InsetGraphicsParams::NONE &&
+               if (lyxrc.use_gui && params.display != InsetGraphicsParams::NONE &&
                    cacheHandle.get())
                        status = cacheHandle->getImageStatus();
-               
                // Check if the image is now ready.
                if (status == GraphicsCacheItem::Loaded) {
                        imageLoaded = true;
-
                        // Tell BufferView we need to be updated!
                        bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
                        return;
                }
-
                paint.rectangle(old_x + 2, baseline - lascent,
                                lwidth - 4,
                                lascent + ldescent);
-
                // Print the file name.
                LyXFont msgFont(font);
                msgFont.setFamily(LyXFont::SANS_FAMILY);
-
                string const justname = OnlyFilename (params.filename);
                if (!justname.empty()) {
                        msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
@@ -291,7 +284,6 @@
                                   baseline - lyxfont::maxAscent(msgFont) - 4,
                                   justname, msgFont);
                }
-
                // Print the message.
                string const msg = statusMessage();
                if (!msg.empty()) {
@@ -498,84 +490,26 @@
 }
 
 namespace {
-
-enum FileType {
-       EPS,
-       PNG,
-       JPEG,
-       GIF,
-       PDF,
-       UNKNOWN
-};
-
-bool isEPS(string const & filename)
-{
-       if (filename.empty() || !IsFileReadable(filename)) return false;
-
-       ifstream ifs(filename.c_str());
-
-       if (!ifs) return false; // Couldn't open file...
-
-       bool is_eps = false; // Have we recognized the file as EPS?
-       string to_find = "%!PS-Adobe-"; // The string we use to recognize
-       int const max_attempts = 500; // Maximum strings to read to attempt recognition
-       int count = 0; // Counter of attempts.
-       string str;
-       for (; count < max_attempts; ++count) {
-               if (ifs.eof()) {
-                       lyxerr[Debug::INFO] << "InsetGraphics (isEPS)"
-                               " End of file reached and it wasn't found to be EPS!" 
<< endl;
-                       break;
-               }
-
-               ifs >> str;
-               if (str.find(to_find)) {
-                       is_eps = true;
-                       break;
-               }
-       }
-
-       return is_eps;
-}
-
-enum FileType classifyFileType(string const & filename, string const & suffix)
-{
-       if (suffix == "png")
-               return PNG;
-       else if (suffix == "jpg" || suffix == "jpeg")
-               return JPEG;
-       else if (suffix == "gif")
-               return GIF;
-       else if (suffix == "pdf")
-               return PDF;
-       else if (isEPS(filename))
-               return EPS;
-
-       return UNKNOWN;
-}
-
-string decideOutputImageFormat(string const & suffix, enum FileType type)
+string decideOutputImageFormat(string const & suffix)
 {
        // lyxrc.pdf_mode means:
        // Are we creating a PDF or a PS file?
        // (Should actually mean, are we using latex or pdflatex).      
+       lyxerr << "decideOutput::lyxrc.pdf_mode = " << lyxrc.pdf_mode << "\n";
        if (lyxrc.pdf_mode) {
-               if (type == EPS || type == EPS || type == PDF)
+               if (contains(suffix,"ps") || suffix == "pdf")
                        return "pdf";
-               else if (type == JPEG)
+               else if (suffix == "jpg")
                        return suffix;
                else
                        return "png";
        }
-
        // If it's postscript, we always do eps.
-       // There are many suffixes that are actually EPS (ask Garst for example)
-       // so we detect if it's an EPS by looking in the file, if it is, we return
-       // the same suffix of the file so it won't be converted.
-       if (type == EPS)
-               return suffix;
-       
-       return "eps";
+       lyxerr << "decideOutput: we have PostScript mode\n";
+       if (suffix != "ps")
+           return "eps";
+       else
+           return "ps";
 }
 
 } // Anon. namespace
@@ -585,10 +519,8 @@
        // do_convert = Do we need to convert the file?
        // nice = Do we create a nice version?
        //        This is used when exporting the latex file only.
-       // 
        // if (!do_convert)
        //   return original filename
-       // 
        // if (!nice)
        //   convert_place = temp directory
        //   return new filename in temp directory
@@ -596,32 +528,28 @@
        //   convert_place = original file directory
        //   return original filename without the extension
        //
-       
        // Get the extension (format) of the original file.
-       string const extension = GetExtension(params.filename);
-       FileType type = classifyFileType(params.filename, extension);
-       
+       // 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).
-       string const image_target = decideOutputImageFormat(extension, type);
-
+       string const image_target = decideOutputImageFormat(extension);
        if (extension == image_target)
                return params.filename;
-
        string outfile;
        if (!buf->niceFile) {
                string const temp = AddName(buf->tmppath, params.filename);
+               lyxerr << "temp = " << temp << "\n";
                outfile = RemoveExtension(temp);
-               
-               //lyxerr << "buf::tmppath = " << buf->tmppath << "\n";
-               //lyxerr << "filename = " << params.filename << "\n";
-               //lyxerr << "temp = " << temp << "\n";
-               //lyxerr << "outfile = " << outfile << endl;
        } else {
                string const path = buf->filePath();
                string const relname = MakeRelPath(params.filename, path);
                outfile = RemoveExtension(relname);
        }
+       lyxerr << "buf::tmppath = " << buf->tmppath << "\n";
+       lyxerr << "filename = " << params.filename << "\n";
+       lyxerr << "outfile = " << outfile << endl;
        converters.convert(buf, params.filename, outfile, extension, image_target);
        return outfile;
 }
@@ -665,14 +593,14 @@
 }
 
 
-int InsetGraphics::ascii(Buffer const *, ostream &, int) const
+int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
 {
        // No graphics in ascii output. Possible to use gifscii to convert
        // images to ascii approximation.
-       
        // 1. Convert file to ascii using gifscii
        // 2. Read ascii output file and add it to the output stream.
-       
+       // at least we send the filename
+       os << '<' << _("Graphicfile:") << params.filename << ">\n";
        return 0;
 }
 
@@ -728,7 +656,6 @@
        // We do it this way so that in the face of some error, we will still
        // be in a valid state.
        if (!params.filename.empty() && lyxrc.use_gui
-           && lyxrc.display_graphics != "no" 
            && params.display != InsetGraphicsParams::NONE) {
                temp = gc.addFile(params.filename);
        }
Index: src/insets/insetgraphicsParams.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphicsParams.C,v
retrieving revision 1.19
diff -u -r1.19 insetgraphicsParams.C
--- src/insets/insetgraphicsParams.C    2002/01/29 09:26:24     1.19
+++ src/insets/insetgraphicsParams.C    2002/01/31 11:03:24
@@ -22,6 +22,7 @@
 #include "support/filetools.h"
 #include "support/lyxlib.h"
 #include "support/LOstream.h"
+#include "lyxrc.h"
 
 #include "support/LAssert.h"
 
@@ -98,16 +99,23 @@
        bb = string();                  // bounding box
        draft = false;                  // draft mode
        clip = false;                   // clip image
-       display = MONOCHROME;           // LyX-View
+       if (lyxrc.display_graphics == "mono") 
+           display = MONOCHROME;
+       else if (lyxrc.display_graphics == "gray") 
+           display = GRAYSCALE;
+       else if (lyxrc.display_graphics == "color") 
+           display = COLOR;
+       else
+           display = NONE;
        subcaption = false;             // subfigure
        width = LyXLength();            // set to 0pt
        height = LyXLength();
        lyxwidth = LyXLength();         // for the view in lyx
        lyxheight = LyXLength();
        scale = 0;
-       size_type = DEFAULT_SIZE;
-       keepAspectRatio = false;
-       rotateOrigin = string();        // 
+       size_type = DEFAULT_SIZE;       // do nothing
+       keepAspectRatio = false;        //
+       rotateOrigin = "center";        // 
        rotateAngle = 0.0;              // in degrees
        special = string();             // userdefined stuff
 
@@ -298,6 +306,9 @@
                size_type = WH;
        } else if (token == "keepAspectRatio") {
                keepAspectRatio = true;
+       } else if (token == "scale") {
+               lex.next();
+               scale = lex.getInteger();
        } else if (token == "rotateAngle") {
                lex.next();
                rotateAngle = lex.getFloat();
Index: src/support/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/ChangeLog,v
retrieving revision 1.58
diff -u -r1.58 ChangeLog
--- src/support/ChangeLog       2002/01/30 13:15:19     1.58
+++ src/support/ChangeLog       2002/01/31 11:03:24
@@ -1,3 +1,8 @@
+2002-01-31  Herbert Voss  <[EMAIL PROTECTED]>
+
+       * filetools.[Ch]: add getExtFromContents(), which returns
+       the type of the (graphic) file 
+
 2002-01-30  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
        * copy.C (copy): open file in binary mode (for cygwin)
Index: src/support/filetools.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.C,v
retrieving revision 1.92
diff -u -r1.92 filetools.C
--- src/support/filetools.C     2002/01/07 10:17:44     1.92
+++ src/support/filetools.C     2002/01/31 11:03:24
@@ -71,6 +71,7 @@
 extern string build_lyxdir;
 extern string user_lyxdir;
 extern string system_tempdir;
+extern string system_packageList;
 
 
 bool IsLyXFilename(string const & filename)
@@ -185,6 +186,7 @@
                        do {
                                tmppath = split(tmppath, path_element, ';');
                        } while (!tmppath.empty() && path_element.empty());
+                               tmppath = split(tmppath, path_element, ';');
                } else {
                        notfound = false;
                }
@@ -962,6 +964,55 @@
                return string();
 }
 
+// the different filetypes and what they contain in one of the first lines
+// (dots are any characters).          (Herbert 20020131)
+// EPS %!PS-Adobe-3.0 EPSF...
+// GIF GIF...
+// JPG JFIF
+// PDF %PDF-...
+// PNG .PNG...
+// PS  %!PS-Adobe-2.0
+// XBM static char ...
+// XPM /* XPM */
+/// return the "extension" which belongs to the contents
+string const getExtFromContents(string const & filename) {
+       if (filename.empty() || !IsFileReadable(filename)) 
+           return string();    // paranoia check
+       ifstream ifs(filename.c_str());
+       if (!ifs) 
+           return string();    // Couldn't open file...
+       int const max_count = 50; // Maximum strings to read to attempt recognition
+       int count = 0; // Counter of attempts.
+       string str;
+       for (; count < max_count; ++count) {
+               if (ifs.eof()) {
+                       lyxerr[Debug::INFO] << "InsetGraphics (classifyFiletype)"
+                               " End of file reached and it wasn't found to be a 
+known Type!" << endl;
+                       break;
+               }
+               ifs >> str;
+               if (contains(str,"EPSF"))
+                   return "eps";
+               else if (contains(str,"GIF"))
+                   return "gif";
+               else if (contains(str,"JFIF"))
+                   return "jpg";
+               else if (contains(str,"%PDF"))
+                   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"))
+                   return "xbm";
+               else if (contains(str,"XPM"))
+                   return "xpm";
+       }
+       lyxerr[Debug::INFO] << "InsetGraphics (classifyFiletype)"
+               " Couldn't find a known Type!" << endl;
+       return string();
+}
+
 
 // Creates a nice compact path for displaying
 string const
@@ -1116,3 +1167,4 @@
                }
        }
 }
+
Index: src/support/filetools.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.h,v
retrieving revision 1.28
diff -u -r1.28 filetools.h
--- src/support/filetools.h     2001/10/08 14:09:06     1.28
+++ src/support/filetools.h     2002/01/31 11:03:24
@@ -132,6 +132,10 @@
 /// Return the extension of the file (not including the .)
 string const GetExtension(string const & name);
 
+/// Return the type of the file as an extension from contents
+///
+string const getExtFromContents(string const & name);
+
 /// Returns true is path is absolute
 bool AbsolutePath(string const & path);
 
@@ -190,5 +194,6 @@
 
 /// remove the autosave-file and give a Message if it can't be done
 void removeAutosaveFile(string const & filename);
+
 
 #endif

Reply via email to