From 36415c91311b2eff03fee45923a9237d8a1f5312 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Mon, 8 Apr 2002 14:55:52 +0000 Subject: [PATCH] (Herbert): move readBB from ControlGraphics to support/filetools. (Angus): re-format getExtFromContents a little and remove replicated "sgi" entry. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3946 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/controllers/ChangeLog | 4 ++ src/frontends/controllers/ControlGraphics.C | 32 +-------- src/support/ChangeLog | 10 +++ src/support/filetools.C | 72 +++++++++++++++------ src/support/filetools.h | 3 + 5 files changed, 71 insertions(+), 50 deletions(-) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index fc898d6e00..28c0c339c4 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,7 @@ +2002-04-07 Herbert Voss + + * ControlGraphics.[C]: move readBB as readBB_from_PSFile into filetools + 2002-04-05 Angus Leeming * ControlGraphics.C (readBB): sigh. Make sure that the correct path is diff --git a/src/frontends/controllers/ControlGraphics.C b/src/frontends/controllers/ControlGraphics.C index 6c42c5f5cb..5487afba3a 100644 --- a/src/frontends/controllers/ControlGraphics.C +++ b/src/frontends/controllers/ControlGraphics.C @@ -104,37 +104,7 @@ string const ControlGraphics::Browse(string const & in_name) string const ControlGraphics::readBB(string const & file) { - // in a file it's an entry like %%BoundingBox:23 45 321 345 - // The first number can following without a space, so we have - // to be a little careful. - // 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. - string file_ = MakeAbsPath(file, lv_.buffer()->filePath()); - if (zippedFile(file_)) - file_ = unzipFile(file_); - - string const format = getExtFromContents(file_); - if (format != "eps" && format != "ps") - return string(); - - std::ifstream is(file_.c_str()); - while (is) { - string s; - is >> s; - if (contains(s,"%%BoundingBox:")) { - string a, b, c, d; - is >> a >> b >> c >> d; - if (is && !contains(a,"atend")) { // bb at the end? - if (s != "%%BoundingBox:") - return (s.substr(14)+" "+a+" "+b+" "+c+" "); - else - return (a+" "+b+" "+c+" "+d+" "); - } - } - } - return string(); + return readBB_from_PSFile(MakeAbsPath(file, lv_.buffer()->filePath())); } diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 6b14f8c1ba..8628a65484 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,13 @@ +2002-04-08 Angus Leeming + + * filetools.C (getExtFromContents): re-format a little and remove + replicated "sgi" entry. + +2002-04-07 Herbert Voss + + * filetools.[Ch]: add readBB_from_PSFile() to make bb available + for the lyx-view in graphics (moved from ControlGraphics) + 2002-04-07 Herbert Voss * filetools.C: fix bug for eps. scans now a whole line diff --git a/src/support/filetools.C b/src/support/filetools.C index a17b9158ab..04ef9bb01c 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -1043,7 +1043,7 @@ string const getExtFromContents(string const & filename) break; } - getline(ifs, str); + std::getline(ifs, str); lyxerr[Debug::GRAPHICS] << "Scanstring: " << str << endl; string const stamp = str.substr(0,2); @@ -1052,25 +1052,26 @@ string const getExtFromContents(string const & filename) // information is saved in the first bytes of the file! // also some graphic formats which save the information // in the first line, too. - if (prefixIs(str, gzipStamp)) + if (prefixIs(str, gzipStamp)) { format = "gzip"; - else if (stamp == zipStamp) + } else if (stamp == zipStamp) { format = "zip"; - else if (stamp == compressStamp) + } else if (stamp == compressStamp) { format = "compress"; // the graphics part - else if (stamp == "BM") + } else if (stamp == "BM") { format = "bmp"; - else if (stamp == "\001\332") + } else if (stamp == "\001\332") { format = "sgi"; + // PBM family // Don't need to use str.at(0), str.at(1) because // we already know that str.size() >= 2 - else if (str[0] == 'P') { + } else if (str[0] == 'P') { switch (str[1]) { case '1': case '4': @@ -1085,21 +1086,27 @@ string const getExtFromContents(string const & filename) format = "ppm"; } break; + + } else if ((stamp == "II") || (stamp == "MM")) { + format = "tiff"; + + } else if (str == "%TGIF") { + format = "tgif"; + + } else if (prefixIs(str,"GIF")) { + format = "gif"; + + } else if (str.size() > 3) { + int const c = ((str[0] << 24) & (str[1] << 16) & + (str[2] << 8) & str[3]); + if (c == 105) { + format = "xwd"; + } } - if (stamp == "\001\332") - format = "sgi"; - else if ((stamp == "II") || (stamp == "MM")) - format = "tiff"; - else if (str == "%TGIF") - format = "tgif"; - else if (prefixIs(str,"GIF")) - format = "gif"; - else if (str.size() > 3) // get long - if (((str[0] << 24) + (str[1] << 16) + - (str[2] << 8) + str[3]) == 105) - format = "xwd"; + firstLine = false; } + if (!format.empty()) break; else if (contains(str,"EPSF")) @@ -1344,3 +1351,30 @@ void removeAutosaveFile(string const & filename) } } } + + +string const readBB_from_PSFile(string 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. + string const file_ = zippedFile(file) ? + string(unzipFile(file)) : string(file); + string const format = getExtFromContents(file_); + if (format != "eps" && format != "ps") + return string(); + + std::ifstream is(file_.c_str()); + while (is) { + string s; + std::getline(is,s); + if (contains(s,"%%BoundingBox:") && !contains(s,"atend")) + return (frontStrip(s.substr(14))); + } + return string(); +} + diff --git a/src/support/filetools.h b/src/support/filetools.h index c04180ae58..322cdeab35 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -206,5 +206,8 @@ 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); +/// read the BoundingBox entry from a ps/eps/pdf-file +string const readBB_from_PSFile(string const & file); + #endif -- 2.39.2