KMHeaderFooterObject.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // KMHeaderFooterObject.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/27.
  6. //
  7. import Cocoa
  8. class KMHeaderFooterObject: NSObject {
  9. var fontName: String = "Helvetica"
  10. var fontsize: CGFloat = 12
  11. var color: NSColor = NSColor.black
  12. var leftMargin: Int = 30
  13. var rightMargin: Int = 30
  14. var bottomMargin: Int = 30
  15. var topMargin: Int = 30
  16. var topLeftString: String = ""
  17. var topCenterString: String = ""
  18. var topRightString: String = ""
  19. var bottomLeftString: String = ""
  20. var bottomCenterString: String = ""
  21. var bottomRightString: String = ""
  22. var dateFormatString: String = KMHeaderFooterManager.getdateFormatArray().first!
  23. var pageFormatString: String = KMHeaderFooterManager.getPageFormats().first!
  24. var startString: String = "1"
  25. var name: String = ""
  26. var tag: String = ""
  27. override init() {
  28. super.init()
  29. self.name = ""
  30. self.tag = NSString.tagString()
  31. }
  32. static func parseModel(model: KMHeaderFooterObject, pageCount: Int) -> [String] {
  33. var topLeftString: String = ""
  34. if (!model.topLeftString.isEmpty) {
  35. var string = KMHeaderFooterObject.parsePageFormat(formatString: model.topLeftString, startPage: model.startString, pageCount: "\(pageCount)")
  36. string = KMHeaderFooterObject.parseDateFormat(formatString: string)
  37. topLeftString = string
  38. }
  39. var topCenterString: String = ""
  40. if (!model.topCenterString.isEmpty) {
  41. var string = KMHeaderFooterObject.parsePageFormat(formatString: model.topCenterString, startPage: model.startString, pageCount: "\(pageCount)")
  42. string = KMHeaderFooterObject.parseDateFormat(formatString: string)
  43. topCenterString = string
  44. }
  45. var topRightString: String = ""
  46. if (!model.topRightString.isEmpty) {
  47. var string = KMHeaderFooterObject.parsePageFormat(formatString: model.topRightString, startPage: model.startString, pageCount: "\(pageCount)")
  48. string = KMHeaderFooterObject.parseDateFormat(formatString: string)
  49. topRightString = string
  50. }
  51. var bottomLeftString: String = ""
  52. if (!model.bottomLeftString.isEmpty) {
  53. var string = KMHeaderFooterObject.parsePageFormat(formatString: model.bottomLeftString, startPage: model.startString, pageCount: "\(pageCount)")
  54. string = KMHeaderFooterObject.parseDateFormat(formatString: string)
  55. bottomLeftString = string
  56. }
  57. var bottomCenterString: String = ""
  58. if (!model.bottomCenterString.isEmpty) {
  59. var string = KMHeaderFooterObject.parsePageFormat(formatString: model.bottomCenterString, startPage: model.startString, pageCount: "\(pageCount)")
  60. string = KMHeaderFooterObject.parseDateFormat(formatString: string)
  61. bottomCenterString = string
  62. }
  63. var bottomRightString: String = ""
  64. if (!model.bottomRightString.isEmpty) {
  65. var string = KMHeaderFooterObject.parsePageFormat(formatString: model.bottomRightString, startPage: model.startString, pageCount: "\(pageCount)")
  66. string = KMHeaderFooterObject.parseDateFormat(formatString: string)
  67. bottomRightString = string
  68. }
  69. return [topLeftString, topCenterString, topRightString, bottomLeftString, bottomCenterString, bottomRightString]
  70. }
  71. class func parsePageFormat(formatString: String, startPage: String, pageCount: String) -> String {
  72. var result = formatString
  73. for pageFormat in KMHeaderFooterManager.getPageFormats() {
  74. let string = "<<\(pageFormat)>>"
  75. if (result.contains(string)) {
  76. var tempString = ""
  77. if (string == "<<1>>") {
  78. tempString.append("<<\(startPage)>>")
  79. } else if (string == "<<1 of n>>") {
  80. tempString.append("<<\(startPage)>>")
  81. tempString.append(" of \(pageCount)")
  82. } else if (string == "<<1/n>>") {
  83. tempString.append("<<\(startPage)>>")
  84. tempString.append("/\(pageCount)")
  85. } else if (string == "<<Page 1>>") {
  86. tempString.append("Page \(startPage)")
  87. } else if (string == "<<Page 1 of n>>") {
  88. tempString.append("Page \(startPage)")
  89. tempString.append("of \(pageCount)")
  90. }
  91. result = result.replacingOccurrences(of: string, with: tempString)
  92. }
  93. }
  94. return result
  95. }
  96. class func parseDateFormat(formatString: String) -> String {
  97. var result: String = formatString
  98. for dateFormat in KMHeaderFooterManager.getdateFormatArray() {
  99. if (result.contains(dateFormat)) {
  100. let formatString: String = dateFormat.replacingOccurrences(of: "m", with: "M")
  101. let replace = "<<\(dateFormat)>>"
  102. let date = Date()
  103. let dateFormatter = DateFormatter()
  104. dateFormatter.dateFormat = formatString
  105. let dateString = dateFormatter.string(from: date)
  106. result = result.replacingOccurrences(of: replace, with: dateString)
  107. }
  108. }
  109. return result
  110. }
  111. }