From 0230f73831c493b629e0d55fc598639198c70b7b Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sun, 12 May 2024 17:50:05 +0200 Subject: [PATCH] refactor: moved public pagination structures to directory API (#378) --- .../Pagination/PDFPagination+Equatable.swift | 0 Source/API/Pagination/PDFPagination.swift | 67 +++++++++++++++++++ .../API/Pagination/PDFPaginationClosure.swift | 16 +++++ .../PDFPaginationStyle+Equatable.swift | 4 +- .../Pagination/PDFPaginationStyle.swift | 48 ++++++------- .../Internal/Pagination/PDFPagination.swift | 63 ----------------- .../Pagination/PDFPaginationClosure.swift | 16 ----- .../Int+RomanNumerals.swift | 0 TPPDF.xcodeproj/project.pbxproj | 18 ++--- 9 files changed, 111 insertions(+), 121 deletions(-) rename Source/{Internal => API}/Pagination/PDFPagination+Equatable.swift (100%) create mode 100644 Source/API/Pagination/PDFPagination.swift create mode 100644 Source/API/Pagination/PDFPaginationClosure.swift rename Source/{Internal => API}/Pagination/PDFPaginationStyle+Equatable.swift (81%) rename Source/{Internal => API}/Pagination/PDFPaginationStyle.swift (51%) delete mode 100644 Source/Internal/Pagination/PDFPagination.swift delete mode 100644 Source/Internal/Pagination/PDFPaginationClosure.swift rename Source/Internal/{Pagination => Utils}/Int+RomanNumerals.swift (100%) diff --git a/Source/Internal/Pagination/PDFPagination+Equatable.swift b/Source/API/Pagination/PDFPagination+Equatable.swift similarity index 100% rename from Source/Internal/Pagination/PDFPagination+Equatable.swift rename to Source/API/Pagination/PDFPagination+Equatable.swift diff --git a/Source/API/Pagination/PDFPagination.swift b/Source/API/Pagination/PDFPagination.swift new file mode 100644 index 00000000..ce98328d --- /dev/null +++ b/Source/API/Pagination/PDFPagination.swift @@ -0,0 +1,67 @@ +// +// PDFPagination.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11/08/2017. +// + +#if os(iOS) + import UIKit +#elseif os(macOS) + import AppKit +#endif + +/** + * Structure used to configure the pagination + */ +public struct PDFPagination { + /// Container where the pagination will be placed + public var container: PDFContainer + + /// Style of the pagination + public var style: PDFPaginationStyle + + /** + * Range of pages which will be paginated + * + * This range starts at zero + */ + public var range: (start: Int, end: Int) + + /** + * Add a page number to this list to exclude it from the pagination + * + * This will not skip the page but instead not render the pagination object + */ + public var hiddenPages: [Int] + + /** + * Text attribtues are used to create the attributed pagination string + * + * See ``NSAttributedString`` for reference + */ + public var textAttributes: [NSAttributedString.Key: Any] + + /** + * Creates a new pagination configuration + * + * - Parameters; + * - container: Container where pagination is placed, defaults to `PDFContainer.none`, meaning it won't be rendered + * - style: Style of pagination + * - range: Start and end of pages which will be included + * - hiddenPages: List of numbers which are excluded from rendering + */ + public init( + container: PDFContainer = .none, + style: PDFPaginationStyle = .default, + range: (start: Int, end: Int) = (0, Int.max), + hiddenPages: [Int] = [], + textAttributes: [NSAttributedString.Key: Any] = [:] + ) { + self.container = container + self.style = style + self.range = range + self.hiddenPages = hiddenPages + self.textAttributes = textAttributes + } +} diff --git a/Source/API/Pagination/PDFPaginationClosure.swift b/Source/API/Pagination/PDFPaginationClosure.swift new file mode 100644 index 00000000..22b99c82 --- /dev/null +++ b/Source/API/Pagination/PDFPaginationClosure.swift @@ -0,0 +1,16 @@ +// +// PDFPaginationClosure.swift +// TPPDF +// +// Created by Philip Niedertscheider on 04/11/2017. +// + +/** + * Closure for custom pagination formatting. + * + * - Parameter page: `Int` - Current page number + * - Parameter total: `Int` - Total amount of pages + * + * - Returns: Formatted pagination string + */ +public typealias PDFPaginationClosure = (_ page: Int, _ total: Int) -> String diff --git a/Source/Internal/Pagination/PDFPaginationStyle+Equatable.swift b/Source/API/Pagination/PDFPaginationStyle+Equatable.swift similarity index 81% rename from Source/Internal/Pagination/PDFPaginationStyle+Equatable.swift rename to Source/API/Pagination/PDFPaginationStyle+Equatable.swift index f9c05fcf..04761c00 100644 --- a/Source/Internal/Pagination/PDFPaginationStyle+Equatable.swift +++ b/Source/API/Pagination/PDFPaginationStyle+Equatable.swift @@ -22,8 +22,8 @@ extension PDFPaginationStyle: Equatable { } if case PDFPaginationStyle.customClosure = lhs, case PDFPaginationStyle.customClosure = rhs { - // Always return false if a custom closure is used - // https://stackoverflow.com/questions/24111984/how-do-you-test-functions-and-closures-for-equality + // Always return false if a custom closure is used, because closures can not be equated + // Reference: https://stackoverflow.com/questions/24111984/how-do-you-test-functions-and-closures-for-equality return false } diff --git a/Source/Internal/Pagination/PDFPaginationStyle.swift b/Source/API/Pagination/PDFPaginationStyle.swift similarity index 51% rename from Source/Internal/Pagination/PDFPaginationStyle.swift rename to Source/API/Pagination/PDFPaginationStyle.swift index 339dca0d..4605ff09 100644 --- a/Source/Internal/Pagination/PDFPaginationStyle.swift +++ b/Source/API/Pagination/PDFPaginationStyle.swift @@ -5,52 +5,46 @@ // Created by Philip Niedertscheider on 13/06/2017. // -#if os(iOS) - import UIKit -#elseif os(macOS) - import AppKit -#endif +import Foundation /** - Use predefined pagination styles or create a custom one, using `.CustomNumberFormat` or `.CustomClosure`. - - Enums using a template String as parameter will replace the first instance of `%@` with the index and the second one with the total amount of pages. + * Use predefined pagination styles or create a custom one, using `.CustomNumberFormat` or `.CustomClosure`. + * + * Enums using a template String as parameter will replace the first instance of `%@` with the index and the second one with the total amount of pages. */ public enum PDFPaginationStyle { /** - Default format, concats current page and total pages with a dash. - - e.g. Converts page 1 of 3 to **"1 - 3"** + * Default format, concats current page and total pages with a dash. + * + * e.g. Converts page 1 of 3 to **"1 - 3"** */ case `default` /** - Returns pagination in roman numerals. - - - Parameter template: Template `String`, instances of `%@` will be replaced. + * Returns pagination in roman numerals. + * + * - Parameter template: Template `String`, instances of `%@` will be replaced. */ case roman(template: String) /** - Formats pagination numbers using the `formatter` and formatting the string using the given `template`. - - - Parameter template: Template string where `$@` is replaced - - Parameter formatter: Number formatter + * Formats pagination numbers using the `formatter` and formatting the string using the given `template`. + * + * - Parameter template: Template string where `$@` is replaced + * - Parameter formatter: Number formatter */ case customNumberFormat(template: String, formatter: NumberFormatter) - /** - Formats the pagination using the provided closure - */ + /// Formats the pagination using the provided closure case customClosure(PDFPaginationClosure) /** - Creates formatted pagination string. - - - Parameter page: `Int` - Current page - - Parameter total: `Int` - Total amount of pages. - - - Returns: Formatted `String` + * Creates formatted pagination string. + * + * - Parameter page: Current page + * - Parameter total: Total amount of pages. + * + * - Returns: Formatted `String` */ public func format(page: Int, total: Int) -> String { switch self { diff --git a/Source/Internal/Pagination/PDFPagination.swift b/Source/Internal/Pagination/PDFPagination.swift deleted file mode 100644 index 6a88597e..00000000 --- a/Source/Internal/Pagination/PDFPagination.swift +++ /dev/null @@ -1,63 +0,0 @@ -// -// PDFPagination.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11/08/2017. -// - -#if os(iOS) - import UIKit -#elseif os(macOS) - import AppKit -#endif - -/** - Used to define the pagination behaviour of a document. - */ -public struct PDFPagination { - /** - Container where the pagination will be placed - */ - public var container: PDFContainer - - /** - Style of the pagination - */ - public var style: PDFPaginationStyle - - /** - Range of pages which will be paginated - */ - public var range: (start: Int, end: Int) - - /** - Add a page number to this list to exclude it from the pagination. - This will not skip the page but instead not render the pagination object - */ - public var hiddenPages: [Int] - - /** - These text attribtues are used to create the attributed pagination string - */ - public var textAttributes: [NSAttributedString.Key: Any] - - /** - Initializer - - - Parameter container: Container where pagination is placed, defaults to `PDFContainer.none`, meaning it won't be rendered - - Parameter style: Style of pagination, defaults to `PDFPaginationStyle.default` - - Parameter range: Start and end of pages which will be included - - Parameter hiddenPages: List of numbers which are excluded from rendering - */ - public init(container: PDFContainer = .none, - style: PDFPaginationStyle = .default, - range: (start: Int, end: Int) = (0, Int.max), - hiddenPages: [Int] = [], - textAttributes: [NSAttributedString.Key: Any] = [:]) { - self.container = container - self.style = style - self.range = range - self.hiddenPages = hiddenPages - self.textAttributes = textAttributes - } -} diff --git a/Source/Internal/Pagination/PDFPaginationClosure.swift b/Source/Internal/Pagination/PDFPaginationClosure.swift deleted file mode 100644 index c675de5f..00000000 --- a/Source/Internal/Pagination/PDFPaginationClosure.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// PDFPaginationClosure.swift -// TPPDF -// -// Created by Philip Niedertscheider on 04/11/2017. -// - -/** - Closure for custom pagination formatting. - - - Parameter page: `Int` - Current page number - - Parameter total: `Int` - Total amount of pages - - - Returns: Formatted pagination string - */ -public typealias PDFPaginationClosure = (_ page: Int, _ total: Int) -> String diff --git a/Source/Internal/Pagination/Int+RomanNumerals.swift b/Source/Internal/Utils/Int+RomanNumerals.swift similarity index 100% rename from Source/Internal/Pagination/Int+RomanNumerals.swift rename to Source/Internal/Utils/Int+RomanNumerals.swift diff --git a/TPPDF.xcodeproj/project.pbxproj b/TPPDF.xcodeproj/project.pbxproj index a140a4fe..732257d0 100644 --- a/TPPDF.xcodeproj/project.pbxproj +++ b/TPPDF.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ 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 */; }; + D4E49EED2BF11C0300BC7B0A /* Int+RomanNumerals.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4E49EEC2BF11C0200BC7B0A /* Int+RomanNumerals.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 */; }; @@ -215,8 +216,6 @@ OBJ_673 /* PDFLineSeparatorObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_106 /* PDFLineSeparatorObject.swift */; }; OBJ_674 /* PDFRectangleObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_107 /* PDFRectangleObject.swift */; }; OBJ_675 /* UIColor+CloseToEqual.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_108 /* UIColor+CloseToEqual.swift */; }; - OBJ_676 /* UIColor+Hex.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_109 /* UIColor+Hex.swift */; }; - OBJ_677 /* UIImage+Pixel.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_110 /* UIImage+Pixel.swift */; }; OBJ_678 /* PDFGroupObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_112 /* PDFGroupObject.swift */; }; OBJ_679 /* PDFImageObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_114 /* PDFImageObject.swift */; }; OBJ_680 /* PDFImageRowObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_115 /* PDFImageRowObject.swift */; }; @@ -242,7 +241,6 @@ OBJ_700 /* CGPoint+Null.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_138 /* CGPoint+Null.swift */; }; OBJ_701 /* PDFGenerator+Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_139 /* PDFGenerator+Debug.swift */; }; OBJ_702 /* PDFGenerator+Layout.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_140 /* PDFGenerator+Layout.swift */; }; - OBJ_703 /* Int+RomanNumerals.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_142 /* Int+RomanNumerals.swift */; }; OBJ_704 /* PDFPagination+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_143 /* PDFPagination+Equatable.swift */; }; OBJ_705 /* PDFPagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_144 /* PDFPagination.swift */; }; OBJ_706 /* PDFPaginationClosure.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_145 /* PDFPaginationClosure.swift */; }; @@ -639,6 +637,7 @@ 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 = ""; }; + D4E49EEC2BF11C0200BC7B0A /* Int+RomanNumerals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Int+RomanNumerals.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 = ""; }; @@ -649,8 +648,6 @@ OBJ_106 /* PDFLineSeparatorObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFLineSeparatorObject.swift; sourceTree = ""; }; OBJ_107 /* PDFRectangleObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFRectangleObject.swift; sourceTree = ""; }; OBJ_108 /* UIColor+CloseToEqual.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+CloseToEqual.swift"; sourceTree = ""; }; - OBJ_109 /* UIColor+Hex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+Hex.swift"; sourceTree = ""; }; - OBJ_110 /* UIImage+Pixel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Pixel.swift"; sourceTree = ""; }; OBJ_112 /* PDFGroupObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFGroupObject.swift; sourceTree = ""; }; OBJ_114 /* PDFImageObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFImageObject.swift; sourceTree = ""; }; OBJ_115 /* PDFImageRowObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFImageRowObject.swift; sourceTree = ""; }; @@ -679,7 +676,6 @@ OBJ_139 /* PDFGenerator+Debug.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PDFGenerator+Debug.swift"; sourceTree = ""; }; OBJ_14 /* PDFBezierPathVertex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFBezierPathVertex.swift; sourceTree = ""; }; OBJ_140 /* PDFGenerator+Layout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PDFGenerator+Layout.swift"; sourceTree = ""; }; - OBJ_142 /* Int+RomanNumerals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+RomanNumerals.swift"; sourceTree = ""; }; OBJ_143 /* PDFPagination+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PDFPagination+Equatable.swift"; sourceTree = ""; }; OBJ_144 /* PDFPagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFPagination.swift; sourceTree = ""; }; OBJ_145 /* PDFPaginationClosure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFPaginationClosure.swift; sourceTree = ""; }; @@ -1135,8 +1131,6 @@ OBJ_106 /* PDFLineSeparatorObject.swift */, OBJ_107 /* PDFRectangleObject.swift */, OBJ_108 /* UIColor+CloseToEqual.swift */, - OBJ_109 /* UIColor+Hex.swift */, - OBJ_110 /* UIImage+Pixel.swift */, ); path = Graphics; sourceTree = ""; @@ -1215,7 +1209,6 @@ OBJ_141 /* Pagination */ = { isa = PBXGroup; children = ( - OBJ_142 /* Int+RomanNumerals.swift */, OBJ_143 /* PDFPagination+Equatable.swift */, OBJ_144 /* PDFPagination.swift */, OBJ_145 /* PDFPaginationClosure.swift */, @@ -1268,6 +1261,7 @@ OBJ_164 /* Utils */ = { isa = PBXGroup; children = ( + D4E49EEC2BF11C0200BC7B0A /* Int+RomanNumerals.swift */, OBJ_165 /* Attributes */, OBJ_168 /* CustomStringConvertible+AutoDescribing.swift */, OBJ_169 /* FileManager+TemporaryFiles.swift */, @@ -2014,6 +2008,7 @@ OBJ_51 /* Table */, OBJ_49 /* Table Of Content */, OBJ_82 /* Text */, + OBJ_141 /* Pagination */, OBJ_88 /* Utils */, ); path = API; @@ -2065,7 +2060,6 @@ OBJ_136 /* Math */, OBJ_139 /* PDFGenerator+Debug.swift */, OBJ_140 /* PDFGenerator+Layout.swift */, - OBJ_141 /* Pagination */, OBJ_148 /* Section */, OBJ_152 /* Table Of Content */, OBJ_154 /* Table */, @@ -2628,6 +2622,7 @@ D49B05E92658E76C0084A0B7 /* PDFDocumentBackground.swift in Sources */, OBJ_624 /* PDFSection.swift in Sources */, OBJ_625 /* PDFSectionColumn.swift in Sources */, + D4E49EED2BF11C0300BC7B0A /* Int+RomanNumerals.swift in Sources */, OBJ_626 /* PDFTableOfContent.swift in Sources */, OBJ_627 /* NSAttributedString+PDFTableContent.swift in Sources */, OBJ_628 /* Number+PDFTableContentable.swift in Sources */, @@ -2680,8 +2675,6 @@ OBJ_673 /* PDFLineSeparatorObject.swift in Sources */, OBJ_674 /* PDFRectangleObject.swift in Sources */, OBJ_675 /* UIColor+CloseToEqual.swift in Sources */, - OBJ_676 /* UIColor+Hex.swift in Sources */, - OBJ_677 /* UIImage+Pixel.swift in Sources */, OBJ_678 /* PDFGroupObject.swift in Sources */, OBJ_679 /* PDFImageObject.swift in Sources */, OBJ_680 /* PDFImageRowObject.swift in Sources */, @@ -2708,7 +2701,6 @@ OBJ_700 /* CGPoint+Null.swift in Sources */, OBJ_701 /* PDFGenerator+Debug.swift in Sources */, OBJ_702 /* PDFGenerator+Layout.swift in Sources */, - OBJ_703 /* Int+RomanNumerals.swift in Sources */, D49B05E82658E76C0084A0B7 /* PDFDocument.swift in Sources */, OBJ_704 /* PDFPagination+Equatable.swift in Sources */, OBJ_705 /* PDFPagination.swift in Sources */,