diff --git a/src/adapters/controllers/page_controller.cpp b/src/adapters/controllers/page_controller.cpp index 65ac8502a..1d1c64c3d 100644 --- a/src/adapters/controllers/page_controller.cpp +++ b/src/adapters/controllers/page_controller.cpp @@ -60,7 +60,7 @@ QImage PageController::renderPage() return m_pageImage; auto zoom = m_matrix.a; - m_pageImage = utils::qImageFromPixmap(m_pageGenerator.renderPage(zoom)); + m_pageImage = utils::qImageFromPixmap(m_pageGenerator.renderPage(zoom, "#CECECE")); // Require custom color passed from qml auto xOffset = m_pageGenerator.getPageXOffset(); auto yOffset = m_pageGenerator.getPageYOffset(); diff --git a/src/application/core/metadata_extractor.cpp b/src/application/core/metadata_extractor.cpp index 76851aa63..d56cf3d96 100644 --- a/src/application/core/metadata_extractor.cpp +++ b/src/application/core/metadata_extractor.cpp @@ -156,7 +156,7 @@ QImage MetadataExtractor::getCover() try { core::PageGenerator page(m_document.get(), 0); - return utils::qImageFromPixmap(page.renderPage(1.0)); + return utils::qImageFromPixmap(page.renderPage(1.0, "#FFFFFF")); // #FFFFFF is white color for default cover } catch(...) { diff --git a/src/application/core/page_generator.cpp b/src/application/core/page_generator.cpp index f9d0d450b..c3d41da34 100644 --- a/src/application/core/page_generator.cpp +++ b/src/application/core/page_generator.cpp @@ -71,7 +71,7 @@ void PageGenerator::setupLinks() } } -mupdf::FzPixmap PageGenerator::renderPage(float zoom) +mupdf::FzPixmap PageGenerator::renderPage(float zoom, const std::string& hexColor) { // Create matrix with zoom mupdf::FzMatrix matrix; @@ -79,6 +79,10 @@ mupdf::FzPixmap PageGenerator::renderPage(float zoom) matrix.d = zoom; auto pixmap = getEmptyPixmap(matrix); + + // Set initial color of pixmap to custom rgb hex code + pixmap.fz_fill_pixmap_with_color(mupdf::FzColorspace::Fixed_RGB, convertHexToRGB(hexColor).data(), mupdf::FzColorParams()); + auto drawDevice = mupdf::fz_new_draw_device(mupdf::FzMatrix(), pixmap); // Determine the page offset the first time we render the page @@ -115,12 +119,30 @@ mupdf::FzPixmap PageGenerator::renderPage(float zoom) m_displayList.fz_run_display_list(drawDevice, matrix, rect, cookie); drawDevice.fz_close_device(); + // A bad attempt to change text color.... + // pixmap.fz_tint_pixmap(0xF4F4F4, 0x000000); + if(m_invertColor) pixmap.fz_invert_pixmap(); return pixmap; } +// Convert hex color to acceptable rgb format for colorspace +std::array PageGenerator::convertHexToRGB(const std::string& hex) +{ + std::array rgb = {0.0f, 0.0f, 0.0f}; + if (hex[0] == '#') { + std::string hexColor = hex.substr(1); + + // Convert hex to rgb + rgb[0] = std::stoi(hexColor.substr(0, 2), nullptr, 16) / 255.0f; + rgb[1] = std::stoi(hexColor.substr(2, 2), nullptr, 16) / 255.0f; + rgb[2] = std::stoi(hexColor.substr(4, 2), nullptr, 16) / 255.0f; + } + return rgb; +} + mupdf::FzPixmap PageGenerator::getEmptyPixmap( const mupdf::FzMatrix& matrix) const { diff --git a/src/application/core/page_generator.hpp b/src/application/core/page_generator.hpp index 2bbdd5cd4..027a4d1fc 100644 --- a/src/application/core/page_generator.hpp +++ b/src/application/core/page_generator.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include #include "application_export.hpp" @@ -29,7 +30,7 @@ class APPLICATION_EXPORT PageGenerator int getPageXOffset() const; int getPageYOffset() const; - mupdf::FzPixmap renderPage(float zoom); + mupdf::FzPixmap renderPage(float zoom, const std::string& hexColor); void setInvertColor(bool newInvertColor); bool pointIsAboveText(mupdf::FzPoint point); @@ -43,6 +44,8 @@ class APPLICATION_EXPORT PageGenerator utils::FzPointPair getPositionsForLineSelection(mupdf::FzPoint point); std::string getTextFromSelection(mupdf::FzPoint start, mupdf::FzPoint end); + std::array convertHexToRGB(const std::string& hex); + private: void setupDisplayList(const mupdf::FzRect& boundPage); void setupTextPage(int pageNumber);