Skip to content

Commit

Permalink
squash to previous: relax conditions when <img alt="cover">
Browse files Browse the repository at this point in the history
  • Loading branch information
poire-z committed Mar 20, 2024
1 parent c95381c commit a6a71cb
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions crengine/src/epubfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,10 @@ bool ExtractCoverFilenameFromCoverPageFragment( LVStreamRef stream, lString32 &
return false;
// We expect to find a single image, and no text, to consider the image as a possible cover.
lString32 img_href;
int nb_images_found = 0;
bool has_text = 0;
bool has_more_images = false;
bool has_text = false;
bool in_body = false;
bool img_has_alt_attribute_equal_cover = false;
ldomNode * coverDocRoot = coverDoc->getRootNode();
/* For debugging:
lString32Collection cssFiles;
Expand All @@ -380,16 +381,27 @@ bool ExtractCoverFilenameFromCoverPageFragment( LVStreamRef stream, lString32 &
}
}
else if (id == el_img ) { // <img src=''>
img_href = n->getAttributeValue(attr_src);
nb_images_found++;
if ( img_href.empty() ) {
img_href = n->getAttributeValue(attr_src);
lString32 img_alt = n->getAttributeValue(attr_alt);
if ( img_alt == U"cover" ) {
img_has_alt_attribute_equal_cover = true;
}
}
else {
// Too many images for a single cover: give up
has_more_images = true;
break;
}
}
else if (id == el_image ) { // <svg>...<image href=''>
img_href = n->getAttributeValue(attr_href);
nb_images_found++;
}
if ( nb_images_found > 1 ) {
// Too many images for a single cover: give up
break;
if ( img_href.empty() ) {
img_href = n->getAttributeValue(attr_href);
}
else {
has_more_images = true;
break;
}
}
}
else if ( in_body && n->isText() ) {
Expand Down Expand Up @@ -426,11 +438,20 @@ bool ExtractCoverFilenameFromCoverPageFragment( LVStreamRef stream, lString32 &
}
}
delete coverDoc;
// printf("EPUB coverpage check: has_text:%d, nb_images:%d, img_href:%d (%s)\n",
// has_text, nb_images_found, !img_href.empty(), LCSTR(img_href));
if ( !has_text && nb_images_found == 1 && !img_href.empty() ) {
cover_image_href = img_href;
return true;
// printf("EPUB coverpage check: has_text:%d, has_more_images:%d, has_alt=cover:%d img_href:%d (%s)\n",
// has_text, has_more_images, img_has_alt_attribute_equal_cover, !img_href.empty(), LCSTR(img_href));
if ( !img_href.empty() ) {
if ( !has_text && !has_more_images ) {
// Single image with no text in this fragment: it must be a cover image.
cover_image_href = img_href;
return true;
}
if ( img_has_alt_attribute_equal_cover ) {
// If we have met an image with alt="cover" before any text or other images,
// consider it valid.
cover_image_href = img_href;
return true;
}
}
return false;
}
Expand Down

0 comments on commit a6a71cb

Please sign in to comment.