KMEditPDfHanddler.swift 58 KB

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