From: André Pönitz Date: Tue, 25 Jun 2002 18:21:17 +0000 (+0000) Subject: first shot at preview. It crashes. Don't know why. To see it work a bit, change X-Git-Tag: 1.6.10~19010 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=6aa8d56bddcb29d2c266dc520a4e725958c31db1;p=features.git first shot at preview. It crashes. Don't know why. To see it work a bit, change 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 --- diff --git a/src/mathed/formula.C b/src/mathed/formula.C index f84f38c259..4e8a018387 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -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(); diff --git a/src/mathed/formula.h b/src/mathed/formula.h index 1d32ce06b8..48916ff90b 100644 --- a/src/mathed/formula.h +++ b/src/mathed/formula.h @@ -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 diff --git a/src/mathed/preview.C b/src/mathed/preview.C index 81e56d569b..d16d546613 100644 --- a/src/mathed/preview.C +++ b/src/mathed/preview.C @@ -1,71 +1,63 @@ #include -#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 -#include +#include +#include using namespace std; -namespace { - - typedef map previews_map; - - // cache for computed previews - previews_map thePreviews; - - // cache for scheduled previews - vector 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(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; } diff --git a/src/mathed/preview.h b/src/mathed/preview.h index 51798ce916..5ba7fd7fd0 100644 --- a/src/mathed/preview.h +++ b/src/mathed/preview.h @@ -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