File indexing completed on 2022-12-06 18:50:21

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2019 Sirgienko Nikita <warquark@gmail.com>
0004 */
0005 
0006 #include "mathrender.h"
0007 
0008 #include <QThreadPool>
0009 #include <QDebug>
0010 #include <QStandardPaths>
0011 #include <QFile>
0012 #include <QFileInfo>
0013 
0014 #include "mathrendertask.h"
0015 #include "lib/renderer.h"
0016 
0017 MathRenderer::MathRenderer(): m_scale(1.0), m_useHighRes(false)
0018 {
0019     qRegisterMetaType<QSharedPointer<MathRenderResult>>();
0020 }
0021 
0022 MathRenderer::~MathRenderer()
0023 {
0024 }
0025 
0026 bool MathRenderer::mathRenderAvailable()
0027 {
0028     QFileInfo info(QStandardPaths::findExecutable(QLatin1String("pdflatex")));
0029     return info.exists() && info.isExecutable();
0030 }
0031 
0032 qreal MathRenderer::scale()
0033 {
0034     return m_scale;
0035 }
0036 
0037 void MathRenderer::setScale(qreal scale)
0038 {
0039     m_scale = scale;
0040 }
0041 
0042 void MathRenderer::useHighResolution(bool b)
0043 {
0044     m_useHighRes = b;
0045 }
0046 
0047 void MathRenderer::renderExpression(int jobId, const QString& mathExpression, Cantor::LatexRenderer::EquationType type, const QObject* receiver, const char* resultHandler)
0048 {
0049     MathRenderTask* task = new MathRenderTask(jobId, mathExpression, type, m_scale, m_useHighRes);
0050     task->setHandler(receiver, resultHandler);
0051     task->setAutoDelete(false);
0052 
0053     QThreadPool::globalInstance()->start(task);
0054 }
0055 
0056 void MathRenderer::rerender(QTextDocument* document, const QTextImageFormat& math)
0057 {
0058     const QString& filename = math.property(Cantor::Renderer::ImagePath).toString();
0059     if (!QFile::exists(filename))
0060         return;
0061 
0062     QString errorMessage;
0063     QImage img = Cantor::Renderer::pdfRenderToImage(QUrl::fromLocalFile(filename), m_scale, m_useHighRes, nullptr, &errorMessage);
0064     bool success = img.isNull() == false;
0065 
0066     if (success)
0067     {
0068         QUrl internal(math.name());
0069         document->addResource(QTextDocument::ImageResource, internal, QVariant(img));
0070     }
0071     else
0072     {
0073         qDebug() << "Rerender embedded math failed with message: " << errorMessage;
0074     }
0075 }
0076 
0077 std::pair<QTextImageFormat, QImage> MathRenderer::renderExpressionFromPdf(const QString& filename, const QString& uuid, const QString& code, Cantor::LatexRenderer::EquationType type, bool* outSuccess)
0078 {
0079     if (!QFile::exists(filename))
0080     {
0081         if (outSuccess)
0082             *outSuccess = false;
0083         return std::make_pair(QTextImageFormat(), QImage());
0084     }
0085 
0086     bool success; QString errorMessage;
0087     const auto& data = MathRenderTask::renderPdfToFormat(filename, code, uuid, type, m_scale, m_useHighRes, &success, &errorMessage);
0088     if (success == false)
0089         qDebug() << "Render embedded math from pdf failed with message: " << errorMessage;
0090     if (outSuccess)
0091         *outSuccess = success;
0092     return data;
0093 }