KMPDFCropWindowController.swift 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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 = NSRect.zero
  23. var defaultCropRect: CGRect = NSRect.zero
  24. var selectPages = NSMutableArray()
  25. var cropEdgeInsets: NSEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0)
  26. var drawPageRect: CGRect = NSRect.zero
  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 fileAttribute: KMFileAttribute!
  75. var isChangePageSize = false
  76. var cropWindowCloseBlock: cropWindowCloseBlock?
  77. convenience init(document:CPDFDocument, pwd:String, currentPage:Int, cropSize:CGRect, selectPageString:String) {
  78. self.init(windowNibName: "KMPDFCropWindowController")
  79. self.currentPage = currentPage
  80. self.defaultCropRect = cropRect
  81. if selectPageString.count > 0 {
  82. self.pagesString = String(format: "%ld", (Int(selectPageString) ?? 0) + 1)
  83. self.pageType = .PagesString
  84. let nu: NSNumber = Int(selectPageString)! as NSNumber
  85. self.selectPages.add(nu)
  86. }else {
  87. self.pageType = .AllPages
  88. }
  89. self.password = pwd
  90. self.originalDocument = document
  91. }
  92. convenience init(documentPath: URL, selectPages: String){
  93. self.init(windowNibName: "KMPDFCropWindowController")
  94. if selectPages.count > 0 {
  95. self.pagesString = String(format: "%ld", (Int(selectPages) ?? 0) + 1)
  96. self.pageType = .PagesString
  97. let nu: NSNumber = Int(selectPages)! as NSNumber
  98. self.selectPages.add(nu)
  99. }else {
  100. self.pageType = .AllPages
  101. }
  102. self.originalDocument = CPDFDocument(url: documentPath)
  103. }
  104. override func windowDidLoad() {
  105. super.windowDidLoad()
  106. self.cropRect = self.defaultCropRect
  107. fileAttribute = KMFileAttribute()
  108. fileAttribute.filePath = self.originalDocument?.documentURL.path ?? ""
  109. self.pdfView.wantsLayer = true
  110. self.pdfView.layer?.borderColor = NSColor.black.cgColor
  111. self.pdfView.layer?.borderWidth = 2
  112. self.pdfView.backgroundColor = NSColor.white
  113. self.pdfView.autoScales = true
  114. self.pdfView.delegate = self
  115. self.localizedString()
  116. self.fixedSizeButton.state = .on
  117. self.customButton.state = .off
  118. self.centerButton.isEnabled = false
  119. self.xText.isEnabled = false
  120. self.yText.isEnabled = false
  121. self.widthText.isEnabled = false
  122. self.heightText.isEnabled = false
  123. self.centerButton.state = .on
  124. let numberFormatter = self.currentPageIndexTextF.formatter as! NumberFormatter
  125. numberFormatter.maximum = NSNumber(value: self.originalDocument?.pageCount ?? 0)
  126. self.totalPageCountLabel.stringValue = String(format: "%ld", self.originalDocument?.pageCount ?? 0)
  127. let currentPageIndex = self.pdfView.currentPageIndex//self.pdfView.document!.index(for: self.pdfView.currentPage()!)
  128. self.currentPageIndexTextF.stringValue = "\(currentPageIndex + 1)"
  129. self.updateSizeView()
  130. self.topStepper.minValue = 0
  131. self.leftStepper.minValue = 0
  132. self.rightStepper.minValue = 0
  133. self.bottomStepper.minValue = 0
  134. self.changePrePDFView()
  135. self.reloadPDFWithIndex()
  136. self.updateCropViewLabel()
  137. let tPage = self.previewDocument?.page(at: UInt(self.currentPage))
  138. self.pdfView.go(to: tPage)
  139. }
  140. func windowShouldClose(_ sender: NSWindow) -> Bool {
  141. close()
  142. return true
  143. }
  144. override func close() {
  145. if (self.cropWindowCloseBlock != nil) {
  146. cropWindowCloseBlock!(originalDocument!)
  147. }
  148. if ((self.window?.isSheet) != nil) {
  149. // self.window?.sheetParent?.endSheet(self.window!)
  150. } else {
  151. super.close()
  152. }
  153. }
  154. func updateCropViewLabel() {
  155. let unitScanl: CGFloat = self.unitConversion()
  156. var mat: String = NSLocalizedString("Cropped page size:", comment: "")
  157. if (0 == self.applyPopUpButton.indexOfSelectedItem) {
  158. mat = NSLocalizedString("Cropped page size:", comment: "")
  159. } else if (1 == self.applyPopUpButton.indexOfSelectedItem) {
  160. mat = NSLocalizedString("ArtBox size:", comment: "")
  161. } else if (2 == self.applyPopUpButton.indexOfSelectedItem) {
  162. mat = NSLocalizedString("TrimBox size:", comment: "")
  163. } else {
  164. mat = NSLocalizedString("BleedBox size:", comment: "")
  165. }
  166. if (0 == self.pageSizePopUpButton.indexOfSelectedItem && self.fixedSizeButton.state == .on) {
  167. if (.on == self.marginsButton.state) {
  168. let orgPage: CPDFPage = (self.originalDocument?.page(at: UInt(self.currentPage)))!
  169. let rect: NSRect = KMCropTools.getPageForegroundBox(orgPage)
  170. self.cropLabel.stringValue = String(format: "%@%@ * %@%@", mat, self.formatFloat(Float(rect.size.width/unitScanl)), self.formatFloat(Float(rect.size.height/unitScanl)), self.unitPopUpButton.title)
  171. } else {
  172. 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)
  173. }
  174. } else {
  175. self.cropLabel.stringValue = String(format: "%@%@ * %@%@", KMLocalizedString("Expanded page size:"), self.formatFloat(Float(self.drawPageRect.size.width/unitScanl)), self.formatFloat(Float(self.drawPageRect.size.height/unitScanl)), self.unitPopUpButton.title)
  176. }
  177. }
  178. func formatFloat(_ f: Float) -> String {
  179. if f.truncatingRemainder(dividingBy: 1) == 0 { // If there is one decimal place
  180. return String(format: "%.0f", f)
  181. } else if (f * 10).truncatingRemainder(dividingBy: 1) == 0 { // If there are two decimal places
  182. return String(format: "%.1f", f)
  183. } else {
  184. return String(format: "%.2f", f)
  185. }
  186. }
  187. func handlerReDraw() {
  188. if drawPageRect.size.width < 0 || drawPageRect.size.height < 0 {
  189. NSSound.beep()
  190. return
  191. } else {
  192. for i in 0..<(originalDocument?.pageCount ?? 0) {
  193. if selectPages.contains(i) {
  194. let page = KMCropPDFPage()
  195. page.setBounds(NSRect(x: 0, y: 0, width: drawPageRect.size.width, height: drawPageRect.size.height), for: .mediaBox)
  196. let cropPage = originalDocument?.page(at: i)
  197. page.cropDrawingPage = cropPage
  198. originalDocument?.insertPageObject(page, at: i)
  199. originalDocument?.removePage(at: i)
  200. }
  201. }
  202. }
  203. }
  204. func updateView() {
  205. let page = originalDocument?.page(at: UInt(currentPage))
  206. let pageBounds = page?.bounds(for: .mediaBox)
  207. 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)
  208. cropRect = tCropRect
  209. changePrePDFView()
  210. }
  211. func unitConversion() -> CGFloat {
  212. var unitScanl: CGFloat = 1.0
  213. if (0 == self.unitPopUpButton.indexOfSelectedItem) {
  214. unitScanl = 595.0/21.0
  215. } else if (1 == self.unitPopUpButton.indexOfSelectedItem) {
  216. unitScanl = 595.0/210.0
  217. } else {
  218. unitScanl = (595.0/21.0) * 2.54
  219. }
  220. return unitScanl
  221. }
  222. func getDrawPages() {
  223. let pages = NSMutableArray()
  224. if 0 == self.pageRangeComboBox.indexOfSelectedItem {
  225. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  226. pages.add(i)
  227. }
  228. } else if 1 == self.pageRangeComboBox.indexOfSelectedItem {
  229. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  230. if i % 2 == 0 {
  231. pages.add(i)
  232. }
  233. }
  234. } else if 2 == self.pageRangeComboBox.indexOfSelectedItem {
  235. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  236. if i % 2 != 0 {
  237. pages.add(i)
  238. }
  239. }
  240. } else {
  241. fileAttribute.bAllPage = false
  242. fileAttribute.pagesType = .custom
  243. fileAttribute.pagesString = self.pageRangeComboBox.stringValue
  244. for num in fileAttribute.fetchSelectPages() {
  245. pages.add(num - 1)
  246. }
  247. }
  248. if pages.count < 1 {
  249. let alert = NSAlert()
  250. alert.alertStyle = .critical
  251. alert.messageText = String(format: "%@%@", self.originalDocument?.documentURL.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  252. alert.runModal()
  253. } else {
  254. self.selectPages = pages
  255. }
  256. }
  257. func reloadPDFWithIndex() {
  258. self.pdfView.documentView().enclosingScrollView?.hasVerticalScroller = false
  259. self.pdfView.documentView().enclosingScrollView?.hasHorizontalScroller = false
  260. let pageCount = self.previewDocument?.pageCount
  261. let currentPageIndex = self.previewDocument?.index(for: self.pdfView.currentPage())
  262. self.currentPageIndexTextF.stringValue = "\((currentPageIndex ?? 0)+1)"
  263. self.totalPageCountLabel.stringValue = String(format:NSLocalizedString(" / %ld", comment: ""), pageCount ?? 0)
  264. }
  265. func updateLocationXY() {
  266. guard centerButton.state == .on else { return }
  267. let unitScanl = unitConversion()
  268. let page = originalDocument?.page(at: UInt(currentPage))
  269. let pageBounds = page?.bounds(for: .mediaBox)
  270. let x = (drawPageRect.size.width - (pageBounds?.size.width ?? 0)) / (2 * unitScanl)
  271. let y = (drawPageRect.size.height - (pageBounds?.size.height ?? 0)) / (2 * unitScanl)
  272. xText.stringValue = formatFloat(Float(x))
  273. yText.stringValue = formatFloat(Float(y))
  274. }
  275. func handerReDraw() {
  276. if drawPageRect.size.width < 0 || drawPageRect.size.height < 0 {
  277. NSSound.beep()
  278. return
  279. } else {
  280. for i in 0..<(originalDocument?.pageCount ?? 0) {
  281. if selectPages.contains(i) {
  282. let newPage = KMCropPDFPage()
  283. newPage.setBounds(NSMakeRect(0, 0, drawPageRect.size.width, drawPageRect.size.height), for: .mediaBox)
  284. let cropPage = self.originalDocument?.page(at: i)
  285. newPage.cropDrawingPage = cropPage
  286. self.originalDocument?.insertPageObject(newPage, at: i)
  287. self.originalDocument?.removePage(at: i)
  288. }
  289. }
  290. }
  291. }
  292. @IBAction func buttonItemClick_Proport(_ sender: NSButton) {
  293. var mint = min(cropEdgeInsets.bottom, cropEdgeInsets.top)
  294. mint = min(mint, cropEdgeInsets.left)
  295. mint = min(mint, cropEdgeInsets.right)
  296. cropEdgeInsets = NSEdgeInsets(top: mint, left: mint, bottom: mint, right: mint)
  297. let unitScanl = unitConversion()
  298. rightStepper.stringValue = formatFloat(Float(cropEdgeInsets.right/unitScanl))
  299. topDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.top/unitScanl)))"
  300. leftDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.left/unitScanl)))"
  301. bottomDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.bottom/unitScanl)))"
  302. rightDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.right/unitScanl)))"
  303. if proportButton.state == .on {
  304. marginsButton.state = .off
  305. }
  306. updateView()
  307. updateCropViewLabel()
  308. }
  309. @IBAction func buttonClicked_Margin(_ sender: NSButton) {
  310. if marginsButton.state == .on {
  311. proportButton.state = .off
  312. topDistanceText.isEnabled = false
  313. bottomDistanceText.isEnabled = false
  314. leftDistanceText.isEnabled = false
  315. rightDistanceText.isEnabled = false
  316. topStepper.isEnabled = false
  317. bottomStepper.isEnabled = false
  318. leftStepper.isEnabled = false
  319. rightStepper.isEnabled = false
  320. previewDocument = CPDFDocument()
  321. for i in 0..<(originalDocument?.pageCount ?? 0) {
  322. var page: CPDFPage? = nil
  323. if selectPages.contains(i) {
  324. page = CPDFPage()
  325. let orgPage = originalDocument?.page(at: i)
  326. let rect = KMCropTools.getPageForegroundBox(orgPage ?? CPDFPage())
  327. let pageBounds = orgPage?.bounds(for: .mediaBox) ?? NSRect.zero
  328. page?.setBounds(pageBounds, for: .mediaBox)
  329. page?.cropDrawingPage = orgPage
  330. page?.cropRect = rect
  331. } else {
  332. page = originalDocument?.page(at: i)
  333. }
  334. previewDocument?.insertPageObject(page, at: previewDocument?.pageCount ?? 0)
  335. }
  336. pdfView.document = previewDocument
  337. pdfView.needsDisplay = true
  338. } else {
  339. topDistanceText.isEnabled = true
  340. bottomDistanceText.isEnabled = true
  341. leftDistanceText.isEnabled = true
  342. rightDistanceText.isEnabled = true
  343. topStepper.isEnabled = true
  344. bottomStepper.isEnabled = true
  345. leftStepper.isEnabled = true
  346. rightStepper.isEnabled = true
  347. cropEdgeInsets = NSEdgeInsets(top: CGFloat(topDistanceText.floatValue), left: CGFloat(leftDistanceText.floatValue), bottom: CGFloat(bottomDistanceText.floatValue), right: CGFloat(rightDistanceText.floatValue))
  348. let unitScanl = unitConversion()
  349. rightStepper.stringValue = formatFloat(Float(cropEdgeInsets.right/unitScanl))
  350. topDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.top/unitScanl)))"
  351. leftDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.left/unitScanl)))"
  352. bottomDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.bottom/unitScanl)))"
  353. rightDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.right/unitScanl)))"
  354. updateView()
  355. }
  356. updateCropViewLabel()
  357. }
  358. @IBAction func buttonItemClick_Zero(_ sender: NSButton) {
  359. cropEdgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  360. let unitScanl = unitConversion()
  361. rightStepper.stringValue = formatFloat(Float(cropEdgeInsets.right/unitScanl))
  362. topDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.top/unitScanl)))"
  363. leftDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.left/unitScanl)))"
  364. bottomDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.bottom/unitScanl)))"
  365. rightDistanceText.stringValue = "\(formatFloat(Float(cropEdgeInsets.right/unitScanl)))"
  366. marginsButton.state = .off
  367. proportButton.state = .off
  368. updateView()
  369. updateCropViewLabel()
  370. }
  371. @IBAction func buttonItemClick_Revert(_ sender: NSButton) {
  372. cropRect = defaultCropRect
  373. marginsButton.state = .off
  374. proportButton.state = .off
  375. changePrePDFView()
  376. updateCropViewLabel()
  377. }
  378. @IBAction func buttonItemClick_Cancel(_ sender: NSButton) {
  379. self.km_quick_endSheet()
  380. super.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) {
  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. let doc = CPDFDocument()
  844. if self.marginsButton.state == .on {
  845. page = CPDFPage()
  846. let orgPage: CPDFPage = (self.originalDocument?.page(at: i))!
  847. let rect = KMCropTools.getPageForegroundBox(orgPage)
  848. let pageBounds = orgPage.bounds(for: .mediaBox)
  849. page?.setBounds(pageBounds, for: .mediaBox)
  850. page?.cropDrawingPage = orgPage
  851. page?.cropRect = rect
  852. } else {
  853. page = self.originalDocument?.page(at: i)!
  854. let orgPage = self.originalDocument?.page(at: i)!
  855. if self.isChangePageSize {
  856. page?.setBounds(self.drawPageRect, for: .mediaBox)
  857. page?.isChangePageSize = true
  858. } else {
  859. let pageBounds = orgPage?.bounds(for: .mediaBox)
  860. page.setBounds(pageBounds ?? .zero, for: .mediaBox)
  861. }
  862. page?.cropRect = self.cropRect
  863. page?.cropDrawingPage = orgPage
  864. page?.cropOffsetX = CGFloat(self.xText.floatValue)
  865. page?.cropOffsetY = CGFloat(self.yText.floatValue)
  866. //
  867. // let info = KMCropInfo()
  868. // info.drawPage = page
  869. // self.pdfView.drawInfo.append(info)
  870. }
  871. } else {
  872. page = self.originalDocument?.page(at: i)!
  873. }
  874. self.previewDocument?.insertPageObject(page, at: self.previewDocument?.pageCount ?? 0)
  875. }
  876. self.pdfView.document = self.previewDocument
  877. self.pdfView.layoutDocumentView()
  878. self.pdfView.needsDisplay = true
  879. let tPage = self.previewDocument?.page(at: 0)!
  880. self.pdfView.go(to: tPage)
  881. self.currentPageIndexTextF.stringValue = "\(1)"
  882. }
  883. func updateSizeView() {
  884. let page = self.originalDocument?.page(at: UInt(self.currentPage))!
  885. let pageBounds = page?.bounds(for: .mediaBox)
  886. self.cropEdgeInsets = NSEdgeInsets(top: (pageBounds?.size.height ?? 0) - self.cropRect.maxY,
  887. left: self.cropRect.origin.x,
  888. bottom: self.cropRect.origin.y,
  889. right: (pageBounds?.size.width ?? 0) - self.cropRect.maxX)
  890. let unitScale = self.unitConversion()
  891. self.topStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.top / unitScale))
  892. self.leftStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.left / unitScale))
  893. self.bottomStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.bottom / unitScale))
  894. self.rightStepper.stringValue = self.formatFloat(Float(self.cropEdgeInsets.right / unitScale))
  895. self.topDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.top / unitScale)))"
  896. self.leftDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.left / unitScale)))"
  897. self.bottomDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.bottom / unitScale)))"
  898. self.rightDistanceText.stringValue = "\(self.formatFloat(Float(self.cropEdgeInsets.right / unitScale)))"
  899. self.leftStepper.maxValue = (pageBounds?.size.width ?? 0) / unitScale
  900. self.rightStepper.maxValue = (pageBounds?.size.width ?? 0) / unitScale
  901. self.topStepper.maxValue = (pageBounds?.size.height ?? 0) / unitScale
  902. self.bottomStepper.maxValue = (pageBounds?.size.height ?? 0) / unitScale
  903. }
  904. func localizedString() {
  905. self.unitLabel.stringValue = NSLocalizedString("Unit:", comment: "")
  906. self.marginLabel.stringValue = NSLocalizedString("Margin Controls", comment: "")
  907. self.applyLabel.stringValue = NSLocalizedString("Apply to:", comment: "")
  908. self.pageSizeLabel.stringValue = NSLocalizedString("Change Page Size", comment: "")
  909. self.pageRangeLabel.stringValue = NSLocalizedString("Page Range:", comment: "")
  910. self.widthLabel.stringValue = NSLocalizedString("Width:", comment: "")
  911. self.heightLabel.stringValue = NSLocalizedString("Height:", comment: "")
  912. self.xLabel.stringValue = NSLocalizedString("XOffset:", comment: "")
  913. self.yLabel.stringValue = NSLocalizedString("YOffset:", comment: "")
  914. self.topLabel.stringValue = NSLocalizedString("Top:", comment: "")
  915. self.bottomLabel.stringValue = NSLocalizedString("Bottom:", comment: "")
  916. self.leftLabel.stringValue = NSLocalizedString("Left:", comment: "")
  917. self.rightLabel.stringValue = NSLocalizedString("Right:", comment: "")
  918. self.zeroButton.title = NSLocalizedString("Set To Zero", comment: "")
  919. self.marginsButton.title = NSLocalizedString("Remove White Margins", comment: "")
  920. self.proportButton.title = NSLocalizedString("Constrain Proport", comment: "")
  921. self.revertButton.title = NSLocalizedString("Revert To Selection", comment: "")
  922. self.fixedSizeButton.title = NSLocalizedString("Fixed Sizes", comment: "")
  923. self.customButton.title = NSLocalizedString("Custom", comment: "")
  924. self.centerButton.title = NSLocalizedString("Center", comment: "")
  925. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  926. self.saveButton.title = NSLocalizedString("Done", comment: "")
  927. self.printButton.title = NSLocalizedString("Print", comment: "")
  928. self.pageRangeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""),
  929. NSLocalizedString("Odd Pages Only", comment: ""),
  930. NSLocalizedString("Even Pages Only", comment: ""),
  931. NSLocalizedString("e.g. 1,3-5,10", comment: "")])
  932. self.pageRangeComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  933. if (.AllPages == self.pageType) {
  934. self.pageRangeComboBox.selectItem(at: 0)
  935. self.pageRangeComboBox.isEditable = false
  936. } else if(.OnlyOdd == self.pageType){
  937. self.pageRangeComboBox.selectItem(at: 1)
  938. self.pageRangeComboBox.isEditable = false
  939. } else if(.AllPages == self.pageType){
  940. self.pageRangeComboBox.selectItem(at: 2)
  941. self.pageRangeComboBox.isEditable = false
  942. } else {
  943. self.pageRangeComboBox.selectItem(at: 3)
  944. self.pageRangeComboBox.isEditable = true
  945. self.pageRangeComboBox.stringValue = self.pagesString
  946. }
  947. self.unitPopUpButton.removeAllItems()
  948. let items = [NSLocalizedString("cm", comment: ""),
  949. NSLocalizedString("mm", comment: ""),
  950. NSLocalizedString("in", comment: "")]
  951. self.unitPopUpButton.addItems(withTitles: items)
  952. self.unitPopUpButton.selectItem(at: 0)
  953. self.xText.stringValue = "0" + self.unitPopUpButton.title
  954. self.yText.stringValue = "0" + self.unitPopUpButton.title
  955. self.widthText.stringValue = "0" + self.unitPopUpButton.title
  956. self.heightText.stringValue = "0" + self.unitPopUpButton.title
  957. self.applyPopUpButton.removeAllItems()
  958. let applyItems = [NSLocalizedString("CropBox", comment: ""),
  959. NSLocalizedString("ArtBox", comment: ""),
  960. NSLocalizedString("TrimBox", comment: ""),
  961. NSLocalizedString("BleedBox", comment: "")]
  962. self.applyPopUpButton.addItems(withTitles: applyItems)
  963. self.applyPopUpButton.selectItem(at: 0)
  964. let paperArray = KMPageSizeTool.paperSize()
  965. var pageSizeItems = [Any]()
  966. pageSizeItems.append(contentsOf: paperArray)
  967. self.pageSizePopUpButton.removeAllItems()
  968. self.pageSizePopUpButton.addItems(withTitles: pageSizeItems as! [String])
  969. self.pageSizePopUpButton.insertItem(withTitle: NSLocalizedString("None", comment: ""), at: 0)
  970. self.pageSizePopUpButton.selectItem(at: 0)
  971. }
  972. func kCropPrintFolderPath() -> String {
  973. let rootPath = KMDataManager.fetchAppSupportOfBundleIdentifierDirectory().path
  974. return rootPath.stringByAppendingPathComponent("Crop")
  975. }
  976. }