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
This commit is contained in:
Georg Baum 2012-02-15 20:50:52 +00:00
parent 433b919457
commit 9fe72c3501
8 changed files with 130 additions and 56 deletions

View File

@ -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 \

View File

@ -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;

View File

@ -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;

90
src/graphics/epstools.cpp Normal file
View File

@ -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 <config.h>
#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

32
src/graphics/epstools.h Normal file
View File

@ -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 <string>
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

View File

@ -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<unsigned int>(token(tmp, ' ', 0));

View File

@ -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

View File

@ -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,