File indexing completed on 2024-04-28 04:32:43

0001 /*
0002     SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "generator_p.h"
0008 
0009 #include <QDebug>
0010 
0011 #include "fontinfo.h"
0012 #include "utils.h"
0013 
0014 using namespace Okular;
0015 
0016 PixmapGenerationThread::PixmapGenerationThread(Generator *generator)
0017     : mGenerator(generator)
0018     , mRequest(nullptr)
0019     , mCalcBoundingBox(false)
0020 {
0021 }
0022 
0023 void PixmapGenerationThread::startGeneration(PixmapRequest *request, bool calcBoundingBox)
0024 {
0025     mRequest = request;
0026     mCalcBoundingBox = calcBoundingBox;
0027 
0028     start(QThread::InheritPriority);
0029 }
0030 
0031 void PixmapGenerationThread::endGeneration()
0032 {
0033     mRequest = nullptr;
0034 }
0035 
0036 PixmapRequest *PixmapGenerationThread::request() const
0037 {
0038     return mRequest;
0039 }
0040 
0041 QImage PixmapGenerationThread::image() const
0042 {
0043     return mRequest ? PixmapRequestPrivate::get(mRequest)->mResultImage : QImage();
0044 }
0045 
0046 bool PixmapGenerationThread::calcBoundingBox() const
0047 {
0048     return mCalcBoundingBox;
0049 }
0050 
0051 NormalizedRect PixmapGenerationThread::boundingBox() const
0052 {
0053     return mBoundingBox;
0054 }
0055 
0056 void PixmapGenerationThread::run()
0057 {
0058     if (mRequest) {
0059         PixmapRequestPrivate::get(mRequest)->mResultImage = mGenerator->image(mRequest);
0060 
0061         if (mCalcBoundingBox) {
0062             mBoundingBox = Utils::imageBoundingBox(&PixmapRequestPrivate::get(mRequest)->mResultImage);
0063         }
0064     }
0065 }
0066 
0067 TextPageGenerationThread::TextPageGenerationThread(Generator *generator)
0068     : mGenerator(generator)
0069     , mTextPage(nullptr)
0070 {
0071     TextRequestPrivate *treqPriv = TextRequestPrivate::get(&mTextRequest);
0072     treqPriv->mPage = nullptr;
0073     treqPriv->mShouldAbortExtraction = 0;
0074 }
0075 
0076 void TextPageGenerationThread::startGeneration()
0077 {
0078     if (page()) {
0079         start(QThread::InheritPriority);
0080     }
0081 }
0082 
0083 void TextPageGenerationThread::endGeneration()
0084 {
0085     TextRequestPrivate *treqPriv = TextRequestPrivate::get(&mTextRequest);
0086     treqPriv->mPage = nullptr;
0087     treqPriv->mShouldAbortExtraction = 0;
0088 }
0089 
0090 void TextPageGenerationThread::setPage(Page *page)
0091 {
0092     TextRequestPrivate *treqPriv = TextRequestPrivate::get(&mTextRequest);
0093     treqPriv->mPage = page;
0094     treqPriv->mShouldAbortExtraction = 0;
0095 }
0096 
0097 Page *TextPageGenerationThread::page() const
0098 {
0099     return mTextRequest.page();
0100 }
0101 
0102 TextPage *TextPageGenerationThread::textPage() const
0103 {
0104     return mTextPage;
0105 }
0106 
0107 void TextPageGenerationThread::abortExtraction()
0108 {
0109     // If extraction already finished no point in aborting
0110     if (!mTextPage) {
0111         TextRequestPrivate *treqPriv = TextRequestPrivate::get(&mTextRequest);
0112         treqPriv->mShouldAbortExtraction = 1;
0113     }
0114 }
0115 
0116 bool TextPageGenerationThread::shouldAbortExtraction() const
0117 {
0118     return mTextRequest.shouldAbortExtraction();
0119 }
0120 
0121 void TextPageGenerationThread::run()
0122 {
0123     mTextPage = nullptr;
0124 
0125     Q_ASSERT(page());
0126 
0127     mTextPage = mGenerator->textPage(&mTextRequest);
0128 
0129     if (mTextRequest.shouldAbortExtraction()) {
0130         delete mTextPage;
0131         mTextPage = nullptr;
0132     }
0133 }
0134 
0135 FontExtractionThread::FontExtractionThread(Generator *generator, int pages)
0136     : mGenerator(generator)
0137     , mNumOfPages(pages)
0138     , mGoOn(true)
0139 {
0140 }
0141 
0142 void FontExtractionThread::startExtraction(bool async)
0143 {
0144     if (async) {
0145         connect(this, &FontExtractionThread::finished, this, &FontExtractionThread::deleteLater);
0146         start(QThread::InheritPriority);
0147     } else {
0148         run();
0149         deleteLater();
0150     }
0151 }
0152 
0153 void FontExtractionThread::stopExtraction()
0154 {
0155     mGoOn = false;
0156 }
0157 
0158 void FontExtractionThread::run()
0159 {
0160     for (int i = -1; i < mNumOfPages && mGoOn; ++i) {
0161         const FontInfo::List list = mGenerator->fontsForPage(i);
0162         for (const FontInfo &fi : list) {
0163             Q_EMIT gotFont(fi);
0164         }
0165         Q_EMIT progress(i);
0166     }
0167 }