KMPrintPaperModel.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // KMPrintPaperModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/21.
  6. //
  7. import Cocoa
  8. enum KMPrintPaperDirectionType: String, CaseIterable {
  9. case vertical = "Vertical" //纵向
  10. case horizontal = "Horizontal" //横向
  11. static func allValues() -> [String] {
  12. var array: [String] = []
  13. for key in KMPrintPaperDirectionType.allCases {
  14. array.append(key.rawValue)
  15. }
  16. return array
  17. }
  18. static func typeOfRawValue(_ rawValue: String) -> KMPrintPaperDirectionType {
  19. var type: KMPrintPaperDirectionType = .vertical
  20. switch rawValue {
  21. case KMPrintPaperDirectionType.vertical.rawValue, NSLocalizedString(KMPrintPaperDirectionType.vertical.rawValue, comment: ""):
  22. type = .vertical
  23. case KMPrintPaperDirectionType.horizontal.rawValue, NSLocalizedString(KMPrintPaperDirectionType.horizontal.rawValue, comment: ""):
  24. type = .horizontal
  25. default:
  26. type = .vertical
  27. }
  28. return type
  29. }
  30. }
  31. enum KMPrintPaperInfoUnit: String, CaseIterable {
  32. case mm = "mm"
  33. case cm = "cm"
  34. case `in` = "in"
  35. case px = "px"
  36. static func allValues() -> [String] {
  37. var array: [String] = []
  38. for key in KMPrintPaperInfoUnit.allCases {
  39. array.append(key.rawValue)
  40. }
  41. array.removeLast()
  42. return array
  43. }
  44. }
  45. //打印纸张设置
  46. struct KMPrintPaperInfo {
  47. var type: KMPaperType = .A4
  48. var inset: NSEdgeInsets = NSEdgeInsetsMake(30, 30, 30, 30)
  49. var customSize: CGSize = CGSize(width: 210, height: 297)
  50. var unit: KMPrintPaperInfoUnit = .mm
  51. var size: CGSize {
  52. return KMPaperType.paperSize(type: type, unit: unit)
  53. }
  54. // var showSize: CGSize {
  55. // return KMPaperType.paperSize(type: type, px: false)
  56. // }
  57. enum KMPaperType: String, CaseIterable {
  58. case Actual = "Actual Size"
  59. case A3 = "A3"
  60. case A4 = "A4"
  61. case A5 = "A5"
  62. case B5 = "B5"
  63. case Envelope_10 = "Envelope #10"
  64. case Envelope_Choukei_3 = "Envelope Choukei 3"
  65. case Envelope_DL = "DL信封"
  66. case JIS_B5 = "JIS B5"
  67. case ROC_16K = "ROC 16K"
  68. case SuperB_A3 = "Super B/A3"
  69. case Tabloid = "Tabloid"
  70. case Tabloid_Oversize = "Tabloid Oversize"
  71. case US_Legal = "US Legal"
  72. case US_Letter = "US Letter"
  73. case custom = "Custom"
  74. static func allValues() -> [String] {
  75. var array: [String] = []
  76. for key in KMPaperType.allCases {
  77. array.append(NSLocalizedString(key.rawValue, comment: ""))
  78. }
  79. return array
  80. }
  81. static func paperSize(type: KMPaperType, unit: KMPrintPaperInfoUnit) -> CGSize {
  82. var size = NSZeroSize
  83. switch type {
  84. //28.3
  85. case KMPaperType.A3:
  86. size = CGSize(width: 297, height: 420)
  87. case KMPaperType.A4:
  88. size = CGSize(width: 210, height: 297)
  89. case KMPaperType.A5:
  90. size = CGSize(width: 148, height: 210)
  91. case KMPaperType.B5:
  92. size = CGSize(width: 176, height: 250)
  93. case KMPaperType.Envelope_10:
  94. size = CGSize(width: 105, height: 241)
  95. case KMPaperType.Envelope_Choukei_3:
  96. size = CGSize(width: 120, height: 235)
  97. case KMPaperType.Envelope_DL:
  98. size = CGSize(width: 110, height: 220)
  99. case KMPaperType.JIS_B5:
  100. size = CGSize(width: 182, height: 257)
  101. case KMPaperType.ROC_16K:
  102. size = CGSize(width: 197, height: 273)
  103. case KMPaperType.SuperB_A3:
  104. size = CGSize(width: 330, height: 483)
  105. case KMPaperType.Tabloid:
  106. size = CGSize(width: 279, height: 432)
  107. case KMPaperType.Tabloid_Oversize:
  108. size = CGSize(width: 305, height: 457)
  109. case KMPaperType.US_Legal:
  110. size = CGSize(width: 216, height: 356)
  111. case KMPaperType.US_Letter:
  112. size = CGSize(width: 216, height: 279)
  113. default:
  114. size = CGSize(width: 210, height: 297)
  115. }
  116. if unit == .mm {
  117. } else if unit == .cm {
  118. size = CGSize(width: floor(size.width * 0.1),
  119. height: floor(size.height * 0.1))
  120. } else if unit == .in {
  121. size = CGSize(width: floor(size.width * 0.254),
  122. height: floor(size.height * 0.254))
  123. } else {
  124. size = CGSize(width: floor(size.width * (595.0 / 210.0)),
  125. height: floor(size.height * (842.0 / 297.0)))
  126. }
  127. return CGSize(width: size.width * 100 / 100, height: size.height * 100 / 100)
  128. }
  129. }
  130. func unitScanel(unit: KMPrintPaperInfoUnit) -> CGFloat {
  131. if unit == .mm {
  132. return 1.0
  133. } else if unit == .cm {
  134. return 0.1
  135. } else if unit == .in {
  136. return 0.254
  137. } else {
  138. return 1.0
  139. }
  140. }
  141. }
  142. class KMPrintPaperModel: NSObject {
  143. var info: KMPrintPaperInfo = KMPrintPaperInfo()
  144. var direction: KMPrintPaperDirectionType = .vertical
  145. }