]> git.lyx.org Git - features.git/commitdiff
Herbert's latest graphics patch.
authorAngus Leeming <leeming@lyx.org>
Thu, 31 Jan 2002 14:20:09 +0000 (14:20 +0000)
committerAngus Leeming <leeming@lyx.org>
Thu, 31 Jan 2002 14:20:09 +0000 (14:20 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3465 a592a061-630c-0410-9148-cb99ea01b6c8

12 files changed:
lib/ChangeLog
lib/configure.m4
src/frontends/controllers/ChangeLog
src/frontends/controllers/ControlGraphics.C
src/graphics/ChangeLog
src/graphics/GraphicsCacheItem.C
src/insets/ChangeLog
src/insets/insetgraphics.C
src/insets/insetgraphicsParams.C
src/support/ChangeLog
src/support/filetools.C
src/support/filetools.h

index 535318d677b4d6fd392dd041f010c4e69b099c16..8b355389d03cd71d31b4e2ac6d30661dda8f1f04 100644 (file)
@@ -1,3 +1,7 @@
+2002-01-30  Herbert Voss  <voss@lyx.org>
+
+       * configure.m4: add convert-support for ps-files
+
 2002-01-29  Herbert Voss  <voss@lyx.org>
 
        * ui/default.ui: delete old figure entries
index a68db828059ce5c166eec8c641c815d8e07a3454..431589569c5be37da5d2fbc582afa837aa493b43 100644 (file)
@@ -371,6 +371,7 @@ if test "$TOXPM" = "convert"; then
        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 844af780e5006ffce744593b3574fc12a0dada40..c93c39e32408843274e429e3b095f2564d216e99 100644 (file)
@@ -1,3 +1,8 @@
+2002-01-30  Herbert Voss  <voss@lyx.org>
+
+       * ControlGraphic.[C]: do not search the whole file, when
+       getting the bb
+
 2002-01-29  Herbert Voss  <voss@lyx.org>
 
        * ControlGraphic.[C]: added a button for document path
index a6901546eec66c6379009ca594153d55530e84a0..a6df4b5d3d465e58915902d0da32300d44f63cd6 100644 (file)
@@ -40,7 +40,6 @@
 
 using std::pair;
 using std::make_pair;
-
 using std::ifstream;
 
 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
@@ -107,7 +106,9 @@ string const ControlGraphics::readBB(string const & file)
 // 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 781881e39cf89d2f4b2c3b6d7c3a6b0b3f1c30f0..2fb11db6374448fa5424794ed981e03e1c048790 100644 (file)
@@ -1,3 +1,8 @@
+2002-01-31  Herbert Voss  <voss@lyx.org>
+
+       * GraphicsCacheItem.h: handle filetype without the file extension;
+       getting type from contents; handle zipped files
+
 2002-01-27  Herbert Voss  <voss@lyx.org>
 
        * GraphicsCacheItem.h: added Converting to the ImageStatus enum.
index 9aef12198455c02c1e5c86de554057cbe6dd7c86..8c171714fb43addef26ee3ec3074179407ef3722 100644 (file)
@@ -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 <voss@lyx.org>
  * ================================================= */
 
 #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 @@ LyXImage *
 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 @@ string const findTargetFormat(string const & from)
                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 cf872e7712a1d7ed7f484fcbbece26b580e3586c..b67afb22c07ef3328ced20a8830f4058317da025 100644 (file)
@@ -1,3 +1,9 @@
+2002-01-30  Herbert Voss  <voss@lyx.org>
+
+       * 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  <a.leeming@ic.ac.uk>
 
        * figinset.C: added using std::ios directive.
index 5c2a21d56198b2f525b1af0e1e3aab9ce51366f1..bd03664fe3f8247f4c6ea9acb0e359d34c6201c7 100644 (file)
@@ -81,6 +81,7 @@ TODO Before initial production release:
  *     Image format
  *     from        to
  *     EPS         epstopdf
+ *     PS          ps2pdf
  *     JPG/PNG     direct
  *     PDF         direct
  *     others      PNG
@@ -161,8 +162,7 @@ InsetGraphics::~InsetGraphics()
 }
 
 
-string const
-InsetGraphics::statusMessage() const
+string const InsetGraphics::statusMessage() const
 {
        string msg;
        if (cacheHandle.get()) {
@@ -258,32 +258,25 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
                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 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
                                   baseline - lyxfont::maxAscent(msgFont) - 4,
                                   justname, msgFont);
                }
-
                // Print the message.
                string const msg = statusMessage();
                if (!msg.empty()) {
@@ -498,84 +490,26 @@ string const InsetGraphics::createLatexOptions() const
 }
 
 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 @@ string const InsetGraphics::prepareFile(Buffer const *buf) const
        // 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 @@ 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.
-       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::latex(Buffer const *buf, ostream & os,
 }
 
 
-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 @@ void InsetGraphics::updateInset() const
        // 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 5008d2ace3c55b268bece1b6ee2ec39ec5e394b3..b790ce1c5f3bb861b432c2aa41a60bc315e140b2 100644 (file)
@@ -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 @@ void InsetGraphicsParams::init()
        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 @@ bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
                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 1f5124d28e36fa861d02a2e33cd510a530f694ba..e513ec93e3d73b4f9f02417101628b3780258cb5 100644 (file)
@@ -1,3 +1,8 @@
+2002-01-31  Herbert Voss  <voss@perce.de>
+
+       * filetools.[Ch]: add getExtFromContents(), which returns
+       the type of the (graphic) file 
+
 2002-01-30  Jean-Marc Lasgouttes  <lasgouttes@freesurf.fr>
 
        * copy.C (copy): open file in binary mode (for cygwin)
index 54d58d7b93e606cd6581d3f713fa8f3c92519bc2..a666079437df35d306422d23992eeada4821e49d 100644 (file)
@@ -71,6 +71,7 @@ extern string system_lyxdir;
 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 @@ string const FileOpenSearch (string const & path, string const & name,
                        do {
                                tmppath = split(tmppath, path_element, ';');
                        } while (!tmppath.empty() && path_element.empty());
+                               tmppath = split(tmppath, path_element, ';');
                } else {
                        notfound = false;
                }
@@ -962,6 +964,55 @@ string const GetExtension(string const & name)
                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 @@ void removeAutosaveFile(string const & filename)
                }
        }
 }
+
index 4c5bc161bccded82e5a21341ded56449bba2c6b7..5a4d0eb64dbd08080bc9ad85c51e4363640b8bb7 100644 (file)
@@ -132,6 +132,10 @@ ChangeExtension(string const & oldname, string const & extension);
 /// 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);
 
@@ -191,4 +195,5 @@ string const findtexfile(string const & fil, string const & format);
 /// remove the autosave-file and give a Message if it can't be done
 void removeAutosaveFile(string const & filename);
 
+
 #endif