]> git.lyx.org Git - features.git/commitdiff
first shot at preview. It crashes. Don't know why. To see it work a bit, change
authorAndré Pönitz <poenitz@gmx.net>
Tue, 25 Jun 2002 18:21:17 +0000 (18:21 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Tue, 25 Jun 2002 18:21:17 +0000 (18:21 +0000)
formula.C:191, to see it crash remove the comment further down.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4482 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/formula.C
src/mathed/formula.h
src/mathed/preview.C
src/mathed/preview.h

index f84f38c25965fc8c0a50330d8dead9934e99a01b..4e8a018387b8c16cdcd8ff0903cf1a2ab3116c4b 100644 (file)
@@ -43,6 +43,8 @@
 #include "textpainter.h"
 #include "preview.h"
 
+#include "graphics/GraphicsCacheItem.h"
+
 using std::ostream;
 using std::ifstream;
 using std::istream;
@@ -190,8 +192,11 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & font,
        ostringstream os;
        WriteStream wi(os, false, false);
        par_->write(wi);
-       if (grfx::ImagePtr image = preview(os.str()))
-               pain.image(x, y, w, h, *image);
+       if (preview(os.str(), preview_) && preview_.get()) {
+               cerr << "image could be drawn\n";
+               // LyX crashes if we try that. Why? Andre'
+               //pi.pain.image(x, y, w, h, *(preview_->image()));
+       }
 #endif
 
        xx += par_->width();
index 1d32ce06b8baed0e1aab7270fba7d57659b22975..48916ff90b9d88fcf79b759e48f8a3c764c18337 100644 (file)
@@ -24,6 +24,7 @@
 #include "formulabase.h"
 #include "math_defs.h"
 #include "math_atom.h"
+#include "graphics/GraphicsTypes.h"
 
 class MathHullInset;
 
@@ -89,7 +90,9 @@ private:
        ///
        void handleExtern(string const & arg);
 
-       ///
+       /// contents
        MathAtom par_;
+       /// LaTeX preview
+       mutable grfx::GraphicPtr preview_;
 };
 #endif
index 81e56d569b86d3d6ee68d3bf3d22318da3529b42..d16d5466131bf677b251ccc58ed9e05501ab4802 100644 (file)
@@ -1,71 +1,63 @@
 #include <config.h>
 
-#include "formula.h"
 #include "debug.h"
-#include "frontends/Painter.h"
 #include "support/systemcall.h"
 #include "graphics/GraphicsTypes.h"
 #include "graphics/GraphicsImage.h"
-#include "graphics/GraphicsImageXPM.h"
-#include "graphics/GraphicsCacheItem.h"
 #include "graphics/GraphicsCache.h"
+#include "graphics/GraphicsCacheItem.h"
+#include "Lsstream.h"
 
 #include <fstream>
-#include <map>
 
+#include <boost/utility.hpp>
+#include <boost/bind.hpp>
 
 using namespace std;
 
 
-namespace {
-
-       typedef map<string, grfx::ImagePtr> previews_map;
-
-       // cache for computed previews
-       previews_map thePreviews;
-
-       // cache for scheduled previews
-       vector<string> theSchedule;
+// built some unique filename
+string canonical_name(string const & str)
+{
+       ostringstream os;
+       for (string::const_iterator it = str.begin(); it != str.end(); ++it) 
+               os << char('A' + (*it & 15)) << char('a' + (*it >> 4));
+       return os.str();
 }
 
 
-#if 0
-class PreviewCallbackLoaded {
-public:
-       ///
-       PreviewCallbackLoaded(string const & filename)
-               : image_(grfx::GImageXPM::newImage())
-       {
-               lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
-
-               grfx::GImage::SignalTypePtr s(new grfx::GImage::SignalType(&PreviewCallbackLoaded::imageLoaded));
-               image_->load(filename, s);
-       }
-
-       ///
-       void imageLoaded(bool result)
-       {
-               lyxerr << "Image loaded with result: " << result << endl;
-       }
-
-       grfx::ImagePtr image_;
-
-};
-#endif
-
-
-grfx::ImagePtr preview(string const & str)
+bool preview(string const & str, grfx::GraphicPtr & graphic)
 {
-       // do we already have access to a rendered version?
-       previews_map::const_iterator it = thePreviews.find(str);
-       if (it != thePreviews.end())
-               return it->second;
-
-       // constructing new item
-       //grfx::ImagePtr & im = thePreviews[str];
+       string base = canonical_name(str);
+       string dir  = "/tmp/lyx/";
+       string file = dir + base + ".ps";
+       cerr << "writing '" << str << "' to '" << file << "'\n";
+
+       // get the cache
+       grfx::GCache & gc = grfx::GCache::get();
+
+       // look up the file
+       if (gc.inCache(file)) {
+
+               // it's already in there. Get hold of it.
+               grfx::GraphicPtr gr = grfx::GraphicPtr(gc.graphic(file));
+
+               // is it already loaded?
+               if (gr->status() == grfx::Loaded) {
+                       cerr << "file '" << file << "' ready for display\n";
+                       cerr << "im: " << graphic.get() << " " << gr.get() << "\n";
+                       graphic = gr;
+                       return true;
+               }
+
+               // otherwise we have to wait again
+               cerr << "file '" << file << "' not finished loading\n";
+               return false;
+       }
 
-       lyxerr << "writing: " << str << endl;
-       std::ofstream of("/tmp/previewlyx.tex");
+       // The real work starts.
+       string const texfile = dir + base + ".tex";
+       std::ofstream of(texfile.c_str());
        of << "\\documentclass{article}"
           << "\\usepackage{amssymb}"
           << "\\thispagestyle{empty}"
@@ -74,58 +66,17 @@ grfx::ImagePtr preview(string const & str)
           << "\\end{document}\n";
        of.close();
 
-       Systemcall sc1;
-       sc1.startscript(Systemcall::Wait,
-               "(cd /tmp ; latex previewlyx.tex ; dvips previewlyx.dvi)");
-
-       Systemcall sc2;
-       sc2.startscript(Systemcall::Wait,
-               "(cd /tmp ; convert previewlyx.ps previewlyx.xpm)");
-
-       //PreviewCallbackLoaded cb("/tmp/previewlyx.xpm");
-
-#if 0
-       //grfx::SignalLoadTypePtr on_finish;
-       //on_finish.reset(new SignalLoadType);
-       //on_finish->connect(SigC::slot(this, &imageLoaded));
-
-       // load image
-       XpmImage * xpm_image = new XpmImage;
-       int const success =
-               XpmReadFileToXpmImage("/tmp/previewlyx.ps", xpm_image, 0);
-
-       switch (success) {
-       case XpmOpenFailed:
-               lyxerr[Debug::GRAPHICS]
-                       << "No XPM image file found." << std::endl;
-               break;
-
-       case XpmFileInvalid:
-               lyxerr[Debug::GRAPHICS]
-                       << "File format is invalid" << std::endl;
-               break;
-
-       case XpmNoMemory:
-               lyxerr[Debug::GRAPHICS]
-                       << "Insufficient memory to read in XPM file"
-                       << std::endl;
-               break;
-       }
+       string const cmd =
+               "latex " + base + ".tex ; dvips -o " + base + ".ps " + base + ".dvi ";
+       //cerr << "calling: '" << "(cd " + dir + "; " + cmd + ")\n";
+       Systemcall sc;
+       sc.startscript(Systemcall::Wait, "(cd " + dir + "; " + cmd + ")");
 
-       if (success != XpmSuccess) {
-               XpmFreeXpmImage(xpm_image);
-               delete xpm_image;
-
-               lyxerr[Debug::GRAPHICS]
-                       << "Error reading XPM file '"
-                       << XpmGetErrorString(success) << "'"
-                       << std::endl;
-       } else {
-               //grfx::GImageXPM * xim = static_cast<grfx::GImageXPM *>(im.get());
-               //xim->image_.reset(*xpm_image);
-       }
+       // now we are done, add the file to the cache
+       gc.add(file);
+       gc.graphic(file)->startLoading();
 
-       return im;
-#endif
-       return it->second;      
+       // This might take a while. Wait for the next round.
+       cerr << "file '" << file << "' registered\n";
+       return false;
 }
index 51798ce916a92286162f7462461dabcae4551420..5ba7fd7fd0894f7af14fdd5b26a5af5ba011ab56 100644 (file)
@@ -4,6 +4,6 @@
 #include "LString.h"
 #include "graphics/GraphicsTypes.h"
 
-grfx::ImagePtr preview(string const & str);
+bool preview(string const & str, grfx::GraphicPtr & graphic);
 
 #endif