From 096c56297846db77846a33e8d2cb29dc6ab60281 Mon Sep 17 00:00:00 2001 From: Chris Gonzales Date: Sat, 10 Oct 2020 16:58:46 +1300 Subject: [PATCH 1/2] add PDFImageDelegate --- Source/API/Image/PDFImage.swift | 13 ++++++++++ Source/API/PDFGenerator+Generation.swift | 3 +++ Source/API/PDFGenerator.swift | 2 ++ Source/API/PDFGeneratorImageDelegate.swift | 14 +++++++++++ .../Text/PDFAttributedText+Equatable.swift | 22 ---------------- Source/API/Text/PDFAttributedText.swift | 14 +++++++++++ Source/API/Text/PDFSimpleText+Equatable.swift | 25 ------------------- Source/API/Text/PDFSimpleText.swift | 15 +++++++++++ Source/API/Text/PDFText.swift | 7 +++++- 9 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 Source/API/PDFGeneratorImageDelegate.swift delete mode 100644 Source/API/Text/PDFAttributedText+Equatable.swift delete mode 100644 Source/API/Text/PDFSimpleText+Equatable.swift diff --git a/Source/API/Image/PDFImage.swift b/Source/API/Image/PDFImage.swift index 78d3c51f..45d435ee 100644 --- a/Source/API/Image/PDFImage.swift +++ b/Source/API/Image/PDFImage.swift @@ -94,3 +94,16 @@ public class PDFImage: PDFDocumentObject { cornerRadius: self.cornerRadius) } } + + +extension PDFImage: Hashable { + public func hash(into hasher: inout Hasher) { + hasher.combine(image) + hasher.combine(caption) + hasher.combine(NSCoder.string(for: size)) + hasher.combine(sizeFit) + hasher.combine(quality) + hasher.combine(options.rawValue) + hasher.combine(cornerRadius) + } +} diff --git a/Source/API/PDFGenerator+Generation.swift b/Source/API/PDFGenerator+Generation.swift index 5ccc975a..0b9161bc 100644 --- a/Source/API/PDFGenerator+Generation.swift +++ b/Source/API/PDFGenerator+Generation.swift @@ -341,6 +341,9 @@ extension PDFGenerator { */ internal func render(object: PDFRenderObject, in container: PDFContainer, in context: CGContext) throws { try object.draw(generator: self, container: container, in: context) + if let imageObject = object as? PDFImageObject { + imageDelegate?.generator(willBeginDrawingImage: imageObject.image, with: context, in: object.frame) + } } // MARK: - INTERNAL STATIC FUNCS diff --git a/Source/API/PDFGenerator.swift b/Source/API/PDFGenerator.swift index cd032f56..c5d5f575 100644 --- a/Source/API/PDFGenerator.swift +++ b/Source/API/PDFGenerator.swift @@ -62,6 +62,8 @@ public class PDFGenerator: PDFGeneratorProtocol, CustomStringConvertible { Relative value tracking progress */ public let progress = Progress.discreteProgress(totalUnitCount: 3) + + public var imageDelegate: PDFGeneratorImageDelegate? /** Font of each container. diff --git a/Source/API/PDFGeneratorImageDelegate.swift b/Source/API/PDFGeneratorImageDelegate.swift new file mode 100644 index 00000000..ee7543a8 --- /dev/null +++ b/Source/API/PDFGeneratorImageDelegate.swift @@ -0,0 +1,14 @@ +// +// PDFGeneratorImageDelegate.swift +// TPPDF +// +// Created by Chris Gonzales on 10/10/20. +// + +import Foundation + +public protocol PDFGeneratorImageDelegate: class { + func generator(willBeginDrawingImage image: PDFImage, with context: CGContext, in frame: CGRect) +} + +typealias PDFGeneratorDelegate = PDFGeneratorImageDelegate // for extensibility diff --git a/Source/API/Text/PDFAttributedText+Equatable.swift b/Source/API/Text/PDFAttributedText+Equatable.swift deleted file mode 100644 index 85120548..00000000 --- a/Source/API/Text/PDFAttributedText+Equatable.swift +++ /dev/null @@ -1,22 +0,0 @@ -// -// PDFAttributedText+Equatable.swift -// TPPDF -// -// Created by Philip Niedertscheider on 09/11/2017. -// - -/** - TODO: Documentation - */ -extension PDFAttributedText: Equatable { - - /** - TODO: Documentation - */ - public static func == (lhs: PDFAttributedText, rhs: PDFAttributedText) -> Bool { - guard lhs.text == rhs.text else { - return false - } - return true - } -} diff --git a/Source/API/Text/PDFAttributedText.swift b/Source/API/Text/PDFAttributedText.swift index 2930a64a..65fc76c3 100644 --- a/Source/API/Text/PDFAttributedText.swift +++ b/Source/API/Text/PDFAttributedText.swift @@ -38,4 +38,18 @@ public class PDFAttributedText: PDFText { override internal var copy: PDFText { PDFAttributedText(text: text) } + + public override func hash(into hasher: inout Hasher) { + hasher.combine(text) + } + + /** + TODO: Documentation + */ + public static func == (lhs: PDFAttributedText, rhs: PDFAttributedText) -> Bool { + guard lhs.text == rhs.text else { + return false + } + return true + } } diff --git a/Source/API/Text/PDFSimpleText+Equatable.swift b/Source/API/Text/PDFSimpleText+Equatable.swift deleted file mode 100644 index 161acf11..00000000 --- a/Source/API/Text/PDFSimpleText+Equatable.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// PDFSimpleText+Equatable.swift -// TPPDF -// -// Created by Philip Niedertscheider on 09/11/2017. -// - -/** - TODO: Documentation - */ -extension PDFSimpleText: Equatable { - - /** - TODO: Documentation - */ - public static func == (lhs: PDFSimpleText, rhs: PDFSimpleText) -> Bool { - guard lhs.text == rhs.text else { - return false - } - guard lhs.spacing == rhs.spacing else { - return false - } - return true - } -} diff --git a/Source/API/Text/PDFSimpleText.swift b/Source/API/Text/PDFSimpleText.swift index bb812803..2d1972c6 100644 --- a/Source/API/Text/PDFSimpleText.swift +++ b/Source/API/Text/PDFSimpleText.swift @@ -52,4 +52,19 @@ public class PDFSimpleText: PDFText { override internal var copy: PDFText { PDFSimpleText(text: self.text, spacing: self.spacing, style: self.style) } + + override public func hash(into hasher: inout Hasher) { + hasher.combine(text) + hasher.combine(spacing) + } + + public static func == (lhs: PDFSimpleText, rhs: PDFSimpleText) -> Bool { + guard lhs.text == rhs.text else { + return false + } + guard lhs.spacing == rhs.spacing else { + return false + } + return true + } } diff --git a/Source/API/Text/PDFText.swift b/Source/API/Text/PDFText.swift index 319a5a26..e53310a2 100644 --- a/Source/API/Text/PDFText.swift +++ b/Source/API/Text/PDFText.swift @@ -8,7 +8,7 @@ /** Protocol all text objects should implement */ -public class PDFText: PDFDocumentObject, CustomStringConvertible { +public class PDFText: PDFDocumentObject, CustomStringConvertible, Hashable { /** TODO: Documentation @@ -35,4 +35,9 @@ public class PDFText: PDFDocumentObject, CustomStringConvertible { } return false } + + public func hash(into hasher: inout Hasher) { + fatalError() + } } + From 713a3c7d0b7a9681eb72a35f19fecc04e0c2b089 Mon Sep 17 00:00:00 2001 From: Chris Gonzales Date: Mon, 12 Oct 2020 20:14:21 +1300 Subject: [PATCH 2/2] add documentation for -PDFGenerator.imageDelegate --- Source/API/PDFGenerator.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/API/PDFGenerator.swift b/Source/API/PDFGenerator.swift index c5d5f575..ce3e83b7 100644 --- a/Source/API/PDFGenerator.swift +++ b/Source/API/PDFGenerator.swift @@ -63,6 +63,9 @@ public class PDFGenerator: PDFGeneratorProtocol, CustomStringConvertible { */ public let progress = Progress.discreteProgress(totalUnitCount: 3) + /** + Delegate for image processing. Called after resizing and scaling and before drawing. + */ public var imageDelegate: PDFGeneratorImageDelegate? /**