Use docstring for preview snippet all the way down

No need to convert back and forth
This commit is contained in:
Juergen Spitzmueller 2024-03-16 11:55:17 +01:00
parent 1fca6842a5
commit b1e2986df8
7 changed files with 42 additions and 40 deletions

View File

@ -32,7 +32,7 @@ class PreviewImage::Impl {
public: public:
/// ///
Impl(PreviewImage & p, PreviewLoader & l, Impl(PreviewImage & p, PreviewLoader & l,
string const & s, FileName const & f, double af); docstring const & s, FileName const & f, double af);
/// ///
~Impl(); ~Impl();
/// ///
@ -47,14 +47,14 @@ public:
/// ///
Loader iloader_; Loader iloader_;
/// ///
string const snippet_; docstring const snippet_;
/// ///
double const ascent_frac_; double const ascent_frac_;
}; };
PreviewImage::PreviewImage(PreviewLoader & l, PreviewImage::PreviewImage(PreviewLoader & l,
string const & s, docstring const & s,
FileName const & f, FileName const & f,
double af) double af)
: pimpl_(new Impl(*this, l, s, f, af)) : pimpl_(new Impl(*this, l, s, f, af))
@ -67,7 +67,7 @@ PreviewImage::~PreviewImage()
} }
string const & PreviewImage::snippet() const docstring const & PreviewImage::snippet() const
{ {
return pimpl_->snippet_; return pimpl_->snippet_;
} }
@ -105,7 +105,7 @@ PreviewLoader & PreviewImage::previewLoader() const
} }
PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l, string const & s, PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l, docstring const & s,
FileName const & bf, double af) FileName const & bf, double af)
: parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf), : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf),
snippet_(s), ascent_frac_(af) snippet_(s), ascent_frac_(af)

View File

@ -31,14 +31,14 @@ public:
* descent = height * (1 - ascent_frac) * descent = height * (1 - ascent_frac)
*/ */
PreviewImage(PreviewLoader & parent, PreviewImage(PreviewLoader & parent,
std::string const & latex_snippet, docstring const & latex_snippet,
support::FileName const & bitmap_file, support::FileName const & bitmap_file,
double ascent_frac); double ascent_frac);
/// ///
~PreviewImage(); ~PreviewImage();
/// ///
std::string const & snippet() const; docstring const & snippet() const;
/// ///
Dimension dim() const; Dimension dim() const;

View File

@ -47,16 +47,17 @@
#include <QTimer> #include <QTimer>
using namespace std; using namespace std;
using namespace lyx;
using namespace lyx::support; using namespace lyx::support;
namespace { namespace {
typedef pair<string, FileName> SnippetPair; typedef pair<docstring, FileName> SnippetPair;
// A list of all snippets to be converted to previews // A list of all snippets to be converted to previews
typedef list<string> PendingSnippets; typedef list<docstring> PendingSnippets;
// Each item in the vector is a pair<snippet, image file name>. // Each item in the vector is a pair<snippet, image file name>.
typedef vector<SnippetPair> BitmapFile; typedef vector<SnippetPair> BitmapFile;
@ -121,7 +122,7 @@ void setAscentFractions(vector<double> & ascent_fractions,
} }
std::function <bool (SnippetPair const &)> FindFirst(string const & comp) std::function <bool (SnippetPair const &)> FindFirst(docstring const & comp)
{ {
return [&comp](SnippetPair const & sp) { return sp.first == comp; }; return [&comp](SnippetPair const & sp) { return sp.first == comp; };
} }
@ -166,13 +167,13 @@ public:
/// Stop any InProgress items still executing. /// Stop any InProgress items still executing.
~Impl(); ~Impl();
/// ///
PreviewImage const * preview(string const & latex_snippet) const; PreviewImage const * preview(docstring const & latex_snippet) const;
/// ///
PreviewLoader::Status status(string const & latex_snippet) const; PreviewLoader::Status status(docstring const & latex_snippet) const;
/// ///
void add(string const & latex_snippet); void add(docstring const & latex_snippet);
/// ///
void remove(string const & latex_snippet); void remove(docstring const & latex_snippet);
/// \p wait whether to wait for the process to complete or, instead, /// \p wait whether to wait for the process to complete or, instead,
/// to do it in the background. /// to do it in the background.
void startLoading(bool wait = false); void startLoading(bool wait = false);
@ -199,7 +200,7 @@ private:
*/ */
typedef std::shared_ptr<PreviewImage> PreviewImagePtr; typedef std::shared_ptr<PreviewImage> PreviewImagePtr;
/// ///
typedef map<string, PreviewImagePtr> Cache; typedef map<docstring, PreviewImagePtr> Cache;
/// ///
Cache cache_; Cache cache_;
@ -246,25 +247,25 @@ PreviewLoader::PreviewLoader(Buffer const & b)
{} {}
PreviewImage const * PreviewLoader::preview(string const & latex_snippet) const PreviewImage const * PreviewLoader::preview(docstring const & latex_snippet) const
{ {
return pimpl_->preview(latex_snippet); return pimpl_->preview(latex_snippet);
} }
PreviewLoader::Status PreviewLoader::status(string const & latex_snippet) const PreviewLoader::Status PreviewLoader::status(docstring const & latex_snippet) const
{ {
return pimpl_->status(latex_snippet); return pimpl_->status(latex_snippet);
} }
void PreviewLoader::add(string const & latex_snippet) const void PreviewLoader::add(docstring const & latex_snippet) const
{ {
pimpl_->add(latex_snippet); pimpl_->add(latex_snippet);
} }
void PreviewLoader::remove(string const & latex_snippet) const void PreviewLoader::remove(docstring const & latex_snippet) const
{ {
pimpl_->remove(latex_snippet); pimpl_->remove(latex_snippet);
} }
@ -315,7 +316,7 @@ public:
: to_format_(to_format), base_(filename_base), counter_(1) : to_format_(to_format), base_(filename_base), counter_(1)
{} {}
SnippetPair const operator()(string const & snippet) SnippetPair const operator()(docstring const & snippet)
{ {
ostringstream os; ostringstream os;
os << base_ << counter_++ << '.' << to_format_; os << base_ << counter_++ << '.' << to_format_;
@ -430,7 +431,7 @@ PreviewLoader::Impl::~Impl()
PreviewImage const * PreviewImage const *
PreviewLoader::Impl::preview(string const & latex_snippet) const PreviewLoader::Impl::preview(docstring const & latex_snippet) const
{ {
int fs = int(buffer_.fontScalingFactor()); int fs = int(buffer_.fontScalingFactor());
int fg = 0x0; int fg = 0x0;
@ -475,7 +476,7 @@ void PreviewLoader::Impl::refreshPreviews()
namespace { namespace {
std::function<bool (InProgressProcess const &)> FindSnippet(string const & s) std::function<bool (InProgressProcess const &)> FindSnippet(docstring const & s)
{ {
return [&s](InProgressProcess const & process) { return [&s](InProgressProcess const & process) {
BitmapFile const & snippets = process.second.snippets; BitmapFile const & snippets = process.second.snippets;
@ -488,7 +489,7 @@ std::function<bool (InProgressProcess const &)> FindSnippet(string const & s)
} // namespace } // namespace
PreviewLoader::Status PreviewLoader::Status
PreviewLoader::Impl::status(string const & latex_snippet) const PreviewLoader::Impl::status(docstring const & latex_snippet) const
{ {
Cache::const_iterator cit = cache_.find(latex_snippet); Cache::const_iterator cit = cache_.find(latex_snippet);
if (cit != cache_.end()) if (cit != cache_.end())
@ -512,12 +513,12 @@ PreviewLoader::Impl::status(string const & latex_snippet) const
} }
void PreviewLoader::Impl::add(string const & latex_snippet) void PreviewLoader::Impl::add(docstring const & latex_snippet)
{ {
if (!pconverter_ || status(latex_snippet) != NotFound) if (!pconverter_ || status(latex_snippet) != NotFound)
return; return;
string const snippet = trim(latex_snippet); docstring const snippet = trim(latex_snippet);
if (snippet.empty()) if (snippet.empty())
return; return;
@ -529,7 +530,7 @@ void PreviewLoader::Impl::add(string const & latex_snippet)
namespace { namespace {
std::function<void (InProgressProcess &)> EraseSnippet(string const & s) std::function<void (InProgressProcess &)> EraseSnippet(docstring const & s)
{ {
return [&s](InProgressProcess & process) { return [&s](InProgressProcess & process) {
BitmapFile & snippets = process.second.snippets; BitmapFile & snippets = process.second.snippets;
@ -545,7 +546,7 @@ std::function<void (InProgressProcess &)> EraseSnippet(string const & s)
} // namespace } // namespace
void PreviewLoader::Impl::remove(string const & latex_snippet) void PreviewLoader::Impl::remove(docstring const & latex_snippet)
{ {
Cache::iterator cit = cache_.find(latex_snippet); Cache::iterator cit = cache_.find(latex_snippet);
if (cit != cache_.end()) if (cit != cache_.end())
@ -768,7 +769,7 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
size_t metrics_counter = 0; size_t metrics_counter = 0;
for (; it != end; ++it, ++metrics_counter) { for (; it != end; ++it, ++metrics_counter) {
string const & snip = it->first; docstring const & snip = it->first;
FileName const & file = it->second; FileName const & file = it->second;
double af = ascent_fractions[metrics_counter]; double af = ascent_fractions[metrics_counter];
@ -857,19 +858,19 @@ void PreviewLoader::Impl::dumpData(odocstream & os,
// FIXME: the preview loader should be able // FIXME: the preview loader should be able
// to handle multiple encodings // to handle multiple encodings
// or we should generally use utf8 // or we should generally use utf8
for (char_type n : from_utf8(it->first)) { for (char_type n : it->first) {
if (!enc.encodable(n)) { if (!enc.encodable(n)) {
LYXERR0("Uncodable character '" LYXERR0("Uncodable character '"
<< docstring(1, n) << docstring(1, n)
<< "' in preview snippet!"); << "' in preview snippet!");
uncodable_content = true; uncodable_content = true;
break;
} }
} }
// FIXME UNICODE
os << "\\begin{preview}\n"; os << "\\begin{preview}\n";
// do not show incomplete preview // do not show incomplete preview
if (!uncodable_content) if (!uncodable_content)
os << from_utf8(it->first); os << it->first;
os << "\n\\end{preview}\n\n"; os << "\n\\end{preview}\n\n";
} }
} }

View File

@ -19,6 +19,7 @@
#define PREVIEWLOADER_H #define PREVIEWLOADER_H
#include "ColorCode.h" #include "ColorCode.h"
#include "support/docstring.h"
#include "support/signals.h" #include "support/signals.h"
#include <QObject> #include <QObject>
@ -44,7 +45,7 @@ public:
/** Is there an image already associated with this snippet of LaTeX? /** Is there an image already associated with this snippet of LaTeX?
* If so, returns a pointer to it, else returns 0. * If so, returns a pointer to it, else returns 0.
*/ */
PreviewImage const * preview(std::string const & latex_snippet) const; PreviewImage const * preview(docstring const & latex_snippet) const;
/// ///
enum Status { enum Status {
@ -59,13 +60,13 @@ public:
}; };
/// How far have we got in loading the image? /// How far have we got in loading the image?
Status status(std::string const & latex_snippet) const; Status status(docstring const & latex_snippet) const;
/// Add a snippet of LaTeX to the queue for processing. /// Add a snippet of LaTeX to the queue for processing.
void add(std::string const & latex_snippet) const; void add(docstring const & latex_snippet) const;
/// Remove this snippet of LaTeX from the PreviewLoader. /// Remove this snippet of LaTeX from the PreviewLoader.
void remove(std::string const & latex_snippet) const; void remove(docstring const & latex_snippet) const;
/** We have accumulated several latex snippets with status "InQueue". /** We have accumulated several latex snippets with status "InQueue".
* Initiate their transformation into bitmap images. * Initiate their transformation into bitmap images.

View File

@ -661,7 +661,7 @@ void InsetText::docbookRenderAsImage(XMLStream & xs, OutputParams const & rp, XH
// Also, the image must be generated before the DocBook output is finished, // Also, the image must be generated before the DocBook output is finished,
// unlike a preview that is not immediately required for display. // unlike a preview that is not immediately required for display.
docstring const latex_snippet = insetToLaTeXSnippet(&buffer(), this); docstring const latex_snippet = insetToLaTeXSnippet(&buffer(), this);
std::string const snippet = support::trim(to_utf8(latex_snippet)); docstring const snippet = support::trim(latex_snippet);
// TODO: no real support for Unicode. This code is very similar to RenderPreview::addPreview, the same gotcha applies. // TODO: no real support for Unicode. This code is very similar to RenderPreview::addPreview, the same gotcha applies.
graphics::PreviewLoader* loader = buffer().loader(); graphics::PreviewLoader* loader = buffer().loader();
@ -681,7 +681,7 @@ void InsetText::docbookRenderAsImage(XMLStream & xs, OutputParams const & rp, XH
// same hash (by design of cryptographic hash functions). Computing a hash // same hash (by design of cryptographic hash functions). Computing a hash
// is typically slow, but extremely fast compared to compilation of the // is typically slow, but extremely fast compared to compilation of the
// preview and image rendering. // preview and image rendering.
std::string newFileName = "lyx_" + sanitizeFileName(toHexHash(snippet)) + "." + filename.extension(); std::string newFileName = "lyx_" + sanitizeFileName(toHexHash(to_utf8(snippet))) + "." + filename.extension();
// Copy the image into the right folder. // Copy the image into the right folder.
rp.exportdata->addExternalFile("docbook5", filename, newFileName); rp.exportdata->addExternalFile("docbook5", filename, newFileName);

View File

@ -88,7 +88,7 @@ RenderBase * RenderPreview::clone(Inset const * inset) const
namespace { namespace {
docstring const statusMessage(BufferView const * bv, string const & snippet) docstring const statusMessage(BufferView const * bv, docstring const & snippet)
{ {
LASSERT(bv, return docstring()); LASSERT(bv, return docstring());
@ -221,7 +221,7 @@ void RenderPreview::addPreview(docstring const & latex_snippet,
// FIXME UNICODE // FIXME UNICODE
// We have to make sure that we call latex with the right encoding // We have to make sure that we call latex with the right encoding
snippet_ = support::trim(to_utf8(latex_snippet)); snippet_ = support::trim(latex_snippet);
if (snippet_.empty()) if (snippet_.empty())
return; return;

View File

@ -96,7 +96,7 @@ private:
void imageReady(graphics::PreviewImage const &); void imageReady(graphics::PreviewImage const &);
/// The thing that we're trying to generate a preview of. /// The thing that we're trying to generate a preview of.
std::string snippet_; docstring snippet_;
/** Store the connection to the preview loader so that we connect /** Store the connection to the preview loader so that we connect
* only once. * only once.