KMPageSizeTool.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // KMPageSizeTool.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/9/26.
  6. //
  7. import Cocoa
  8. let KMPaperSizeNameA3: String = NSLocalizedString("A3", comment: "")
  9. let KMPaperSizeNameA4: String = NSLocalizedString("A4", comment: "")
  10. let KMPaperSizeNameA5: String = NSLocalizedString("A5", comment: "")
  11. let KMPaperSizeNameB5: String = NSLocalizedString("B5", comment: "")
  12. let KMPaperSizeNameEnvelope10: String = NSLocalizedString("Envelope #10", comment: "")
  13. let KMPaperSizeNameEnvelopeChoukei: String = NSLocalizedString("Envelope Choukei 3", comment: "")
  14. let KMPaperSizeNameEnvelopeDL: String = NSLocalizedString("Envelope DL", comment: "")
  15. let KMPaperSizeNameJISB5: String = NSLocalizedString("JIS B5", comment: "")
  16. let KMPaperSizeNameROC16K: String = NSLocalizedString("ROC 16K", comment: "")
  17. let KMPaperSizeNameSuper: String = NSLocalizedString("Super B/A3", comment: "")
  18. let KMPaperSizeNameTabloid: String = NSLocalizedString("Tabloid", comment: "")
  19. let KMPaperSizeNameTabloidOversize: String = NSLocalizedString("Tabloid Oversize", comment: "")
  20. let KMPaperSizeNameUSLegal: String = NSLocalizedString("US Legal", comment: "")
  21. let KMPaperSizeNameUSLetter: String = NSLocalizedString("US Letter", comment: "")
  22. @objcMembers class KMPageSizeTool: NSObject {
  23. static func paperSize() -> [String] {
  24. return [KMPaperSizeNameA3,
  25. KMPaperSizeNameA4,
  26. KMPaperSizeNameA5,
  27. KMPaperSizeNameB5,
  28. KMPaperSizeNameEnvelope10,
  29. KMPaperSizeNameEnvelopeChoukei,
  30. KMPaperSizeNameEnvelopeDL,
  31. KMPaperSizeNameJISB5,
  32. KMPaperSizeNameROC16K,
  33. KMPaperSizeNameSuper,
  34. KMPaperSizeNameTabloid,
  35. KMPaperSizeNameTabloidOversize,
  36. KMPaperSizeNameUSLegal,
  37. KMPaperSizeNameUSLetter]
  38. }
  39. static func getPaperSize(paperName: String) -> NSSize {
  40. var size = CGSizeZero
  41. if (paperName == KMPaperSizeNameA3) {
  42. size = CGSizeMake(297, 420)
  43. } else if (paperName == KMPaperSizeNameA4){
  44. size = CGSizeMake(210, 297)
  45. } else if (paperName == KMPaperSizeNameA5){
  46. size = CGSizeMake(148, 210)
  47. } else if (paperName == KMPaperSizeNameB5){
  48. size = CGSizeMake(176, 250)
  49. } else if (paperName == KMPaperSizeNameEnvelope10){
  50. size = CGSizeMake(105, 241)
  51. } else if (paperName == KMPaperSizeNameEnvelopeChoukei){
  52. size = CGSizeMake(120, 235)
  53. } else if (paperName == KMPaperSizeNameEnvelopeDL){
  54. size = CGSizeMake(110, 220)
  55. } else if (paperName == KMPaperSizeNameJISB5){
  56. size = CGSizeMake(182, 257)
  57. } else if (paperName == KMPaperSizeNameROC16K){
  58. size = CGSizeMake(197, 273)
  59. } else if (paperName == KMPaperSizeNameSuper){
  60. size = CGSizeMake(330, 483)
  61. } else if (paperName == KMPaperSizeNameTabloid){
  62. size = CGSizeMake(279, 432)
  63. } else if (paperName == KMPaperSizeNameTabloidOversize){
  64. size = CGSizeMake(305, 457)
  65. } else if (paperName == KMPaperSizeNameUSLegal){
  66. size = CGSizeMake(216, 356)
  67. } else if (paperName == KMPaperSizeNameUSLetter){
  68. size = CGSizeMake(216, 279)
  69. }
  70. return size
  71. }
  72. static func conversion(withUnit: String, value: CGFloat) -> String {
  73. var f = value
  74. if (withUnit == "cm") {
  75. f = value / 10.0
  76. } else if (withUnit == "in") {
  77. f = value / 25.4
  78. } else {
  79. f = value
  80. }
  81. if (fmod(f, 1) == 0) { //如果有一位小数点
  82. return String(format: "%.0f", f)
  83. } else if (fmod(value * 10, 1) == 0) { //如果有两位小数点
  84. return String(format: "%.1f", f)
  85. } else {
  86. return String(format: "%.2f", f)
  87. }
  88. }
  89. static func conversion(with unit: String, from funit: String, value: CGFloat) -> String {
  90. var f = value
  91. if (unit == funit) {
  92. } else if(unit == "cm") {
  93. if(funit == "mm") {
  94. f = value/10.0
  95. } else if(funit == "in") {
  96. f = value * 25.4
  97. }
  98. } else if(unit == "mm") {
  99. if(funit == "cm") {
  100. f = value * 10.0
  101. } else if(funit == "in") {
  102. f = value * 10 * 25.4
  103. }
  104. } else if(unit == "in") {
  105. if(funit == "cm") {
  106. f = value/25.4
  107. } else if(funit == "mm") {
  108. f = value/10.0
  109. f = value/25.4
  110. }
  111. }
  112. if (fmod(f, 1) == 0) { //如果有一位小数点
  113. return String(format: "%.0f", f)
  114. } else if (fmod(value * 10, 1) == 0) { //如果有两位小数点
  115. return String(format: "%.1f", f)
  116. } else {
  117. return String(format: "%.2f", f)
  118. }
  119. }
  120. }