From c1e4304322c6299abd794e476f9b31214bfaeac5 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Wed, 5 Sep 2007 17:50:07 +0000 Subject: [PATCH] Embedding feature patch 5: handling of embedded files (operation without external files) * src/insets/InsetGraphicsParams.h|cpp: * src/insets/InsetExternal.cpp: read/write buffer with embedded file * src/insets/InsetGraphics.cpp: ditto * src/EmbeddedFiles.h|cpp: add find() function git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20075 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/EmbeddedFiles.cpp | 11 +++++++++++ src/EmbeddedFiles.h | 2 ++ src/insets/InsetExternal.cpp | 20 ++++++++++++++++---- src/insets/InsetGraphics.cpp | 11 +++++++++-- src/insets/InsetGraphicsParams.cpp | 13 +++++++++++-- src/insets/InsetGraphicsParams.h | 4 +++- 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/EmbeddedFiles.cpp b/src/EmbeddedFiles.cpp index 180b88ed92..9f25de6f72 100644 --- a/src/EmbeddedFiles.cpp +++ b/src/EmbeddedFiles.cpp @@ -275,6 +275,17 @@ bool EmbeddedFiles::write(DocFileName const & filename) } +EmbeddedFiles::EmbeddedFileList::const_iterator EmbeddedFiles::find(std::string filename) const +{ + EmbeddedFileList::const_iterator it = file_list_.begin(); + EmbeddedFileList::const_iterator it_end = file_list_.end(); + for (; it != it_end; ++it) + if (it->absFilename() == filename || it->embeddedFile(buffer_) == filename) + return it; + return file_list_.end(); +} + + bool EmbeddedFiles::extractAll() const { EmbeddedFileList::const_iterator it = file_list_.begin(); diff --git a/src/EmbeddedFiles.h b/src/EmbeddedFiles.h index 132c84927c..d1aad34ecf 100644 --- a/src/EmbeddedFiles.h +++ b/src/EmbeddedFiles.h @@ -225,6 +225,8 @@ public: EmbeddedFileList::iterator end() { return file_list_.end(); } EmbeddedFileList::const_iterator begin() const { return file_list_.begin(); } EmbeddedFileList::const_iterator end() const { return file_list_.end(); } + // try to locate filename, using either absFilename() or embeddedFile() + EmbeddedFileList::const_iterator find(std::string filename) const; /// bool extractAll() const; /// diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index 31115a4a85..29e7e70c12 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -50,6 +50,7 @@ using std::ostream; using std::ostringstream; using std::vector; +using lyx::support::DocFileName; namespace { @@ -186,10 +187,15 @@ void InsetExternalParams::write(Buffer const & buffer, ostream & os) const os << "External\n" << "\ttemplate " << templatename() << '\n'; - if (!filename.empty()) - os << "\tfilename " - << filename.outputFilename(buffer.filePath()) - << '\n'; + if (!filename.empty()) { + // when we save, we still use the original filename + EmbeddedFiles::EmbeddedFileList::const_iterator it = + buffer.embeddedFiles().find(filename.toFilesystemEncoding()); + if (it != buffer.embeddedFiles().end()) + os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n'; + else + os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n'; + } if (display != defaultDisplayType) os << "\tdisplay " @@ -297,6 +303,12 @@ bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex) lex.eatLine(); string const name = lex.getString(); filename.set(name, buffer.filePath()); + // maybe this file is embedded + EmbeddedFiles::EmbeddedFileList::const_iterator it = buffer.embeddedFiles().find(filename.toFilesystemEncoding()); + if (it != buffer.embeddedFiles().end()) + // using available file, embedded or external, depending on file availability and + // embedding status. + filename = DocFileName(it->availableFile(&buffer)); break; } diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp index 247268fada..f7aac86629 100644 --- a/src/insets/InsetGraphics.cpp +++ b/src/insets/InsetGraphics.cpp @@ -270,7 +270,7 @@ Inset::EDITABLE InsetGraphics::editable() const void InsetGraphics::write(Buffer const & buf, ostream & os) const { os << "Graphics\n"; - params().Write(os, buf.filePath()); + params().Write(os, buf); } @@ -283,6 +283,13 @@ void InsetGraphics::read(Buffer const & buf, Lexer & lex) else LYXERR(Debug::GRAPHICS) << "Not a Graphics inset!" << endl; + // InsetGraphics is read, with filename in params_. We do not know if this file actually + // exists or is embedded so we need to get the 'availableFile' from buf.embeddedFiles() + EmbeddedFiles::EmbeddedFileList::const_iterator it = buf.embeddedFiles().find(params_.filename.toFilesystemEncoding()); + if (it != buf.embeddedFiles().end()) + // using available file, embedded or external, depending on file availability and + // embedding status. + params_.filename = DocFileName(it->availableFile(&buf)); graphic_->update(params().as_grfxParams()); } @@ -1016,7 +1023,7 @@ InsetGraphicsMailer::params2string(InsetGraphicsParams const & params, { ostringstream data; data << name_ << ' '; - params.Write(data, buffer.filePath()); + params.Write(data, buffer); data << "\\end_inset\n"; return data.str(); } diff --git a/src/insets/InsetGraphicsParams.cpp b/src/insets/InsetGraphicsParams.cpp index b80e10fbf0..6b1bc4cfd4 100644 --- a/src/insets/InsetGraphicsParams.cpp +++ b/src/insets/InsetGraphicsParams.cpp @@ -17,6 +17,8 @@ #include "LyX.h" // for use_gui #include "Lexer.h" #include "LyXRC.h" +#include "Buffer.h" +#include "EmbeddedFiles.h" #include "graphics/GraphicsParams.h" @@ -32,6 +34,7 @@ namespace lyx { using support::float_equal; using support::readBB_from_PSFile; using support::token; +using support::DocFileName; using std::string; using std::ostream; @@ -148,12 +151,18 @@ bool operator!=(InsetGraphicsParams const & left, } -void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const +void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const { // Do not write the default values if (!filename.empty()) { - os << "\tfilename " << filename.outputFilename(bufpath) << '\n'; + // when we save, we still use the original filename + EmbeddedFiles::EmbeddedFileList::const_iterator it = + buffer.embeddedFiles().find(filename.toFilesystemEncoding()); + if (it != buffer.embeddedFiles().end()) + os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n'; + else + os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n'; } if (lyxscale != 100) os << "\tlyxscale " << lyxscale << '\n'; diff --git a/src/insets/InsetGraphicsParams.h b/src/insets/InsetGraphicsParams.h index b6cea1fd9b..172215dd22 100644 --- a/src/insets/InsetGraphicsParams.h +++ b/src/insets/InsetGraphicsParams.h @@ -23,6 +23,7 @@ namespace lyx { namespace graphics { class Params; } class Lexer; +class Buffer; /// This class holds all the parameters needed by insetGraphics. @@ -73,7 +74,8 @@ public: /// InsetGraphicsParams & operator=(InsetGraphicsParams const &); /// Save the parameters in the LyX format stream. - void Write(std::ostream & os, std::string const & bufpath) const; + /// Buffer is needed to figure out if a figure is embedded. + void Write(std::ostream & os, Buffer const & buf) const; /// If the token belongs to our parameters, read it. bool Read(Lexer & lex, std::string const & token, std::string const & bufpath); /// convert -- 2.39.2