KMPDFCropWindowController.swift 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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 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(self.pagesString)! 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(self.pagesString)! 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:", nil), 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.", nil))
  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. close()
  380. }
  381. @IBAction func buttonItemClick_Save(_ sender: NSButton) {
  382. self.window?.makeFirstResponder(nil)
  383. self.getDrawPages()
  384. if self.selectPages.count < 1 {
  385. let alert = NSAlert()
  386. alert.alertStyle = .critical
  387. alert.messageText = "\(self.originalDocument?.documentURL.path.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  388. alert.runModal()
  389. return
  390. }
  391. if self.pageSizePopUpButton.indexOfSelectedItem == 0 && self.fixedSizeButton.state == .on {
  392. var box = CPDFDisplayBox.mediaBox
  393. if self.applyPopUpButton.indexOfSelectedItem == 0 {
  394. box = .cropBox
  395. } else if self.applyPopUpButton.indexOfSelectedItem == 1 {
  396. box = .artBox
  397. } else if self.applyPopUpButton.indexOfSelectedItem == 2 {
  398. box = .trimBox
  399. } else {
  400. box = .bleedBox
  401. }
  402. let page = self.originalDocument?.page(at: UInt(currentPage))
  403. let pageBounds = page?.bounds(for: .mediaBox)
  404. if self.marginsButton.state == .on {
  405. for i in 0..<(selectPages.count) {
  406. let num: NSNumber = selectPages[i] as! NSNumber
  407. let page = self.originalDocument?.page(at: UInt((num.intValue)))
  408. let rect = KMCropTools.getPageForegroundBox(page ?? CPDFPage())
  409. page?.setBounds(rect, for: .cropBox)
  410. }
  411. } else {
  412. let tCropRect = NSMakeRect(
  413. self.cropEdgeInsets.left,
  414. self.cropEdgeInsets.bottom,
  415. pageBounds!.size.width - self.cropEdgeInsets.left - self.cropEdgeInsets.right,
  416. pageBounds!.size.height - self.cropEdgeInsets.top - self.cropEdgeInsets.bottom
  417. )
  418. self.selectPages.forEach { num in
  419. let page = self.originalDocument?.page(at: UInt((num as! NSNumber).intValue))
  420. page?.setBounds(tCropRect, for: box)
  421. if self.applyPopUpButton.indexOfSelectedItem != 0 {
  422. page?.setBounds(tCropRect, for: .cropBox)
  423. }
  424. }
  425. }
  426. } else {
  427. var isAllow = true
  428. for num in self.selectPages {
  429. let page = self.originalDocument?.page(at: UInt((num as! NSNumber).intValue))
  430. let pageBounds = page?.bounds(for: .mediaBox)
  431. if self.drawPageRect.size.width < pageBounds!.size.width || self.drawPageRect.size.height < pageBounds!.size.height {
  432. isAllow = false
  433. break
  434. }
  435. }
  436. if !isAllow {
  437. NSSound.beep()
  438. return
  439. }
  440. self.handerReDraw()
  441. }
  442. self.close()
  443. }
  444. @IBAction func buttonItemClick_Print(_ sender: NSButton) {
  445. self.window?.makeFirstResponder(nil)
  446. self.getDrawPages()
  447. if self.selectPages.count < 1 {
  448. let alert = NSAlert()
  449. alert.alertStyle = .critical
  450. 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: ""))"
  451. alert.runModal()
  452. return
  453. }
  454. var fileName: String = ""
  455. if FileManager.default.fileExists(atPath: kCropPrintFolderPath()) == false {
  456. let success: ()? = try?FileManager.default.createDirectory(atPath: kCropPrintFolderPath(), withIntermediateDirectories: false)
  457. if success == nil {
  458. return
  459. }
  460. }
  461. fileName = "cropPDF.pdf"
  462. fileName = kCropPrintFolderPath().stringByAppendingPathComponent(fileName)
  463. let tDocuemnt = CPDFDocument()
  464. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  465. if let page = self.originalDocument?.page(at: i)?.copy() {
  466. tDocuemnt?.insertPageObject(page as? CPDFPage, at: tDocuemnt?.pageCount ?? 0)
  467. }
  468. }
  469. if 0 == self.pageSizePopUpButton.indexOfSelectedItem && self.fixedSizeButton.state == .on {
  470. var box: CPDFDisplayBox = .mediaBox
  471. if 0 == self.applyPopUpButton.indexOfSelectedItem {
  472. box = .cropBox
  473. } else if 1 == self.applyPopUpButton.indexOfSelectedItem {
  474. box = .artBox
  475. } else if 2 == self.applyPopUpButton.indexOfSelectedItem {
  476. box = .trimBox
  477. } else {
  478. box = .bleedBox
  479. }
  480. if let page = self.originalDocument?.page(at: UInt(self.currentPage)) {
  481. let pageBounds = page.bounds(for: .mediaBox)
  482. if self.marginsButton.state == .on {
  483. for i in 0..<self.selectPages.count {
  484. let num: NSNumber = self.selectPages[i] as! NSNumber
  485. if let page = tDocuemnt?.page(at: UInt(num.intValue)) {
  486. let rect = KMCropTools.getPageForegroundBox(page)
  487. page.setBounds(rect, for: .cropBox)
  488. }
  489. }
  490. } else {
  491. 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)
  492. for i in 0..<self.selectPages.count {
  493. let num: NSNumber = self.selectPages[i] as! NSNumber
  494. if let page = tDocuemnt?.page(at: UInt(num.intValue)) {
  495. page.setBounds(tCropRect, for: box)
  496. if 0 != self.applyPopUpButton.indexOfSelectedItem {
  497. page.setBounds(tCropRect, for: .cropBox)
  498. }
  499. }
  500. }
  501. }
  502. }
  503. } else {
  504. var isAllow = true
  505. for num in self.selectPages {
  506. if let page = self.originalDocument?.page(at: UInt((num as! NSNumber).intValue)) {
  507. let pageBounds = page.bounds(for: .mediaBox)
  508. if self.drawPageRect.size.width < pageBounds.size.width || self.drawPageRect.size.height < pageBounds.size.height {
  509. isAllow = false
  510. break
  511. }
  512. }
  513. }
  514. if !isAllow {
  515. NSSound.beep()
  516. return
  517. }
  518. if self.drawPageRect.size.width < 0 || self.drawPageRect.size.height < 0 {
  519. NSSound.beep()
  520. return
  521. } else {
  522. for i in 0..<(tDocuemnt?.pageCount ?? 0) {
  523. let page: KMCropPDFPage?
  524. if self.selectPages.contains(NSNumber(value: i)) {
  525. page = KMCropPDFPage()
  526. page?.setBounds(NSRect(x: 0, y: 0, width: self.drawPageRect.size.width, height: self.drawPageRect.size.height), for: .mediaBox)
  527. if let cropPage = tDocuemnt?.page(at: i) {
  528. page?.cropDrawingPage = cropPage
  529. }
  530. if let thePage = page {
  531. tDocuemnt?.insertPageObject(thePage, at: i)
  532. tDocuemnt?.removePage(at: i+1)
  533. }
  534. }
  535. }
  536. }
  537. }
  538. guard let isSuc = tDocuemnt?.write(to: URL(fileURLWithPath: fileName )) else { return }
  539. if isSuc {
  540. let pdf = PDFDocument(url: URL(fileURLWithPath: fileName ))
  541. let printInfo = NSPrintInfo.shared
  542. var printOperation: NSPrintOperation?
  543. printOperation = pdf?.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)
  544. printOperation?.runModal(for: self.window!, delegate: self, didRun: nil, contextInfo: nil)
  545. }
  546. }
  547. @IBAction func buttonItemClick_FixedSize(_ sender: NSButton) {
  548. self.fixedSizeButton.state = .on
  549. self.customButton.state = .off
  550. self.pageSizePopUpButton.isEnabled = true
  551. self.widthText.isEnabled = false
  552. self.heightText.isEnabled = false
  553. if self.pageSizePopUpButton.indexOfSelectedItem != 0 {
  554. self.centerButton.isEnabled = true
  555. self.xText.isEnabled = true
  556. self.yText.isEnabled = true
  557. self.xText.stringValue = "0"
  558. self.yText.stringValue = "0"
  559. self.isChangePageSize = false
  560. self.topDistanceText.isEnabled = false
  561. self.bottomDistanceText.isEnabled = false
  562. self.leftDistanceText.isEnabled = false
  563. self.rightDistanceText.isEnabled = false
  564. self.topStepper.isEnabled = false
  565. self.bottomStepper.isEnabled = false
  566. self.leftStepper.isEnabled = false
  567. self.rightStepper.isEnabled = false
  568. self.marginsButton.isEnabled = false
  569. self.zeroButton.isEnabled = false
  570. self.revertButton.isEnabled = false
  571. self.proportButton.isEnabled = false
  572. self.applyPopUpButton.isEnabled = false
  573. } else {
  574. self.isChangePageSize = true
  575. self.centerButton.isEnabled = false
  576. self.xText.isEnabled = false
  577. self.yText.isEnabled = false
  578. let tSize = KMPageSizeTool.getPaperSize(paperName: self.pageSizePopUpButton.stringValue)
  579. self.drawPageRect = CGRect(x: 0, y: 0, width: tSize.width * 595.0/210, height: tSize.height * 842.0/297.0)
  580. self.topDistanceText.isEnabled = true
  581. self.bottomDistanceText.isEnabled = true
  582. self.leftDistanceText.isEnabled = true
  583. self.rightDistanceText.isEnabled = true
  584. self.topStepper.isEnabled = true
  585. self.bottomStepper.isEnabled = true
  586. self.leftStepper.isEnabled = true
  587. self.rightStepper.isEnabled = true
  588. self.marginsButton.isEnabled = true
  589. self.zeroButton.isEnabled = true
  590. self.revertButton.isEnabled = true
  591. self.proportButton.isEnabled = true
  592. self.applyPopUpButton.isEnabled = true
  593. }
  594. updateCropViewLabel()
  595. }
  596. @IBAction func buttonClicked_PageSize(_ sender: NSPopUpButton) {
  597. if (0 == sender.indexOfSelectedItem) {
  598. self.isChangePageSize = false
  599. self.centerButton.isEnabled = false
  600. self.xText.isEnabled = false
  601. self.yText.isEnabled = false
  602. self.xText.floatValue = 0
  603. self.yText.floatValue = 0
  604. self.marginsButton.state = NSControl.StateValue.off
  605. if (NSControl.StateValue.on != self.marginsButton.state) {
  606. self.topDistanceText.isEnabled = true
  607. self.bottomDistanceText.isEnabled = true
  608. self.leftDistanceText.isEnabled = true
  609. self.rightDistanceText.isEnabled = true
  610. self.topStepper.isEnabled = true
  611. self.bottomStepper.isEnabled = true
  612. self.leftStepper.isEnabled = true
  613. self.rightStepper.isEnabled = true
  614. } else {
  615. self.topDistanceText.isEnabled = false
  616. self.bottomDistanceText.isEnabled = false
  617. self.leftDistanceText.isEnabled = false
  618. self.rightDistanceText.isEnabled = false
  619. self.topStepper.isEnabled = false
  620. self.bottomStepper.isEnabled = false
  621. self.leftStepper.isEnabled = false
  622. self.rightStepper.isEnabled = false
  623. }
  624. self.marginsButton.isEnabled = true
  625. self.zeroButton.isEnabled = true
  626. self.revertButton.isEnabled = true
  627. self.proportButton.isEnabled = true
  628. self.applyPopUpButton.isEnabled = true
  629. } else {
  630. self.topDistanceText.isEnabled = false
  631. self.bottomDistanceText.isEnabled = false
  632. self.leftDistanceText.isEnabled = false
  633. self.rightDistanceText.isEnabled = false
  634. self.topStepper.isEnabled = false
  635. self.bottomStepper.isEnabled = false
  636. self.leftStepper.isEnabled = false
  637. self.rightStepper.isEnabled = false
  638. self.marginsButton.isEnabled = false
  639. self.zeroButton.isEnabled = false
  640. self.revertButton.isEnabled = false
  641. self.proportButton.isEnabled = false
  642. self.applyPopUpButton.isEnabled = false
  643. self.isChangePageSize = true
  644. let menuItem = self.pageSizePopUpButton.itemTitle(at: self.pageSizePopUpButton.indexOfSelectedItem)
  645. let tSize = KMPageSizeTool.getPaperSize(paperName: menuItem)
  646. self.drawPageRect = CGRect(x: 0, y: 0, width: tSize.width * 595.0/210, height: tSize.height * 842.0/297.0)
  647. self.centerButton.isEnabled = true
  648. self.xText.isEnabled = true
  649. self.yText.isEnabled = true
  650. }
  651. updateLocationXY()
  652. changePrePDFView()
  653. updateCropViewLabel()
  654. }
  655. @IBAction func buttonItemClick_CustomSize(_ sender: NSButton) {
  656. self.fixedSizeButton.state = NSControl.StateValue.off
  657. self.customButton.state = NSControl.StateValue.on
  658. self.widthText.isEnabled = true
  659. self.heightText.isEnabled = true
  660. self.centerButton.isEnabled = true
  661. self.xText.isEnabled = true
  662. self.yText.isEnabled = true
  663. self.topDistanceText.isEnabled = false
  664. self.bottomDistanceText.isEnabled = false
  665. self.leftDistanceText.isEnabled = false
  666. self.rightDistanceText.isEnabled = false
  667. self.topStepper.isEnabled = false
  668. self.bottomStepper.isEnabled = false
  669. self.leftStepper.isEnabled = false
  670. self.rightStepper.isEnabled = false
  671. self.marginsButton.isEnabled = false
  672. self.zeroButton.isEnabled = false
  673. self.revertButton.isEnabled = false
  674. self.proportButton.isEnabled = false
  675. self.applyPopUpButton.isEnabled = false
  676. self.pageSizePopUpButton.isEnabled = false
  677. self.isChangePageSize = true
  678. self.drawPageRect = CGRect(x: 0, y: 0, width: Int(self.widthText.floatValue), height: Int(self.heightText.floatValue))
  679. updateLocationXY()
  680. changePrePDFView()
  681. updateCropViewLabel()
  682. }
  683. @IBAction func buttonItemClick_Center(_ sender: NSButton) {
  684. if (NSControl.StateValue.on == sender.state) {
  685. updateLocationXY()
  686. } else {
  687. self.xText.stringValue = "0 \(self.unitPopUpButton.title)"
  688. self.yText.stringValue = "0 \(self.unitPopUpButton.title)"
  689. }
  690. }
  691. @IBAction func comboBoxItemClick_PageRange(_ sender: NSButton) {
  692. if (0 == self.pageRangeComboBox.indexOfSelectedItem || 1 == self.pageRangeComboBox.indexOfSelectedItem || 2 == self.pageRangeComboBox.indexOfSelectedItem) {
  693. self.pageRangeComboBox.delegate = nil
  694. self.window?.makeFirstResponder(self)
  695. self.pageRangeComboBox.isEditable = false
  696. self.getDrawPages()
  697. self.changePrePDFView()
  698. } else if (3 == self.pageRangeComboBox.indexOfSelectedItem) {
  699. self.pageRangeComboBox.delegate = self
  700. self.pageRangeComboBox.stringValue = ""
  701. self.pageRangeComboBox.isEditable = true
  702. self.window?.makeFirstResponder(self.pageRangeComboBox)
  703. }
  704. }
  705. @IBAction func buttonClicked_Unit(_ sender: NSPopUpButton) {
  706. if self.unitPopUpButton.indexOfSelectedItem == 1 {
  707. self.cropLabel.stringValue = "(\(NSLocalizedString("mm", comment: "")))"
  708. } else if self.unitPopUpButton.indexOfSelectedItem == 0 {
  709. self.cropLabel.stringValue = "(\(NSLocalizedString("cm", comment: "")))"
  710. } else {
  711. self.cropLabel.stringValue = "(\(NSLocalizedString("in", comment: "")))"
  712. }
  713. updateSizeView()
  714. updateCropViewLabel()
  715. }
  716. @IBAction func buttonClicked_AppleType(_ sender: NSPopUpButton) {
  717. if (0 == sender.indexOfSelectedItem) {
  718. self.marginsButton.isEnabled = true
  719. } else {
  720. self.marginsButton.isEnabled = false
  721. }
  722. updateCropViewLabel()
  723. }
  724. @IBAction func stepperItemClick_Distance(_ sender: NSButton) {
  725. let unitScanl: CGFloat = unitConversion()
  726. if sender == topStepper {
  727. self.topDistanceText.stringValue = String(format: "%.2f", self.topStepper.floatValue)
  728. let formatter = self.topStepper.floatValue * Float(unitScanl)
  729. self.cropEdgeInsets = NSEdgeInsetsMake(CGFloat(formatter), self.cropEdgeInsets.left, self.cropEdgeInsets.bottom, self.cropEdgeInsets.right)
  730. } else if sender == bottomStepper {
  731. self.bottomDistanceText.stringValue = String(format: "%.2f", self.bottomStepper.floatValue)
  732. let formatter = self.bottomStepper.floatValue * Float(unitScanl)
  733. self.cropEdgeInsets = NSEdgeInsetsMake(self.cropEdgeInsets.top, self.cropEdgeInsets.left, CGFloat(formatter), self.cropEdgeInsets.right)
  734. } else if sender == leftStepper {
  735. self.leftDistanceText.stringValue = String(format: "%.2f", self.leftStepper.floatValue)
  736. let formatter = self.leftStepper.floatValue * Float(unitScanl)
  737. self.cropEdgeInsets = NSEdgeInsetsMake(self.cropEdgeInsets.top, CGFloat(formatter), self.cropEdgeInsets.bottom, self.cropEdgeInsets.right)
  738. } else if sender == rightStepper {
  739. self.rightDistanceText.stringValue = String(format: "%.2f", self.rightStepper.floatValue)
  740. let formatter = self.rightStepper.floatValue * Float(unitScanl)
  741. self.cropEdgeInsets = NSEdgeInsetsMake(self.cropEdgeInsets.top, self.cropEdgeInsets.left, self.cropEdgeInsets.bottom, CGFloat(formatter))
  742. }
  743. updateView()
  744. updateCropViewLabel()
  745. }
  746. @IBAction func goPrevious(_ sender: NSButton) {
  747. if self.pdfView.canGoToPreviousPage() {
  748. self.pdfView.goToPreviousPage(nil)
  749. }
  750. self.reloadPDFWithIndex()
  751. }
  752. @IBAction func goNext(_ sender: NSButton) {
  753. if self.pdfView.canGoToNextPage() {
  754. self.pdfView.goToNextPage(nil)
  755. }
  756. self.reloadPDFWithIndex()
  757. }
  758. //MARK: NSTextFieldDelegate
  759. func controlTextDidEndEditing(_ obj: Notification) {
  760. guard let textField = obj.object as? NSTextField else { return }
  761. guard let page = originalDocument?.page(at: UInt(currentPage)) else {
  762. return
  763. }
  764. let pageBounds = page.bounds(for: .mediaBox)
  765. if textField == topDistanceText {
  766. let unitScale = unitConversion()
  767. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  768. if unitMax > pageBounds.size.height {
  769. unitMax = pageBounds.size.width
  770. textField.stringValue = "\(Float(pageBounds.size.height/unitScale))"
  771. } else if unitMax <= 0 {
  772. unitMax = 0
  773. textField.stringValue = "0"
  774. }
  775. topStepper.stringValue = textField.stringValue
  776. cropEdgeInsets = NSEdgeInsetsMake(unitMax, cropEdgeInsets.left, cropEdgeInsets.bottom, cropEdgeInsets.right)
  777. updateView()
  778. } else if textField == bottomDistanceText {
  779. let unitScale = unitConversion()
  780. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  781. if unitMax > pageBounds.size.height {
  782. unitMax = pageBounds.size.height
  783. textField.stringValue = "\(formatFloat(Float(pageBounds.size.height/unitScale)))"
  784. } else if unitMax <= 0 {
  785. unitMax = 0
  786. textField.stringValue = "0"
  787. }
  788. bottomStepper.stringValue = textField.stringValue
  789. cropEdgeInsets = NSEdgeInsetsMake(cropEdgeInsets.top, cropEdgeInsets.left, unitMax, cropEdgeInsets.right)
  790. updateView()
  791. } else if textField == leftDistanceText {
  792. let unitScale = unitConversion()
  793. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  794. if unitMax > pageBounds.size.width {
  795. unitMax = pageBounds.size.width
  796. textField.stringValue = "\(formatFloat(Float(pageBounds.size.width/unitScale)))"
  797. } else if unitMax <= 0 {
  798. unitMax = 0
  799. textField.stringValue = "0"
  800. }
  801. leftStepper.stringValue = textField.stringValue
  802. cropEdgeInsets = NSEdgeInsetsMake(cropEdgeInsets.top, unitMax, cropEdgeInsets.bottom, cropEdgeInsets.right)
  803. updateView()
  804. } else if textField == rightDistanceText {
  805. let unitScale = unitConversion()
  806. var unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  807. if unitMax > pageBounds.size.height {
  808. unitMax = pageBounds.size.width
  809. textField.stringValue = "\(formatFloat(Float(pageBounds.size.width/unitScale)))"
  810. } else if unitMax <= 0 {
  811. unitMax = 0
  812. textField.stringValue = "0"
  813. }
  814. rightStepper.stringValue = textField.stringValue
  815. cropEdgeInsets = NSEdgeInsetsMake(cropEdgeInsets.top, cropEdgeInsets.left, cropEdgeInsets.bottom, unitMax)
  816. updateView()
  817. } else if textField == widthText {
  818. let unitScale = unitConversion()
  819. let unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  820. drawPageRect = CGRect(x: 0, y: 0, width: unitMax, height: drawPageRect.size.height)
  821. updateLocationXY()
  822. changePrePDFView()
  823. } else if textField == heightText {
  824. let unitScale = unitConversion()
  825. let unitMax = CGFloat(textField.floatValue) * CGFloat(unitScale)
  826. drawPageRect = CGRect(x: 0, y: 0, width: drawPageRect.size.width, height: unitMax)
  827. updateLocationXY()
  828. changePrePDFView()
  829. } else if textField == currentPageIndexTextF {
  830. pdfView.go(to: originalDocument?.page(at: UInt(Int(currentPageIndexTextF.stringValue)! - 1))!)
  831. } else if textField == pageRangeComboBox {
  832. getDrawPages()
  833. changePrePDFView()
  834. }
  835. updateCropViewLabel()
  836. }
  837. func changePrePDFView() {
  838. previewDocument = CPDFDocument()
  839. for i in 0..<(self.originalDocument?.pageCount ?? 0) {
  840. var page: CPDFPage!
  841. if self.selectPages.contains(i) {
  842. if self.marginsButton.state == .on {
  843. page = CPDFPage()
  844. let orgPage: CPDFPage = (self.originalDocument?.page(at: i))!
  845. let rect = KMCropTools.getPageForegroundBox(orgPage)
  846. let pageBounds = orgPage.bounds(for: .mediaBox)
  847. page?.setBounds(pageBounds, for: .mediaBox)
  848. page?.cropDrawingPage = orgPage
  849. page?.cropRect = rect
  850. } else {
  851. page = CPDFPage()
  852. let orgPage = self.originalDocument?.page(at: i)!
  853. if self.isChangePageSize {
  854. page?.setBounds(self.drawPageRect, for: .mediaBox)
  855. page?.isChangePageSize = true
  856. } else {
  857. let pageBounds = orgPage?.bounds(for: .mediaBox)
  858. page.setBounds(pageBounds ?? .zero, for: .mediaBox)
  859. }
  860. page?.cropRect = self.cropRect
  861. page?.cropDrawingPage = orgPage
  862. page?.cropOffsetX = CGFloat(self.xText.floatValue)
  863. page?.cropOffsetY = CGFloat(self.yText.floatValue)
  864. }
  865. } else {
  866. page = self.originalDocument?.page(at: i)!
  867. }
  868. self.previewDocument?.insertPageObject(page, at: self.previewDocument?.pageCount ?? 0)
  869. }
  870. self.pdfView.document = self.previewDocument
  871. self.pdfView.layoutDocumentView()
  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. }