]> git.lyx.org Git - lyx.git/blob - src/graphics/epstools.cpp
Replace calls from make_unique to lyx::make_unique
[lyx.git] / src / graphics / epstools.cpp
1 /**
2  * \file epstools.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
7  *
8  * \author Ivan Schreter
9  * \author Dirk Niggemann
10  * \author Asger Alstrup
11  * \author Lars Gullik Bjønnes
12  * \author Jean-Marc Lasgouttes
13  * \author Angus Leeming
14  * \author John Levon
15  * \author Herbert Voß
16  *
17  * Full author contact details are available in file CREDITS.
18  *
19  * Utilities for manipulation of Encapsulated Postscript files
20  */
21
22 #include <config.h>
23
24 #include "graphics/epstools.h"
25
26 #include "Format.h"
27
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/filetools.h"
31 #include "support/FileName.h"
32
33 #include <regex>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39 namespace graphics {
40
41
42 string const readBB_from_PSFile(FileName const & file)
43 {
44         // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
45         // It seems that every command in the header has an own line,
46         // getline() should work for all files.
47         // On the other hand some plot programs write the bb at the
48         // end of the file. Than we have in the header:
49         // %%BoundingBox: (atend)
50         // In this case we must check the end.
51         bool const zipped = theFormats().isZippedFile(file);
52         FileName const file_ = zipped ? unzipFile(file) : file;
53         string const format = theFormats().getFormatFromFile(file_);
54
55         if (!Formats::isPostScriptFileFormat(format)) {
56                 LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] no(e)ps-format");
57                 if (zipped)
58                         file_.removeFile();
59                 return string();
60         }
61
62         static regex bbox_re("^%%BoundingBox:\\s*([-]*[[:digit:]]+)"
63                 "\\s+([-]*[[:digit:]]+)\\s+([-]*[[:digit:]]+)\\s+([-]*[[:digit:]]+)");
64         ifstream is(file_.toFilesystemEncoding().c_str());
65         while (is) {
66                 string s;
67                 getline(is,s);
68                 smatch what;
69                 if (regex_match(s, what, bbox_re)) {
70                         // Our callers expect the tokens in the string
71                         // separated by single spaces.
72                         // FIXME: change return type from string to something
73                         // sensible
74                         ostringstream os;
75                         os << what.str(1) << ' ' << what.str(2) << ' '
76                            << what.str(3) << ' ' << what.str(4);
77                         string const bb = os.str();
78                         LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << bb);
79                         if (zipped)
80                                 file_.removeFile();
81                         return bb;
82                 }
83         }
84         LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] no bb found");
85         if (zipped)
86                 file_.removeFile();
87         return string();
88 }
89
90
91 } // namespace graphics
92 } // namespace lyx