KMEditPDfHanddler.swift 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. //
  2. // KMEditPDfHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/16.
  6. //
  7. import Cocoa
  8. @objc enum KMRightSideLastState: Int {
  9. case none = 0
  10. case open = 1
  11. case close = 2
  12. }
  13. // EditPDF处理对象
  14. class KMEditPDfHanddler: NSObject {
  15. weak var viewC: KMMainViewController?
  16. static let kRightSideLastStateKey = "KMRightSideLastStateKey"
  17. weak var listView: CPDFListView? {
  18. get {
  19. return self.viewC?.listView
  20. }
  21. }
  22. var annotationType: CAnnotationType {
  23. get {
  24. return self.listView?.annotationType ?? .unkown
  25. }
  26. }
  27. weak var rightViewC: KMRightSideViewController? {
  28. get {
  29. return self.viewC?.rightSideViewController
  30. }
  31. }
  32. var subViewType: RightSubViewType {
  33. get {
  34. return self.rightViewC?.subViewType ?? .None
  35. }
  36. }
  37. // var toolMode: CToolMode {
  38. // get {
  39. // return
  40. // }
  41. // }
  42. var isEditImage: Bool {
  43. get {
  44. return self.listView?.isEditImage ?? false
  45. }
  46. }
  47. var isEditing: Bool {
  48. get {
  49. return self.listView?.isEditing() ?? false
  50. }
  51. }
  52. var editingConfig: CPDFEditingConfig? {
  53. get {
  54. return self.listView?.editingConfig()
  55. }
  56. }
  57. var editingAreas: [CPDFEditArea] {
  58. get {
  59. return self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  60. }
  61. }
  62. var editingImageAreas: [CPDFEditImageArea] {
  63. get {
  64. var areas: [CPDFEditImageArea] = []
  65. for area in self.editingAreas {
  66. if let data = area as? CPDFEditImageArea {
  67. areas.append(data)
  68. }
  69. }
  70. return areas
  71. }
  72. }
  73. var editingTextAreas: [CPDFEditTextArea] {
  74. get {
  75. var areas: [CPDFEditTextArea] = []
  76. for area in self.editingAreas {
  77. if let data = area as? CPDFEditTextArea {
  78. areas.append(data)
  79. }
  80. }
  81. return areas
  82. }
  83. }
  84. var rightSideLastState: KMRightSideLastState {
  85. get {
  86. let state = KMDataManager.ud_integer(forKey: Self.kRightSideLastStateKey)
  87. return KMRightSideLastState(rawValue: state) ?? .none
  88. }
  89. set {
  90. KMDataManager.ud_set(newValue.rawValue, forKey: Self.kRightSideLastStateKey)
  91. }
  92. }
  93. private var startPoint_: NSPoint = .zero
  94. func enterEditPDF() {
  95. let toolMode = self.listView?.toolMode ?? .none
  96. if toolMode != .editPDFToolMode { // 退出
  97. self.listView?.updateActiveAnnotations([])
  98. self.listView?.setNeedsDisplayForVisiblePages()
  99. self.listView?.commitEditFormText()
  100. self.closeRightPane()
  101. self.hiddenPopWindow()
  102. self._removeNotification()
  103. return
  104. }
  105. self._addNotification()
  106. if self.rightSideLastState == .open {
  107. self.openRightPane()
  108. } else {
  109. self.closeRightPane()
  110. }
  111. self.listView?.updateActiveAnnotations([])
  112. self.listView?.setNeedsDisplayForVisiblePages()
  113. self.listView?.commitEditFormText()
  114. self.listView?.annotationType = .editTextImage
  115. // 设置边框颜色
  116. self.editingConfig?.editingBorderColor = .clear
  117. // 设置边框宽度
  118. // self.editingConfig?.editingBorderWidth = 10
  119. // 内容与边框的间距
  120. // self.editingConfig?.editAreaMargin = .init(floatLiteral: 5)
  121. // 设置选中块边框颜色
  122. // self.editingConfig?.editingSelectionBorderColor = .red
  123. // 显示hover边框
  124. self.editingConfig?.isShowMouseAreaHover = true
  125. // hover
  126. // 边框宽度
  127. // self.editingConfig?.mouseHoverBorderWidth = 1
  128. // 边框颜色
  129. self.editingConfig?.mouseHoverBorderColor = NSColor(hex: "#999999")
  130. // 边框虚线设置
  131. self.editingConfig?.mouseHoverBorderDashPattern = [3,3,3]
  132. // 块填充颜色(拖拽中)
  133. // self.editingConfig?.editAreaMoveFillColor = .cyan
  134. // 是否显示位置辅助线
  135. self.editingConfig?.isShowEditingAreaHover = true
  136. // 辅助线颜色
  137. // self.editingConfig?.editingHoverBorderColor = .brown
  138. // 支持多选
  139. self.editingConfig?.isSupportMultipleSelectEditingArea = true
  140. // 图片是否显示8个操作点
  141. self.editingConfig?.isDrawRectWithDot = true
  142. }
  143. func commitEditing() {
  144. let isEdited = self.listView?.isEdited() ?? false
  145. let isPDFTextImageEdited = self.viewC?.model.isPDFTextImageEdited ?? false
  146. if isEdited || isPDFTextImageEdited {
  147. self.listView?.commitEditing()
  148. }
  149. self.hiddenPopWindow()
  150. self.hiddenCropComfirmWindow()
  151. }
  152. func openRightPane() {
  153. let state = self.rightSideLastState
  154. if state == .none || state == .open {
  155. self.viewC?.openRightPane()
  156. }
  157. }
  158. func closeRightPane() {
  159. self.rightViewC?.isHidden = true
  160. self.viewC?.closeRightPane()
  161. }
  162. func showPopWindow(positionRect: NSRect) {
  163. let show = KMPreference.shared.editPDFPopWindowIsShow
  164. if !show {
  165. return
  166. }
  167. let win = KMEditPDFPopToolBarWindow.shared
  168. self._kRemoveChildWindow(win)
  169. let areas = self.editingAreas
  170. win.isMultiple = areas.count > 1
  171. var hasText = false
  172. var hasImage = false
  173. var fontColors: [NSColor] = []
  174. for area in areas {
  175. if let data = area as? CPDFEditTextArea {
  176. hasText = true
  177. if let color = self.listView?.editingSelectionFontColor(with: data) {
  178. fontColors.append(color)
  179. }
  180. }
  181. if area is CPDFEditImageArea {
  182. hasImage = true
  183. }
  184. }
  185. var style: KMEditPDFToolbarStyle = []
  186. if hasText {
  187. style.insert(.text)
  188. }
  189. if hasImage {
  190. style.insert(.image)
  191. }
  192. win.style = style
  193. win.model.editingAreas = areas
  194. win.model.fontColors = fontColors
  195. let area = (self.listView?.editingAreas().first as? CPDFEditArea)
  196. let areaBounds = (self.listView?.convert(area!.bounds, from: area!.page) as? NSRect) ?? .zero
  197. self.startPoint_ = self.listView?.documentView().documentVisibleRect.origin ?? .zero
  198. win.show(relativeTo: areaBounds, of: self.viewC!.listView, preferredEdge: .maxY)
  199. win.animator().alphaValue = 1
  200. self._kAddchildwindow(win)
  201. win.itemClick = { [weak self] itemKey, obj in
  202. if itemKey == .color {
  203. self?.fontColorAction(color: obj as? NSColor)
  204. } else if itemKey == .fontStyle {
  205. self?.fontStyleAction(fontName: obj as? String)
  206. } else if itemKey == .fontAdd {
  207. self?.fontAddAction()
  208. } else if itemKey == .fontReduce {
  209. self?.fontReduceAction()
  210. } else if itemKey == .fontBold {
  211. self?.fontBoldAction()
  212. } else if itemKey == .fontItalic {
  213. self?.fontItalicAction()
  214. } else if itemKey == .textAlignment {
  215. self?.textAlignmentAction(align: obj as? NSTextAlignment ?? .left)
  216. }
  217. // 图片
  218. else if itemKey == .leftRotate {
  219. self?.leftRotateAction()
  220. } else if itemKey == .rightRotate {
  221. self?.rightRotateAction()
  222. } else if itemKey == .reverseX {
  223. self?.reverseXAction()
  224. } else if itemKey == .reverseY {
  225. self?.reverseYAction()
  226. } else if itemKey == .crop {
  227. self?.cropAction()
  228. } else if itemKey == .replace {
  229. self?.replaceAction()
  230. } else if itemKey == .export {
  231. self?.exportAction()
  232. }
  233. // 对齐
  234. else if itemKey == .alignmentLeft {
  235. self?.alignmentAction(align: .Left)
  236. } else if itemKey == .alignmentCenterX {
  237. self?.alignmentAction(align: .Horizontally)
  238. } else if itemKey == .alignmentRight {
  239. self?.alignmentAction(align: .Right)
  240. } else if itemKey == .alignmentjustifiedX {
  241. self?.alignmentAction(align: .DisHorizontally)
  242. } else if itemKey == .alignmentTop {
  243. self?.alignmentAction(align: .Top)
  244. } else if itemKey == .alignmentCenterY {
  245. self?.alignmentAction(align: .Vertical)
  246. } else if itemKey == .alignmentBottom {
  247. self?.alignmentAction(align: .Bottom)
  248. } else if itemKey == .alignmentjustifiedY {
  249. self?.alignmentAction(align: .DisVertical)
  250. }
  251. }
  252. // 显示新手引导
  253. if let toolbarView = (win.contentViewController as? KMEditPDFPopToolBarController)?.toolbarView {
  254. self.showGuideView(toolbarView)
  255. }
  256. }
  257. func hiddenPopWindow() {
  258. let win = KMEditPDFPopToolBarWindow.shared
  259. win.orderOut(nil)
  260. win.setIsVisible(false)
  261. self._kRemoveChildWindow(win)
  262. }
  263. func showCropComfirmWindow() {
  264. let winC = KMEditPDFCropComfirmWindowController.shared
  265. if KMEditPDFPopToolBarWindow.shared.isVisible {
  266. let winFrame = KMEditPDFPopToolBarWindow.shared.frame
  267. let x = winFrame.origin.x + (NSWidth(winFrame)-84) * 0.5
  268. let frame = NSMakeRect(x, winFrame.origin.y, 84, 44)
  269. winC.window?.setFrame(frame, display: true)
  270. } else {
  271. let area = (self.listView?.editingAreas().first as? CPDFEditArea)
  272. let areaBounds = (self.listView?.convert(area!.bounds, from: area!.page) as? NSRect) ?? .zero
  273. let positioningView = self.listView
  274. let winFrame = positioningView?.window?.frame ?? .zero
  275. let toView: NSView? = nil
  276. var position = positioningView?.convert(areaBounds.origin, to: toView) ?? .zero
  277. position.x += winFrame.origin.x
  278. position.y += winFrame.origin.y
  279. position.y += areaBounds.size.height
  280. position.y += 26
  281. let x = position.x + (NSWidth(areaBounds)-84) * 0.5
  282. let frame = NSMakeRect(x, position.y, 84, 44)
  283. winC.window?.setFrame(frame, display: true)
  284. }
  285. winC.showWindow(nil)
  286. self._kAddchildwindow(winC.window!)
  287. winC.itemAction = { [weak self] idx, _ in
  288. if idx == 0 { // 确认
  289. self?.cropComfirmAction()
  290. } else if idx == 1 { // 取消
  291. self?.cropCancelAction()
  292. }
  293. }
  294. }
  295. func hiddenCropComfirmWindow() {
  296. let winC = KMEditPDFCropComfirmWindowController.shared
  297. winC.window?.orderOut(nil)
  298. winC.window?.setIsVisible(false)
  299. self._kRemoveChildWindow(winC.window)
  300. }
  301. func showGuideView(_ view: NSView) {
  302. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  303. if KMGuideInfoWindowController.availableShow(.editPDFPopWindow) {
  304. var winFrame = self.viewC?.view.window?.frame ?? .zero
  305. winFrame.size.height -= 20
  306. let area = (self.listView?.editingAreas().first as? CPDFEditArea)
  307. let areaBounds = (self.listView?.convert(area!.bounds, from: area!.page) as? NSRect) ?? .zero
  308. let guideWC = KMGuideInfoWindowController.currentWC()
  309. guideWC.type = .editPDFPopWindow
  310. var viewFrame = areaBounds
  311. let tmpY = areaBounds.origin.y+(areaBounds.size.height-KMEditPDFPopGuideView.kHeight+80)
  312. if tmpY < 50 {
  313. guideWC.editPDFPopWindowFlag = true
  314. viewFrame.origin.y += (areaBounds.size.height)
  315. viewFrame.origin.x += (areaBounds.size.width*0.5+KMEditPDFPopGuideView.kWidth*0.5)
  316. } else {
  317. guideWC.editPDFPopWindowFlag = false
  318. viewFrame.origin.y += (areaBounds.size.height-KMEditPDFPopGuideView.kHeight+80)
  319. viewFrame.origin.x += (areaBounds.size.width*0.5+KMEditPDFPopGuideView.kWidth*0.5)
  320. }
  321. guideWC.digitalBoxRect = viewFrame
  322. var beh = view.window?.collectionBehavior ?? []
  323. beh.insert(.canJoinAllSpaces)
  324. guideWC.window?.collectionBehavior = beh
  325. guideWC.window?.setFrame(winFrame, display: false)
  326. guideWC.window?.minSize = winFrame.size
  327. guideWC.window?.maxSize = winFrame.size
  328. self._kAddchildwindow(guideWC.window!)
  329. guideWC.show()
  330. DispatchQueue.main.async {
  331. guideWC.interfaceThemeDidChanged(NSApp.appearance?.name ?? .aqua)
  332. }
  333. guideWC.settingCallback = {
  334. KMPreferenceController.shared.showWindow(nil)
  335. }
  336. }
  337. }
  338. }
  339. }
  340. // MARK: - Private Methods
  341. extension KMEditPDfHanddler {
  342. private func _kAddchildwindow(_ childW: NSWindow?) {
  343. guard let win = childW else {
  344. return
  345. }
  346. self.viewC?.view.window?.addChildWindow(win, ordered: .above)
  347. }
  348. private func _kRemoveChildWindow(_ childW: NSWindow?) {
  349. guard let win = childW else {
  350. return
  351. }
  352. let contains = self.viewC?.view.window?.childWindows?.contains(win) ?? false
  353. if contains {
  354. self.viewC?.view.window?.removeChildWindow(win)
  355. }
  356. }
  357. private func _addNotification() {
  358. NotificationCenter.default.addObserver(self, selector: #selector(_scrollViewDidScroll), name: NSScrollView.didLiveScrollNotification, object: self.listView?.documentView())
  359. }
  360. private func _removeNotification() {
  361. NotificationCenter.default.removeObserver(self, name: NSScrollView.didLiveScrollNotification, object: self.listView?.documentView())
  362. }
  363. @objc private func _scrollViewDidScroll(_ noti: Notification) {
  364. if let data = self.listView?.documentView().isEqual(to: noti.object), data {
  365. let win = KMEditPDFPopToolBarWindow.shared
  366. if win.isVisible == false {
  367. return
  368. }
  369. guard let area = (self.listView?.editingAreas().first as? CPDFEditArea) else {
  370. self.hiddenPopWindow()
  371. return
  372. }
  373. let areaBounds = (self.listView?.convert(area.bounds, from: area.page) as? NSRect) ?? .zero
  374. let winFrame = self.listView?.window?.frame ?? .zero
  375. let view: NSView? = nil
  376. var position = self.listView?.convert(areaBounds.origin, to: view) ?? .zero
  377. position.x += winFrame.origin.x
  378. position.y += winFrame.origin.y
  379. position.y += areaBounds.size.height
  380. position.y += 26
  381. let wframe = NSMakeRect(position.x, position.y, win.frame.size.width, 44)
  382. win.setFrame(wframe, display: true)
  383. }
  384. }
  385. }
  386. // MARK: - Tools
  387. extension KMEditPDfHanddler {
  388. func editAreasIsEmpty() -> Bool {
  389. return self.editingAreas.isEmpty
  390. }
  391. func editAreasHavTextArea() -> Bool {
  392. return self.editingTextAreas.isEmpty == false
  393. }
  394. func editAreasHavImageArea() -> Bool {
  395. return self.editingImageAreas.isEmpty == false
  396. }
  397. func editAreasBoundsIsEqualForWidth() -> Bool {
  398. if self.editAreasIsEmpty() {
  399. return false
  400. }
  401. let rects = self._editAreasBounds()
  402. if rects.count == 1 {
  403. return true
  404. }
  405. let width = rects.first?.width ?? 0
  406. for (i, rect) in rects.enumerated() {
  407. if i == 0 {
  408. continue
  409. }
  410. if width != rect.size.width {
  411. return false
  412. }
  413. }
  414. return true
  415. }
  416. func editAreasBoundsIsEqualForHeight() -> Bool {
  417. if self.editAreasIsEmpty() {
  418. return false
  419. }
  420. let rects = self._editAreasBounds()
  421. if rects.count == 1 {
  422. return true
  423. }
  424. let height = rects.first?.height ?? 0
  425. for (i, rect) in rects.enumerated() {
  426. if i == 0 {
  427. continue
  428. }
  429. if height != rect.size.height {
  430. return false
  431. }
  432. }
  433. return true
  434. }
  435. func editAreasRotateIsEqual() -> Bool {
  436. if self.editAreasIsEmpty() {
  437. return false
  438. }
  439. if self.editAreasHavTextArea() {
  440. return false
  441. }
  442. let datas = self._editAreasRotates()
  443. if datas.count == 1 {
  444. return true
  445. }
  446. let data = datas.first ?? 0
  447. for (i, d) in datas.enumerated() {
  448. if i == 0 {
  449. continue
  450. }
  451. if data != d {
  452. return false
  453. }
  454. }
  455. return true
  456. }
  457. func editAreasOpacityIsEqual() -> Bool {
  458. if self.editAreasIsEmpty() {
  459. return false
  460. }
  461. let datas = self._editAreasOpacitys()
  462. if datas.count == 1 {
  463. return true
  464. }
  465. let data = datas.first ?? 0
  466. for (i, d) in datas.enumerated() {
  467. if i == 0 {
  468. continue
  469. }
  470. if data != d {
  471. return false
  472. }
  473. }
  474. return true
  475. }
  476. private func _editAreasBounds() -> [NSRect] {
  477. var rects: [NSRect] = []
  478. for area in self.editingAreas {
  479. rects.append(area.bounds)
  480. }
  481. return rects
  482. }
  483. private func _editAreasRotates() -> [CGFloat] {
  484. var arr: [CGFloat] = []
  485. for area in self.editingImageAreas {
  486. if let data = self.listView?.getRotateWith(area) {
  487. arr.append(data)
  488. }
  489. }
  490. return arr
  491. }
  492. private func _editAreasOpacitys() -> [CGFloat] {
  493. var arr: [CGFloat] = []
  494. for area in self.editingAreas {
  495. if let data = self.listView?.opacity(for: area) {
  496. arr.append(data)
  497. }
  498. }
  499. return arr
  500. }
  501. }
  502. // MARK: - Action
  503. extension KMEditPDfHanddler {
  504. func fontColorAction(color: NSColor?) {
  505. guard let theColor = color else {
  506. return
  507. }
  508. let editingAreas = self.editingAreas
  509. for area in editingAreas {
  510. if let data = area as? CPDFEditTextArea {
  511. self.listView?.setEditingSelectionFontColor(theColor, with: data)
  512. }
  513. }
  514. }
  515. func fontStyleAction(fontName: String?) {
  516. guard let font = CPDFFont.mappingFont(withFontString: fontName) else {
  517. return
  518. }
  519. let editingAreas = self.editingAreas
  520. for area in editingAreas {
  521. if let data = area as? CPDFEditTextArea {
  522. self.listView?.setEditSelectionCFont(font, with: data)
  523. }
  524. }
  525. }
  526. func fontAddAction() {
  527. let editingAreas = self.editingAreas
  528. if editingAreas.isEmpty {
  529. return
  530. }
  531. guard let area = editingAreas.last else {
  532. return
  533. }
  534. if area.isTextArea() == false {
  535. return
  536. }
  537. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  538. self.listView?.setEditingSelectionFontSize(fontSize+1, with: area as! CPDFEditTextArea, isAutoSize: false)
  539. }
  540. }
  541. func fontReduceAction() {
  542. let editingAreas = self.editingAreas
  543. if editingAreas.isEmpty {
  544. return
  545. }
  546. guard let area = editingAreas.last else {
  547. return
  548. }
  549. if area.isTextArea() == false {
  550. return
  551. }
  552. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  553. self.listView?.setEditingSelectionFontSize(fontSize-1, with: area as! CPDFEditTextArea, isAutoSize: false)
  554. }
  555. }
  556. func fontBoldAction() {
  557. let editingAreas = self.editingAreas
  558. if editingAreas.isEmpty {
  559. return
  560. }
  561. guard let area = editingAreas.last else {
  562. return
  563. }
  564. if area.isTextArea() == false {
  565. return
  566. }
  567. self.listView?.setCurrentSelectionIsBold(true, with: area as! CPDFEditTextArea)
  568. }
  569. func fontItalicAction() {
  570. let editingAreas = self.editingAreas
  571. if editingAreas.isEmpty {
  572. return
  573. }
  574. guard let area = editingAreas.last else {
  575. return
  576. }
  577. if area.isTextArea() == false {
  578. return
  579. }
  580. self.listView?.setCurrentSelectionIsItalic(true, with: area as! CPDFEditTextArea)
  581. }
  582. func textAlignmentAction(align: NSTextAlignment) {
  583. let editingAreas = self.editingAreas
  584. for area in editingAreas {
  585. if let data = area as? CPDFEditTextArea {
  586. self.listView?.setCurrentSelectionAlignment(align, with: data)
  587. }
  588. }
  589. }
  590. func leftRotateAction() {
  591. let editingAreas = self.editingAreas
  592. if editingAreas.isEmpty {
  593. return
  594. }
  595. guard let area = editingAreas.last as? CPDFEditImageArea else {
  596. return
  597. }
  598. // if let ang = self.listView?.getRotateWith(area) {
  599. // self.listView?.rotate(with: area, rotate: ang-90)
  600. // }
  601. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Rotate"])
  602. self.listView?.rotate(with: area, rotate: -90)
  603. // var frame = area.bounds
  604. // frame.size.width += 100
  605. // self.listView?.setBoundsEditArea(area, withBounds: frame)
  606. // 文档拖拽、tooltip、多选场景、暗黑场景
  607. // if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  608. // self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  609. // }
  610. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  611. // self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  612. }
  613. func rightRotateAction() {
  614. let editingAreas = self.editingAreas
  615. if editingAreas.isEmpty {
  616. return
  617. }
  618. guard let area = editingAreas.last as? CPDFEditImageArea else {
  619. return
  620. }
  621. self.listView?.rotate(with: area, rotate: 90)
  622. }
  623. func reverseXAction() {
  624. let editingAreas = self.editingAreas
  625. if editingAreas.isEmpty {
  626. return
  627. }
  628. for area in editingAreas {
  629. if let data = area as? CPDFEditImageArea {
  630. self.listView?.horizontalMirror(with: data)
  631. }
  632. }
  633. }
  634. func reverseYAction() {
  635. let editingAreas = self.editingAreas
  636. if editingAreas.isEmpty {
  637. return
  638. }
  639. for area in editingAreas {
  640. if let data = area as? CPDFEditImageArea {
  641. self.listView?.verticalMirror(with: data)
  642. }
  643. }
  644. }
  645. func cropAction() {
  646. let editingAreas = self.editingAreas
  647. if editingAreas.isEmpty {
  648. return
  649. }
  650. self.listView?.isEditImage = true
  651. for area in editingAreas {
  652. if let data = area as? CPDFEditImageArea {
  653. self.listView?.enterCrop(with: data)
  654. }
  655. }
  656. self.hiddenPopWindow()
  657. Task { @MainActor in
  658. self.showCropComfirmWindow()
  659. }
  660. self.rightViewC?.eidtPDFImageProperty.updateButtonState(hidden: false)
  661. }
  662. func cropCancelAction() {
  663. self.hiddenCropComfirmWindow()
  664. let areas = self.editingAreas
  665. if areas.isEmpty {
  666. return
  667. }
  668. for area in areas {
  669. if let data = area as? CPDFEditImageArea {
  670. self.listView?.exitCrop(with: data)
  671. }
  672. }
  673. self.listView?.cropAreas = nil
  674. self.listView?.isEditImage = false
  675. self.rightViewC?.eidtPDFImageProperty.updateButtonState(hidden: true)
  676. Task { @MainActor [weak self] in
  677. if let data = self?.listView?.selectImageAreas {
  678. self?.showPopWindow(positionRect:data.bounds)
  679. }
  680. }
  681. }
  682. func cropComfirmAction() {
  683. guard let selectImageAreas = self.listView?.selectImageAreas else {
  684. self.cropCancelAction()
  685. return
  686. }
  687. self.listView?.cropEditImageArea(selectImageAreas, withBounds: self.listView?.cropAreas.cropRect ?? .zero)
  688. self.cropCancelAction()
  689. }
  690. func replaceAction() {
  691. let editingAreas = self.editingAreas
  692. if editingAreas.isEmpty {
  693. return
  694. }
  695. let panel = NSOpenPanel()
  696. panel.allowsMultipleSelection = false
  697. panel.allowedFileTypes = ["png","jpg"]
  698. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  699. if response == .OK {
  700. let openPath = panel.url?.path
  701. for area in editingAreas {
  702. if let data = area as? CPDFEditImageArea {
  703. // , rect: data.bounds
  704. self.listView?.replace(data, imagePath: openPath!)
  705. }
  706. }
  707. }
  708. }
  709. }
  710. func exportAction() {
  711. let editingAreas = self.editingAreas
  712. if editingAreas.isEmpty {
  713. return
  714. }
  715. var imagesAreas: [CPDFEditImageArea] = []
  716. for area in editingAreas {
  717. if let data = area as? CPDFEditImageArea {
  718. imagesAreas.append(data)
  719. }
  720. }
  721. if imagesAreas.count == 1 {
  722. let panel = NSSavePanel()
  723. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  724. panel.isExtensionHidden = true
  725. let response = panel.runModal()
  726. if response == .OK {
  727. let url = panel.url
  728. if FileManager.default.fileExists(atPath: url!.path) {
  729. try?FileManager.default.removeItem(atPath: url!.path)
  730. }
  731. let result = self.listView?.extractImage(with: imagesAreas.first, toImagePath: url!.path) ?? false
  732. if result {
  733. NSWorkspace.shared.activateFileViewerSelecting([url!])
  734. }
  735. }
  736. } else if imagesAreas.count > 1 {
  737. let panel = NSOpenPanel()
  738. panel.canChooseFiles = false
  739. panel.canChooseDirectories = true
  740. panel.canCreateDirectories = true
  741. panel.allowsMultipleSelection = false
  742. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  743. if response == .OK {
  744. let outputURL = panel.url
  745. let s = self.listView?.document?.documentURL.lastPathComponent
  746. let folderPath = (self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? "") + "_extract"
  747. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  748. var i = 1
  749. let testFilePath = filePath
  750. while FileManager.default.fileExists(atPath: filePath!) {
  751. filePath = testFilePath! + "\(i)"
  752. i = i + 1
  753. }
  754. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  755. var saveURLs : [URL] = []
  756. for j in 0 ... imagesAreas.count-1 {
  757. let documentFileName = self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  758. var outPath = filePath!
  759. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  760. outPath = outPath + "page \(j+1)"
  761. outPath = outPath.stringByAppendingPathExtension("jpg")
  762. let result = self.listView?.extractImage(with: imagesAreas[j], toImagePath: outPath) ?? false
  763. if result {
  764. saveURLs.append(URL(fileURLWithPath: outPath))
  765. }
  766. }
  767. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  768. }
  769. }
  770. }
  771. }
  772. func alignmentAction(align: CPDFActiveAreasAlignType) {
  773. KMPrint("updateFormAearsAlignMangent")
  774. let stype = align
  775. let editingAreas = self.editingAreas
  776. if editingAreas.count >= 2 {
  777. var zeroRect = NSRect.null
  778. var highestRect = NSZeroRect
  779. var widthestRect = NSZeroRect
  780. let fristArea : CPDFEditArea = editingAreas.first as! CPDFEditArea
  781. var leftestRect = fristArea.bounds
  782. var rightestRect = fristArea.bounds
  783. var topestRect = fristArea.bounds
  784. var bottomestRect = fristArea.bounds
  785. var leftestArea : CPDFEditArea = fristArea
  786. var rightestArea : CPDFEditArea = fristArea
  787. var topestArea : CPDFEditArea = fristArea
  788. var bottomestArea : CPDFEditArea = fristArea
  789. var totalWidth = 0.0
  790. var totalHeight = 0.0
  791. for i in 0 ... editingAreas.count-1 {
  792. let area : CPDFEditArea = editingAreas[i] as! CPDFEditArea
  793. zeroRect = zeroRect.union(area.bounds)
  794. totalWidth = totalWidth + area.bounds.width
  795. totalHeight = totalHeight + area.bounds.height
  796. if area.bounds.height > highestRect.height {
  797. highestRect = area.bounds
  798. }
  799. if area.bounds.width > widthestRect.width {
  800. widthestRect = area.bounds
  801. }
  802. if leftestRect.minX > area.bounds.minX {
  803. leftestRect = area.bounds
  804. leftestArea = area
  805. }
  806. if area.bounds.maxX > rightestRect.maxX {
  807. rightestRect = area.bounds
  808. rightestArea = area
  809. }
  810. if area.bounds.maxY > topestRect.maxY {
  811. topestRect = area.bounds
  812. topestArea = area
  813. }
  814. if bottomestRect.minY > area.bounds.minY {
  815. bottomestRect = area.bounds
  816. bottomestArea = area
  817. }
  818. }
  819. var resultAreasArray: [Any] = []
  820. var newBoundsArray: [String] = []
  821. if stype == .Left {
  822. for i in 0 ... editingAreas.count-1 {
  823. let areas = editingAreas[i] as! CPDFEditArea
  824. var bounds = areas.bounds
  825. bounds.origin.x = zeroRect.origin.x
  826. newBoundsArray.append(NSStringFromRect(bounds))
  827. }
  828. resultAreasArray = editingAreas
  829. } else if stype == .Right {
  830. for i in 0 ... editingAreas.count-1 {
  831. let areas = editingAreas[i] as! CPDFEditArea
  832. var bounds = areas.bounds
  833. bounds.origin.x = zeroRect.maxX - bounds.size.width
  834. newBoundsArray.append(NSStringFromRect(bounds))
  835. }
  836. resultAreasArray = editingAreas
  837. } else if stype == .Top {
  838. for i in 0 ... editingAreas.count-1 {
  839. let areas = editingAreas[i] as! CPDFEditArea
  840. var bounds = areas.bounds
  841. bounds.origin.y = zeroRect.maxY - bounds.size.height
  842. newBoundsArray.append(NSStringFromRect(bounds))
  843. }
  844. resultAreasArray = editingAreas
  845. } else if stype == .Bottom {
  846. for i in 0 ... editingAreas.count-1 {
  847. let areas = editingAreas[i] as! CPDFEditArea
  848. var bounds = areas.bounds
  849. bounds.origin.y = zeroRect.minY
  850. newBoundsArray.append(NSStringFromRect(bounds))
  851. }
  852. resultAreasArray = editingAreas
  853. } else if stype == .Horizontally {
  854. for i in 0 ... editingAreas.count-1 {
  855. let areas = editingAreas[i] as! CPDFEditArea
  856. var bounds = areas.bounds
  857. bounds.origin.y = highestRect.midY - bounds.height/2
  858. newBoundsArray.append(NSStringFromRect(bounds))
  859. }
  860. resultAreasArray = editingAreas
  861. } else if stype == .Vertical {
  862. for i in 0 ... editingAreas.count-1 {
  863. let areas = editingAreas[i] as! CPDFEditArea
  864. var bounds = areas.bounds
  865. bounds.origin.x = widthestRect.midX - bounds.width/2
  866. newBoundsArray.append(NSStringFromRect(bounds))
  867. }
  868. resultAreasArray = editingAreas
  869. } else if stype == .DisHorizontally {
  870. let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
  871. let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
  872. let gap = (middleGap - otherAreasTotalWidth)/CGFloat(editingAreas.count - 1)
  873. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  874. areasCopyArray.sorted(by: { obj1, obj2 in
  875. let area1 = obj1
  876. let area2 = obj2
  877. if area1.bounds.origin.x < area2.bounds.origin.x {
  878. return true
  879. } else {
  880. return false
  881. }
  882. })
  883. if let index = areasCopyArray.firstIndex(of: leftestArea) {
  884. areasCopyArray.remove(at: index)
  885. }
  886. if let index = areasCopyArray.firstIndex(of: rightestArea) {
  887. areasCopyArray.remove(at: index)
  888. }
  889. var leftStartX = leftestRect.maxX + gap
  890. for i in 0 ... areasCopyArray.count-1 {
  891. let areas = areasCopyArray[i]
  892. var bounds = areas.bounds
  893. bounds.origin.x = leftStartX
  894. newBoundsArray.append(NSStringFromRect(bounds))
  895. leftStartX = leftStartX + bounds.width + gap
  896. }
  897. resultAreasArray = areasCopyArray
  898. } else if stype == .DisVertical {
  899. let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
  900. let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
  901. let gap = (middleGap - otherAreasTotalHeight)/CGFloat(editingAreas.count - 1)
  902. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  903. areasCopyArray.sorted(by: { obj1, obj2 in
  904. let area1 = obj1
  905. let area2 = obj2
  906. if area1.bounds.origin.x < area2.bounds.origin.x {
  907. return true
  908. } else {
  909. return false
  910. }
  911. })
  912. if let index = areasCopyArray.firstIndex(of: topestArea) {
  913. areasCopyArray.remove(at: index)
  914. }
  915. if let index = areasCopyArray.firstIndex(of: bottomestArea) {
  916. areasCopyArray.remove(at: index)
  917. }
  918. var bottomStartY = bottomestRect.maxY + gap
  919. for i in 0 ... areasCopyArray.count-1 {
  920. let areas = areasCopyArray[i]
  921. var bounds = areas.bounds
  922. bounds.origin.y = bottomStartY
  923. newBoundsArray.append(NSStringFromRect(bounds))
  924. bottomStartY = bottomStartY + bounds.height + gap
  925. }
  926. resultAreasArray = areasCopyArray
  927. }
  928. var oldBounds : [String] = []
  929. for i in 0 ... resultAreasArray.count-1 {
  930. let area : CPDFEditArea = resultAreasArray[i] as! CPDFEditArea
  931. oldBounds.append(NSStringFromRect(area.bounds))
  932. self.listView?.setBoundsEditArea(area, withBounds: NSRectFromString(newBoundsArray[i]))
  933. }
  934. self.listView?.setNeedsDisplayForVisiblePages()
  935. }
  936. }
  937. }
  938. // MARK: - CPDFViewDelegate
  939. extension KMEditPDfHanddler: CPDFViewDelegate {
  940. // 编辑区块已经改变
  941. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  942. let isEdited = self.listView?.isEdited() ?? false
  943. if isEdited {
  944. // 记录编辑状态
  945. self.viewC?.recordIsPDFDocumentEdited(type: .editText)
  946. }
  947. if annotationType != .addText {
  948. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView?.document)
  949. }
  950. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  951. if areas.isEmpty {
  952. self.hiddenPopWindow()
  953. let toolMode = self.listView?.toolMode ?? .none
  954. let annotationType = self.annotationType
  955. if toolMode == .editPDFToolMode {
  956. if annotationType == .addImage || annotationType == .addText {
  957. if self.isEditImage {
  958. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  959. } else {
  960. // if self.listView.annotationType == .addImage {
  961. // self.closeRightPane()
  962. // }
  963. if annotationType == .addImage {
  964. if self.rightViewC?.eidtPDFImageProperty != nil {
  965. self.rightViewC?.eidtPDFImageProperty.reloadData()
  966. }
  967. }
  968. // self.openRightPane()
  969. }
  970. } else {
  971. // self.viewC?.closeRightPane()
  972. self.rightViewC?.isHidden = true
  973. }
  974. } else {
  975. self.rightViewC?.isHidden = true
  976. self.viewC?.closeRightPane()
  977. if self.subViewType == .EditPDFAddText && annotationType == .addText {
  978. self.rightViewC?.eidtPDFTextProperty.initData()
  979. }
  980. }
  981. return
  982. }
  983. self.viewC?.model.isPDFTextImageEdited = true
  984. let subViewType = self.rightViewC?.subViewType ?? .None
  985. if self.annotationType == .addImage {
  986. var isImageArea = false
  987. for i in 0 ..< areas.count {
  988. if areas[i] is CPDFEditImageArea {
  989. isImageArea = true
  990. }
  991. }
  992. if isImageArea {
  993. self.rightViewC?.isHidden = false
  994. if self.subViewType == .EditPDFAddImage {
  995. self.rightViewC?.subViewType = .EditPDFAddImage
  996. self.rightViewC?.eidtPDFImageProperty.handdler = self
  997. self.rightViewC?.eidtPDFImageProperty.reloadData()
  998. }
  999. self.openRightPane()
  1000. } else {
  1001. self.rightViewC?.isHidden = true
  1002. // self.viewC?.closeRightPane()
  1003. }
  1004. } else if self.subViewType == .EditPDFAddText && annotationType == .addText {
  1005. self.rightViewC?.isHidden = false
  1006. let count = self.listView?.editingSelectionString()?.count ?? 0
  1007. if count != 0 {
  1008. self.rightViewC?.eidtPDFTextProperty.reloadData()
  1009. } else {
  1010. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  1011. }
  1012. self.openRightPane()
  1013. } else {
  1014. var textsAreas : [CPDFEditTextArea] = []
  1015. var imagesAreas : [CPDFEditImageArea] = []
  1016. let count = self.listView?.editingAreas()?.count ?? 0
  1017. if count < 1 {
  1018. return
  1019. }
  1020. for i in 0 ..< areas.count {
  1021. if areas[i] is CPDFEditTextArea {
  1022. textsAreas.append(areas[i] as! CPDFEditTextArea)
  1023. }
  1024. if areas[i] is CPDFEditImageArea {
  1025. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  1026. }
  1027. }
  1028. if textsAreas.count > 0 && textsAreas.count == areas.count {
  1029. self.rightViewC?.isHidden = false
  1030. self.rightViewC?.subViewType = .EditPDFAddText
  1031. self.rightViewC?.eidtPDFTextProperty?.reloadData()
  1032. self.openRightPane()
  1033. } else if imagesAreas.count > 0 {
  1034. self.rightViewC?.isHidden = false
  1035. self.rightViewC?.subViewType = .EditPDFAddImage
  1036. self.rightViewC?.eidtPDFImageProperty.handdler = self
  1037. self.rightViewC?.eidtPDFImageProperty?.reloadData()
  1038. self.openRightPane()
  1039. }
  1040. }
  1041. var flag: CPDFEditArea?
  1042. for area in areas {
  1043. if flag == nil {
  1044. flag = area
  1045. continue
  1046. }
  1047. if let data = flag, data.bounds.maxY < area.bounds.maxY {
  1048. flag = area
  1049. }
  1050. }
  1051. if let data = flag {
  1052. self.showPopWindow(positionRect: data.bounds)
  1053. }
  1054. }
  1055. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  1056. if editArea != nil && (editArea is CPDFEditImageArea){
  1057. self.listView?.cropAreas = editArea as? CPDFEditImageArea
  1058. }
  1059. }
  1060. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1061. if self.isEditImage {
  1062. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1063. } else {
  1064. let panel = NSOpenPanel()
  1065. panel.allowsMultipleSelection = false
  1066. panel.allowedFileTypes = ["png","jpg"]
  1067. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1068. if response == .OK {
  1069. var filePath = panel.url?.path
  1070. var image = NSImage.init(contentsOf: panel.url!)
  1071. //图片自适应范围
  1072. if image != nil {
  1073. var imageRect = rect
  1074. let imageSize = image!.size
  1075. var previewSize = rect.size
  1076. var isChangeSize = false
  1077. if previewSize.width == 0 && previewSize.height == 0 {
  1078. previewSize = CGSize(width: 500, height: 500)
  1079. isChangeSize = true
  1080. }
  1081. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  1082. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  1083. if isChangeSize {
  1084. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  1085. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  1086. } else {
  1087. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  1088. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  1089. }
  1090. imageRect.size = newSize
  1091. let limitWidth = 1920.0
  1092. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  1093. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  1094. targetSize: CGSize(width: limitWidth, height: limitWidth),
  1095. maxSizeInBytes: 1024 * 1024 * 5,
  1096. targetCompression: 1.0)
  1097. }
  1098. //自适应page
  1099. let pageRect = self.listView?.currentPage().bounds ?? .zero
  1100. if imageRect.width > pageRect.width ||
  1101. imageRect.height > pageRect.height {
  1102. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  1103. imageRect = CGRect(x: imageRect.origin.x,
  1104. y: imageRect.origin.y,
  1105. width: imageRect.width * pageScale,
  1106. height: imageRect.height * pageScale)
  1107. }
  1108. if imageRect.origin.x < 0 {
  1109. imageRect.origin.x = 5
  1110. }
  1111. if imageRect.origin.y < 0 {
  1112. imageRect.origin.y = 5
  1113. }
  1114. if imageRect.origin.x + imageRect.width > pageRect.width ||
  1115. imageRect.origin.y + imageRect.height > pageRect.height {
  1116. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  1117. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  1118. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  1119. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  1120. }
  1121. DispatchQueue.main.async {
  1122. self.listView?.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  1123. self.viewC?.model.isPDFTextImageEdited = true
  1124. self.viewC?.recordIsPDFDocumentEdited(type: .editImage)
  1125. self.showPopWindow(positionRect: imageRect)
  1126. }
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1133. let window = KMEditPDFPopToolBarWindow.shared
  1134. if (window.isVisible) {
  1135. self.hiddenPopWindow()
  1136. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  1137. if let area = areas.last {
  1138. if let data = area as? CPDFEditTextArea {
  1139. if let str = data.editTextAreaString(), str.isEmpty {
  1140. self.listView?.remove(with: [area])
  1141. }
  1142. }
  1143. }
  1144. return
  1145. }
  1146. var newRect = rect
  1147. if CGSizeEqualToSize(rect.size, .zero) {
  1148. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  1149. } else {
  1150. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  1151. }
  1152. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1153. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  1154. let fontSize = model.fontSize
  1155. let fontColor = model.color
  1156. let fontAlign = model.alignment
  1157. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  1158. NSColorPanel.shared.color = fontColor
  1159. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  1160. let attri = CEditAttributes()
  1161. attri.font = font
  1162. attri.fontColor = fontColor
  1163. attri.alignment = fontAlign
  1164. self.listView?.createStringBounds(newRect, with: attri, page: page)
  1165. // self.rightViewC != nil &&
  1166. if self.subViewType == .EditPDFAddText && self.annotationType == .addText {
  1167. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  1168. }
  1169. self.showPopWindow(positionRect: newRect)
  1170. }
  1171. // 文本区块 选中文本已经变化
  1172. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1173. // self.viewC?.rightSideViewController != nil &&
  1174. if self.subViewType == .EditPDFAddText {
  1175. self.rightViewC?.eidtPDFTextProperty.reloadData()
  1176. self.rightViewC?.eidtPDFTextProperty.updateTextTextPresuppositionState()
  1177. }
  1178. }
  1179. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  1180. let areas = self.editingAreas
  1181. if areas.count == 1 {
  1182. if let data = areas.first as? CPDFEditImageArea {
  1183. let updating = self.listView?.editAreaBoundUpdating ?? false
  1184. if updating {
  1185. self.listView?.editAreaBoundUpdating = false
  1186. } else {
  1187. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1188. }
  1189. }
  1190. }
  1191. }
  1192. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  1193. }
  1194. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1195. self.hiddenPopWindow()
  1196. }
  1197. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1198. }
  1199. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1200. self.showPopWindow(positionRect: .zero)
  1201. }
  1202. }