Skip to content

Commit 5075703

Browse files
authored
fix: added edge-case handling for images with zero available height (#377)
1 parent 0230f73 commit 5075703

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

Example iOS-SwiftPM/Example_iOS-SPM.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@
523523
GCC_WARN_UNUSED_LABEL = YES;
524524
GCC_WARN_UNUSED_PARAMETER = YES;
525525
INFOPLIST_FILE = Example/Info.plist;
526-
IPHONEOS_DEPLOYMENT_TARGET = 12;
526+
IPHONEOS_DEPLOYMENT_TARGET = 16;
527527
LD_RUNPATH_SEARCH_PATHS = (
528528
"$(inherited)",
529529
"@executable_path/Frameworks",
@@ -548,7 +548,7 @@
548548
GCC_WARN_UNUSED_LABEL = YES;
549549
GCC_WARN_UNUSED_PARAMETER = YES;
550550
INFOPLIST_FILE = Example/Info.plist;
551-
IPHONEOS_DEPLOYMENT_TARGET = 12;
551+
IPHONEOS_DEPLOYMENT_TARGET = 16;
552552
LD_RUNPATH_SEARCH_PATHS = (
553553
"$(inherited)",
554554
"@executable_path/Frameworks",

Shared/Examples/ExperimentFactory.swift

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,37 @@
88

99
import Foundation
1010
import TPPDF
11+
import UIKit
1112

1213
class ExperimentFactory: ExampleFactory {
1314
func generateDocument() -> [PDFDocument] {
14-
let document = PDFDocument(format: .a4)
15-
16-
let items = (0..<40).map { $0.description }
17-
18-
// Simple bullet point list
19-
let featureList = PDFList(indentations: [
20-
(pre: 10.0, past: 20.0),
21-
(pre: 20.0, past: 20.0),
22-
(pre: 40.0, past: 20.0),
23-
])
24-
25-
// By adding the item first to a list item with the dot symbol, all of them will inherit it
26-
featureList
27-
.addItem(PDFListItem(symbol: .dot)
28-
.addItems(items.map { item in
29-
PDFListItem(content: item)
30-
}))
31-
document.add(list: featureList)
32-
33-
document.add(space: 20)
34-
35-
// Numbered list with unusual indentation
36-
let weirdIndentationList = PDFList(indentations: [
37-
(pre: 10.0, past: 20.0),
38-
(pre: 40.0, past: 30.0),
39-
(pre: 20.0, past: 50.0),
40-
])
41-
42-
weirdIndentationList
43-
.addItems(items.enumerated().map { arg in
44-
PDFListItem(symbol: .numbered(value: "\(arg.offset + 1)"), content: arg.element)
45-
})
46-
document.add(list: weirdIndentationList)
47-
15+
let document = PDFDocument(format: .b5)
16+
document.add(.contentCenter, text: "Some Test Name")
17+
let images = [
18+
"file:///Users/Philip/Downloads/test_images/0000.jpg",
19+
"file:///Users/Philip/Downloads/test_images/0001.jpg",
20+
"file:///Users/Philip/Downloads/test_images/0002.jpg",
21+
"file:///Users/Philip/Downloads/test_images/0003.jpg",
22+
"file:///Users/Philip/Downloads/test_images/0004.jpg",
23+
"file:///Users/Philip/Downloads/test_images/0005.jpg",
24+
"file:///Users/Philip/Downloads/test_images/0006.jpg",
25+
"file:///Users/Philip/Downloads/test_images/0007.jpg",
26+
"file:///Users/Philip/Downloads/test_images/0008.jpg",
27+
"file:///Users/Philip/Downloads/test_images/0009.jpg",
28+
]
29+
images[0..<images.count].compactMap({ imageFileUrl -> PDFImage? in
30+
guard let imageURL = URL(string: imageFileUrl),
31+
let image = UIImage(contentsOfFile: imageURL.path(percentEncoded: false)),
32+
image.size != .zero
33+
else {
34+
return nil
35+
}
36+
37+
let pdfImage = PDFImage(image: image)
38+
return pdfImage
39+
}).forEach({ pdfImage in
40+
document.add(image: pdfImage)
41+
})
4842
return [document]
4943
}
5044
}

Source/Internal/Image/PDFImageObject.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class PDFImageObject: PDFRenderObject {
5656
sizeFit: image.sizeFit)
5757
let availableSize = PDFCalculations.calculateAvailableFrame(for: generator, in: container)
5858
if container.isCenter {
59-
if imageSize.height + captionSize.height > availableSize.height || (image.sizeFit == .height && imageSize.height < image.size.height) {
59+
let isAvailableHeightZero = availableSize.height == 0
60+
let isImageCaptionHeightCombinedTooSmall = imageSize.height + captionSize.height > availableSize.height
61+
let isImageHeightTooSmall = image.sizeFit == .height && imageSize.height < image.size.height
62+
if isAvailableHeightZero || isImageCaptionHeightCombinedTooSmall || isImageHeightTooSmall {
6063
result += try PDFPageBreakObject().calculate(generator: generator, container: container)
6164
generator.layout.heights.content = 0
6265

0 commit comments

Comments
 (0)