Skip to content

Commit

Permalink
fix: added edge-case handling for images with zero available height
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed May 12, 2024
1 parent f092f10 commit 0d6bfb4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Example iOS-SwiftPM/Example_iOS-SPM.xcodeproj/project.pbxproj
Expand Up @@ -523,7 +523,7 @@
GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES;
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12;
IPHONEOS_DEPLOYMENT_TARGET = 16;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -548,7 +548,7 @@
GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES;
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12;
IPHONEOS_DEPLOYMENT_TARGET = 16;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
62 changes: 28 additions & 34 deletions Shared/Examples/ExperimentFactory.swift
Expand Up @@ -8,43 +8,37 @@

import Foundation
import TPPDF
import UIKit

class ExperimentFactory: ExampleFactory {
func generateDocument() -> [PDFDocument] {
let document = PDFDocument(format: .a4)

let items = (0..<40).map { $0.description }

// Simple bullet point list
let featureList = PDFList(indentations: [
(pre: 10.0, past: 20.0),
(pre: 20.0, past: 20.0),
(pre: 40.0, past: 20.0),
])

// By adding the item first to a list item with the dot symbol, all of them will inherit it
featureList
.addItem(PDFListItem(symbol: .dot)
.addItems(items.map { item in
PDFListItem(content: item)
}))
document.add(list: featureList)

document.add(space: 20)

// Numbered list with unusual indentation
let weirdIndentationList = PDFList(indentations: [
(pre: 10.0, past: 20.0),
(pre: 40.0, past: 30.0),
(pre: 20.0, past: 50.0),
])

weirdIndentationList
.addItems(items.enumerated().map { arg in
PDFListItem(symbol: .numbered(value: "\(arg.offset + 1)"), content: arg.element)
})
document.add(list: weirdIndentationList)

let document = PDFDocument(format: .b5)
document.add(.contentCenter, text: "Some Test Name")
let images = [
"file:///Users/Philip/Downloads/test_images/0000.jpg",
"file:///Users/Philip/Downloads/test_images/0001.jpg",
"file:///Users/Philip/Downloads/test_images/0002.jpg",
"file:///Users/Philip/Downloads/test_images/0003.jpg",
"file:///Users/Philip/Downloads/test_images/0004.jpg",
"file:///Users/Philip/Downloads/test_images/0005.jpg",
"file:///Users/Philip/Downloads/test_images/0006.jpg",
"file:///Users/Philip/Downloads/test_images/0007.jpg",
"file:///Users/Philip/Downloads/test_images/0008.jpg",
"file:///Users/Philip/Downloads/test_images/0009.jpg",
]
images[0..<images.count].compactMap({ imageFileUrl -> PDFImage? in
guard let imageURL = URL(string: imageFileUrl),
let image = UIImage(contentsOfFile: imageURL.path(percentEncoded: false)),
image.size != .zero
else {
return nil
}

let pdfImage = PDFImage(image: image)
return pdfImage
}).forEach({ pdfImage in
document.add(image: pdfImage)
})
return [document]
}
}
5 changes: 4 additions & 1 deletion Source/Internal/Image/PDFImageObject.swift
Expand Up @@ -56,7 +56,10 @@ class PDFImageObject: PDFRenderObject {
sizeFit: image.sizeFit)
let availableSize = PDFCalculations.calculateAvailableFrame(for: generator, in: container)
if container.isCenter {
if imageSize.height + captionSize.height > availableSize.height || (image.sizeFit == .height && imageSize.height < image.size.height) {
let isAvailableHeightZero = availableSize.height == 0
let isImageCaptionHeightCombinedTooSmall = imageSize.height + captionSize.height > availableSize.height
let isImageHeightTooSmall = image.sizeFit == .height && imageSize.height < image.size.height
if isAvailableHeightZero || isImageCaptionHeightCombinedTooSmall || isImageHeightTooSmall {
result += try PDFPageBreakObject().calculate(generator: generator, container: container)
generator.layout.heights.content = 0

Expand Down

0 comments on commit 0d6bfb4

Please sign in to comment.