// // KMHeaderFooterObject.swift // PDF Reader Pro // // Created by tangchao on 2022/12/27. // import Cocoa class KMHeaderFooterObject: NSObject { var fontName: String = "Helvetica" var fontsize: CGFloat = 12 var color: NSColor = NSColor.black var leftMargin: Int = 30 var rightMargin: Int = 30 var bottomMargin: Int = 30 var topMargin: Int = 30 var topLeftString: String = "" var topCenterString: String = "" var topRightString: String = "" var bottomLeftString: String = "" var bottomCenterString: String = "" var bottomRightString: String = "" var dateFormatString: String = KMHeaderFooterManager.getdateFormatArray().first! var pageFormatString: String = KMHeaderFooterManager.getPageFormats().first! var startString: String = "1" var name: String = "" var tag: String = "" override init() { super.init() self.name = "" self.tag = NSString.tagString() } static func parseModel(model: KMHeaderFooterObject, pageCount: Int) -> [String] { var topLeftString: String = "" if (!model.topLeftString.isEmpty) { var string = KMHeaderFooterObject.parsePageFormat(formatString: model.topLeftString, startPage: model.startString, pageCount: "\(pageCount)") string = KMHeaderFooterObject.parseDateFormat(formatString: string) topLeftString = string } var topCenterString: String = "" if (!model.topCenterString.isEmpty) { var string = KMHeaderFooterObject.parsePageFormat(formatString: model.topCenterString, startPage: model.startString, pageCount: "\(pageCount)") string = KMHeaderFooterObject.parseDateFormat(formatString: string) topCenterString = string } var topRightString: String = "" if (!model.topRightString.isEmpty) { var string = KMHeaderFooterObject.parsePageFormat(formatString: model.topRightString, startPage: model.startString, pageCount: "\(pageCount)") string = KMHeaderFooterObject.parseDateFormat(formatString: string) topRightString = string } var bottomLeftString: String = "" if (!model.bottomLeftString.isEmpty) { var string = KMHeaderFooterObject.parsePageFormat(formatString: model.bottomLeftString, startPage: model.startString, pageCount: "\(pageCount)") string = KMHeaderFooterObject.parseDateFormat(formatString: string) bottomLeftString = string } var bottomCenterString: String = "" if (!model.bottomCenterString.isEmpty) { var string = KMHeaderFooterObject.parsePageFormat(formatString: model.bottomCenterString, startPage: model.startString, pageCount: "\(pageCount)") string = KMHeaderFooterObject.parseDateFormat(formatString: string) bottomCenterString = string } var bottomRightString: String = "" if (!model.bottomRightString.isEmpty) { var string = KMHeaderFooterObject.parsePageFormat(formatString: model.bottomRightString, startPage: model.startString, pageCount: "\(pageCount)") string = KMHeaderFooterObject.parseDateFormat(formatString: string) bottomRightString = string } return [topLeftString, topCenterString, topRightString, bottomLeftString, bottomCenterString, bottomRightString] } class func parsePageFormat(formatString: String, startPage: String, pageCount: String) -> String { var result = formatString for pageFormat in KMHeaderFooterManager.getPageFormats() { let string = "<<\(pageFormat)>>" if (result.contains(string)) { var tempString = "" if (string == "<<1>>") { tempString.append("<<\(startPage)>>") } else if (string == "<<1 of n>>") { tempString.append("<<\(startPage)>>") tempString.append(" of \(pageCount)") } else if (string == "<<1/n>>") { tempString.append("<<\(startPage)>>") tempString.append("/\(pageCount)") } else if (string == "<>") { tempString.append("Page \(startPage)") } else if (string == "<>") { tempString.append("Page \(startPage)") tempString.append("of \(pageCount)") } result = result.replacingOccurrences(of: string, with: tempString) } } return result } class func parseDateFormat(formatString: String) -> String { var result: String = formatString for dateFormat in KMHeaderFooterManager.getdateFormatArray() { if (result.contains(dateFormat)) { let formatString: String = dateFormat.replacingOccurrences(of: "m", with: "M") let replace = "<<\(dateFormat)>>" let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = formatString let dateString = dateFormatter.string(from: date) result = result.replacingOccurrences(of: replace, with: dateString) } } return result } }