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:
///
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)

View File

@ -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;

View File

@ -47,16 +47,17 @@
#include <QTimer>
using namespace std;
using namespace lyx;
using namespace lyx::support;
namespace {
typedef pair<string, FileName> SnippetPair;
typedef pair<docstring, FileName> SnippetPair;
// 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>.
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; };
}
@ -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<PreviewImage> PreviewImagePtr;
///
typedef map<string, PreviewImagePtr> Cache;
typedef map<docstring, PreviewImagePtr> 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<bool (InProgressProcess const &)> FindSnippet(string const & s)
std::function<bool (InProgressProcess const &)> FindSnippet(docstring const & s)
{
return [&s](InProgressProcess const & process) {
BitmapFile const & snippets = process.second.snippets;
@ -488,7 +489,7 @@ std::function<bool (InProgressProcess const &)> 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<void (InProgressProcess &)> EraseSnippet(string const & s)
std::function<void (InProgressProcess &)> EraseSnippet(docstring const & s)
{
return [&s](InProgressProcess & process) {
BitmapFile & snippets = process.second.snippets;
@ -545,7 +546,7 @@ std::function<void (InProgressProcess &)> 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";
}
}

View File

@ -19,6 +19,7 @@
#define PREVIEWLOADER_H
#include "ColorCode.h"
#include "support/docstring.h"
#include "support/signals.h"
#include <QObject>
@ -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.

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,
// 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);

View File

@ -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;

View File

@ -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.