KMCropTools.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }
  66. return NSIntegralRect(NSInsetRect(foregroundBox, -marginWidth, -marginHeight))
  67. }
  68. class func newBitmapImageRepForBox(_ page: CPDFPage, _ box: CPDFDisplayBox) -> NSBitmapImageRep? {
  69. let bounds = page.bounds(for: box)
  70. var imageRep = NSBitmapImageRep(bitmapDataPlanes: nil,
  71. pixelsWide: Int(NSWidth(bounds)),
  72. pixelsHigh: Int(NSHeight(bounds)),
  73. bitsPerSample: 8,
  74. samplesPerPixel: 4,
  75. hasAlpha: true,
  76. isPlanar: false,
  77. colorSpaceName: .calibratedRGB,
  78. bitmapFormat: NSBitmapImageRep.Format(rawValue: 0),
  79. bytesPerRow: 0,
  80. bitsPerPixel: 32)
  81. if (imageRep != nil) {
  82. NSGraphicsContext.saveGraphicsState()
  83. NSGraphicsContext.current = NSGraphicsContext.init(bitmapImageRep: imageRep!)
  84. NSGraphicsContext.current?.imageInterpolation = .none
  85. NSGraphicsContext.current?.shouldAntialias = false
  86. if (page.rotation != 0) {
  87. var transform = NSAffineTransform()
  88. if (page.rotation == 90) {
  89. transform.translateX(by: NSWidth(bounds), yBy: 0)
  90. } else if (page.rotation == 180) {
  91. transform.translateX(by: NSHeight(bounds), yBy: NSWidth(bounds))
  92. } else if (page.rotation == 270) {
  93. transform.translateX(by: 0, yBy: NSHeight(bounds))
  94. }
  95. transform.rotate(byDegrees: CGFloat(page.rotation))
  96. transform.concat()
  97. }
  98. page.draw(with: box, to: (NSGraphicsContext.current?.cgContext))
  99. NSGraphicsContext.current?.imageInterpolation = .default
  100. NSGraphicsContext.restoreGraphicsState()
  101. }
  102. return imageRep
  103. }
  104. }