KMEditPDfHanddler.swift 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. //
  2. // KMEditPDfHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/16.
  6. //
  7. import Cocoa
  8. // EditPDF处理对象
  9. class KMEditPDfHanddler: NSObject {
  10. weak var viewC: KMMainViewController?
  11. weak var listView: CPDFListView? {
  12. get {
  13. return self.viewC?.listView
  14. }
  15. }
  16. var annotationType: CAnnotationType {
  17. get {
  18. return self.listView?.annotationType ?? .unkown
  19. }
  20. }
  21. weak var rightViewC: KMRightSideViewController? {
  22. get {
  23. return self.viewC?.rightSideViewController
  24. }
  25. }
  26. var subViewType: RightSubViewType {
  27. get {
  28. return self.rightViewC?.subViewType ?? .None
  29. }
  30. }
  31. // var toolMode: CToolMode {
  32. // get {
  33. // return
  34. // }
  35. // }
  36. var isEditImage: Bool {
  37. get {
  38. return self.listView?.isEditImage ?? false
  39. }
  40. }
  41. var isEditing: Bool {
  42. get {
  43. return self.listView?.isEditing() ?? false
  44. }
  45. }
  46. var editingConfig: CPDFEditingConfig? {
  47. get {
  48. return self.listView?.editingConfig()
  49. }
  50. }
  51. var editingAreas: [CPDFEditArea] {
  52. get {
  53. return self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  54. }
  55. }
  56. func enterEditPDF() {
  57. self.listView?.updateActiveAnnotations([])
  58. self.listView?.setNeedsDisplayForVisiblePages()
  59. self.listView?.commitEditFormText()
  60. self.listView?.annotationType = .editTextImage
  61. // 设置边框颜色
  62. self.editingConfig?.editingBorderColor = .clear
  63. // 设置边框宽度
  64. // self.editingConfig?.editingBorderWidth = 10
  65. // 内容与边框的间距
  66. // self.editingConfig?.editAreaMargin = .init(floatLiteral: 5)
  67. // 设置选中块边框颜色
  68. // self.editingConfig?.editingSelectionBorderColor = .red
  69. // 显示hover边框
  70. self.editingConfig?.isShowMouseAreaHover = true
  71. // hover
  72. // 边框宽度
  73. // self.editingConfig?.mouseHoverBorderWidth = 1
  74. // 边框颜色
  75. self.editingConfig?.mouseHoverBorderColor = .purple
  76. // 边框虚线设置
  77. self.editingConfig?.mouseHoverBorderDashPattern = [3,3,3]
  78. // 块填充颜色(拖拽中)
  79. // self.editingConfig?.editAreaMoveFillColor = .cyan
  80. // 是否显示位置辅助线
  81. self.editingConfig?.isShowEditingAreaHover = true
  82. // 辅助线颜色
  83. // self.editingConfig?.editingHoverBorderColor = .brown
  84. // 支持多选
  85. self.editingConfig?.isSupportMultipleSelectEditingArea = true
  86. // 图片是否显示8个操作点
  87. self.editingConfig?.isDrawRectWithDot = true
  88. }
  89. func fontColorAction(color: NSColor?) {
  90. guard let theColor = color else {
  91. return
  92. }
  93. let editingAreas = self.editingAreas
  94. for area in editingAreas {
  95. if let data = area as? CPDFEditTextArea {
  96. self.listView?.setEditingSelectionFontColor(theColor, with: data)
  97. }
  98. }
  99. }
  100. func fontStyleAction(fontName: String?) {
  101. guard let font = CPDFFont.mappingFont(withFontString: fontName) else {
  102. return
  103. }
  104. let editingAreas = self.editingAreas
  105. for area in editingAreas {
  106. if let data = area as? CPDFEditTextArea {
  107. self.listView?.setEditSelectionCFont(font, with: data)
  108. }
  109. }
  110. }
  111. func fontAddAction() {
  112. let editingAreas = self.editingAreas
  113. if editingAreas.isEmpty {
  114. return
  115. }
  116. guard let area = editingAreas.last else {
  117. return
  118. }
  119. if area.isTextArea() == false {
  120. return
  121. }
  122. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  123. self.listView?.setEditingSelectionFontSize(fontSize+1, with: area as! CPDFEditTextArea, isAutoSize: false)
  124. }
  125. }
  126. func fontReduceAction() {
  127. let editingAreas = self.editingAreas
  128. if editingAreas.isEmpty {
  129. return
  130. }
  131. guard let area = editingAreas.last else {
  132. return
  133. }
  134. if area.isTextArea() == false {
  135. return
  136. }
  137. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  138. self.listView?.setEditingSelectionFontSize(fontSize-1, with: area as! CPDFEditTextArea, isAutoSize: false)
  139. }
  140. }
  141. func fontBoldAction() {
  142. let editingAreas = self.editingAreas
  143. if editingAreas.isEmpty {
  144. return
  145. }
  146. guard let area = editingAreas.last else {
  147. return
  148. }
  149. if area.isTextArea() == false {
  150. return
  151. }
  152. self.listView?.setCurrentSelectionIsBold(true, with: area as! CPDFEditTextArea)
  153. }
  154. func fontItalicAction() {
  155. let editingAreas = self.editingAreas
  156. if editingAreas.isEmpty {
  157. return
  158. }
  159. guard let area = editingAreas.last else {
  160. return
  161. }
  162. if area.isTextArea() == false {
  163. return
  164. }
  165. self.listView?.setCurrentSelectionIsItalic(true, with: area as! CPDFEditTextArea)
  166. }
  167. func textAlignmentAction(align: NSTextAlignment) {
  168. let editingAreas = self.editingAreas
  169. for area in editingAreas {
  170. if let data = area as? CPDFEditTextArea {
  171. self.listView?.setCurrentSelectionAlignment(align, with: data)
  172. }
  173. }
  174. }
  175. func leftRotateAction() {
  176. let editingAreas = self.editingAreas
  177. if editingAreas.isEmpty {
  178. return
  179. }
  180. guard let area = editingAreas.last as? CPDFEditImageArea else {
  181. return
  182. }
  183. // if let ang = self.listView?.getRotateWith(area) {
  184. // self.listView?.rotate(with: area, rotate: ang-90)
  185. // }
  186. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Rotate"])
  187. self.listView?.rotate(with: area, rotate: -90)
  188. // if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  189. // self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  190. // }
  191. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  192. // self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  193. }
  194. func rightRotateAction() {
  195. let editingAreas = self.editingAreas
  196. if editingAreas.isEmpty {
  197. return
  198. }
  199. guard let area = editingAreas.last as? CPDFEditImageArea else {
  200. return
  201. }
  202. self.listView?.rotate(with: area, rotate: 90)
  203. }
  204. func reverseXAction() {
  205. let editingAreas = self.editingAreas
  206. if editingAreas.isEmpty {
  207. return
  208. }
  209. for area in editingAreas {
  210. if let data = area as? CPDFEditImageArea {
  211. self.listView?.horizontalMirror(with: data)
  212. }
  213. }
  214. }
  215. func reverseYAction() {
  216. let editingAreas = self.editingAreas
  217. if editingAreas.isEmpty {
  218. return
  219. }
  220. for area in editingAreas {
  221. if let data = area as? CPDFEditImageArea {
  222. self.listView?.verticalMirror(with: data)
  223. }
  224. }
  225. }
  226. func cropAction() {
  227. self.listView?.isEditImage = true
  228. let editingAreas = self.editingAreas
  229. if editingAreas.isEmpty {
  230. return
  231. }
  232. for area in editingAreas {
  233. if let data = area as? CPDFEditImageArea {
  234. self.listView?.enterCrop(with: data)
  235. }
  236. }
  237. }
  238. func replaceAction() {
  239. let editingAreas = self.editingAreas
  240. if editingAreas.isEmpty {
  241. return
  242. }
  243. let panel = NSOpenPanel()
  244. panel.allowsMultipleSelection = false
  245. panel.allowedFileTypes = ["png","jpg"]
  246. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  247. if response == .OK {
  248. let openPath = panel.url?.path
  249. for area in editingAreas {
  250. if let data = area as? CPDFEditImageArea {
  251. // self.listView?.replace(data, imagePath: openPath!)
  252. self.listView?.replace(data, imagePath: openPath!, rect: data.bounds)
  253. }
  254. }
  255. }
  256. }
  257. }
  258. func exportAction() {
  259. let editingAreas = self.editingAreas
  260. if editingAreas.isEmpty {
  261. return
  262. }
  263. var imagesAreas: [CPDFEditImageArea] = []
  264. for area in editingAreas {
  265. if let data = area as? CPDFEditImageArea {
  266. imagesAreas.append(data)
  267. }
  268. }
  269. if imagesAreas.count == 1 {
  270. let panel = NSSavePanel()
  271. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  272. panel.isExtensionHidden = true
  273. let response = panel.runModal()
  274. if response == .OK {
  275. let url = panel.url
  276. if FileManager.default.fileExists(atPath: url!.path) {
  277. try?FileManager.default.removeItem(atPath: url!.path)
  278. }
  279. let result = self.listView?.extractImage(with: imagesAreas.first, toImagePath: url!.path) ?? false
  280. if result {
  281. NSWorkspace.shared.activateFileViewerSelecting([url!])
  282. }
  283. }
  284. } else if imagesAreas.count > 1 {
  285. let panel = NSOpenPanel()
  286. panel.canChooseFiles = false
  287. panel.canChooseDirectories = true
  288. panel.canCreateDirectories = true
  289. panel.allowsMultipleSelection = false
  290. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  291. if response == .OK {
  292. let outputURL = panel.url
  293. let s = self.listView?.document?.documentURL.lastPathComponent
  294. let folderPath = (self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? "") + "_extract"
  295. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  296. var i = 1
  297. let testFilePath = filePath
  298. while FileManager.default.fileExists(atPath: filePath!) {
  299. filePath = testFilePath! + "\(i)"
  300. i = i + 1
  301. }
  302. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  303. var saveURLs : [URL] = []
  304. for j in 0 ... imagesAreas.count-1 {
  305. let documentFileName = self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  306. var outPath = filePath!
  307. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  308. outPath = outPath + "page \(j+1)"
  309. outPath = outPath.stringByAppendingPathExtension("jpg")
  310. let result = self.listView?.extractImage(with: imagesAreas[j], toImagePath: outPath) ?? false
  311. if result {
  312. saveURLs.append(URL(fileURLWithPath: outPath))
  313. }
  314. }
  315. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  316. }
  317. }
  318. }
  319. }
  320. func alignmentAction(align: CPDFActiveAreasAlignType) {
  321. KMPrint("updateFormAearsAlignMangent")
  322. let stype = align
  323. let editingAreas = self.editingAreas
  324. if editingAreas.count >= 2 {
  325. var zeroRect = NSRect.null
  326. var highestRect = NSZeroRect
  327. var widthestRect = NSZeroRect
  328. let fristArea : CPDFEditArea = editingAreas.first as! CPDFEditArea
  329. var leftestRect = fristArea.bounds
  330. var rightestRect = fristArea.bounds
  331. var topestRect = fristArea.bounds
  332. var bottomestRect = fristArea.bounds
  333. var leftestArea : CPDFEditArea = fristArea
  334. var rightestArea : CPDFEditArea = fristArea
  335. var topestArea : CPDFEditArea = fristArea
  336. var bottomestArea : CPDFEditArea = fristArea
  337. var totalWidth = 0.0
  338. var totalHeight = 0.0
  339. for i in 0 ... editingAreas.count-1 {
  340. let area : CPDFEditArea = editingAreas[i] as! CPDFEditArea
  341. zeroRect = zeroRect.union(area.bounds)
  342. totalWidth = totalWidth + area.bounds.width
  343. totalHeight = totalHeight + area.bounds.height
  344. if area.bounds.height > highestRect.height {
  345. highestRect = area.bounds
  346. }
  347. if area.bounds.width > widthestRect.width {
  348. widthestRect = area.bounds
  349. }
  350. if leftestRect.minX > area.bounds.minX {
  351. leftestRect = area.bounds
  352. leftestArea = area
  353. }
  354. if area.bounds.maxX > rightestRect.maxX {
  355. rightestRect = area.bounds
  356. rightestArea = area
  357. }
  358. if area.bounds.maxY > topestRect.maxY {
  359. topestRect = area.bounds
  360. topestArea = area
  361. }
  362. if bottomestRect.minY > area.bounds.minY {
  363. bottomestRect = area.bounds
  364. bottomestArea = area
  365. }
  366. }
  367. var resultAreasArray: [Any] = []
  368. var newBoundsArray: [String] = []
  369. if stype == .Left {
  370. for i in 0 ... editingAreas.count-1 {
  371. let areas = editingAreas[i] as! CPDFEditArea
  372. var bounds = areas.bounds
  373. bounds.origin.x = zeroRect.origin.x
  374. newBoundsArray.append(NSStringFromRect(bounds))
  375. }
  376. resultAreasArray = editingAreas
  377. } else if stype == .Right {
  378. for i in 0 ... editingAreas.count-1 {
  379. let areas = editingAreas[i] as! CPDFEditArea
  380. var bounds = areas.bounds
  381. bounds.origin.x = zeroRect.maxX - bounds.size.width
  382. newBoundsArray.append(NSStringFromRect(bounds))
  383. }
  384. resultAreasArray = editingAreas
  385. } else if stype == .Top {
  386. for i in 0 ... editingAreas.count-1 {
  387. let areas = editingAreas[i] as! CPDFEditArea
  388. var bounds = areas.bounds
  389. bounds.origin.y = zeroRect.maxY - bounds.size.height
  390. newBoundsArray.append(NSStringFromRect(bounds))
  391. }
  392. resultAreasArray = editingAreas
  393. } else if stype == .Bottom {
  394. for i in 0 ... editingAreas.count-1 {
  395. let areas = editingAreas[i] as! CPDFEditArea
  396. var bounds = areas.bounds
  397. bounds.origin.y = zeroRect.minY
  398. newBoundsArray.append(NSStringFromRect(bounds))
  399. }
  400. resultAreasArray = editingAreas
  401. } else if stype == .Horizontally {
  402. for i in 0 ... editingAreas.count-1 {
  403. let areas = editingAreas[i] as! CPDFEditArea
  404. var bounds = areas.bounds
  405. bounds.origin.y = highestRect.midY - bounds.height/2
  406. newBoundsArray.append(NSStringFromRect(bounds))
  407. }
  408. resultAreasArray = editingAreas
  409. } else if stype == .Vertical {
  410. for i in 0 ... editingAreas.count-1 {
  411. let areas = editingAreas[i] as! CPDFEditArea
  412. var bounds = areas.bounds
  413. bounds.origin.x = widthestRect.midX - bounds.width/2
  414. newBoundsArray.append(NSStringFromRect(bounds))
  415. }
  416. resultAreasArray = editingAreas
  417. } else if stype == .DisHorizontally {
  418. let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
  419. let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
  420. let gap = (middleGap - otherAreasTotalWidth)/CGFloat(editingAreas.count - 1)
  421. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  422. areasCopyArray.sorted(by: { obj1, obj2 in
  423. let area1 = obj1
  424. let area2 = obj2
  425. if area1.bounds.origin.x < area2.bounds.origin.x {
  426. return true
  427. } else {
  428. return false
  429. }
  430. })
  431. if let index = areasCopyArray.firstIndex(of: leftestArea) {
  432. areasCopyArray.remove(at: index)
  433. }
  434. if let index = areasCopyArray.firstIndex(of: rightestArea) {
  435. areasCopyArray.remove(at: index)
  436. }
  437. var leftStartX = leftestRect.maxX + gap
  438. for i in 0 ... areasCopyArray.count-1 {
  439. let areas = areasCopyArray[i]
  440. var bounds = areas.bounds
  441. bounds.origin.x = leftStartX
  442. newBoundsArray.append(NSStringFromRect(bounds))
  443. leftStartX = leftStartX + bounds.width + gap
  444. }
  445. resultAreasArray = areasCopyArray
  446. } else if stype == .DisVertical {
  447. let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
  448. let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
  449. let gap = (middleGap - otherAreasTotalHeight)/CGFloat(editingAreas.count - 1)
  450. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  451. areasCopyArray.sorted(by: { obj1, obj2 in
  452. let area1 = obj1
  453. let area2 = obj2
  454. if area1.bounds.origin.x < area2.bounds.origin.x {
  455. return true
  456. } else {
  457. return false
  458. }
  459. })
  460. if let index = areasCopyArray.firstIndex(of: topestArea) {
  461. areasCopyArray.remove(at: index)
  462. }
  463. if let index = areasCopyArray.firstIndex(of: bottomestArea) {
  464. areasCopyArray.remove(at: index)
  465. }
  466. var bottomStartY = bottomestRect.maxY + gap
  467. for i in 0 ... areasCopyArray.count-1 {
  468. let areas = areasCopyArray[i]
  469. var bounds = areas.bounds
  470. bounds.origin.y = bottomStartY
  471. newBoundsArray.append(NSStringFromRect(bounds))
  472. bottomStartY = bottomStartY + bounds.height + gap
  473. }
  474. resultAreasArray = areasCopyArray
  475. }
  476. var oldBounds : [String] = []
  477. for i in 0 ... resultAreasArray.count-1 {
  478. let area : CPDFEditArea = resultAreasArray[i] as! CPDFEditArea
  479. oldBounds.append(NSStringFromRect(area.bounds))
  480. self.listView?.setBoundsEditArea(area, withBounds: NSRectFromString(newBoundsArray[i]))
  481. }
  482. self.listView?.setNeedsDisplayForVisiblePages()
  483. }
  484. }
  485. func showPopWindow(positionRect: NSRect) {
  486. let show = KMPreference.shared.editPDFPopWindowIsShow
  487. if !show {
  488. return
  489. }
  490. let win = KMEditPDFPopToolBarWindow.shared
  491. let contains = self.viewC?.view.window?.childWindows?.contains(win) ?? false
  492. if contains {
  493. self.viewC?.view.window?.removeChildWindow(win)
  494. }
  495. let areas = self.editingAreas
  496. win.isMultiple = areas.count > 1
  497. var hasText = false
  498. var hasImage = false
  499. var fontColors: [NSColor] = []
  500. for area in areas {
  501. if let data = area as? CPDFEditTextArea {
  502. hasText = true
  503. if let color = self.listView?.editingSelectionFontColor(with: data) {
  504. fontColors.append(color)
  505. }
  506. }
  507. if area is CPDFEditImageArea {
  508. hasImage = true
  509. }
  510. }
  511. var style: KMEditPDFToolbarStyle = []
  512. if hasText {
  513. style.insert(.text)
  514. }
  515. if hasImage {
  516. style.insert(.image)
  517. }
  518. win.style = style
  519. win.model.editingAreas = areas
  520. win.model.fontColors = fontColors
  521. win.show(relativeTo: positionRect, of: self.viewC!.listView, preferredEdge: .maxY)
  522. self.viewC?.view.window?.addChildWindow(win, ordered: .above)
  523. win.itemClick = { [weak self] itemKey, obj in
  524. if itemKey == .color {
  525. self?.fontColorAction(color: obj as? NSColor)
  526. } else if itemKey == .fontStyle {
  527. self?.fontStyleAction(fontName: obj as? String)
  528. } else if itemKey == .fontAdd {
  529. self?.fontAddAction()
  530. } else if itemKey == .fontReduce {
  531. self?.fontReduceAction()
  532. } else if itemKey == .fontBold {
  533. self?.fontBoldAction()
  534. } else if itemKey == .fontItalic {
  535. self?.fontItalicAction()
  536. } else if itemKey == .textAlignment {
  537. self?.textAlignmentAction(align: obj as? NSTextAlignment ?? .left)
  538. }
  539. // 图片
  540. else if itemKey == .leftRotate {
  541. self?.leftRotateAction()
  542. } else if itemKey == .rightRotate {
  543. self?.rightRotateAction()
  544. } else if itemKey == .reverseX {
  545. self?.reverseXAction()
  546. } else if itemKey == .reverseY {
  547. self?.reverseYAction()
  548. } else if itemKey == .crop {
  549. self?.cropAction()
  550. } else if itemKey == .replace {
  551. self?.replaceAction()
  552. } else if itemKey == .export {
  553. self?.exportAction()
  554. }
  555. // 对齐
  556. else if itemKey == .alignmentLeft {
  557. self?.alignmentAction(align: .Left)
  558. } else if itemKey == .alignmentCenterX {
  559. self?.alignmentAction(align: .Horizontally)
  560. } else if itemKey == .alignmentRight {
  561. self?.alignmentAction(align: .Right)
  562. } else if itemKey == .alignmentjustifiedX {
  563. self?.alignmentAction(align: .DisHorizontally)
  564. } else if itemKey == .alignmentTop {
  565. self?.alignmentAction(align: .Top)
  566. } else if itemKey == .alignmentCenterY {
  567. self?.alignmentAction(align: .Vertical)
  568. } else if itemKey == .alignmentBottom {
  569. self?.alignmentAction(align: .Bottom)
  570. } else if itemKey == .alignmentjustifiedY {
  571. self?.alignmentAction(align: .DisVertical)
  572. }
  573. }
  574. // 显示新手引导
  575. if let toolbarView = (win.contentViewController as? KMEditPDFPopToolBarController)?.toolbarView {
  576. self.showGuideView(toolbarView)
  577. }
  578. }
  579. func showGuideView(_ view: NSView) {
  580. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  581. if KMGuideInfoWindowController.availableShow(.editPDFPopWindow) {
  582. var winFrame = self.viewC?.view.window?.frame ?? .zero
  583. winFrame.size.height -= 20
  584. let area = (self.listView?.editingAreas().first as? CPDFEditArea)
  585. let areaBounds = (self.listView?.convert(area!.bounds, from: area!.page) as? NSRect) ?? .zero
  586. let guideWC = KMGuideInfoWindowController.currentWC()
  587. guideWC.type = .editPDFPopWindow
  588. var viewFrame = areaBounds
  589. let tmpY = areaBounds.origin.y+(areaBounds.size.height-KMEditPDFPopGuideView.kHeight+80)
  590. if tmpY < 50 {
  591. guideWC.editPDFPopWindowFlag = true
  592. viewFrame.origin.y += (areaBounds.size.height)
  593. viewFrame.origin.x += (areaBounds.size.width*0.5+KMEditPDFPopGuideView.kWidth*0.5)
  594. } else {
  595. guideWC.editPDFPopWindowFlag = false
  596. viewFrame.origin.y += (areaBounds.size.height-KMEditPDFPopGuideView.kHeight+80)
  597. viewFrame.origin.x += (areaBounds.size.width*0.5+KMEditPDFPopGuideView.kWidth*0.5)
  598. }
  599. guideWC.digitalBoxRect = viewFrame
  600. var beh = view.window?.collectionBehavior ?? []
  601. beh.insert(.canJoinAllSpaces)
  602. guideWC.window?.collectionBehavior = beh
  603. guideWC.window?.setFrame(winFrame, display: false)
  604. guideWC.window?.minSize = winFrame.size
  605. guideWC.window?.maxSize = winFrame.size
  606. self.viewC?.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  607. guideWC.show()
  608. DispatchQueue.main.async {
  609. guideWC.interfaceThemeDidChanged(NSApp.appearance?.name ?? .aqua)
  610. }
  611. guideWC.settingCallback = {
  612. KMPreferenceController.shared.showWindow(nil)
  613. }
  614. }
  615. }
  616. }
  617. }
  618. extension KMEditPDfHanddler: CPDFViewDelegate {
  619. // 编辑区块已经改变
  620. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  621. let isEdited = self.listView?.isEdited() ?? false
  622. if isEdited {
  623. // 记录编辑状态
  624. self.viewC?.recordIsPDFDocumentEdited(type: .editText)
  625. }
  626. if annotationType != .addText {
  627. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView?.document)
  628. }
  629. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  630. if areas.isEmpty {
  631. let toolMode = self.listView?.toolMode ?? .none
  632. let annotationType = self.annotationType
  633. if toolMode == .editPDFToolMode {
  634. if annotationType == .addImage || annotationType == .addText {
  635. if self.isEditImage {
  636. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  637. } else {
  638. // if self.listView.annotationType == .addImage {
  639. // self.closeRightPane()
  640. // }
  641. if annotationType == .addImage {
  642. if self.rightViewC?.eidtPDFImageProperty != nil {
  643. self.rightViewC?.eidtPDFImageProperty.reloadData()
  644. }
  645. }
  646. // self.openRightPane()
  647. }
  648. } else {
  649. self.viewC?.closeRightPane()
  650. }
  651. } else {
  652. self.rightViewC?.isHidden = true
  653. self.viewC?.closeRightPane()
  654. if self.subViewType == .EditPDFAddText && annotationType == .addText {
  655. self.rightViewC?.eidtPDFTextProperty.initData()
  656. }
  657. }
  658. return
  659. }
  660. self.viewC?.model.isPDFTextImageEdited = true
  661. let subViewType = self.rightViewC?.subViewType ?? .None
  662. if self.annotationType == .addImage {
  663. var isImageArea = false
  664. for i in 0 ..< areas.count {
  665. if areas[i] is CPDFEditImageArea {
  666. isImageArea = true
  667. }
  668. }
  669. if isImageArea {
  670. self.rightViewC?.isHidden = false
  671. if self.subViewType == .EditPDFAddImage {
  672. self.rightViewC?.subViewType = .EditPDFAddImage
  673. self.rightViewC?.eidtPDFImageProperty.reloadData()
  674. }
  675. self.viewC?.openRightPane()
  676. } else {
  677. self.rightViewC?.isHidden = true
  678. self.viewC?.closeRightPane()
  679. }
  680. } else if self.subViewType == .EditPDFAddText && annotationType == .addText {
  681. self.rightViewC?.isHidden = false
  682. let count = self.listView?.editingSelectionString()?.count ?? 0
  683. if count != 0 {
  684. self.rightViewC?.eidtPDFTextProperty.reloadData()
  685. } else {
  686. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  687. }
  688. self.viewC?.openRightPane()
  689. } else {
  690. var textsAreas : [CPDFEditTextArea] = []
  691. var imagesAreas : [CPDFEditImageArea] = []
  692. let count = self.listView?.editingAreas()?.count ?? 0
  693. if count < 1 {
  694. return
  695. }
  696. for i in 0 ..< areas.count {
  697. if areas[i] is CPDFEditTextArea {
  698. textsAreas.append(areas[i] as! CPDFEditTextArea)
  699. }
  700. if areas[i] is CPDFEditImageArea {
  701. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  702. }
  703. }
  704. if textsAreas.count > 0 && textsAreas.count == areas.count {
  705. self.rightViewC?.isHidden = false
  706. self.rightViewC?.subViewType = .EditPDFAddText
  707. self.rightViewC?.eidtPDFTextProperty?.reloadData()
  708. self.viewC?.openRightPane()
  709. } else if imagesAreas.count > 0 {
  710. self.rightViewC?.isHidden = false
  711. self.rightViewC?.subViewType = .EditPDFAddImage
  712. self.rightViewC?.eidtPDFImageProperty?.reloadData()
  713. self.viewC?.openRightPane()
  714. }
  715. }
  716. var flag: CPDFEditArea?
  717. for area in areas {
  718. if flag == nil {
  719. flag = area
  720. continue
  721. }
  722. if let data = flag, data.bounds.maxY < area.bounds.maxY {
  723. flag = area
  724. }
  725. }
  726. if let data = flag {
  727. self.showPopWindow(positionRect: data.bounds)
  728. }
  729. }
  730. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  731. if editArea != nil && (editArea is CPDFEditImageArea){
  732. self.listView?.cropAreas = editArea as? CPDFEditImageArea
  733. }
  734. }
  735. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  736. if self.isEditImage {
  737. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  738. } else {
  739. let panel = NSOpenPanel()
  740. panel.allowsMultipleSelection = false
  741. panel.allowedFileTypes = ["png","jpg"]
  742. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  743. if response == .OK {
  744. var filePath = panel.url?.path
  745. var image = NSImage.init(contentsOf: panel.url!)
  746. //图片自适应范围
  747. if image != nil {
  748. var imageRect = rect
  749. let imageSize = image!.size
  750. var previewSize = rect.size
  751. var isChangeSize = false
  752. if previewSize.width == 0 && previewSize.height == 0 {
  753. previewSize = CGSize(width: 500, height: 500)
  754. isChangeSize = true
  755. }
  756. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  757. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  758. if isChangeSize {
  759. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  760. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  761. } else {
  762. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  763. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  764. }
  765. imageRect.size = newSize
  766. let limitWidth = 1920.0
  767. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  768. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  769. targetSize: CGSize(width: limitWidth, height: limitWidth),
  770. maxSizeInBytes: 1024 * 1024 * 5,
  771. targetCompression: 1.0)
  772. }
  773. //自适应page
  774. let pageRect = self.listView?.currentPage().bounds ?? .zero
  775. if imageRect.width > pageRect.width ||
  776. imageRect.height > pageRect.height {
  777. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  778. imageRect = CGRect(x: imageRect.origin.x,
  779. y: imageRect.origin.y,
  780. width: imageRect.width * pageScale,
  781. height: imageRect.height * pageScale)
  782. }
  783. if imageRect.origin.x < 0 {
  784. imageRect.origin.x = 5
  785. }
  786. if imageRect.origin.y < 0 {
  787. imageRect.origin.y = 5
  788. }
  789. if imageRect.origin.x + imageRect.width > pageRect.width ||
  790. imageRect.origin.y + imageRect.height > pageRect.height {
  791. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  792. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  793. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  794. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  795. }
  796. DispatchQueue.main.async {
  797. self.listView?.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  798. self.viewC?.model.isPDFTextImageEdited = true
  799. self.viewC?.recordIsPDFDocumentEdited(type: .editImage)
  800. self.showPopWindow(positionRect: imageRect)
  801. }
  802. }
  803. }
  804. }
  805. }
  806. }
  807. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  808. let window = KMEditPDFPopToolBarWindow.shared
  809. if (window.isVisible) {
  810. window.orderOut(nil)
  811. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  812. if let area = areas.last {
  813. if let data = area as? CPDFEditTextArea {
  814. if let str = data.editTextAreaString(), str.isEmpty {
  815. self.listView?.remove(with: [area])
  816. }
  817. }
  818. }
  819. return
  820. }
  821. var newRect = rect
  822. if CGSizeEqualToSize(rect.size, .zero) {
  823. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  824. } else {
  825. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  826. }
  827. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  828. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  829. let fontSize = model.fontSize
  830. let fontColor = model.color
  831. let fontAlign = model.alignment
  832. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  833. NSColorPanel.shared.color = fontColor
  834. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  835. let attri = CEditAttributes()
  836. attri.font = font
  837. attri.fontColor = fontColor
  838. attri.alignment = fontAlign
  839. self.listView?.createStringBounds(newRect, with: attri, page: page)
  840. // self.rightViewC != nil &&
  841. if self.subViewType == .EditPDFAddText && self.annotationType == .addText {
  842. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  843. }
  844. self.showPopWindow(positionRect: newRect)
  845. }
  846. // 文本区块 选中文本已经变化
  847. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  848. // self.viewC?.rightSideViewController != nil &&
  849. if self.subViewType == .EditPDFAddText {
  850. self.rightViewC?.eidtPDFTextProperty.reloadData()
  851. self.rightViewC?.eidtPDFTextProperty.updateTextTextPresuppositionState()
  852. }
  853. }
  854. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  855. KMPrint("pdfViewMobileEditingBegan")
  856. }
  857. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  858. KMPrint("pdfViewMobileEditingMove")
  859. }
  860. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  861. KMPrint("pdfViewMobileEditingEnd")
  862. }
  863. }