From b1e2986df8ad819ac732e8a86b6dff09333a95ca Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sat, 16 Mar 2024 11:55:17 +0100 Subject: [PATCH] Use docstring for preview snippet all the way down No need to convert back and forth --- src/graphics/PreviewImage.cpp | 10 +++---- src/graphics/PreviewImage.h | 4 +-- src/graphics/PreviewLoader.cpp | 49 +++++++++++++++++----------------- src/graphics/PreviewLoader.h | 9 ++++--- src/insets/InsetText.cpp | 4 +-- src/insets/RenderPreview.cpp | 4 +-- src/insets/RenderPreview.h | 2 +- 7 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/graphics/PreviewImage.cpp b/src/graphics/PreviewImage.cpp index b31af6fbd9..e94384c168 100644 --- a/src/graphics/PreviewImage.cpp +++ b/src/graphics/PreviewImage.cpp @@ -32,7 +32,7 @@ class PreviewImage::Impl { public: /// Impl(PreviewImage & p, PreviewLoader & l, - string const & s, FileName const & f, double af); + docstring const & s, FileName const & f, double af); /// ~Impl(); /// @@ -47,14 +47,14 @@ public: /// Loader iloader_; /// - string const snippet_; + docstring const snippet_; /// double const ascent_frac_; }; PreviewImage::PreviewImage(PreviewLoader & l, - string const & s, + docstring const & s, FileName const & f, double 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_; } @@ -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) : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf), snippet_(s), ascent_frac_(af) diff --git a/src/graphics/PreviewImage.h b/src/graphics/PreviewImage.h index bea2553eeb..6d26748092 100644 --- a/src/graphics/PreviewImage.h +++ b/src/graphics/PreviewImage.h @@ -31,14 +31,14 @@ public: * descent = height * (1 - ascent_frac) */ PreviewImage(PreviewLoader & parent, - std::string const & latex_snippet, + docstring const & latex_snippet, support::FileName const & bitmap_file, double ascent_frac); /// ~PreviewImage(); /// - std::string const & snippet() const; + docstring const & snippet() const; /// Dimension dim() const; diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp index d39a6a8513..4c37f7a8d0 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -47,16 +47,17 @@ #include using namespace std; +using namespace lyx; using namespace lyx::support; namespace { -typedef pair SnippetPair; +typedef pair SnippetPair; // A list of all snippets to be converted to previews -typedef list PendingSnippets; +typedef list PendingSnippets; // Each item in the vector is a pair. typedef vector BitmapFile; @@ -121,7 +122,7 @@ void setAscentFractions(vector & ascent_fractions, } -std::function FindFirst(string const & comp) +std::function FindFirst(docstring const & comp) { return [&comp](SnippetPair const & sp) { return sp.first == comp; }; } @@ -166,13 +167,13 @@ public: /// Stop any InProgress items still executing. ~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, /// to do it in the background. void startLoading(bool wait = false); @@ -199,7 +200,7 @@ private: */ typedef std::shared_ptr PreviewImagePtr; /// - typedef map Cache; + typedef map 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); } -PreviewLoader::Status PreviewLoader::status(string const & latex_snippet) const +PreviewLoader::Status PreviewLoader::status(docstring const & latex_snippet) const { 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); } -void PreviewLoader::remove(string const & latex_snippet) const +void PreviewLoader::remove(docstring const & latex_snippet) const { pimpl_->remove(latex_snippet); } @@ -315,7 +316,7 @@ public: : to_format_(to_format), base_(filename_base), counter_(1) {} - SnippetPair const operator()(string const & snippet) + SnippetPair const operator()(docstring const & snippet) { ostringstream os; os << base_ << counter_++ << '.' << to_format_; @@ -430,7 +431,7 @@ PreviewLoader::Impl::~Impl() PreviewImage const * -PreviewLoader::Impl::preview(string const & latex_snippet) const +PreviewLoader::Impl::preview(docstring const & latex_snippet) const { int fs = int(buffer_.fontScalingFactor()); int fg = 0x0; @@ -475,7 +476,7 @@ void PreviewLoader::Impl::refreshPreviews() namespace { -std::function FindSnippet(string const & s) +std::function FindSnippet(docstring const & s) { return [&s](InProgressProcess const & process) { BitmapFile const & snippets = process.second.snippets; @@ -488,7 +489,7 @@ std::function FindSnippet(string const & s) } // namespace 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); 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) return; - string const snippet = trim(latex_snippet); + docstring const snippet = trim(latex_snippet); if (snippet.empty()) return; @@ -529,7 +530,7 @@ void PreviewLoader::Impl::add(string const & latex_snippet) namespace { -std::function EraseSnippet(string const & s) +std::function EraseSnippet(docstring const & s) { return [&s](InProgressProcess & process) { BitmapFile & snippets = process.second.snippets; @@ -545,7 +546,7 @@ std::function EraseSnippet(string const & s) } // namespace -void PreviewLoader::Impl::remove(string const & latex_snippet) +void PreviewLoader::Impl::remove(docstring const & latex_snippet) { Cache::iterator cit = cache_.find(latex_snippet); if (cit != cache_.end()) @@ -768,7 +769,7 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval) size_t metrics_counter = 0; for (; it != end; ++it, ++metrics_counter) { - string const & snip = it->first; + docstring const & snip = it->first; FileName const & file = it->second; double af = ascent_fractions[metrics_counter]; @@ -857,19 +858,19 @@ void PreviewLoader::Impl::dumpData(odocstream & os, // FIXME: the preview loader should be able // to handle multiple encodings // or we should generally use utf8 - for (char_type n : from_utf8(it->first)) { + for (char_type n : it->first) { if (!enc.encodable(n)) { LYXERR0("Uncodable character '" << docstring(1, n) << "' in preview snippet!"); uncodable_content = true; + break; } } - // FIXME UNICODE os << "\\begin{preview}\n"; // do not show incomplete preview if (!uncodable_content) - os << from_utf8(it->first); + os << it->first; os << "\n\\end{preview}\n\n"; } } diff --git a/src/graphics/PreviewLoader.h b/src/graphics/PreviewLoader.h index 272c4d361b..151be82b5f 100644 --- a/src/graphics/PreviewLoader.h +++ b/src/graphics/PreviewLoader.h @@ -19,6 +19,7 @@ #define PREVIEWLOADER_H #include "ColorCode.h" +#include "support/docstring.h" #include "support/signals.h" #include @@ -44,7 +45,7 @@ public: /** Is there an image already associated with this snippet of LaTeX? * 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 { @@ -59,13 +60,13 @@ public: }; /// 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. - void add(std::string const & latex_snippet) const; + void add(docstring const & latex_snippet) const; /// 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". * Initiate their transformation into bitmap images. diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index 7dbf43e3a7..d65cbf9f31 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -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, // unlike a preview that is not immediately required for display. 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. 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 // is typically slow, but extremely fast compared to compilation of the // 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. rp.exportdata->addExternalFile("docbook5", filename, newFileName); diff --git a/src/insets/RenderPreview.cpp b/src/insets/RenderPreview.cpp index 313dface96..48d471c3c5 100644 --- a/src/insets/RenderPreview.cpp +++ b/src/insets/RenderPreview.cpp @@ -88,7 +88,7 @@ RenderBase * RenderPreview::clone(Inset const * inset) const namespace { -docstring const statusMessage(BufferView const * bv, string const & snippet) +docstring const statusMessage(BufferView const * bv, docstring const & snippet) { LASSERT(bv, return docstring()); @@ -221,7 +221,7 @@ void RenderPreview::addPreview(docstring const & latex_snippet, // FIXME UNICODE // 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()) return; diff --git a/src/insets/RenderPreview.h b/src/insets/RenderPreview.h index 24eda3a691..f76d50b4f9 100644 --- a/src/insets/RenderPreview.h +++ b/src/insets/RenderPreview.h @@ -96,7 +96,7 @@ private: void imageReady(graphics::PreviewImage const &); /// 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 * only once. -- 2.39.2