KMPDFCropWindowController.swift 49 KB

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