KMCropTools.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // KMCropTools.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/1.
  6. //
  7. import Cocoa
  8. @objcMembers class KMCropTools: NSObject {
  9. class func getPageSize() -> Array<String> {
  10. return ["A3","A4","A5","A6",
  11. "Envelope #10","Envelope Choukei 3","Envelope DL",
  12. "JIS B5","ROC 16K","Super B/A3",
  13. "Tabloid","Tabloid Oversize",
  14. "US Legal","US Letter"]
  15. }
  16. class func getPageSizeValue(_ pageSize: String) -> NSSize {
  17. var size = NSZeroSize
  18. if (pageSize == "A3") {
  19. size = NSSize(width:297 , height: 420)
  20. } else if (pageSize == "A4") {
  21. size = NSSize(width:210 , height: 297)
  22. } else if (pageSize == "A5") {
  23. size = NSSize(width:148 , height: 210)
  24. } else if (pageSize == "A6") {
  25. size = NSSize(width:176 , height: 250)
  26. } else if (pageSize == "Envelope #10") {
  27. size = NSSize(width:105 , height: 241)
  28. } else if (pageSize == "Envelope Choukei 3") {
  29. size = NSSize(width:120 , height: 235)
  30. } else if (pageSize == "Envelope DL") {
  31. size = NSSize(width:110 , height: 220)
  32. } else if (pageSize == "JIS B5") {
  33. size = NSSize(width:182 , height: 257)
  34. } else if (pageSize == "ROC 16K") {
  35. size = NSSize(width:197 , height: 273)
  36. } else if (pageSize == "Super B/A3") {
  37. size = NSSize(width:330 , height: 483)
  38. } else if (pageSize == "Tabloid") {
  39. size = NSSize(width:279 , height: 432)
  40. } else if (pageSize == "Tabloid Oversize") {
  41. size = NSSize(width:305 , height: 457)
  42. } else if (pageSize == "US Legal") {
  43. size = NSSize(width:216 , height: 356)
  44. } else if (pageSize == "US Letter") {
  45. size = NSSize(width:216 , height: 279)
  46. }
  47. return size
  48. }
  49. class func getPageForegroundBox(_ page: CPDFPage) -> NSRect {
  50. let marginWidth: CGFloat = 10
  51. let marginHeight: CGFloat = 10
  52. let imageRep = KMCropTools.newBitmapImageRepForBox(page, .mediaBox)
  53. let bounds = page.bounds(for: .mediaBox)
  54. var foregroundBox = imageRep?.foregroundRect() ?? .zero
  55. if (imageRep == nil) {
  56. foregroundBox = bounds
  57. } else if (NSIsEmptyRect(foregroundBox)) {
  58. let centerPoint = NSPoint(x: bounds.midX, y: bounds.midY)
  59. let origin = NSPoint(x: round(centerPoint.x), y: round(centerPoint.y))
  60. foregroundBox.origin = origin
  61. foregroundBox.size = NSZeroSize
  62. } else {
  63. let origin = NSPoint(x: foregroundBox.origin.x+bounds.origin.x, y: foregroundBox.origin.y+bounds.origin.y)
  64. foregroundBox.origin = origin
  65. foregroundBox.origin.x += 5
  66. foregroundBox.origin.y += 5
  67. foregroundBox.size.width -= 10
  68. foregroundBox.size.height -= 10
  69. }
  70. return NSIntegralRect(NSInsetRect(foregroundBox, -marginWidth, -marginHeight))
  71. }
  72. class func newBitmapImageRepForBox(_ page: CPDFPage, _ box: CPDFDisplayBox) -> NSBitmapImageRep? {
  73. let bounds = page.bounds(for: box)
  74. var imageRep = NSBitmapImageRep(bitmapDataPlanes: nil,
  75. pixelsWide: Int(NSWidth(bounds)),
  76. pixelsHigh: Int(NSHeight(bounds)),
  77. bitsPerSample: 8,
  78. samplesPerPixel: 4,
  79. hasAlpha: true,
  80. isPlanar: false,
  81. colorSpaceName: .calibratedRGB,
  82. bitmapFormat: NSBitmapImageRep.Format(rawValue: 0),
  83. bytesPerRow: 0,
  84. bitsPerPixel: 32)
  85. if (imageRep != nil) {
  86. NSGraphicsContext.saveGraphicsState()
  87. NSGraphicsContext.current = NSGraphicsContext.init(bitmapImageRep: imageRep!)
  88. NSGraphicsContext.current?.imageInterpolation = .none
  89. NSGraphicsContext.current?.shouldAntialias = false
  90. if (page.rotation != 0) {
  91. var transform = NSAffineTransform()
  92. if (page.rotation == 90) {
  93. transform.translateX(by: NSWidth(bounds), yBy: 0)
  94. } else if (page.rotation == 180) {
  95. transform.translateX(by: NSHeight(bounds), yBy: NSWidth(bounds))
  96. } else if (page.rotation == 270) {
  97. transform.translateX(by: 0, yBy: NSHeight(bounds))
  98. }
  99. transform.rotate(byDegrees: CGFloat(page.rotation))
  100. transform.concat()
  101. }
  102. page.drawEdit(with: box, to: (NSGraphicsContext.current?.cgContext))
  103. NSGraphicsContext.current?.imageInterpolation = .default
  104. NSGraphicsContext.restoreGraphicsState()
  105. }
  106. return imageRep
  107. }
  108. class func newBitmapImageRepFromAnnotationForBox(_ page: CPDFPage, _ box: CPDFDisplayBox) -> NSBitmapImageRep? {
  109. let bounds = page.bounds(for: box)
  110. var imageRep = NSBitmapImageRep(bitmapDataPlanes: nil,
  111. pixelsWide: Int(NSWidth(bounds)),
  112. pixelsHigh: Int(NSHeight(bounds)),
  113. bitsPerSample: 8,
  114. samplesPerPixel: 4,
  115. hasAlpha: true,
  116. isPlanar: false,
  117. colorSpaceName: .calibratedRGB,
  118. bitmapFormat: NSBitmapImageRep.Format(rawValue: 0),
  119. bytesPerRow: 0,
  120. bitsPerPixel: 32)
  121. if (imageRep != nil) {
  122. NSGraphicsContext.saveGraphicsState()
  123. NSGraphicsContext.current = NSGraphicsContext.init(bitmapImageRep: imageRep!)
  124. NSGraphicsContext.current?.imageInterpolation = .none
  125. NSGraphicsContext.current?.shouldAntialias = false
  126. if (page.rotation != 0) {
  127. var transform = NSAffineTransform()
  128. if (page.rotation == 90) {
  129. transform.translateX(by: NSWidth(bounds), yBy: 0)
  130. } else if (page.rotation == 180) {
  131. transform.translateX(by: NSHeight(bounds), yBy: NSWidth(bounds))
  132. } else if (page.rotation == 270) {
  133. transform.translateX(by: 0, yBy: NSHeight(bounds))
  134. }
  135. transform.rotate(byDegrees: CGFloat(page.rotation))
  136. transform.concat()
  137. }
  138. for annotation in page.annotations {
  139. annotation.draw(with: box, in: (NSGraphicsContext.current?.cgContext))
  140. }
  141. // page.drawEdit(with: box, to: (NSGraphicsContext.current?.cgContext))
  142. NSGraphicsContext.current?.imageInterpolation = .default
  143. NSGraphicsContext.restoreGraphicsState()
  144. }
  145. return imageRep
  146. }
  147. }