X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fgraphics%2FPreviewLoader.C;h=5eb37010644edb289abdabada41b42da36cc3ee2;hb=3aa7e91a827fa15b3e0906b975c4755a2dcdb76d;hp=9344e69e79e917271e10adaf2fff4e63c73c8b0b;hpb=b676c4e77dece2a0642c898e27d76a726a0855f7;p=lyx.git diff --git a/src/graphics/PreviewLoader.C b/src/graphics/PreviewLoader.C index 9344e69e79..5eb3701064 100644 --- a/src/graphics/PreviewLoader.C +++ b/src/graphics/PreviewLoader.C @@ -1,9 +1,10 @@ -/* +/** * \file PreviewLoader.C - * Copyright 2002 the LyX Team * Read the file COPYING * - * \author Angus Leeming + * \author Angus Leeming + * + * Full author contact details available in file CREDITS */ #include @@ -16,11 +17,9 @@ #include "PreviewImage.h" #include "buffer.h" -#include "bufferparams.h" #include "converter.h" #include "debug.h" #include "lyxrc.h" -#include "lyxtextclasslist.h" #include "LColor.h" #include "insets/inset.h" @@ -70,9 +69,6 @@ typedef list PendingSnippets; // Each item in the vector is a pair. typedef vector BitmapFile; - -double setFontScalingFactor(Buffer &); - string const unique_filename(string const bufferpath); Converter const * setConverter(); @@ -99,7 +95,7 @@ struct InProgress { InProgress(string const & filename_base, PendingSnippets const & pending, string const & to_format); - /// Remove any files left lying around and kill the forked process. + /// Remove any files left lying around and kill the forked process. void stop() const; /// @@ -135,6 +131,11 @@ struct PreviewLoader::Impl : public boost::signals::trackable { /// void startLoading(); + /// Emit this signal when an image is ready for display. + boost::signal1 imageReady; + + Buffer const & buffer() const { return buffer_; } + private: /// Called by the Forkedcall process that generated the bitmap files. void finishedGenerating(string const &, pid_t, int); @@ -171,7 +172,7 @@ private: double font_scaling_factor_; /// We don't own this - static Converter const * pconverter_; + static Converter const * pconverter_; }; @@ -201,23 +202,41 @@ PreviewLoader::Status PreviewLoader::status(string const & latex_snippet) const } -void PreviewLoader::add(string const & latex_snippet) +void PreviewLoader::add(string const & latex_snippet) const { pimpl_->add(latex_snippet); } -void PreviewLoader::remove(string const & latex_snippet) +void PreviewLoader::remove(string const & latex_snippet) const { pimpl_->remove(latex_snippet); } -void PreviewLoader::startLoading() +void PreviewLoader::startLoading() const { pimpl_->startLoading(); } + +boost::signals::connection PreviewLoader::connect(slot_type const & slot) const +{ + return pimpl_->imageReady.connect(slot); +} + + +void PreviewLoader::emitSignal(PreviewImage const & pimage) const +{ + pimpl_->imageReady(pimage); +} + + +Buffer const & PreviewLoader::buffer() const +{ + return pimpl_->buffer(); +} + } // namespace grfx @@ -235,10 +254,7 @@ struct IncrementedFileName { StrPair const operator()(string const & snippet) { ostringstream os; - os << base_ - << setfill('0') << setw(3) << counter_++ - << "." << to_format_; - + os << base_ << counter_++ << "." << to_format_; string const file = os.str().c_str(); return make_pair(snippet, file); @@ -250,7 +266,7 @@ private: int counter_; }; - + InProgress::InProgress(string const & filename_base, PendingSnippets const & pending, string const & to_format) @@ -262,7 +278,7 @@ InProgress::InProgress(string const & filename_base, PendingSnippets::const_iterator pend = pending.end(); BitmapFile::iterator sit = snippets.begin(); - std::transform(pit, pend, sit, + std::transform(pit, pend, sit, IncrementedFileName(to_format, filename_base)); } @@ -282,7 +298,7 @@ void InProgress::stop() const lyx::unlink(vit->second); } } - + } // namespace anon @@ -291,7 +307,8 @@ namespace grfx { PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b) : parent_(p), buffer_(b), font_scaling_factor_(0.0) { - font_scaling_factor_ = setFontScalingFactor(const_cast(b)); + font_scaling_factor_ = 0.01 * lyxrc.dpi * lyxrc.zoom * + lyxrc.preview_scale_factor; lyxerr[Debug::GRAPHICS] << "The font scaling factor is " << font_scaling_factor_ << endl; @@ -369,7 +386,13 @@ void PreviewLoader::Impl::add(string const & latex_snippet) if (!pconverter_ || status(latex_snippet) != NotFound) return; - pending_.push_back(latex_snippet); + string const snippet = trim(latex_snippet); + if (snippet.empty()) + return; + + lyxerr[Debug::GRAPHICS] << "adding snippet:\n" << snippet << endl; + + pending_.push_back(snippet); } @@ -450,9 +473,9 @@ void PreviewLoader::Impl::startLoading() // The conversion command. ostringstream cs; cs << pconverter_->command << " " << latexfile << " " - << font_scaling_factor_; + << int(font_scaling_factor_) << " " << pconverter_->to; - string const command = cs.str().c_str(); + string const command = "sh " + LibScriptSearch(cs.str().c_str()); // Initiate the conversion from LaTeX to bitmap images files. Forkedcall::SignalTypePtr convert_ptr; @@ -526,7 +549,7 @@ void PreviewLoader::Impl::finishedGenerating(string const & command, std::list::const_iterator nit = newimages.begin(); std::list::const_iterator nend = newimages.end(); for (; nit != nend; ++nit) { - parent_.imageReady(*nit->get()); + imageReady(*nit->get()); } } @@ -536,17 +559,15 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const // Why on earth is Buffer::makeLaTeXFile a non-const method? Buffer & tmp = const_cast(buffer_); // Dump the preamble only. - tmp.makeLaTeXFile(os, string(), true, false, true); + tmp.makeLaTeXFile(os, buffer_.filePath(), true, false, true); // Loop over the insets in the buffer and dump all the math-macros. Buffer::inset_iterator it = buffer_.inset_const_iterator_begin(); Buffer::inset_iterator end = buffer_.inset_const_iterator_end(); - for (; it != end; ++it) { - if ((*it)->lyxCode() == Inset::MATHMACRO_CODE) { - (*it)->latex(&buffer_, os, true, true); - } - } + for (; it != end; ++it) + if (it->lyxCode() == Inset::MATHMACRO_CODE) + it->latex(&buffer_, os, true, true); // All equation lables appear as "(#)" + preview.sty's rendering of // the label name @@ -556,7 +577,7 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const // Use the preview style file to ensure that each snippet appears on a // fresh page. os << "\n" - << "\\usepackage[active,delayed,dvips,tightpage,showlabels]{preview}\n" + << "\\usepackage[active,delayed,dvips,tightpage,showlabels,lyx]{preview}\n" << "\n"; // This piece of PostScript magic ensures that the foreground and @@ -605,8 +626,6 @@ string const unique_filename(string const bufferpath) Converter const * setConverter() { - Converter const * converter = 0; - string const from = "lyxpreview"; Formats::FormatList::const_iterator it = formats.begin(); @@ -630,68 +649,8 @@ Converter const * setConverter() "defined." << endl; } - - return 0; -} - - -double setFontScalingFactor(Buffer & buffer) -{ - double scale_factor = 0.01 * lyxrc.dpi * lyxrc.zoom * - lyxrc.preview_scale_factor; - - // Has the font size been set explicitly? - string const & fontsize = buffer.params.fontsize; - lyxerr[Debug::GRAPHICS] << "PreviewLoader::scaleToFitLyXView()\n" - << "font size is " << fontsize << endl; - - if (isStrUnsignedInt(fontsize)) - return 10.0 * scale_factor / strToDbl(fontsize); - - // No. We must extract it from the LaTeX class file. - LyXTextClass const & tclass = textclasslist[buffer.params.textclass]; - string const textclass(tclass.latexname() + ".cls"); - string const classfile(findtexfile(textclass, "cls")); - - lyxerr[Debug::GRAPHICS] << "text class is " << textclass << '\n' - << "class file is " << classfile << endl; - - ifstream ifs(classfile.c_str()); - if (!ifs.good()) { - lyxerr[Debug::GRAPHICS] << "Unable to open class file!" << endl; - return scale_factor; - } - - string str; - double scaling = scale_factor; - while (ifs.good()) { - getline(ifs, str); - // To get the default font size, look for a line like - // "\ExecuteOptions{letterpaper,10pt,oneside,onecolumn,final}" - if (!prefixIs(frontStrip(str), "\\ExecuteOptions")) - continue; - - // str contains just the options of \ExecuteOptions - string const tmp = split(str, '{'); - split(tmp, str, '}'); - - int count = 0; - string tok = token(str, ',', count++); - while (!isValidLength(tok) && !tok.empty()) - tok = token(str, ',', count++); - - if (!tok.empty()) { - lyxerr[Debug::GRAPHICS] - << "Extracted default font size from " - "LaTeX class file successfully!" << endl; - LyXLength fsize(tok); - scaling *= 10.0 / fsize.value(); - break; - } - } - - return scaling; + return 0; } @@ -704,37 +663,72 @@ void setAscentFractions(vector & ascent_fractions, vector::iterator end = ascent_fractions.end(); fill(it, end, 0.5); - ifstream ifs(metrics_file.c_str()); - if (!ifs.good()) { - lyxerr[Debug::GRAPHICS] << "setAscentFractions(" - << metrics_file << ")\n" - << "Unable to open file!" - << endl; + ifstream in(metrics_file.c_str()); + if (!in.good()) { + lyxerr[Debug::GRAPHICS] + << "setAscentFractions(" << metrics_file << ")\n" + << "Unable to open file!" << endl; return; } - for (; it != end; ++it) { - string page; - string page_id; - int dummy; - double ascent; - double descent; - - ifs >> page >> page_id >> dummy >> dummy >> dummy >> dummy - >> ascent >> descent >> dummy; - - if (!ifs.good() || - page != "%%Page" || - !isStrUnsignedInt(strip(page_id, ':'))) { - lyxerr[Debug::GRAPHICS] << "setAscentFractions(" - << metrics_file << ")\n" - << "Error reading file!" - << endl; + bool error = false; + + // Tightpage dimensions affect all subsequent dimensions + int tp_ascent; + int tp_descent; + + int snippet_counter = 0; + while (!in.eof()) { + // Expecting lines of the form + // Preview: Tightpage tp_bl_x tp_bl_y tp_tr_x tp_tr_y + // Preview: Snippet id ascent descent width + string preview; + string type; + in >> preview >> type; + + if (!in.good()) + // eof after all break; + + error = preview != "Preview:" + || (type != "Tightpage" && type != "Snippet"); + if (error) + break; + + if (type == "Tightpage") { + int dummy; + in >> dummy >> tp_descent >> dummy >> tp_ascent; + + error = !in.good(); + if (error) + break; + + } else { + int dummy; + int snippet_id; + int ascent; + int descent; + in >> snippet_id >> ascent >> descent >> dummy; + + error = !in.good() || ++snippet_counter != snippet_id; + if (error) + break; + + double const a = ascent + tp_ascent; + double const d = descent - tp_descent; + + if (!lyx::float_equal(a + d, 0, 0.1)) + *it = a / (a + d); + + if (++it == end) + break; } + } - if (ascent + descent != 0) - *it = ascent / (ascent + descent); + if (error) { + lyxerr[Debug::GRAPHICS] + << "setAscentFractions(" << metrics_file << ")\n" + << "Error reading file!\n" << endl; } }