From: Georg Baum Date: Wed, 15 Feb 2012 20:50:52 +0000 (+0000) Subject: Move readBB_from_PSFile() out of support (no code change), X-Git-Tag: 2.1.0beta1~2050 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=9fe72c3501e966ea7e19f7229a37388de7171887;p=features.git Move readBB_from_PSFile() out of support (no code change), since it soon will need to use the Formats class. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40757 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/Makefile.am b/src/Makefile.am index 84f4e85b71..a96472fb70 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -339,6 +339,8 @@ liblyxcore_a_DEPENDENCIES = $(MOCEDFILES) noinst_LIBRARIES += liblyxgraphics.a liblyxgraphics_a_SOURCES = \ + graphics/epstools.h \ + graphics/epstools.cpp \ graphics/GraphicsCache.h \ graphics/GraphicsCache.cpp \ graphics/GraphicsCacheItem.h \ diff --git a/src/frontends/qt4/GuiExternal.cpp b/src/frontends/qt4/GuiExternal.cpp index 94decc7b3b..d944bc0793 100644 --- a/src/frontends/qt4/GuiExternal.cpp +++ b/src/frontends/qt4/GuiExternal.cpp @@ -23,6 +23,7 @@ #include "insets/ExternalTemplate.h" #include "insets/InsetExternal.h" +#include "graphics/epstools.h" #include "graphics/GraphicsCache.h" #include "graphics/GraphicsCacheItem.h" #include "graphics/GraphicsImage.h" @@ -299,7 +300,7 @@ void GuiExternal::getbbClicked() FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath()))); // try to get it from the file, if possible - string bb = readBB_from_PSFile(abs_file); + string bb = graphics::readBB_from_PSFile(abs_file); if (bb.empty()) { // we don't, so ask the Graphics Cache if it has loaded the file int width = 0; diff --git a/src/frontends/qt4/GuiGraphics.cpp b/src/frontends/qt4/GuiGraphics.cpp index 465ffcea34..99cf2d85e2 100644 --- a/src/frontends/qt4/GuiGraphics.cpp +++ b/src/frontends/qt4/GuiGraphics.cpp @@ -25,6 +25,7 @@ #include "Length.h" #include "LyXRC.h" +#include "graphics/epstools.h" #include "graphics/GraphicsCache.h" #include "graphics/GraphicsCacheItem.h" #include "graphics/GraphicsImage.h" @@ -806,7 +807,7 @@ string GuiGraphics::readBoundingBox(string const & file) // try to get it from the file, if possible. Zipped files are // unzipped in the readBB_from_PSFile-Function - string const bb = readBB_from_PSFile(abs_file); + string const bb = graphics::readBB_from_PSFile(abs_file); if (!bb.empty()) return bb; diff --git a/src/graphics/epstools.cpp b/src/graphics/epstools.cpp new file mode 100644 index 0000000000..44d577c14f --- /dev/null +++ b/src/graphics/epstools.cpp @@ -0,0 +1,90 @@ +/** + * \file epstools.cpp + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc. + * + * \author Ivan Schreter + * \author Dirk Niggemann + * \author Asger Alstrup + * \author Lars Gullik Bjønnes + * \author Jean-Marc Lasgouttes + * \author Angus Leeming + * \author John Levon + * \author Herbert Voß + * + * Full author contact details are available in file CREDITS. + * + * Utilities for manipulation of Encapsulated Postscript files + */ + +#include + +#include "graphics/epstools.h" + +#include "Format.h" + +#include "support/debug.h" +#include "support/filetools.h" +#include "support/FileName.h" +#include "support/regex.h" + +using namespace std; +using namespace lyx::support; + +namespace lyx { +namespace graphics { + + +string const readBB_from_PSFile(FileName const & file) +{ + // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345 + // It seems that every command in the header has an own line, + // getline() should work for all files. + // On the other hand some plot programs write the bb at the + // end of the file. Than we have in the header: + // %%BoundingBox: (atend) + // In this case we must check the end. + bool zipped = file.isZippedFile(); + FileName const file_ = zipped ? unzipFile(file) : file; + string const format = file_.guessFormatFromContents(); + + if (format != "eps" && format != "ps") { + LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] no(e)ps-format"); + if (zipped) + file_.removeFile(); + return string(); + } + + static lyx::regex bbox_re( + "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)"); + ifstream is(file_.toFilesystemEncoding().c_str()); + while (is) { + string s; + getline(is,s); + lyx::smatch what; + if (regex_match(s, what, bbox_re)) { + // Our callers expect the tokens in the string + // separated by single spaces. + // FIXME: change return type from string to something + // sensible + ostringstream os; + os << what.str(1) << ' ' << what.str(2) << ' ' + << what.str(3) << ' ' << what.str(4); + string const bb = os.str(); + LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << bb); + if (zipped) + file_.removeFile(); + return bb; + } + } + LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] no bb found"); + if (zipped) + file_.removeFile(); + return string(); +} + + +} // namespace graphics +} // namespace lyx diff --git a/src/graphics/epstools.h b/src/graphics/epstools.h new file mode 100644 index 0000000000..043d6bbdba --- /dev/null +++ b/src/graphics/epstools.h @@ -0,0 +1,32 @@ +// -*- C++ -*- +/** + * \file epstools.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Lars Gullik Bjønnes + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef LYX_EPSTOOLS_H +#define LYX_EPSTOOLS_H + +#include + +namespace lyx { +namespace support { + +class FileName; + +} + +namespace graphics { + +/// read the BoundingBox entry from a ps/eps-file +std::string const readBB_from_PSFile(support::FileName const & file); + +} // namespace graphics +} // namespace lyx + +#endif diff --git a/src/insets/InsetGraphicsParams.cpp b/src/insets/InsetGraphicsParams.cpp index 4f0266ccf8..f4cd119f6e 100644 --- a/src/insets/InsetGraphicsParams.cpp +++ b/src/insets/InsetGraphicsParams.cpp @@ -18,12 +18,12 @@ #include "Lexer.h" #include "LyXRC.h" +#include "graphics/epstools.h" #include "graphics/GraphicsParams.h" #include "graphics/GraphicsTypes.h" #include "support/convert.h" #include "support/debug.h" -#include "support/filetools.h" #include "support/lyxlib.h" #include "support/lstrings.h" #include "support/Translator.h" @@ -262,7 +262,7 @@ graphics::Params InsetGraphicsParams::as_grfxParams() const pars.bb = bb; // Get the original Bounding Box from the file - string const tmp = readBB_from_PSFile(filename); + string const tmp = graphics::readBB_from_PSFile(filename); LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp); if (!tmp.empty()) { unsigned int const bb_orig_xl = convert(token(tmp, ' ', 0)); diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index 0dcb2edc57..4f681b009e 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -1016,55 +1016,6 @@ FileName const findtexfile(string const & fil, string const & /*format*/) } -string const readBB_from_PSFile(FileName const & file) -{ - // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345 - // It seems that every command in the header has an own line, - // getline() should work for all files. - // On the other hand some plot programs write the bb at the - // end of the file. Than we have in the header: - // %%BoundingBox: (atend) - // In this case we must check the end. - bool zipped = file.isZippedFile(); - FileName const file_ = zipped ? unzipFile(file) : file; - string const format = file_.guessFormatFromContents(); - - if (format != "eps" && format != "ps") { - LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] no(e)ps-format"); - if (zipped) - file_.removeFile(); - return string(); - } - - static lyx::regex bbox_re( - "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)"); - ifstream is(file_.toFilesystemEncoding().c_str()); - while (is) { - string s; - getline(is,s); - lyx::smatch what; - if (regex_match(s, what, bbox_re)) { - // Our callers expect the tokens in the string - // separated by single spaces. - // FIXME: change return type from string to something - // sensible - ostringstream os; - os << what.str(1) << ' ' << what.str(2) << ' ' - << what.str(3) << ' ' << what.str(4); - string const bb = os.str(); - LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << bb); - if (zipped) - file_.removeFile(); - return bb; - } - } - LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] no bb found"); - if (zipped) - file_.removeFile(); - return string(); -} - - int compare_timestamps(FileName const & file1, FileName const & file2) { // If the original is newer than the copy, then copy the original diff --git a/src/support/filetools.h b/src/support/filetools.h index 48ebbd6046..92c6eb57e7 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -275,9 +275,6 @@ bool readLink(FileName const & file, FileName & link); FileName const findtexfile(std::string const & fil, std::string const & format); -/// read the BoundingBox entry from a ps/eps/pdf-file -std::string const readBB_from_PSFile(FileName const & file); - /** \param file1, file2 the two files to be compared. Must have absolute paths. * \returns 1 if \c file1 has a more recent timestamp than \c file2, * 0 if their timestamps are the same,