Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions src/BloomExe/ImageProcessing/ImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2173,20 +2173,10 @@ public static void ReallyCropImages(
)
{
var images = bookDom.SafeSelectElements("//img").ToList();
// We need to keep the cover image data in the bloomDataDiv consistent if anything
// (e.g., cropping) causes us to rename the cover image file. If we upload the
// book to bloomlibrary.org, the cover image file stored in the bloomDataDiv will
// be needed to harvest the book or to make derivatives of it. (BL-16096)
if (images.Any(x => x.GetAttribute("data-book") == "coverImage"))
{
var coverImageDiv = bookDom
.SafeSelectElements(
"//div[@id='bloomDataDiv']/div[@data-book='coverImage' and @src]"
)
.FirstOrDefault();
if (coverImageDiv != null)
images.Add(coverImageDiv);
}
var bloomDataDivEntriesByDataBook = bookDom
.SafeSelectElements("//div[@id='bloomDataDiv']/*[@data-book]")
.GroupBy(x => x.GetAttribute("data-book"))
.ToDictionary(group => group.Key, group => group.First());
// src values that occur in uncropped images. Note, it is NOT the case that an img whose src
// is in this set never cropped; it just means that at least one occurrence of it is not
// cropped, which makes the image name unavailable to use for a cropped image.
Expand Down Expand Up @@ -2236,7 +2226,8 @@ public static void ReallyCropImages(
cropped,
img,
src,
style
style,
bloomDataDivEntriesByDataBook
);
}
else
Expand All @@ -2251,7 +2242,8 @@ public static void ReallyCropImages(
img,
src,
style,
canvasElementStyle
canvasElementStyle,
bloomDataDivEntriesByDataBook
);
}
}
Expand All @@ -2264,17 +2256,15 @@ private static void TrimTheMetadata(
Dictionary<string, string> cropped,
SafeXmlElement img,
string src,
string style
string style,
Dictionary<string, SafeXmlElement> bloomDataDivEntriesByDataBook
)
{
var key = $"{src}|{style}|0|0";
if (cropped.TryGetValue(key, out string fileName))
{
// This is a duplicate. We can use the same metadata-cropped image file.
img.SetAttribute("src", fileName);
// Handle the bloomDataDiv definition for the cover image. (BL-16096)
if (img.Name.ToLowerInvariant() == "div")
img.InnerText = fileName;
SetImageSrcAndSyncDataDiv(img, fileName, bloomDataDivEntriesByDataBook);
return;
}
TrimMetadataInImage(img, imageSourceFolder, imageDestFolder);
Expand All @@ -2291,7 +2281,8 @@ private static void CropTheImageAndMetadata(
SafeXmlElement img,
string src,
string style,
string canvasElementStyle
string canvasElementStyle,
Dictionary<string, SafeXmlElement> bloomDataDivEntriesByDataBook
)
{
var canvasElementWidth = GetNumberFromPx("width", canvasElementStyle);
Expand All @@ -2301,7 +2292,7 @@ string canvasElementStyle
if (cropped.TryGetValue(key, out string fileName))
{
// This is a duplicate. We can use the same cropped image file.
img.SetAttribute("src", fileName);
SetImageSrcAndSyncDataDiv(img, fileName, bloomDataDivEntriesByDataBook);
// With that src, it's already cropped, so remove the style to avoid
// applying the cropping again to the already-cropped image.
img.RemoveAttribute("style");
Expand All @@ -2314,7 +2305,8 @@ string canvasElementStyle
img,
imageSourceFolder,
imageDestFolder,
needNewName
needNewName,
bloomDataDivEntriesByDataBook
);

// Track if we replaced an original file with a new one
Expand Down Expand Up @@ -2484,7 +2476,8 @@ private static string ReallyCropImage(
SafeXmlElement img,
string imageSourceFolder,
string imageDestFolder,
bool useNewName
bool useNewName,
Dictionary<string, SafeXmlElement> bloomDataDivEntriesByDataBook
)
{
var croppedImagePath = MakeCroppedImage(img, imageSourceFolder, imageDestFolder);
Expand Down Expand Up @@ -2512,7 +2505,7 @@ bool useNewName
// All images with this name and crop should use this
result = Path.GetFileName(croppedImagePath);
// Including the current image
img.SetAttribute("src", result);
SetImageSrcAndSyncDataDiv(img, result, bloomDataDivEntriesByDataBook);
}
else
{
Expand All @@ -2526,6 +2519,24 @@ bool useNewName
return result;
}

private static void SetImageSrcAndSyncDataDiv(
SafeXmlElement img,
string src,
Dictionary<string, SafeXmlElement> bloomDataDivEntriesByDataBook
)
{
img.SetAttribute("src", src);
var dataBook = img.GetAttribute("data-book");
if (string.IsNullOrWhiteSpace(dataBook))
return;

if (bloomDataDivEntriesByDataBook.TryGetValue(dataBook, out var dataDivElement))
{
dataDivElement.SetAttribute("src", src);
dataDivElement.InnerText = src;
}
}

/// <summary>
/// If the specified img is cropped, and we can find and successfully crop the
/// appropriate image file, make a new file containing the cropped image in imageDestFolder
Expand Down