From c262f222de73352571deae230476e5683a55fe96 Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Tue, 16 Dec 2025 03:15:54 +1100 Subject: [PATCH] Parse cover image path from content.opf file (#24) --- lib/Epub/Epub.cpp | 3 +++ lib/Epub/Epub/parsers/ContentOpfParser.cpp | 20 +++++++++++++++++--- lib/Epub/Epub/parsers/ContentOpfParser.h | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index f9273a9..8b4bb9a 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -69,6 +69,9 @@ bool Epub::parseContentOpf(const std::string& contentOpfFilePath) { // Grab data from opfParser into epub title = opfParser.title; + if (!opfParser.coverItemId.empty() && opfParser.items.count(opfParser.coverItemId) > 0) { + coverImageItem = opfParser.items.at(opfParser.coverItemId); + } if (opfParser.items.count("ncx")) { tocNcxItem = opfParser.items.at("ncx"); diff --git a/lib/Epub/Epub/parsers/ContentOpfParser.cpp b/lib/Epub/Epub/parsers/ContentOpfParser.cpp index 1dcdb04..667f679 100644 --- a/lib/Epub/Epub/parsers/ContentOpfParser.cpp +++ b/lib/Epub/Epub/parsers/ContentOpfParser.cpp @@ -90,9 +90,23 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name return; } - // TODO: Support book cover - // if (self->state == IN_METADATA && (strcmp(name, "meta") == 0 || strcmp(name, "opf:meta") == 0)) { - // } + if (self->state == IN_METADATA && (strcmp(name, "meta") == 0 || strcmp(name, "opf:meta") == 0)) { + bool isCover = false; + std::string coverItemId; + + for (int i = 0; atts[i]; i += 2) { + if (strcmp(atts[i], "name") == 0 && strcmp(atts[i + 1], "cover") == 0) { + isCover = true; + } else if (strcmp(atts[i], "content") == 0) { + coverItemId = atts[i + 1]; + } + } + + if (isCover) { + self->coverItemId = coverItemId; + } + return; + } if (self->state == IN_MANIFEST && (strcmp(name, "item") == 0 || strcmp(name, "opf:item") == 0)) { std::string itemId; diff --git a/lib/Epub/Epub/parsers/ContentOpfParser.h b/lib/Epub/Epub/parsers/ContentOpfParser.h index 5da16bd..cba4551 100644 --- a/lib/Epub/Epub/parsers/ContentOpfParser.h +++ b/lib/Epub/Epub/parsers/ContentOpfParser.h @@ -28,6 +28,7 @@ class ContentOpfParser final : public Print { public: std::string title; std::string tocNcxPath; + std::string coverItemId; std::map items; std::vector spineRefs;