From 358c483572b2d9fe3ba4ab511fd89af94ad27ce5 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Thu, 31 Jan 2002 14:20:09 +0000 Subject: [PATCH] Herbert's latest graphics patch. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3465 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ChangeLog | 4 + lib/configure.m4 | 1 + src/frontends/controllers/ChangeLog | 5 + src/frontends/controllers/ControlGraphics.C | 5 +- src/graphics/ChangeLog | 5 + src/graphics/GraphicsCacheItem.C | 59 +++++++--- src/insets/ChangeLog | 6 + src/insets/insetgraphics.C | 121 ++++---------------- src/insets/insetgraphicsParams.C | 19 ++- src/support/ChangeLog | 5 + src/support/filetools.C | 52 +++++++++ src/support/filetools.h | 5 + 12 files changed, 166 insertions(+), 121 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 535318d677..8b355389d0 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2002-01-30 Herbert Voss + + * configure.m4: add convert-support for ps-files + 2002-01-29 Herbert Voss * ui/default.ui: delete old figure entries diff --git a/lib/configure.m4 b/lib/configure.m4 index a68db82805..431589569c 100644 --- a/lib/configure.m4 +++ b/lib/configure.m4 @@ -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) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 844af780e5..c93c39e324 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,8 @@ +2002-01-30 Herbert Voss + + * ControlGraphic.[C]: do not search the whole file, when + getting the bb + 2002-01-29 Herbert Voss * ControlGraphic.[C]: added a button for document path diff --git a/src/frontends/controllers/ControlGraphics.C b/src/frontends/controllers/ControlGraphics.C index a6901546ee..a6df4b5d3d 100644 --- a/src/frontends/controllers/ControlGraphics.C +++ b/src/frontends/controllers/ControlGraphics.C @@ -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:")) { diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index 781881e39c..2fb11db637 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,8 @@ +2002-01-31 Herbert Voss + + * GraphicsCacheItem.h: handle filetype without the file extension; + getting type from contents; handle zipped files + 2002-01-27 Herbert Voss * GraphicsCacheItem.h: added Converting to the ImageStatus enum. diff --git a/src/graphics/GraphicsCacheItem.C b/src/graphics/GraphicsCacheItem.C index 9aef121984..8c171714fb 100644 --- a/src/graphics/GraphicsCacheItem.C +++ b/src/graphics/GraphicsCacheItem.C @@ -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 * ================================================= */ #include @@ -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; } diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index cf872e7712..b67afb22c0 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,9 @@ +2002-01-30 Herbert Voss + + * 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 * figinset.C: added using std::ios directive. diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 5c2a21d561..bd03664fe3 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -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); } diff --git a/src/insets/insetgraphicsParams.C b/src/insets/insetgraphicsParams.C index 5008d2ace3..b790ce1c5f 100644 --- a/src/insets/insetgraphicsParams.C +++ b/src/insets/insetgraphicsParams.C @@ -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(); diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 1f5124d28e..e513ec93e3 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,8 @@ +2002-01-31 Herbert Voss + + * filetools.[Ch]: add getExtFromContents(), which returns + the type of the (graphic) file + 2002-01-30 Jean-Marc Lasgouttes * copy.C (copy): open file in binary mode (for cygwin) diff --git a/src/support/filetools.C b/src/support/filetools.C index 54d58d7b93..a666079437 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -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) } } } + diff --git a/src/support/filetools.h b/src/support/filetools.h index 4c5bc161bc..5a4d0eb64d 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -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 -- 2.39.2