From 1e0e21af75185308eebe70eb0a9c1cd935595f16 Mon Sep 17 00:00:00 2001 From: Chris Gonzales Date: Sat, 10 Oct 2020 16:58:46 +1300 Subject: [PATCH 1/6] add PDFImageDelegate --- Source/API/Image/PDFImage.swift | 13 +++++++++++ Source/API/PDFGenerator+Generation.swift | 3 +++ Source/API/PDFGenerator.swift | 2 ++ Source/API/PDFGeneratorImageDelegate.swift | 14 ++++++++++++ Source/API/Text/PDFAttributedText.swift | 6 ++++- Source/API/Text/PDFSimpleText.swift | 5 +++++ Source/API/Text/PDFText.swift | 26 +++++++++++++++++++++- 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 Source/API/PDFGeneratorImageDelegate.swift diff --git a/Source/API/Image/PDFImage.swift b/Source/API/Image/PDFImage.swift index 05bce8c8..49c9d62d 100644 --- a/Source/API/Image/PDFImage.swift +++ b/Source/API/Image/PDFImage.swift @@ -141,3 +141,16 @@ public class PDFImage: PDFDocumentObject { hasher.combine(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 da210b42..ddc8e92c 100644 --- a/Source/API/PDFGenerator+Generation.swift +++ b/Source/API/PDFGenerator+Generation.swift @@ -355,6 +355,9 @@ extension PDFGenerator { */ internal func render(object: PDFRenderObject, in container: PDFContainer, in context: PDFContext) 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.swift b/Source/API/Text/PDFAttributedText.swift index fede4dff..646c30f8 100644 --- a/Source/API/Text/PDFAttributedText.swift +++ b/Source/API/Text/PDFAttributedText.swift @@ -60,5 +60,9 @@ public class PDFAttributedText: PDFText { return true } - // MARK: - Hasha + // MARK: - Hashable + + public override func hash(into hasher: inout Hasher) { + hasher.combine(text) + } } diff --git a/Source/API/Text/PDFSimpleText.swift b/Source/API/Text/PDFSimpleText.swift index 2da2a9a4..6db54f1a 100644 --- a/Source/API/Text/PDFSimpleText.swift +++ b/Source/API/Text/PDFSimpleText.swift @@ -80,4 +80,9 @@ public class PDFSimpleText: PDFText { } // MARK: - Hashable + + override public func hash(into hasher: inout Hasher) { + hasher.combine(text) + hasher.combine(spacing) + } } diff --git a/Source/API/Text/PDFText.swift b/Source/API/Text/PDFText.swift index 7c0f0cea..be79618b 100644 --- a/Source/API/Text/PDFText.swift +++ b/Source/API/Text/PDFText.swift @@ -6,9 +6,33 @@ // /// Protocol all text objects should implement -public class PDFText: PDFDocumentObject, CustomStringConvertible { +public class PDFText: PDFDocumentObject, CustomStringConvertible, Hashable { var copy: PDFText { fatalError() } + + /** + TODO: Documentation + */ + public static func != (lhs: PDFText, rhs: PDFText) -> Bool { + !(lhs == rhs) + } + + /** + TODO: Documentation + */ + public static func == (lhs: PDFText, rhs: PDFText) -> Bool { + if let attributedLhs = lhs as? PDFAttributedText, let attributedRhs = rhs as? PDFAttributedText { + return attributedLhs == attributedRhs + } else if let simpleLhs = lhs as? PDFSimpleText, let simpleRhs = rhs as? PDFSimpleText { + return simpleLhs == simpleRhs + } + return false + } + + public func hash(into hasher: inout Hasher) { + fatalError() + } } + From b2725839f22177e092286d876cbe4e3925d917c5 Mon Sep 17 00:00:00 2001 From: Chris Gonzales Date: Mon, 12 Oct 2020 20:14:21 +1300 Subject: [PATCH 2/6] 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? /** From 73aef5f5857e79ae41709e1399d74bcab0cf0d7b Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sat, 17 Dec 2022 12:40:35 +0100 Subject: [PATCH 3/6] refactor: renamed the delegate naming --- .../API/Delegation/PDFGeneratorDelegate.swift | 9 +++++++++ .../Delegation/PDFGeneratorImageDelegate.swift | 18 ++++++++++++++++++ Source/API/Image/PDFImage.swift | 13 ------------- Source/API/PDFGenerator+Generation.swift | 4 ++-- Source/API/PDFGenerator.swift | 6 ++---- Source/API/PDFGeneratorImageDelegate.swift | 14 -------------- Source/API/Text/PDFText.swift | 6 +----- 7 files changed, 32 insertions(+), 38 deletions(-) create mode 100644 Source/API/Delegation/PDFGeneratorDelegate.swift create mode 100644 Source/API/Delegation/PDFGeneratorImageDelegate.swift delete mode 100644 Source/API/PDFGeneratorImageDelegate.swift diff --git a/Source/API/Delegation/PDFGeneratorDelegate.swift b/Source/API/Delegation/PDFGeneratorDelegate.swift new file mode 100644 index 00000000..92e203c4 --- /dev/null +++ b/Source/API/Delegation/PDFGeneratorDelegate.swift @@ -0,0 +1,9 @@ +// +// PDFGeneratorImageDelegate.swift +// TPPDF +// +// Created by Chris Gonzales on 10/10/20. +// + +/// Delegation protocol combining all delegates supported by the PDFGenerator +public typealias PDFGeneratorDelegate = PDFGeneratorImageDelegate diff --git a/Source/API/Delegation/PDFGeneratorImageDelegate.swift b/Source/API/Delegation/PDFGeneratorImageDelegate.swift new file mode 100644 index 00000000..b5ab8aa1 --- /dev/null +++ b/Source/API/Delegation/PDFGeneratorImageDelegate.swift @@ -0,0 +1,18 @@ +// +// PDFGeneratorImageDelegate.swift +// TPPDF +// +// Created by Chris Gonzales on 10/10/20. +// + +import CoreGraphics + +public protocol PDFGeneratorImageDelegate: AnyObject { + func generator(willBeginDrawingImage image: PDFImage, with context: PDFContext, in frame: CGRect) +} + +public extension PDFGeneratorImageDelegate { + + func generator(willBeginDrawingImage image: PDFImage, with context: PDFContext, in frame: CGRect){} + +} diff --git a/Source/API/Image/PDFImage.swift b/Source/API/Image/PDFImage.swift index 49c9d62d..05bce8c8 100644 --- a/Source/API/Image/PDFImage.swift +++ b/Source/API/Image/PDFImage.swift @@ -141,16 +141,3 @@ public class PDFImage: PDFDocumentObject { hasher.combine(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 ddc8e92c..63c5195e 100644 --- a/Source/API/PDFGenerator+Generation.swift +++ b/Source/API/PDFGenerator+Generation.swift @@ -354,10 +354,10 @@ extension PDFGenerator { - throws: PDFError, if rendering fails */ internal func render(object: PDFRenderObject, in container: PDFContainer, in context: PDFContext) 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) + delegate?.generator(willBeginDrawingImage: imageObject.image, with: context, in: object.frame) } + try object.draw(generator: self, container: container, in: context) } // MARK: - INTERNAL STATIC FUNCS diff --git a/Source/API/PDFGenerator.swift b/Source/API/PDFGenerator.swift index ce3e83b7..f726a3d9 100644 --- a/Source/API/PDFGenerator.swift +++ b/Source/API/PDFGenerator.swift @@ -63,10 +63,8 @@ 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? + /// Object acts as a delegate during the generation process + public var delegate: PDFGeneratorDelegate? /** Font of each container. diff --git a/Source/API/PDFGeneratorImageDelegate.swift b/Source/API/PDFGeneratorImageDelegate.swift deleted file mode 100644 index ee7543a8..00000000 --- a/Source/API/PDFGeneratorImageDelegate.swift +++ /dev/null @@ -1,14 +0,0 @@ -// -// 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/PDFText.swift b/Source/API/Text/PDFText.swift index be79618b..6a08e515 100644 --- a/Source/API/Text/PDFText.swift +++ b/Source/API/Text/PDFText.swift @@ -6,7 +6,7 @@ // /// Protocol all text objects should implement -public class PDFText: PDFDocumentObject, CustomStringConvertible, Hashable { +public class PDFText: PDFDocumentObject, CustomStringConvertible { var copy: PDFText { fatalError() @@ -30,9 +30,5 @@ public class PDFText: PDFDocumentObject, CustomStringConvertible, Hashable { } return false } - - public func hash(into hasher: inout Hasher) { - fatalError() - } } From 9a2415cc9350c1a74f7083670bc4e502e07f2a9e Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sat, 17 Dec 2022 12:42:04 +0100 Subject: [PATCH 4/6] fix: fixed unnecessary equality methods --- Source/API/Text/PDFText.swift | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/Source/API/Text/PDFText.swift b/Source/API/Text/PDFText.swift index 6a08e515..29dbc083 100644 --- a/Source/API/Text/PDFText.swift +++ b/Source/API/Text/PDFText.swift @@ -8,27 +8,8 @@ /// Protocol all text objects should implement public class PDFText: PDFDocumentObject, CustomStringConvertible { - var copy: PDFText { + public var copy: PDFText { fatalError() } - - /** - TODO: Documentation - */ - public static func != (lhs: PDFText, rhs: PDFText) -> Bool { - !(lhs == rhs) - } - - /** - TODO: Documentation - */ - public static func == (lhs: PDFText, rhs: PDFText) -> Bool { - if let attributedLhs = lhs as? PDFAttributedText, let attributedRhs = rhs as? PDFAttributedText { - return attributedLhs == attributedRhs - } else if let simpleLhs = lhs as? PDFSimpleText, let simpleRhs = rhs as? PDFSimpleText { - return simpleLhs == simpleRhs - } - return false - } } From 61e21700aae276083acbf6769a72ccbff260d6c5 Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sat, 17 Dec 2022 12:46:16 +0100 Subject: [PATCH 5/6] fix: carthage project update --- .github/workflows/documentation.yml | 2 -- TPPDF.xcodeproj/project.pbxproj | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index 2215fc10..d3aeb531 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -3,8 +3,6 @@ name: Documentation on: release: types: [published] - push: - branches: [main] jobs: deploy_docs: diff --git a/TPPDF.xcodeproj/project.pbxproj b/TPPDF.xcodeproj/project.pbxproj index 08498216..a140a4fe 100644 --- a/TPPDF.xcodeproj/project.pbxproj +++ b/TPPDF.xcodeproj/project.pbxproj @@ -32,6 +32,8 @@ D49B05E82658E76C0084A0B7 /* PDFDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49B05E62658E76C0084A0B7 /* PDFDocument.swift */; }; D49B05E92658E76C0084A0B7 /* PDFDocumentBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49B05E72658E76C0084A0B7 /* PDFDocumentBackground.swift */; }; D4B30533293CA38600EA24C5 /* PDFPageFormat+NameConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4B30532293CA38600EA24C5 /* PDFPageFormat+NameConstants.swift */; }; + D4E1FB2C294DE37800D654C7 /* PDFGeneratorImageDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4E1FB2A294DE37800D654C7 /* PDFGeneratorImageDelegate.swift */; }; + D4E1FB2D294DE37800D654C7 /* PDFGeneratorDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4E1FB2B294DE37800D654C7 /* PDFGeneratorDelegate.swift */; }; OBJ_415 /* CwlCatchException.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_356 /* CwlCatchException.swift */; }; OBJ_417 /* CwlCatchExceptionSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = "CwlCatchException::CwlCatchExceptionSupport::Product" /* CwlCatchExceptionSupport.framework */; }; OBJ_425 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_362 /* Package.swift */; }; @@ -635,6 +637,8 @@ D49B05E62658E76C0084A0B7 /* PDFDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFDocument.swift; sourceTree = ""; }; D49B05E72658E76C0084A0B7 /* PDFDocumentBackground.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFDocumentBackground.swift; sourceTree = ""; }; D4B30532293CA38600EA24C5 /* PDFPageFormat+NameConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "PDFPageFormat+NameConstants.swift"; sourceTree = ""; }; + D4E1FB2A294DE37800D654C7 /* PDFGeneratorImageDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFGeneratorImageDelegate.swift; sourceTree = ""; }; + D4E1FB2B294DE37800D654C7 /* PDFGeneratorDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFGeneratorDelegate.swift; sourceTree = ""; }; "Nimble::Nimble::Product" /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; }; OBJ_10 /* PDFExternalDocument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFExternalDocument.swift; sourceTree = ""; }; OBJ_101 /* PDFContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFContext.swift; sourceTree = ""; }; @@ -1111,6 +1115,15 @@ path = Document; sourceTree = ""; }; + D4E1FB29294DE37800D654C7 /* Delegation */ = { + isa = PBXGroup; + children = ( + D4E1FB2A294DE37800D654C7 /* PDFGeneratorImageDelegate.swift */, + D4E1FB2B294DE37800D654C7 /* PDFGeneratorDelegate.swift */, + ); + path = Delegation; + sourceTree = ""; + }; OBJ_100 /* Graphics */ = { isa = PBXGroup; children = ( @@ -1987,6 +2000,7 @@ OBJ_38 /* PDFGenerator+Generation.swift */, OBJ_40 /* PDFGeneratorProtocol.swift */, OBJ_41 /* PDFMultiDocumentGenerator.swift */, + D4E1FB29294DE37800D654C7 /* Delegation */, D49B05E52658E76C0084A0B7 /* Document */, OBJ_9 /* External */, OBJ_11 /* Graphics */, @@ -2651,6 +2665,7 @@ OBJ_659 /* CrossPlattformGraphics.swift in Sources */, OBJ_660 /* PDFConstants.swift in Sources */, OBJ_661 /* PDFCopy.swift in Sources */, + D4E1FB2D294DE37800D654C7 /* PDFGeneratorDelegate.swift in Sources */, OBJ_662 /* PDFDocumentObject.swift in Sources */, OBJ_663 /* PDFError.swift in Sources */, OBJ_664 /* PDFRenderObject.swift in Sources */, @@ -2686,6 +2701,7 @@ OBJ_694 /* PDFPageBreakObject.swift in Sources */, OBJ_695 /* PDFPageLayout+Equatable.swift in Sources */, OBJ_696 /* PDFSectionColumnContainer.swift in Sources */, + D4E1FB2C294DE37800D654C7 /* PDFGeneratorImageDelegate.swift in Sources */, OBJ_697 /* PDFSpaceObject.swift in Sources */, OBJ_698 /* PDFListObject.swift in Sources */, OBJ_699 /* CGPoint+Math.swift in Sources */, From 9786cbf7ff56ef2692d913640df2fa770158ab73 Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sun, 21 May 2023 11:45:53 +0200 Subject: [PATCH 6/6] WIP --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 33f2180b..9c2d3b02 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -30,7 +30,7 @@ jobs: xcodebuild -workspace . \ -scheme "TPPDF" \ -sdk iphonesimulator \ - -destination "OS=16.0,name=iPhone 14 Pro" \ + -destination "OS=16.2,name=iPhone 14 Pro" \ -configuration Debug \ -enableCodeCoverage YES \ -derivedDataPath /tmp/DerivedData \