Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added edge-case handling for images with zero available height #377

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Example iOS-SwiftPM/Example_iOS-SPM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading