KMPDFCropWindowController.swift 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. //
  2. // KMPDFCropWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2024/1/9.
  6. //
  7. import Foundation
  8. @objc enum KMPDFCropType: Int {
  9. case CropBox = 0
  10. case ArtBox
  11. case TrimBox
  12. case BleedBox
  13. }
  14. typealias cropWindowCloseBlock = (_ doc: CPDFDocument) ->Void
  15. class KMPDFCropWindowController: NSWindowController, CPDFViewDelegate, NSWindowDelegate, NSTextFieldDelegate, NSComboBoxDelegate{
  16. var previewDocument: CPDFDocument?
  17. var originalDocument: CPDFDocument?
  18. var password: String = ""
  19. var pagesString: String = ""
  20. var pageType: KMPDFSelectPageStringType = .AllPages
  21. var currentPage: Int = 0
  22. var cropRect: CGRect = CGRectZero
  23. var defaultCropRect: CGRect = CGRectZero
  24. var selectPages = NSMutableArray()
  25. var cropEdgeInsets: NSEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0)
  26. var drawPageRect: CGRect = CGRectZero
  27. @IBOutlet var unitLabel: NSTextField!
  28. @IBOutlet var unitPopUpButton: NSPopUpButton!
  29. @IBOutlet var marginBox: NSBox!
  30. @IBOutlet var marginLabel: NSTextField!
  31. @IBOutlet var applyLabel: NSTextField!
  32. @IBOutlet var applyPopUpButton: NSPopUpButton!
  33. @IBOutlet var topLabel: NSTextField!
  34. @IBOutlet var bottomLabel: NSTextField!
  35. @IBOutlet var leftLabel: NSTextField!
  36. @IBOutlet var rightLabel: NSTextField!
  37. @IBOutlet var topDistanceText: NSTextField!
  38. @IBOutlet var bottomDistanceText: NSTextField!
  39. @IBOutlet var leftDistanceText: NSTextField!
  40. @IBOutlet var rightDistanceText: NSTextField!
  41. @IBOutlet var topStepper: NSStepper!
  42. @IBOutlet var bottomStepper: NSStepper!
  43. @IBOutlet var leftStepper: NSStepper!
  44. @IBOutlet var rightStepper: NSStepper!
  45. @IBOutlet var proportButton: NSButton!
  46. @IBOutlet var marginsButton: NSButton!
  47. @IBOutlet var zeroButton: NSButton!
  48. @IBOutlet var revertButton: NSButton!
  49. @IBOutlet var pageSizeBox: NSBox!
  50. @IBOutlet var pageSizeLabel: NSTextField!
  51. @IBOutlet var pageSizePopUpButton: NSPopUpButton!
  52. @IBOutlet var fixedSizeButton: NSButton!
  53. @IBOutlet var customButton: NSButton!
  54. @IBOutlet var centerButton: NSButton!
  55. @IBOutlet var widthLabel: NSTextField!
  56. @IBOutlet var heightLabel: NSTextField!
  57. @IBOutlet var xLabel: NSTextField!
  58. @IBOutlet var yLabel: NSTextField!
  59. @IBOutlet var widthText: NSTextField!
  60. @IBOutlet var heightText: NSTextField!
  61. @IBOutlet var xText: NSTextField!
  62. @IBOutlet var yText: NSTextField!
  63. @IBOutlet var pageRangeLabel: NSTextField!
  64. @IBOutlet var pageRangeComboBox: NSComboBox!
  65. @IBOutlet var cancelButton: NSButton!
  66. @IBOutlet var saveButton: NSButton!
  67. @IBOutlet var printButton: NSButton!
  68. @IBOutlet var previewPageButton: NSButton!
  69. @IBOutlet var nextPageButton: NSButton!
  70. @IBOutlet var currentPageIndexTextF: NSTextField!
  71. @IBOutlet var totalPageCountLabel: NSTextField!
  72. @IBOutlet var pdfView: KMCropPDFView!
  73. @IBOutlet var cropLabel: NSTextField!
  74. var isChangePageSize = false
  75. var cropWindowCloseBlock: cropWindowCloseBlock?
  76. convenience init(document:CPDFDocument, pwd:String, currentPage:Int, cropSize:CGRect, selectPageString:String) {
  77. self.init(windowNibName: "KMPDFCropWindowController")
  78. self.currentPage = currentPage
  79. self.defaultCropRect = cropRect
  80. if selectPageString.count > 0 {
  81. self.pagesString = String(format: "%ld", (Int(selectPageString) ?? 0) + 1)
  82. self.pageType = .PagesString
  83. let nu: NSNumber = Int(self.pagesString)! as NSNumber
  84. self.selectPages.add(nu)
  85. }else {
  86. self.pageType = .AllPages
  87. }
  88. self.password = pwd
  89. self.originalDocument = document
  90. }
  91. convenience init(documentPath: URL, selectPages: String){
  92. self.init(windowNibName: "KMPDFCropWindowController")
  93. if selectPages.count > 0 {
  94. self.pagesString = String(format: "%ld", (Int(selectPages) ?? 0) + 1)
  95. self.pageType = .PagesString
  96. let nu: NSNumber = Int(self.pagesString)! as NSNumber
  97. self.selectPages.add(nu)
  98. }else {
  99. self.pageType = .AllPages
  100. }
  101. self.originalDocument = CPDFDocument(url: documentPath)
  102. }
  103. override func windowDidLoad() {
  104. super.windowDidLoad()
  105. self.cropRect = self.defaultCropRect
  106. self.pdfView.wantsLayer = true
  107. self.pdfView.layer?.borderColor = NSColor.black.cgColor
  108. self.pdfView.layer?.borderWidth = 2
  109. self.pdfView.backgroundColor = NSColor.white
  110. self.pdfView.autoScales = true
  111. self.pdfView.delegate = self
  112. self.localizedString()
  113. self.fixedSizeButton.state = .on
  114. self.customButton.state = .off
  115. self.centerButton.isEnabled = false
  116. self.xText.isEnabled = false
  117. self.yText.isEnabled = false
  118. self.widthText.isEnabled = false
  119. self.heightText.isEnabled = false
  120. self.centerButton.state = .on
  121. let numberFormatter = self.currentPageIndexTextF.formatter as! NumberFormatter
  122. numberFormatter.maximum = NSNumber(value: self.originalDocument?.pageCount ?? 0)
  123. self.totalPageCountLabel.stringValue = String(format: "%ld", self.originalDocument?.pageCount ?? 0)
  124. let currentPageIndex = self.pdfView.currentPageIndex//self.pdfView.document!.index(for: self.pdfView.currentPage()!)
  125. self.currentPageIndexTextF.stringValue = "\(currentPageIndex + 1)"
  126. self.updateSizeView()
  127. self.topStepper.minValue = 0
  128. self.leftStepper.minValue = 0
  129. self.rightStepper.minValue = 0
  130. self.bottomStepper.minValue = 0
  131. self.changePrePDFView()
  132. self.reloadPDFWithIndex()
  133. self.updateCropViewLabel()
  134. let tPage = self.previewDocument?.page(at: UInt(self.currentPage))
  135. self.pdfView.go(to: tPage)
  136. }
  137. func windowShouldClose(_ sender: NSWindow) -> Bool {
  138. close()
  139. return true
  140. }
  141. override func close() {
  142. if (self.cropWindowCloseBlock != nil) {
  143. cropWindowCloseBlock!(originalDocument!)
  144. }
  145. if ((self.window?.isSheet) != nil) {
  146. // self.window?.sheetParent?.endSheet(self.window!)
  147. } else {
  148. super.close()
  149. }
  150. }
  151. func updateCropViewLabel() {
  152. let unitScanl: CGFloat = self.unitConversion()
  153. var mat: String = NSLocalizedString("Cropped page size:", comment: "")
  154. if (0 == self.applyPopUpButton.indexOfSelectedItem) {
  155. mat = NSLocalizedString("Cropped page size:", comment: "")
  156. } else if (1 == self.applyPopUpButton.indexOfSelectedItem) {
  157. mat = NSLocalizedString("ArtBox size:", comment: "")
  158. } else if (2 == self.applyPopUpButton.indexOfSelectedItem) {
  159. mat = NSLocalizedString("TrimBox size:", comment: "")
  160. } else {
  161. mat = NSLocalizedString("BleedBox size:", comment: "")
  162. }
  163. if (0 == self.pageSizePopUpButton.indexOfSelectedItem && self.fixedSizeButton.state == .on) {
  164. if (.on == self.marginsButton.state) {
  165. let orgPage: CPDFPage = (self.originalDocument?.page(at: UInt(self.currentPage)))!
  166. let rect: NSRect = KMCropTools.getPageForegroundBox(orgPage)
  167. self.cropLabel.stringValue = String(format: "%@%@ * %@%@", mat, self.formatFloat(Float(rect.size.width/unitScanl)), self.formatFloat(Float(rect.size.height/unitScanl)), self.unitPopUpButton.title)
  168. } else {
  169. self.cropLabel.stringValue = String(format: "%@%@ * %@%@", mat, self.formatFloat(Float(self.cropRect.size.width/unitScanl)), self.formatFloat(Float(self.cropRect.size.height/unitScanl)), self.unitPopUpButton.title)
  170. }
  171. } else {
  172. self.cropLabel.stringValue = String(format: "%@%@ * %@%@", KMLocalizedString("Expanded page size:", nil), self.formatFloat(Float(self.drawPageRect.size.width/unitScanl)), self.formatFloat(Float(self.drawPageRect.size.height/unitScanl)), self.unitPopUpButton.title)
  173. }
  174. }
  175. func formatFloat(_ f: Float) -> String {
  176. if f.truncatingRemainder(dividingBy: 1) == 0 { // If there is one decimal place
  177. return String(format: "%.0f", f)
  178. } else if (f * 10).truncatingRemainder(dividingBy: 1) == 0 { // If there are two decimal places
  179. return String(format: "%.1f", f)
  180. } else {
  181. return String(format: "%.2f", f)
  182. }
  183. }
  184. func handlerReDraw() {
  185. if drawPageRect.size.width < 0 || drawPageRect.size.height < 0 {
  186. NSSound.beep()
  187. return
  188. } else {
  189. for i in 0..<(originalDocument?.pageCount ?? 0) {
  190. if selectPages.contains(i) {
  191. let page = KMCropPDFPage()
  192. page.setBounds(NSRect(x: 0, y: 0, width: drawPageRect.size.width, height: drawPageRect.size.height), for: .mediaBox)
  193. let cropPage = originalDocument?.page(at: i)
  194. page.cropDrawingPage = cropPage
  195. originalDocument?.insertPageObject(page, at: i)
  196. originalDocument?.removePage(at: i)
  197. }
  198. }
  199. }
  200. }
  201. func updateView() {
  202. let page = originalDocument?.page(at: UInt(currentPage))
  203. let pageBounds = page?.bounds(for: .mediaBox)
  204. let tCropRect = CGRect(x: cropEdgeInsets.left, y: cropEdgeInsets.bottom, width: (pageBounds?.width ?? 0) - cropEdgeInsets.left - cropEdgeInsets.right, height: (pageBounds?.height ?? 0) - cropEdgeInsets.top - cropEdgeInsets.bottom)
  205. cropRect = tCropRect
  206. changePrePDFView()
  207. }
  208. func unitConversion() -> CGFloat {
  209. var unitScanl: CGFloat = 1.0
  210. if (0 == self.unitPopUpButton.indexOfSelectedItem) {
  211. unitScanl = 595.0/21.0
  212. } else if (1 == self.unitPopUpButton.indexOfSelectedItem) {
  213. unitScanl = 595.0/210.0
  214. } else {
  215. unitScanl = (595.0/21.0) * 2.54
  216. }
  217. return unitScanl
  218. }
  219. func getDrawPages() {
  220. let pages = NSMutableArray()
  221. if 0 == self.pageRangeComboBox.indexOfSelectedItem {
  222. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  223. pages.add(i)
  224. }
  225. } else if 1 == self.pageRangeComboBox.indexOfSelectedItem {
  226. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  227. if i % 2 == 0 {
  228. pages.add(i)
  229. }
  230. }
  231. } else if 2 == self.pageRangeComboBox.indexOfSelectedItem {
  232. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  233. if i % 2 != 0 {
  234. pages.add(i)
  235. }
  236. }
  237. } else {
  238. let fileAttribute = KMFileAttribute()
  239. fileAttribute.filePath = self.originalDocument?.documentURL.path ?? ""
  240. fileAttribute.bAllPage = false
  241. fileAttribute.pagesString = self.pageRangeComboBox.stringValue
  242. // if !fileAttribute.selectPages {
  243. //
  244. // }
  245. for num in fileAttribute.fetchSelectPages() {
  246. pages.add(num - 1)
  247. }
  248. }
  249. if pages.count < 1 {
  250. let alert = NSAlert()
  251. alert.alertStyle = .critical
  252. alert.messageText = String(format: "%@%@", self.originalDocument?.documentURL.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  253. alert.runModal()
  254. } else {
  255. self.selectPages = pages
  256. }
  257. }
  258. func reloadPDFWithIndex() {
  259. self.pdfView.documentView().enclosingScrollView?.hasVerticalScroller = false
  260. self.pdfView.documentView().enclosingScrollView?.hasHorizontalScroller = false
  261. let pageCount = self.previewDocument?.pageCount
  262. let currentPageIndex = self.previewDocument?.index(for: self.pdfView.currentPage())
  263. self.currentPageIndexTextF.stringValue = "\((currentPageIndex ?? 0)+1)"
  264. self.totalPageCountLabel.stringValue = String(format:NSLocalizedString(" / %ld", comment: ""), pageCount ?? 0)
  265. }
  266. func updateLocationXY() {
  267. guard centerButton.state == .on else { return }
  268. let unitScanl = unitConversion()
  269. let page = originalDocument?.page(at: UInt(currentPage))
  270. let pageBounds = page?.bounds(for: .mediaBox)
  271. let x = (drawPageRect.size.width - (pageBounds?.size.width ?? 0)) / (2 * unitScanl)
  272. let y = (drawPageRect.size.height - (pageBounds?.size.height ?? 0)) / (2 * unitScanl)
  273. xText.stringValue = formatFloat(Float(x))
  274. yText.stringValue = formatFloat(Float(y))
  275. }
  276. func handerReDraw() {
  277. if drawPageRect.size.width < 0 || drawPageRect.size.height < 0 {
  278. NSSound.beep()
  279. return
  280. } else {
  281. for i in 0..<(originalDocument?.pageCount ?? 0) {
  282. if selectPages.contains(i) {
  283. let newPage = KMCropPDFPage()
  284. newPage.setBounds(NSMakeRect(0, 0, drawPageRect.size.width, drawPageRect.size.height), for: .mediaBox)
  285. let cropPage = self.originalDocument?.page(at: i)
  286. newPage.cropDrawingPage = cropPage
  287. self.originalDocument?.insertPageObject(newPage, at: i)
  288. self.originalDocument?.removePage(at: i)
  289. }
  290. }
  291. }
  292. }
  293. @IBAction func buttonItemClick_Proport(_ sender: NSButton) {
  294. var mint = min(cropEdgeInsets.bottom, cropEdgeInsets.top)
  295. mint = min(mint, cropEdgeInsets.left)
  296. mint = min(mint, cropEdgeInsets.right)
  297. cropEdgeInsets = NSEdgeInsets(top: mint, left: mint, bottom: mint, right: mint)
  298. let unitScanl = unitConversion()
  299. rightStepper.stringValue = formatFloat(Float(cropEdgeInsets.right/unitScanl))
  300. topDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.top/unitScanl)))"
  301. leftDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.left/unitScanl)))"
  302. bottomDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.bottom/unitScanl)))"
  303. rightDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.right/unitScanl)))"
  304. if proportButton.state == .on {
  305. marginsButton.state = .off
  306. }
  307. updateView()
  308. updateCropViewLabel()
  309. }
  310. @IBAction func buttonClicked_Margin(_ sender: NSButton) {
  311. if marginsButton.state == .on {
  312. proportButton.state = .off
  313. topDistanceText.isEnabled = false
  314. bottomDistanceText.isEnabled = false
  315. leftDistanceText.isEnabled = false
  316. rightDistanceText.isEnabled = false
  317. topStepper.isEnabled = false
  318. bottomStepper.isEnabled = false
  319. leftStepper.isEnabled = false
  320. rightStepper.isEnabled = false
  321. previewDocument = CPDFDocument()
  322. for i in 0..<(originalDocument?.pageCount ?? 0) {
  323. var page: CPDFPage? = nil
  324. if selectPages.contains(i) {
  325. page = CPDFPage()
  326. let orgPage = originalDocument?.page(at: i)
  327. let rect = KMCropTools.getPageForegroundBox(orgPage ?? CPDFPage())
  328. let pageBounds = orgPage?.bounds(for: .mediaBox) ?? NSRect.zero
  329. page?.setBounds(pageBounds, for: .mediaBox)
  330. page?.cropDrawingPage = orgPage
  331. page?.cropRect = rect
  332. } else {
  333. page = originalDocument?.page(at: i)
  334. }
  335. previewDocument?.insertPageObject(page, at: previewDocument?.pageCount ?? 0)
  336. }
  337. pdfView.document = previewDocument
  338. pdfView.needsDisplay = true
  339. } else {
  340. topDistanceText.isEnabled = true
  341. bottomDistanceText.isEnabled = true
  342. leftDistanceText.isEnabled = true
  343. rightDistanceText.isEnabled = true
  344. topStepper.isEnabled = true
  345. bottomStepper.isEnabled = true
  346. leftStepper.isEnabled = true
  347. rightStepper.isEnabled = true
  348. cropEdgeInsets = NSEdgeInsets(top: CGFloat(topDistanceText.floatValue), left: CGFloat(leftDistanceText.floatValue), bottom: CGFloat(bottomDistanceText.floatValue), right: CGFloat(rightDistanceText.floatValue))
  349. let unitScanl = unitConversion()
  350. rightStepper.stringValue = formatFloat(Float(cropEdgeInsets.right/unitScanl))
  351. topDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.top/unitScanl)))"
  352. leftDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.left/unitScanl)))"
  353. bottomDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.bottom/unitScanl)))"
  354. rightDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.right/unitScanl)))"
  355. updateView()
  356. }
  357. updateCropViewLabel()
  358. }
  359. @IBAction func buttonItemClick_Zero(_ sender: NSButton) {
  360. cropEdgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  361. let unitScanl = unitConversion()
  362. rightStepper.stringValue = formatFloat(Float(cropEdgeInsets.right/unitScanl))
  363. topDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.top/unitScanl)))"
  364. leftDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.left/unitScanl)))"
  365. bottomDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.bottom/unitScanl)))"
  366. rightDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.right/unitScanl)))"
  367. marginsButton.state = .off
  368. proportButton.state = .off
  369. updateView()
  370. updateCropViewLabel()
  371. }
  372. @IBAction func buttonItemClick_Revert(_ sender: NSButton) {
  373. cropRect = defaultCropRect
  374. marginsButton.state = .off
  375. proportButton.state = .off
  376. changePrePDFView()
  377. updateCropViewLabel()
  378. }
  379. @IBAction func buttonItemClick_Cancel(_ sender: NSButton) {
  380. close()
  381. }
  382. @IBAction func buttonItemClick_Save(_ sender: NSButton) {
  383. self.window?.makeFirstResponder(nil)
  384. self.getDrawPages()
  385. if self.selectPages.count < 1 {
  386. let alert = NSAlert()
  387. alert.alertStyle = .critical
  388. alert.messageText = "\(self.originalDocument?.documentURL.path.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  389. alert.runModal()
  390. return
  391. }
  392. if self.pageSizePopUpButton.indexOfSelectedItem == 0 && self.fixedSizeButton.state == .on {
  393. var box = CPDFDisplayBox.mediaBox
  394. if self.applyPopUpButton.indexOfSelectedItem == 0 {
  395. box = .cropBox
  396. } else if self.applyPopUpButton.indexOfSelectedItem == 1 {
  397. box = .artBox
  398. } else if self.applyPopUpButton.indexOfSelectedItem == 2 {
  399. box = .trimBox
  400. } else {
  401. box = .bleedBox
  402. }
  403. let page = self.originalDocument?.page(at: UInt(currentPage))
  404. let pageBounds = page?.bounds(for: .mediaBox)
  405. if self.marginsButton.state == .on {
  406. for i in 0..<(selectPages.count) {
  407. let num: NSNumber = selectPages[i] as! NSNumber
  408. let page = self.originalDocument?.page(at: UInt((num.intValue)))
  409. let rect = KMCropTools.getPageForegroundBox(page ?? CPDFPage())
  410. page?.setBounds(rect, for: .cropBox)
  411. }
  412. } else {
  413. let tCropRect = NSMakeRect(
  414. self.cropEdgeInsets.left,
  415. self.cropEdgeInsets.bottom,
  416. pageBounds!.size.width - self.cropEdgeInsets.left - self.cropEdgeInsets.right,
  417. pageBounds!.size.height - self.cropEdgeInsets.top - self.cropEdgeInsets.bottom
  418. )
  419. self.selectPages.forEach { num in
  420. let page = self.originalDocument?.page(at: UInt((num as! NSNumber).intValue))
  421. page?.setBounds(tCropRect, for: box)
  422. if self.applyPopUpButton.indexOfSelectedItem != 0 {
  423. page?.setBounds(tCropRect, for: .cropBox)
  424. }
  425. }
  426. }
  427. } else {
  428. var isAllow = true
  429. for num in self.selectPages {
  430. let page = self.originalDocument?.page(at: UInt((num as! NSNumber).intValue))
  431. let pageBounds = page?.bounds(for: .mediaBox)
  432. if self.drawPageRect.size.width < pageBounds!.size.width || self.drawPageRect.size.height < pageBounds!.size.height {
  433. isAllow = false
  434. break
  435. }
  436. }
  437. if !isAllow {
  438. NSSound.beep()
  439. return
  440. }
  441. self.handerReDraw()
  442. }
  443. self.close()
  444. }
  445. @IBAction func buttonItemClick_Print(_ sender: NSButton) {
  446. self.window?.makeFirstResponder(nil)
  447. self.getDrawPages()
  448. if self.selectPages.count < 1 {
  449. let alert = NSAlert()
  450. alert.alertStyle = .critical
  451. alert.messageText = "\(String(describing: self.originalDocument?.documentURL.path.lastPathComponent)) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  452. alert.runModal()
  453. return
  454. }
  455. var fileName: String = ""
  456. if FileManager.default.fileExists(atPath: kCropPrintFolderPath()) == false {
  457. let success: ()? = try?FileManager.default.createDirectory(atPath: kCropPrintFolderPath(), withIntermediateDirectories: false)
  458. if success == nil {
  459. return
  460. }
  461. }
  462. fileName = "cropPDF.pdf"
  463. fileName = kCropPrintFolderPath().stringByAppendingPathComponent(fileName)
  464. let tDocuemnt = CPDFDocument()
  465. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  466. if let page = self.originalDocument?.page(at: i)?.copy() {
  467. tDocuemnt?.insertPageObject(page as? CPDFPage, at: tDocuemnt?.pageCount ?? 0)
  468. }
  469. }
  470. if 0 == self.pageSizePopUpButton.indexOfSelectedItem && self.fixedSizeButton.state == .on {
  471. var box: CPDFDisplayBox = .mediaBox
  472. if 0 == self.applyPopUpButton.indexOfSelectedItem {
  473. box = .cropBox
  474. } else if 1 == self.applyPopUpButton.indexOfSelectedItem {
  475. box = .artBox
  476. } else if 2 == self.applyPopUpButton.indexOfSelectedItem {
  477. box = .trimBox
  478. } else {
  479. box = .bleedBox
  480. }
  481. if let page = self.originalDocument?.page(at: UInt(self.currentPage)) {
  482. let pageBounds = page.bounds(for: .mediaBox)
  483. if self.marginsButton.state == .on {
  484. for i in 0..<self.selectPages.count {
  485. let num: NSNumber = self.selectPages[i] as! NSNumber
  486. if let page = tDocuemnt?.page(at: UInt(num.intValue)) {
  487. let rect = KMCropTools.getPageForegroundBox(page)
  488. page.setBounds(rect, for: .cropBox)
  489. }
  490. }
  491. } else {
  492. let tCropRect = CGRect(x: self.cropEdgeInsets.left, y: self.cropEdgeInsets.bottom, width: pageBounds.size.width - self.cropEdgeInsets.left - self.cropEdgeInsets.right , height: pageBounds.size.height - self.cropEdgeInsets.top - self.cropEdgeInsets.bottom)
  493. for i in 0..<self.selectPages.count {
  494. let num: NSNumber = self.selectPages[i] as! NSNumber
  495. if let page = tDocuemnt?.page(at: UInt(num.intValue)) {
  496. page.setBounds(tCropRect, for: box)
  497. if 0 != self.applyPopUpButton.indexOfSelectedItem {
  498. page.setBounds(tCropRect, for: .cropBox)
  499. }
  500. }
  501. }
  502. }
  503. }
  504. } else {
  505. var isAllow = true
  506. for num in self.selectPages {
  507. if let page = self.originalDocument?.page(at: UInt((num as! NSNumber).intValue)) {
  508. let pageBounds = page.bounds(for: .mediaBox)
  509. if self.drawPageRect.size.width < pageBounds.size.width || self.drawPageRect.size.height < pageBounds.size.height {
  510. isAllow = false
  511. break
  512. }
  513. }
  514. }
  515. if !isAllow {
  516. NSSound.beep()
  517. return
  518. }
  519. if self.drawPageRect.size.width < 0 || self.drawPageRect.size.height < 0 {
  520. NSSound.beep()
  521. return
  522. } else {
  523. for i in 0..<(tDocuemnt?.pageCount ?? 0) {
  524. let page: KMCropPDFPage?
  525. if self.selectPages.contains(NSNumber(value: i)) {
  526. page = KMCropPDFPage()
  527. page?.setBounds(NSRect(x: 0, y: 0, width: self.drawPageRect.size.width, height: self.drawPageRect.size.height), for: .mediaBox)
  528. if let cropPage = tDocuemnt?.page(at: i) {
  529. page?.cropDrawingPage = cropPage
  530. }
  531. if let thePage = page {
  532. tDocuemnt?.insertPageObject(thePage, at: i)
  533. tDocuemnt?.removePage(at: i+1)
  534. }
  535. }
  536. }
  537. }
  538. }
  539. guard let isSuc = tDocuemnt?.write(to: URL(fileURLWithPath: fileName )) else { return }
  540. if isSuc {
  541. let pdf = PDFDocument(url: URL(fileURLWithPath: fileName ))
  542. let printInfo = NSPrintInfo.shared
  543. var printOperation: NSPrintOperation?
  544. printOperation = pdf?.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)
  545. printOperation?.runModal(for: self.window!, delegate: self, didRun: nil, contextInfo: nil)
  546. }
  547. }
  548. @IBAction func buttonItemClick_FixedSize(_ sender: NSButton) {
  549. self.fixedSizeButton.state = .on
  550. self.customButton.state = .off
  551. self.pageSizePopUpButton.isEnabled = true
  552. self.widthText.isEnabled = false
  553. self.heightText.isEnabled = false
  554. if self.pageSizePopUpButton.indexOfSelectedItem != 0 {
  555. self.centerButton.isEnabled = true
  556. self.xText.isEnabled = true
  557. self.yText.isEnabled = true
  558. self.xText.stringValue = "0"
  559. self.yText.stringValue = "0"
  560. self.isChangePageSize = false
  561. self.topDistanceText.isEnabled = false
  562. self.bottomDistanceText.isEnabled = false
  563. self.leftDistanceText.isEnabled = false
  564. self.rightDistanceText.isEnabled = false
  565. self.topStepper.isEnabled = false
  566. self.bottomStepper.isEnabled = false
  567. self.leftStepper.isEnabled = false
  568. self.rightStepper.isEnabled = false
  569. self.marginsButton.isEnabled = false
  570. self.zeroButton.isEnabled = false
  571. self.revertButton.isEnabled = false
  572. self.proportButton.isEnabled = false
  573. self.applyPopUpButton.isEnabled = false
  574. } else {
  575. self.isChangePageSize = true
  576. self.centerButton.isEnabled = false
  577. self.xText.isEnabled = false
  578. self.yText.isEnabled = false
  579. let tSize = KMPageSizeTool.getPaperSize(paperName: self.pageSizePopUpButton.stringValue)
  580. self.drawPageRect = CGRect(x: 0, y: 0, width: tSize.width * 595.0/210, height: tSize.height * 842.0/297.0)
  581. self.topDistanceText.isEnabled = true
  582. self.bottomDistanceText.isEnabled = true
  583. self.leftDistanceText.isEnabled = true
  584. self.rightDistanceText.isEnabled = true
  585. self.topStepper.isEnabled = true
  586. self.bottomStepper.isEnabled = true
  587. self.leftStepper.isEnabled = true
  588. self.rightStepper.isEnabled = true
  589. self.marginsButton.isEnabled = true
  590. self.zeroButton.isEnabled = true
  591. self.revertButton.isEnabled = true
  592. self.proportButton.isEnabled = true
  593. self.applyPopUpButton.isEnabled = true
  594. }
  595. updateCropViewLabel()
  596. }
  597. @IBAction func buttonClicked_PageSize(_ sender: NSPopUpButton) {
  598. if (0 == sender.indexOfSelectedItem) {
  599. self.isChangePageSize = false
  600. self.centerButton.isEnabled = false
  601. self.xText.isEnabled = false
  602. self.yText.isEnabled = false
  603. self.xText.floatValue = 0
  604. self.yText.floatValue = 0
  605. self.marginsButton.state = NSControl.StateValue.off
  606. if (NSControl.StateValue.on != self.marginsButton.state) {
  607. self.topDistanceText.isEnabled = true
  608. self.bottomDistanceText.isEnabled = true
  609. self.leftDistanceText.isEnabled = true
  610. self.rightDistanceText.isEnabled = true
  611. self.topStepper.isEnabled = true
  612. self.bottomStepper.isEnabled = true
  613. self.leftStepper.isEnabled = true
  614. self.rightStepper.isEnabled = true
  615. } else {
  616. self.topDistanceText.isEnabled = false
  617. self.bottomDistanceText.isEnabled = false
  618. self.leftDistanceText.isEnabled = false
  619. self.rightDistanceText.isEnabled = false
  620. self.topStepper.isEnabled = false
  621. self.bottomStepper.isEnabled = false
  622. self.leftStepper.isEnabled = false
  623. self.rightStepper.isEnabled = false
  624. }
  625. self.marginsButton.isEnabled = true
  626. self.zeroButton.isEnabled = true
  627. self.revertButton.isEnabled = true
  628. self.proportButton.isEnabled = true
  629. self.applyPopUpButton.isEnabled = true
  630. } else {
  631. self.topDistanceText.isEnabled = false
  632. self.bottomDistanceText.isEnabled = false
  633. self.leftDistanceText.isEnabled = false
  634. self.rightDistanceText.isEnabled = false
  635. self.topStepper.isEnabled = false
  636. self.bottomStepper.isEnabled = false
  637. self.leftStepper.isEnabled = false
  638. self.rightStepper.isEnabled = false
  639. self.marginsButton.isEnabled = false
  640. self.zeroButton.isEnabled = false
  641. self.revertButton.isEnabled = false
  642. self.proportButton.isEnabled = false
  643. self.applyPopUpButton.isEnabled = false
  644. self.isChangePageSize = true
  645. let menuItem = self.pageSizePopUpButton.itemTitle(at: self.pageSizePopUpButton.indexOfSelectedItem)
  646. let tSize = KMPageSizeTool.getPaperSize(paperName: menuItem)
  647. self.drawPageRect = CGRect(x: 0, y: 0, width: tSize.width * 595.0/210, height: tSize.height * 842.0/297.0)
  648. self.centerButton.isEnabled = true
  649. self.xText.isEnabled = true
  650. self.yText.isEnabled = true
  651. }
  652. updateLocationXY()
  653. changePrePDFView()
  654. updateCropViewLabel()
  655. }
  656. @IBAction func buttonItemClick_CustomSize(_ sender: NSButton) {
  657. self.fixedSizeButton.state = NSControl.StateValue.off
  658. self.customButton.state = NSControl.StateValue.on
  659. self.widthText.isEnabled = true
  660. self.heightText.isEnabled = true
  661. self.centerButton.isEnabled = true
  662. self.xText.isEnabled = true
  663. self.yText.isEnabled = true
  664. self.topDistanceText.isEnabled = false
  665. self.bottomDistanceText.isEnabled = false
  666. self.leftDistanceText.isEnabled = false
  667. self.rightDistanceText.isEnabled = false
  668. self.topStepper.isEnabled = false
  669. self.bottomStepper.isEnabled = false
  670. self.leftStepper.isEnabled = false
  671. self.rightStepper.isEnabled = false
  672. self.marginsButton.isEnabled = false
  673. self.zeroButton.isEnabled = false
  674. self.revertButton.isEnabled = false
  675. self.proportButton.isEnabled = false
  676. self.applyPopUpButton.isEnabled = false
  677. self.pageSizePopUpButton.isEnabled = false
  678. self.isChangePageSize = true
  679. self.drawPageRect = CGRect(x: 0, y: 0, width: Int(self.widthText.floatValue), height: Int(self.heightText.floatValue))
  680. updateLocationXY()
  681. changePrePDFView()
  682. updateCropViewLabel()
  683. }
  684. @IBAction func buttonItemClick_Center(_ sender: NSButton) {
  685. if (NSControl.StateValue.on == sender.state) {
  686. updateLocationXY()
  687. } else {
  688. self.xText.stringValue = "0 \(self.unitPopUpButton.title)"
  689. self.yText.stringValue = "0 \(self.unitPopUpButton.title)"
  690. }
  691. }
  692. @IBAction func comboBoxItemClick_PageRange(_ sender: NSButton) {
  693. if (0 == self.pageRangeComboBox.indexOfSelectedItem || 1 == self.pageRangeComboBox.indexOfSelectedItem || 2 == self.pageRangeComboBox.indexOfSelectedItem) {
  694. self.pageRangeComboBox.delegate = nil
  695. self.window?.makeFirstResponder(self)
  696. self.pageRangeComboBox.isEditable = false
  697. self.getDrawPages()
  698. self.changePrePDFView()
  699. } else if (3 == self.pageRangeComboBox.indexOfSelectedItem) {
  700. self.pageRangeComboBox.delegate = self
  701. self.pageRangeComboBox.stringValue = ""
  702. self.pageRangeComboBox.isEditable = true
  703. self.window?.makeFirstResponder(self.pageRangeComboBox)
  704. }
  705. }
  706. @IBAction func buttonClicked_Unit(_ sender: NSPopUpButton) {
  707. if self.unitPopUpButton.indexOfSelectedItem == 1 {
  708. self.cropLabel.stringValue = "(\(NSLocalizedString("mm", comment: "")))"
  709. } else if self.unitPopUpButton.indexOfSelectedItem == 0 {
  710. self.cropLabel.stringValue = "(\(NSLocalizedString("cm", comment: "")))"
  711. } else {
  712. self.cropLabel.stringValue = "(\(NSLocalizedString("in", comment: "")))"
  713. }
  714. updateSizeView()
  715. updateCropViewLabel()
  716. }
  717. @IBAction func buttonClicked_AppleType(_ sender: NSPopUpButton) {
  718. if (0 == sender.indexOfSelectedItem) {
  719. self.marginsButton.isEnabled = true
  720. } else {
  721. self.marginsButton.isEnabled = false
  722. }
  723. updateCropViewLabel()
  724. }
  725. @IBAction func stepperItemClick_Distance(_ sender: NSButton) {
  726. let unitScanl: CGFloat = unitConversion()
  727. if sender == topStepper {
  728. self.topDistanceText.stringValue = String(format: "%.2f", self.topStepper.floatValue)
  729. let formatter = self.topStepper.floatValue * Float(unitScanl)
  730. self.cropEdgeInsets = NSEdgeInsetsMake(CGFloat(formatter), self.cropEdgeInsets.left, self.cropEdgeInsets.bottom, self.cropEdgeInsets.right)
  731. } else if sender == bottomStepper {
  732. self.bottomDistanceText.stringValue = String(format: "%.2f", self.bottomStepper.floatValue)
  733. let formatter = self.bottomStepper.floatValue * Float(unitScanl)
  734. self.cropEdgeInsets = NSEdgeInsetsMake(self.cropEdgeInsets.top, self.cropEdgeInsets.left, CGFloat(formatter), self.cropEdgeInsets.right)
  735. } else if sender == leftStepper {
  736. self.leftDistanceText.stringValue = String(format: "%.2f", self.leftStepper.floatValue)
  737. let formatter = self.leftStepper.floatValue * Float(unitScanl)
  738. self.cropEdgeInsets = NSEdgeInsetsMake(self.cropEdgeInsets.top, CGFloat(formatter), self.cropEdgeInsets.bottom, self.cropEdgeInsets.right)
  739. } else if sender == rightStepper {
  740. self.rightDistanceText.stringValue = String(format: "%.2f", self.rightStepper.floatValue)
  741. let formatter = self.rightStepper.floatValue * Float(unitScanl)
  742. self.cropEdgeInsets = NSEdgeInsetsMake(self.cropEdgeInsets.top, self.cropEdgeInsets.left, self.cropEdgeInsets.bottom, CGFloat(formatter))
  743. }
  744. updateView()
  745. updateCropViewLabel()
  746. }
  747. @IBAction func goPrevious(_ sender: NSButton) {
  748. if self.pdfView.canGoToPreviousPage() {
  749. self.pdfView.goToPreviousPage(nil)
  750. }
  751. self.reloadPDFWithIndex()
  752. }
  753. @IBAction func goNext(_ sender: NSButton) {
  754. if self.pdfView.canGoToNextPage() {
  755. self.pdfView.goToNextPage(nil)
  756. }
  757. self.reloadPDFWithIndex()
  758. }
  759. //MARK: NSTextFieldDelegate
  760. func controlTextDidEndEditing(_ obj: Notification) {
  761. guard let textField = obj.object as? NSTextField else { return }
  762. guard let page = originalDocument?.page(at: UInt(currentPage)) else {
  763. return
  764. }
  765. let pageBounds = page.bounds(for: .mediaBox)
  766. if textField == topDistanceText {
  767. let unitScale = unitConversion()
  768. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  769. if unitMax > pageBounds.size.height {
  770. unitMax = pageBounds.size.width
  771. textField.stringValue = "\(Float(pageBounds.size.height/unitScale))"
  772. } else if unitMax <= 0 {
  773. unitMax = 0
  774. textField.stringValue = "0"
  775. }
  776. topStepper.stringValue = textField.stringValue
  777. cropEdgeInsets = NSEdgeInsetsMake(unitMax, cropEdgeInsets.left, cropEdgeInsets.bottom, cropEdgeInsets.right)
  778. updateView()
  779. } else if textField == bottomDistanceText {
  780. let unitScale = unitConversion()
  781. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  782. if unitMax > pageBounds.size.height {
  783. unitMax = pageBounds.size.height
  784. textField.stringValue = "\(formatFloat(Float(pageBounds.size.height/unitScale)))"
  785. } else if unitMax <= 0 {
  786. unitMax = 0
  787. textField.stringValue = "0"
  788. }
  789. bottomStepper.stringValue = textField.stringValue
  790. cropEdgeInsets = NSEdgeInsetsMake(cropEdgeInsets.top, cropEdgeInsets.left, unitMax, cropEdgeInsets.right)
  791. updateView()
  792. } else if textField == leftDistanceText {
  793. let unitScale = unitConversion()
  794. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  795. if unitMax > pageBounds.size.width {
  796. unitMax = pageBounds.size.width
  797. textField.stringValue = "\(formatFloat(Float(pageBounds.size.width/unitScale)))"
  798. } else if unitMax <= 0 {
  799. unitMax = 0
  800. textField.stringValue = "0"
  801. }
  802. leftStepper.stringValue = textField.stringValue
  803. cropEdgeInsets = NSEdgeInsetsMake(cropEdgeInsets.top, unitMax, cropEdgeInsets.bottom, cropEdgeInsets.right)
  804. updateView()
  805. } else if textField == rightDistanceText {
  806. let unitScale = unitConversion()
  807. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  808. if unitMax > pageBounds.size.height {
  809. unitMax = pageBounds.size.width
  810. textField.stringValue = "\(formatFloat(Float(pageBounds.size.width/unitScale)))"
  811. } else if unitMax <= 0 {
  812. unitMax = 0
  813. textField.stringValue = "0"
  814. }
  815. rightStepper.stringValue = textField.stringValue
  816. cropEdgeInsets = NSEdgeInsetsMake(cropEdgeInsets.top, cropEdgeInsets.left, cropEdgeInsets.bottom, unitMax)
  817. updateView()
  818. } else if textField == widthText {
  819. let unitScale = unitConversion()
  820. let unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  821. drawPageRect = CGRect(x: 0, y: 0, width: unitMax, height: drawPageRect.size.height)
  822. updateLocationXY()
  823. changePrePDFView()
  824. } else if textField == heightText {
  825. let unitScale = unitConversion()
  826. let unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  827. drawPageRect = CGRect(x: 0, y: 0, width: drawPageRect.size.width, height: unitMax)
  828. updateLocationXY()
  829. changePrePDFView()
  830. } else if textField == currentPageIndexTextF {
  831. pdfView.go(to: originalDocument?.page(at: UInt(Int(currentPageIndexTextF.stringValue)! - 1))!)
  832. } else if textField == pageRangeComboBox {
  833. getDrawPages()
  834. changePrePDFView()
  835. }
  836. updateCropViewLabel()
  837. }
  838. func changePrePDFView() {
  839. previewDocument = CPDFDocument()
  840. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  841. var page: CPDFPage!
  842. if self.selectPages.contains(i) {
  843. if self.marginsButton.state == .on {
  844. page = CPDFPage()
  845. let orgPage: CPDFPage = (self.originalDocument?.page(at: i))!
  846. let rect = KMCropTools.getPageForegroundBox(orgPage)
  847. let pageBounds = orgPage.bounds(for: .mediaBox)
  848. page?.setBounds(pageBounds, for: .mediaBox)
  849. page?.cropDrawingPage = orgPage
  850. page?.cropRect = rect
  851. } else {
  852. page = CPDFPage()
  853. let orgPage = self.originalDocument?.page(at: i)!
  854. if self.isChangePageSize {
  855. page?.setBounds(self.drawPageRect, for: .mediaBox)
  856. page?.isChangePageSize = true
  857. } else {
  858. let pageBounds = orgPage?.bounds(for: .mediaBox)
  859. page.setBounds(pageBounds ?? .zero, for: .mediaBox)
  860. }
  861. page?.cropRect = self.cropRect
  862. page?.cropDrawingPage = orgPage
  863. page?.cropOffsetX = CGFloat(self.xText.floatValue)
  864. page?.cropOffsetY = CGFloat(self.yText.floatValue)
  865. }
  866. } else {
  867. page = self.originalDocument?.page(at: i)!
  868. }
  869. self.previewDocument?.insertPageObject(page, at: self.previewDocument?.pageCount ?? 0)
  870. }
  871. self.pdfView.document = self.previewDocument
  872. self.pdfView.needsDisplay = true
  873. let tPage = self.previewDocument?.page(at: 0)!
  874. self.pdfView.go(to: tPage)
  875. self.currentPageIndexTextF.stringValue = "\(1)"
  876. }
  877. func updateSizeView() {
  878. let page = self.originalDocument?.page(at: UInt(self.currentPage))!
  879. let pageBounds = page?.bounds(for: .mediaBox)
  880. self.cropEdgeInsets = NSEdgeInsets(top: (pageBounds?.size.height ?? 0) - self.cropRect.maxY,
  881. left: self.cropRect.origin.x,
  882. bottom: self.cropRect.origin.y,
  883. right: (pageBounds?.size.width ?? 0) - self.cropRect.maxX)
  884. let unitScale = self.unitConversion()
  885. self.topStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.top / unitScale))
  886. self.leftStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.left / unitScale))
  887. self.bottomStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.bottom / unitScale))
  888. self.rightStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.right / unitScale))
  889. self.topDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.top / unitScale)))"
  890. self.leftDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.left / unitScale)))"
  891. self.bottomDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.bottom / unitScale)))"
  892. self.rightDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.right / unitScale)))"
  893. self.leftStepper.maxValue = (pageBounds?.size.width ?? 0) / unitScale
  894. self.rightStepper.maxValue = (pageBounds?.size.width ?? 0) / unitScale
  895. self.topStepper.maxValue = (pageBounds?.size.height ?? 0) / unitScale
  896. self.bottomStepper.maxValue = (pageBounds?.size.height ?? 0) / unitScale
  897. }
  898. func localizedString() {
  899. self.unitLabel.stringValue = NSLocalizedString("Unit:", comment: "")
  900. self.marginLabel.stringValue = NSLocalizedString("Margin Controls", comment: "")
  901. self.applyLabel.stringValue = NSLocalizedString("Apply to:", comment: "")
  902. self.pageSizeLabel.stringValue = NSLocalizedString("Change Page Size", comment: "")
  903. self.pageRangeLabel.stringValue = NSLocalizedString("Page Range:", comment: "")
  904. self.widthLabel.stringValue = NSLocalizedString("Width:", comment: "")
  905. self.heightLabel.stringValue = NSLocalizedString("Height:", comment: "")
  906. self.xLabel.stringValue = NSLocalizedString("XOffset:", comment: "")
  907. self.yLabel.stringValue = NSLocalizedString("YOffset:", comment: "")
  908. self.topLabel.stringValue = NSLocalizedString("Top:", comment: "")
  909. self.bottomLabel.stringValue = NSLocalizedString("Bottom:", comment: "")
  910. self.leftLabel.stringValue = NSLocalizedString("Left:", comment: "")
  911. self.rightLabel.stringValue = NSLocalizedString("Right:", comment: "")
  912. self.zeroButton.title = NSLocalizedString("Set To Zero", comment: "")
  913. self.marginsButton.title = NSLocalizedString("Remove White Margins", comment: "")
  914. self.proportButton.title = NSLocalizedString("Constrain Proport", comment: "")
  915. self.revertButton.title = NSLocalizedString("Revert To Selection", comment: "")
  916. self.fixedSizeButton.title = NSLocalizedString("Fixed Sizes", comment: "")
  917. self.customButton.title = NSLocalizedString("Custom", comment: "")
  918. self.centerButton.title = NSLocalizedString("Center", comment: "")
  919. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  920. self.saveButton.title = NSLocalizedString("Done", comment: "")
  921. self.printButton.title = NSLocalizedString("Print", comment: "")
  922. self.pageRangeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""),
  923. NSLocalizedString("Odd Pages Only", comment: ""),
  924. NSLocalizedString("Even Pages Only", comment: ""),
  925. NSLocalizedString("e.g. 1,3-5,10", comment: "")])
  926. self.pageRangeComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  927. if (.AllPages == self.pageType) {
  928. self.pageRangeComboBox.selectItem(at: 0)
  929. self.pageRangeComboBox.isEditable = false
  930. } else if(.OnlyOdd == self.pageType){
  931. self.pageRangeComboBox.selectItem(at: 1)
  932. self.pageRangeComboBox.isEditable = false
  933. } else if(.AllPages == self.pageType){
  934. self.pageRangeComboBox.selectItem(at: 2)
  935. self.pageRangeComboBox.isEditable = false
  936. } else {
  937. self.pageRangeComboBox.selectItem(at: 3)
  938. self.pageRangeComboBox.isEditable = true
  939. self.pageRangeComboBox.stringValue = self.pagesString
  940. }
  941. self.unitPopUpButton.removeAllItems()
  942. let items = [NSLocalizedString("cm", comment: ""),
  943. NSLocalizedString("mm", comment: ""),
  944. NSLocalizedString("in", comment: "")]
  945. self.unitPopUpButton.addItems(withTitles: items)
  946. self.unitPopUpButton.selectItem(at: 0)
  947. self.xText.stringValue = "0" + self.unitPopUpButton.title
  948. self.yText.stringValue = "0" + self.unitPopUpButton.title
  949. self.widthText.stringValue = "0" + self.unitPopUpButton.title
  950. self.heightText.stringValue = "0" + self.unitPopUpButton.title
  951. self.applyPopUpButton.removeAllItems()
  952. let applyItems = [NSLocalizedString("CropBox", comment: ""),
  953. NSLocalizedString("ArtBox", comment: ""),
  954. NSLocalizedString("TrimBox", comment: ""),
  955. NSLocalizedString("BleedBox", comment: "")]
  956. self.applyPopUpButton.addItems(withTitles: applyItems)
  957. self.applyPopUpButton.selectItem(at: 0)
  958. let paperArray = KMPageSizeTool.paperSize()
  959. var pageSizeItems = [Any]()
  960. pageSizeItems.append(contentsOf: paperArray)
  961. self.pageSizePopUpButton.removeAllItems()
  962. self.pageSizePopUpButton.addItems(withTitles: pageSizeItems as! [String])
  963. self.pageSizePopUpButton.insertItem(withTitle: NSLocalizedString("None", comment: ""), at: 0)
  964. self.pageSizePopUpButton.selectItem(at: 0)
  965. }
  966. func kCropPrintFolderPath() -> String {
  967. let rootPath: String = KMTools.getTempRootPath() ?? ""
  968. return rootPath.stringByAppendingPathComponent("Crop")
  969. }
  970. }