// // KMPageSizeTool.swift // PDF Reader Pro // // Created by tangchao on 2023/9/26. // import Cocoa let KMPaperSizeNameA3: String = NSLocalizedString("A3", comment: "") let KMPaperSizeNameA4: String = NSLocalizedString("A4", comment: "") let KMPaperSizeNameA5: String = NSLocalizedString("A5", comment: "") let KMPaperSizeNameB5: String = NSLocalizedString("B5", comment: "") let KMPaperSizeNameEnvelope10: String = NSLocalizedString("Envelope #10", comment: "") let KMPaperSizeNameEnvelopeChoukei: String = NSLocalizedString("Envelope Choukei 3", comment: "") let KMPaperSizeNameEnvelopeDL: String = NSLocalizedString("Envelope DL", comment: "") let KMPaperSizeNameJISB5: String = NSLocalizedString("JIS B5", comment: "") let KMPaperSizeNameROC16K: String = NSLocalizedString("ROC 16K", comment: "") let KMPaperSizeNameSuper: String = NSLocalizedString("Super B/A3", comment: "") let KMPaperSizeNameTabloid: String = NSLocalizedString("Tabloid", comment: "") let KMPaperSizeNameTabloidOversize: String = NSLocalizedString("Tabloid Oversize", comment: "") let KMPaperSizeNameUSLegal: String = NSLocalizedString("US Legal", comment: "") let KMPaperSizeNameUSLetter: String = NSLocalizedString("US Letter", comment: "") @objcMembers class KMPageSizeTool: NSObject { static func paperSize() -> [String] { return [KMPaperSizeNameA3, KMPaperSizeNameA4, KMPaperSizeNameA5, KMPaperSizeNameB5, KMPaperSizeNameEnvelope10, KMPaperSizeNameEnvelopeChoukei, KMPaperSizeNameEnvelopeDL, KMPaperSizeNameJISB5, KMPaperSizeNameROC16K, KMPaperSizeNameSuper, KMPaperSizeNameTabloid, KMPaperSizeNameTabloidOversize, KMPaperSizeNameUSLegal, KMPaperSizeNameUSLetter] } static func getPaperSize(paperName: String) -> NSSize { var size = CGSizeZero if (paperName == KMPaperSizeNameA3) { size = CGSizeMake(297, 420) } else if (paperName == KMPaperSizeNameA4){ size = CGSizeMake(210, 297) } else if (paperName == KMPaperSizeNameA5){ size = CGSizeMake(148, 210) } else if (paperName == KMPaperSizeNameB5){ size = CGSizeMake(176, 250) } else if (paperName == KMPaperSizeNameEnvelope10){ size = CGSizeMake(105, 241) } else if (paperName == KMPaperSizeNameEnvelopeChoukei){ size = CGSizeMake(120, 235) } else if (paperName == KMPaperSizeNameEnvelopeDL){ size = CGSizeMake(110, 220) } else if (paperName == KMPaperSizeNameJISB5){ size = CGSizeMake(182, 257) } else if (paperName == KMPaperSizeNameROC16K){ size = CGSizeMake(197, 273) } else if (paperName == KMPaperSizeNameSuper){ size = CGSizeMake(330, 483) } else if (paperName == KMPaperSizeNameTabloid){ size = CGSizeMake(279, 432) } else if (paperName == KMPaperSizeNameTabloidOversize){ size = CGSizeMake(305, 457) } else if (paperName == KMPaperSizeNameUSLegal){ size = CGSizeMake(216, 356) } else if (paperName == KMPaperSizeNameUSLetter){ size = CGSizeMake(216, 279) } return size } static func conversion(withUnit: String, value: CGFloat) -> String { var f = value if (withUnit == "cm") { f = value / 10.0 } else if (withUnit == "in") { f = value / 25.4 } else { f = value } if (fmod(f, 1) == 0) { //如果有一位小数点 return String(format: "%.0f", f) } else if (fmod(value * 10, 1) == 0) { //如果有两位小数点 return String(format: "%.1f", f) } else { return String(format: "%.2f", f) } } static func conversion(with unit: String, from funit: String, value: CGFloat) -> String { var f = value if (unit == funit) { } else if(unit == "cm") { if(funit == "mm") { f = value/10.0 } else if(funit == "in") { f = value * 25.4 } } else if(unit == "mm") { if(funit == "cm") { f = value * 10.0 } else if(funit == "in") { f = value * 10 * 25.4 } } else if(unit == "in") { if(funit == "cm") { f = value/25.4 } else if(funit == "mm") { f = value/10.0 f = value/25.4 } } if (fmod(f, 1) == 0) { //如果有一位小数点 return String(format: "%.0f", f) } else if (fmod(value * 10, 1) == 0) { //如果有两位小数点 return String(format: "%.1f", f) } else { return String(format: "%.2f", f) } } }