]> git.lyx.org Git - lyx.git/blob - src/mathed/preview.C
7c5376330783366d8a4b141fd956b58211ac9d7c
[lyx.git] / src / mathed / preview.C
1 #include <config.h>
2
3 #include "debug.h"
4 #include "support/systemcall.h"
5 #include "graphics/GraphicsTypes.h"
6 #include "graphics/GraphicsImage.h"
7 #include "graphics/GraphicsCache.h"
8 #include "graphics/GraphicsImage.h"
9 #include "graphics/GraphicsParams.h"
10 #include "graphics/GraphicsCacheItem.h"
11 #include "Lsstream.h"
12
13 #include <fstream>
14
15 #include <boost/utility.hpp>
16 #include <boost/bind.hpp>
17
18 using namespace std;
19
20
21 // built some unique filename
22 string canonical_name(string const & str)
23 {
24         ostringstream os;
25         for (string::const_iterator it = str.begin(); it != str.end(); ++it) 
26                 os << char('A' + (*it & 15)) << char('a' + (*it >> 4));
27         return os.str();
28 }
29
30
31 bool preview(string const & str, grfx::GraphicPtr & graphic)
32 {
33         string base = canonical_name(str);
34         string dir  = "/tmp/lyx/";
35         string file = dir + base + ".eps";
36         cerr << "writing '" << str << "' to '" << file << "'\n";
37
38         // get the cache
39         grfx::GCache & gc = grfx::GCache::get();
40
41         // look up the file
42         if (gc.inCache(file)) {
43
44                 // it's already in there. Get hold of it.
45                 grfx::GraphicPtr gr = grfx::GraphicPtr(gc.graphic(file));
46
47                 // is it already loaded?
48                 if (gr->status() == grfx::Loaded) {
49                         cerr << "file '" << file << "' ready for display\n";
50                         graphic = gr;
51                         grfx::GParams pars;
52                         bool const res = graphic->image()->setPixmap(pars);
53                         return res;
54                 }
55
56                 // otherwise we have to wait again
57                 cerr << "file '" << file << "' not finished loading\n";
58                 return false;
59         }
60
61         // The real work starts.
62         string const texfile = dir + base + ".tex";
63         std::ofstream of(texfile.c_str());
64         of << "\\batchmode"
65            << "\\documentclass{article}"
66            << "\\usepackage{amssymb}"
67            << "\\thispagestyle{empty}"
68            << "\\pdfoutput=0"
69            << "\\begin{document}"
70            << str
71            << "\\end{document}\n";
72         of.close();
73
74         string const cmd =
75 //              "latex " + base + ".tex ; " + "
76 //    "dvips -x 2500 -R -E -o " + base + ".eps " + base + ".dvi ";
77         // Herbert says this is faster
78                 "pdflatex --interaction batchmode " + base + "; " +
79                 "dvips -x 2000 -R -E -o " + base + ".eps " + base + ".dvi ";
80         //cerr << "calling: '" << "(cd " + dir + "; " + cmd + ")\n";
81         Systemcall sc;
82         sc.startscript(Systemcall::Wait, "(cd " + dir + "; " + cmd + ")");
83
84         // now we are done, add the file to the cache
85         gc.add(file);
86         gc.graphic(file)->startLoading();
87
88         // This might take a while. Wait for the next round.
89         cerr << "file '" << file << "' registered\n";
90         return false;
91 }