KMEditPDfHanddler.swift 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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.hiddenPopWindow()
  366. self.hiddenCropComfirmWindow()
  367. self._removeNotification()
  368. KMColorPanelCloseIfNeed()
  369. }
  370. }
  371. // MARK: - Private Methods
  372. extension KMEditPDfHanddler {
  373. private func _kAddchildwindow(_ childW: NSWindow?) {
  374. guard let win = childW else {
  375. return
  376. }
  377. self.viewC?.view.window?.addChildWindow(win, ordered: .above)
  378. }
  379. private func _kRemoveChildWindow(_ childW: NSWindow?) {
  380. guard let win = childW else {
  381. return
  382. }
  383. let contains = self.viewC?.view.window?.childWindows?.contains(win) ?? false
  384. if contains {
  385. self.viewC?.view.window?.removeChildWindow(win)
  386. }
  387. }
  388. private func _addNotification() {
  389. NotificationCenter.default.addObserver(self, selector: #selector(_scrollViewDidScroll), name: NSScrollView.didLiveScrollNotification, object: self.listView?.documentView())
  390. }
  391. private func _removeNotification() {
  392. NotificationCenter.default.removeObserver(self, name: NSScrollView.didLiveScrollNotification, object: self.listView?.documentView())
  393. }
  394. @objc private func _scrollViewDidScroll(_ noti: Notification) {
  395. if let data = self.listView?.documentView().isEqual(to: noti.object), data {
  396. let win = KMEditPDFPopToolBarWindow.shared
  397. if win.isVisible == false {
  398. // return
  399. }
  400. guard let area = (self.listView?.editingAreas()?.first as? CPDFEditArea) else {
  401. self.hiddenPopWindow()
  402. self.hiddenCropComfirmWindow()
  403. return
  404. }
  405. let isEditImage = self.listView?.isEditImage ?? false
  406. if isEditImage {
  407. let winC = KMEditPDFCropComfirmWindowController.shared
  408. let winW: CGFloat = 84
  409. let areaBounds = (self.listView?.convert(area.bounds, from: area.page) as? NSRect) ?? .zero
  410. let winFrame = self.listView?.window?.frame ?? .zero
  411. let view: NSView? = nil
  412. var position = self.listView?.convert(areaBounds.origin, to: view) ?? .zero
  413. position.x += winFrame.origin.x
  414. position.y += winFrame.origin.y
  415. position.y += areaBounds.size.height
  416. position.y += 26
  417. var x = position.x + (NSWidth(areaBounds)-84) * 0.5
  418. // let frame = NSMakeRect(x, position.y, 84, 44)
  419. // position.x += (areaBounds.size.width*0.5-win.frame.size.width*0.5)
  420. x = max(0, x)
  421. var y = max(0, position.y)
  422. let screenFrame = NSScreen.main?.frame ?? .zero
  423. if y + 44 + 40 >= screenFrame.size.height {
  424. y = screenFrame.size.height - 44 - 40
  425. }
  426. let wframe = NSMakeRect(x, y, winW, 44)
  427. winC.window?.setFrame(wframe, display: true)
  428. if CGRectContainsRect(winFrame, wframe) == false {
  429. self.hiddenCropComfirmWindow()
  430. } else {
  431. self.showCropComfirmWindow()
  432. }
  433. return
  434. }
  435. let areaBounds = (self.listView?.convert(area.bounds, from: area.page) as? NSRect) ?? .zero
  436. let winFrame = self.listView?.window?.frame ?? .zero
  437. let view: NSView? = nil
  438. var position = self.listView?.convert(areaBounds.origin, to: view) ?? .zero
  439. position.x += winFrame.origin.x
  440. position.y += winFrame.origin.y
  441. position.y += areaBounds.size.height
  442. position.y += 26
  443. position.x += (areaBounds.size.width*0.5-win.frame.size.width*0.5)
  444. let x = max(0, position.x)
  445. var y = max(0, position.y)
  446. let screenFrame = NSScreen.main?.frame ?? .zero
  447. if y + 44 + 40 >= screenFrame.size.height {
  448. y = screenFrame.size.height - 44 - 40
  449. }
  450. let wframe = NSMakeRect(x, y, win.frame.size.width, 44)
  451. win.setFrame(wframe, display: true)
  452. if CGRectContainsRect(winFrame, wframe) == false {
  453. self.hiddenPopWindow()
  454. }
  455. }
  456. }
  457. private func _reloadData_right_text() {
  458. self.rightViewC?.eidtPDFTextProperty.reloadData()
  459. }
  460. private func _reloadData_right_image() {
  461. self.rightViewC?.eidtPDFImageProperty.reloadData()
  462. }
  463. }
  464. // MARK: - Tools
  465. extension KMEditPDfHanddler {
  466. func editAreasIsEmpty() -> Bool {
  467. return self.editingAreas.isEmpty
  468. }
  469. func editAreasHavTextArea() -> Bool {
  470. return self.editingTextAreas.isEmpty == false
  471. }
  472. func editAreasHavImageArea() -> Bool {
  473. return self.editingImageAreas.isEmpty == false
  474. }
  475. func editAreasFontColorIsEqual() -> Bool {
  476. if self.editAreasIsEmpty() {
  477. return false
  478. }
  479. let datas = self._editAreasFontColors()
  480. if datas.count == 1 {
  481. return true
  482. }
  483. let color = datas.first
  484. for (i, d) in datas.enumerated() {
  485. if i == 0 {
  486. continue
  487. }
  488. if d != color {
  489. return false
  490. }
  491. }
  492. return true
  493. }
  494. func editAreasFontNameIsEqual() -> Bool {
  495. if self.editAreasIsEmpty() {
  496. return false
  497. }
  498. let datas = self._editAreasFontNames()
  499. if datas.count == 1 {
  500. return true
  501. }
  502. let data = datas.first
  503. for (i, d) in datas.enumerated() {
  504. if i == 0 {
  505. continue
  506. }
  507. if d != data {
  508. return false
  509. }
  510. }
  511. return true
  512. }
  513. func editAreasFontStyleIsEqual() -> Bool {
  514. if self.editAreasIsEmpty() {
  515. return false
  516. }
  517. let datas = self._editAreasFontStyles()
  518. if datas.count == 1 {
  519. return true
  520. }
  521. let data = datas.first
  522. for (i, d) in datas.enumerated() {
  523. if i == 0 {
  524. continue
  525. }
  526. if d != data {
  527. return false
  528. }
  529. }
  530. return true
  531. }
  532. func editAreasFontSizeIsEqual() -> Bool {
  533. if self.editAreasIsEmpty() {
  534. return false
  535. }
  536. let datas = self._editAreasFontSizes()
  537. if datas.count == 1 {
  538. return true
  539. }
  540. let data = datas.first
  541. for (i, d) in datas.enumerated() {
  542. if i == 0 {
  543. continue
  544. }
  545. if d != data {
  546. return false
  547. }
  548. }
  549. return true
  550. }
  551. func editAreasFontBoldIsEqual() -> Bool {
  552. if self.editAreasIsEmpty() {
  553. return false
  554. }
  555. let datas = self._editAreasFontBolds()
  556. if datas.count == 1 {
  557. return true
  558. }
  559. let data = datas.first
  560. for (i, d) in datas.enumerated() {
  561. if i == 0 {
  562. continue
  563. }
  564. if d != data {
  565. return false
  566. }
  567. }
  568. return true
  569. }
  570. func editAreasFontItalicIsEqual() -> Bool {
  571. if self.editAreasIsEmpty() {
  572. return false
  573. }
  574. let datas = self._editAreasFontItalics()
  575. if datas.count == 1 {
  576. return true
  577. }
  578. let data = datas.first
  579. for (i, d) in datas.enumerated() {
  580. if i == 0 {
  581. continue
  582. }
  583. if d != data {
  584. return false
  585. }
  586. }
  587. return true
  588. }
  589. func editAreasTextAlignmentIsEqual() -> Bool {
  590. if self.editAreasIsEmpty() {
  591. return false
  592. }
  593. let datas = self._editAreasTextAlignments()
  594. if datas.count == 1 {
  595. return true
  596. }
  597. let data = datas.first?.rawValue ?? 0
  598. for (i, d) in datas.enumerated() {
  599. if i == 0 {
  600. continue
  601. }
  602. if d.rawValue != data {
  603. return false
  604. }
  605. }
  606. return true
  607. }
  608. func editAreasBoundsIsEqualForWidth() -> Bool {
  609. if self.editAreasIsEmpty() {
  610. return false
  611. }
  612. let rects = self._editAreasBounds()
  613. if rects.count == 1 {
  614. return true
  615. }
  616. let width = rects.first?.width ?? 0
  617. for (i, rect) in rects.enumerated() {
  618. if i == 0 {
  619. continue
  620. }
  621. if width != rect.size.width {
  622. return false
  623. }
  624. }
  625. return true
  626. }
  627. func editAreasBoundsIsEqualForHeight() -> Bool {
  628. if self.editAreasIsEmpty() {
  629. return false
  630. }
  631. let rects = self._editAreasBounds()
  632. if rects.count == 1 {
  633. return true
  634. }
  635. let height = rects.first?.height ?? 0
  636. for (i, rect) in rects.enumerated() {
  637. if i == 0 {
  638. continue
  639. }
  640. if height != rect.size.height {
  641. return false
  642. }
  643. }
  644. return true
  645. }
  646. func editAreasRotateIsEqual() -> Bool {
  647. if self.editAreasIsEmpty() {
  648. return false
  649. }
  650. if self.editAreasHavTextArea() {
  651. return false
  652. }
  653. let datas = self._editAreasRotates()
  654. if datas.count == 1 {
  655. return true
  656. }
  657. let data = datas.first ?? 0
  658. for (i, d) in datas.enumerated() {
  659. if i == 0 {
  660. continue
  661. }
  662. if data != d {
  663. return false
  664. }
  665. }
  666. return true
  667. }
  668. func editAreasOpacityIsEqual() -> Bool {
  669. if self.editAreasIsEmpty() {
  670. return false
  671. }
  672. let datas = self._editAreasOpacitys()
  673. if datas.count == 1 {
  674. return true
  675. }
  676. let data = datas.first ?? 0
  677. for (i, d) in datas.enumerated() {
  678. if i == 0 {
  679. continue
  680. }
  681. if data != d {
  682. return false
  683. }
  684. }
  685. return true
  686. }
  687. private func _editAreasFontColors() -> [NSColor] {
  688. var datas: [NSColor] = []
  689. for area in self.editingTextAreas {
  690. if let data = self.listView?.editingSelectionFontColor(byRangeEdit: area) {
  691. datas.append(data)
  692. }
  693. }
  694. return datas
  695. }
  696. private func _editAreasFontStyles() -> [String] {
  697. var datas: [String] = []
  698. for area in self.editingTextAreas {
  699. if let data = self.listView?.editingSelectionCFont(byRangeEdit: area)?.styleName {
  700. datas.append(data)
  701. }
  702. }
  703. return datas
  704. }
  705. private func _editAreasFontNames() -> [String] {
  706. var datas: [String] = []
  707. for area in self.editingTextAreas {
  708. if let data = self.listView?.editingSelectionCFont(byRangeEdit: area)?.familyName {
  709. datas.append(data)
  710. }
  711. }
  712. return datas
  713. }
  714. private func _editAreasFontSizes() -> [CGFloat] {
  715. var datas: [CGFloat] = []
  716. for area in self.editingTextAreas {
  717. if let data = self.listView?.editingSelectionFontSize(byRangeEdit: area) {
  718. datas.append(data)
  719. }
  720. }
  721. return datas
  722. }
  723. private func _editAreasFontBolds() -> [Bool] {
  724. var datas: [Bool] = []
  725. for area in self.editingTextAreas {
  726. if let data = self.listView?.isBoldCurrentSelection(byRangeEdit: area) {
  727. datas.append(data)
  728. }
  729. }
  730. return datas
  731. }
  732. private func _editAreasFontItalics() -> [Bool] {
  733. var datas: [Bool] = []
  734. for area in self.editingTextAreas {
  735. if let data = self.listView?.isItalicCurrentSelection(byRangeEdit: area) {
  736. datas.append(data)
  737. }
  738. }
  739. return datas
  740. }
  741. private func _editAreasTextAlignments() -> [NSTextAlignment] {
  742. var datas: [NSTextAlignment] = []
  743. for area in self.editingTextAreas {
  744. if let data = self.listView?.currentSelectionAlignment(byRangeEdit: area) {
  745. datas.append(data)
  746. }
  747. }
  748. return datas
  749. }
  750. private func _editAreasBounds() -> [NSRect] {
  751. var rects: [NSRect] = []
  752. for area in self.editingAreas {
  753. rects.append(area.bounds)
  754. }
  755. return rects
  756. }
  757. private func _editAreasRotates() -> [CGFloat] {
  758. var arr: [CGFloat] = []
  759. for area in self.editingImageAreas {
  760. if let data = self.listView?.getRotateWith(area) {
  761. arr.append(data)
  762. }
  763. }
  764. return arr
  765. }
  766. private func _editAreasOpacitys() -> [CGFloat] {
  767. var arr: [CGFloat] = []
  768. for area in self.editingAreas {
  769. if let data = self.listView?.opacityByRange(for: area) {
  770. arr.append(data)
  771. }
  772. }
  773. return arr
  774. }
  775. }
  776. // MARK: - Action
  777. extension KMEditPDfHanddler {
  778. func fontColorAction(color: NSColor?) {
  779. guard let theColor = color else {
  780. return
  781. }
  782. let areas = self.editingTextAreas
  783. for area in areas {
  784. self.listView?.setEditingSelectionFontColor(theColor, with: area)
  785. }
  786. self._reloadData_right_text()
  787. }
  788. func fontStyleAction(fontName: String?) {
  789. guard let font = CPDFFont.mappingFont(withFontString: fontName) else {
  790. return
  791. }
  792. let areas = self.editingTextAreas
  793. for area in areas {
  794. self.listView?.setEditSelectionCFont(font, with: area)
  795. }
  796. self._reloadData_right_text()
  797. }
  798. func fontAddAction() {
  799. let areas = self.editingTextAreas
  800. for area in areas {
  801. if let fontSize = self.listView?.editingSelectionFontSize(byRangeEdit: area) {
  802. self.listView?.setEditingSelectionFontSize(fontSize+1, with: area, isAutoSize: false)
  803. }
  804. }
  805. self._reloadData_right_text()
  806. }
  807. func fontReduceAction() {
  808. let areas = self.editingTextAreas
  809. for area in areas {
  810. if let fontSize = self.listView?.editingSelectionFontSize(byRangeEdit: area) {
  811. self.listView?.setEditingSelectionFontSize(fontSize-1, with: area, isAutoSize: false)
  812. }
  813. }
  814. self._reloadData_right_text()
  815. }
  816. func fontBoldAction() {
  817. let areas = self.editingTextAreas
  818. for area in areas {
  819. if let data = self.listView?.isBoldCurrentSelection(byRangeEdit: area) {
  820. self.listView?.setCurrentSelectionIsBold(!data, with: area)
  821. }
  822. }
  823. self._reloadData_right_text()
  824. }
  825. func fontItalicAction() {
  826. let areas = self.editingTextAreas
  827. for area in areas {
  828. if let data = self.listView?.isItalicCurrentSelection(byRangeEdit: area) {
  829. self.listView?.setCurrentSelectionIsItalic(!data, with: area)
  830. }
  831. }
  832. self._reloadData_right_text()
  833. }
  834. func textAlignmentAction(align: NSTextAlignment) {
  835. let areas = self.editingTextAreas
  836. for area in areas {
  837. self.listView?.setCurrentSelectionAlignment(align, with: area)
  838. }
  839. self._reloadData_right_text()
  840. }
  841. func leftRotateAction() {
  842. let areas = self.editingImageAreas
  843. for area in areas {
  844. self.listView?.rotate(with: area, rotate: -90)
  845. }
  846. }
  847. func rightRotateAction() {
  848. let areas = self.editingImageAreas
  849. for area in areas {
  850. self.listView?.rotate(with: area, rotate: 90)
  851. }
  852. }
  853. func reverseXAction() {
  854. let areas = self.editingImageAreas
  855. for area in areas {
  856. self.listView?.horizontalMirror(with: area)
  857. }
  858. }
  859. func reverseYAction() {
  860. let areas = self.editingImageAreas
  861. for area in areas {
  862. self.listView?.verticalMirror(with: area)
  863. }
  864. }
  865. func cropAction() {
  866. let areas = self.editingImageAreas
  867. if areas.isEmpty {
  868. return
  869. }
  870. self.listView?.isEditImage = true
  871. for area in areas {
  872. self.listView?.enterCrop(with: area)
  873. }
  874. self.hiddenPopWindow()
  875. Task { @MainActor in
  876. self.showCropComfirmWindow()
  877. }
  878. self.rightViewC?.eidtPDFImageProperty.updateButtonState(hidden: false)
  879. }
  880. func cropCancelAction() {
  881. self.hiddenCropComfirmWindow()
  882. let areas = self.editingImageAreas
  883. if areas.isEmpty {
  884. return
  885. }
  886. for area in areas {
  887. self.listView?.exitCrop(with: area)
  888. }
  889. self.listView?.cropAreas = nil
  890. self.listView?.isEditImage = false
  891. self.rightViewC?.eidtPDFImageProperty.updateButtonState(hidden: true)
  892. Task { @MainActor [weak self] in
  893. if let data = self?.listView?.selectImageAreas {
  894. self?.showPopWindow(positionRect:data.bounds)
  895. }
  896. }
  897. }
  898. func cropComfirmAction() {
  899. guard let selectImageAreas = self.listView?.selectImageAreas else {
  900. self.cropCancelAction()
  901. return
  902. }
  903. self.listView?.cropEditImageArea(selectImageAreas, withBounds: self.listView?.cropAreas.cropRect ?? .zero)
  904. self.cropCancelAction()
  905. }
  906. func replaceAction() {
  907. let areas = self.editingImageAreas
  908. if areas.isEmpty {
  909. return
  910. }
  911. let panel = NSOpenPanel()
  912. panel.allowsMultipleSelection = false
  913. panel.allowedFileTypes = ["png","jpg"]
  914. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  915. if response == .OK {
  916. let openPath = panel.url?.path
  917. for area in areas {
  918. // , rect: data.bounds
  919. self.listView?.replace(area, imagePath: openPath!)
  920. }
  921. }
  922. }
  923. }
  924. func exportAction() {
  925. let areas = self.editingImageAreas
  926. if areas.isEmpty {
  927. return
  928. }
  929. if areas.count == 1 {
  930. let panel = NSSavePanel()
  931. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  932. panel.isExtensionHidden = true
  933. let response = panel.runModal()
  934. if response == .OK {
  935. let url = panel.url
  936. if FileManager.default.fileExists(atPath: url!.path) {
  937. try?FileManager.default.removeItem(atPath: url!.path)
  938. }
  939. let result = self.listView?.extractImage(with: areas.first, toImagePath: url!.path) ?? false
  940. if result {
  941. NSWorkspace.shared.activateFileViewerSelecting([url!])
  942. }
  943. }
  944. } else if areas.count > 1 {
  945. let panel = NSOpenPanel()
  946. panel.canChooseFiles = false
  947. panel.canChooseDirectories = true
  948. panel.canCreateDirectories = true
  949. panel.allowsMultipleSelection = false
  950. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  951. if response == .OK {
  952. let outputURL = panel.url
  953. let s = self.listView?.document?.documentURL.lastPathComponent
  954. let folderPath = (self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? "") + "_extract"
  955. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  956. var i = 1
  957. let testFilePath = filePath
  958. while FileManager.default.fileExists(atPath: filePath!) {
  959. filePath = testFilePath! + "\(i)"
  960. i = i + 1
  961. }
  962. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  963. var saveURLs : [URL] = []
  964. for j in 0 ..< areas.count {
  965. let documentFileName = self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  966. var outPath = filePath!
  967. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  968. outPath = outPath + "page \(j+1)"
  969. outPath = outPath.stringByAppendingPathExtension("jpg")
  970. let result = self.listView?.extractImage(with: areas[j], toImagePath: outPath) ?? false
  971. if result {
  972. saveURLs.append(URL(fileURLWithPath: outPath))
  973. }
  974. }
  975. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  976. }
  977. }
  978. }
  979. }
  980. func alignmentAction(align: CPDFActiveAreasAlignType) {
  981. KMPrint("updateFormAearsAlignMangent")
  982. let stype = align
  983. let editingAreas = self.editingAreas
  984. if editingAreas.count >= 2 {
  985. var zeroRect = NSRect.null
  986. var highestRect = NSZeroRect
  987. var widthestRect = NSZeroRect
  988. let fristArea : CPDFEditArea = editingAreas.first as! CPDFEditArea
  989. var leftestRect = fristArea.bounds
  990. var rightestRect = fristArea.bounds
  991. var topestRect = fristArea.bounds
  992. var bottomestRect = fristArea.bounds
  993. var leftestArea : CPDFEditArea = fristArea
  994. var rightestArea : CPDFEditArea = fristArea
  995. var topestArea : CPDFEditArea = fristArea
  996. var bottomestArea : CPDFEditArea = fristArea
  997. var totalWidth = 0.0
  998. var totalHeight = 0.0
  999. for i in 0 ... editingAreas.count-1 {
  1000. let area : CPDFEditArea = editingAreas[i] as! CPDFEditArea
  1001. zeroRect = zeroRect.union(area.bounds)
  1002. totalWidth = totalWidth + area.bounds.width
  1003. totalHeight = totalHeight + area.bounds.height
  1004. if area.bounds.height > highestRect.height {
  1005. highestRect = area.bounds
  1006. }
  1007. if area.bounds.width > widthestRect.width {
  1008. widthestRect = area.bounds
  1009. }
  1010. if leftestRect.minX > area.bounds.minX {
  1011. leftestRect = area.bounds
  1012. leftestArea = area
  1013. }
  1014. if area.bounds.maxX > rightestRect.maxX {
  1015. rightestRect = area.bounds
  1016. rightestArea = area
  1017. }
  1018. if area.bounds.maxY > topestRect.maxY {
  1019. topestRect = area.bounds
  1020. topestArea = area
  1021. }
  1022. if bottomestRect.minY > area.bounds.minY {
  1023. bottomestRect = area.bounds
  1024. bottomestArea = area
  1025. }
  1026. }
  1027. var resultAreasArray: [Any] = []
  1028. var newBoundsArray: [String] = []
  1029. if stype == .Left {
  1030. for i in 0 ... editingAreas.count-1 {
  1031. let areas = editingAreas[i] as! CPDFEditArea
  1032. var bounds = areas.bounds
  1033. bounds.origin.x = zeroRect.origin.x
  1034. newBoundsArray.append(NSStringFromRect(bounds))
  1035. }
  1036. resultAreasArray = editingAreas
  1037. } else if stype == .Right {
  1038. for i in 0 ... editingAreas.count-1 {
  1039. let areas = editingAreas[i] as! CPDFEditArea
  1040. var bounds = areas.bounds
  1041. bounds.origin.x = zeroRect.maxX - bounds.size.width
  1042. newBoundsArray.append(NSStringFromRect(bounds))
  1043. }
  1044. resultAreasArray = editingAreas
  1045. } else if stype == .Top {
  1046. for i in 0 ... editingAreas.count-1 {
  1047. let areas = editingAreas[i] as! CPDFEditArea
  1048. var bounds = areas.bounds
  1049. bounds.origin.y = zeroRect.maxY - bounds.size.height
  1050. newBoundsArray.append(NSStringFromRect(bounds))
  1051. }
  1052. resultAreasArray = editingAreas
  1053. } else if stype == .Bottom {
  1054. for i in 0 ... editingAreas.count-1 {
  1055. let areas = editingAreas[i] as! CPDFEditArea
  1056. var bounds = areas.bounds
  1057. bounds.origin.y = zeroRect.minY
  1058. newBoundsArray.append(NSStringFromRect(bounds))
  1059. }
  1060. resultAreasArray = editingAreas
  1061. } else if stype == .Horizontally {
  1062. for i in 0 ... editingAreas.count-1 {
  1063. let areas = editingAreas[i] as! CPDFEditArea
  1064. var bounds = areas.bounds
  1065. bounds.origin.y = highestRect.midY - bounds.height/2
  1066. newBoundsArray.append(NSStringFromRect(bounds))
  1067. }
  1068. resultAreasArray = editingAreas
  1069. } else if stype == .Vertical {
  1070. for i in 0 ... editingAreas.count-1 {
  1071. let areas = editingAreas[i] as! CPDFEditArea
  1072. var bounds = areas.bounds
  1073. bounds.origin.x = widthestRect.midX - bounds.width/2
  1074. newBoundsArray.append(NSStringFromRect(bounds))
  1075. }
  1076. resultAreasArray = editingAreas
  1077. } else if stype == .DisHorizontally {
  1078. let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
  1079. let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
  1080. let gap = (middleGap - otherAreasTotalWidth)/CGFloat(editingAreas.count - 1)
  1081. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  1082. areasCopyArray.sorted(by: { obj1, obj2 in
  1083. let area1 = obj1
  1084. let area2 = obj2
  1085. if area1.bounds.origin.x < area2.bounds.origin.x {
  1086. return true
  1087. } else {
  1088. return false
  1089. }
  1090. })
  1091. if let index = areasCopyArray.firstIndex(of: leftestArea) {
  1092. areasCopyArray.remove(at: index)
  1093. }
  1094. if let index = areasCopyArray.firstIndex(of: rightestArea) {
  1095. areasCopyArray.remove(at: index)
  1096. }
  1097. var leftStartX = leftestRect.maxX + gap
  1098. for i in 0 ... areasCopyArray.count-1 {
  1099. let areas = areasCopyArray[i]
  1100. var bounds = areas.bounds
  1101. bounds.origin.x = leftStartX
  1102. newBoundsArray.append(NSStringFromRect(bounds))
  1103. leftStartX = leftStartX + bounds.width + gap
  1104. }
  1105. resultAreasArray = areasCopyArray
  1106. } else if stype == .DisVertical {
  1107. let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
  1108. let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
  1109. let gap = (middleGap - otherAreasTotalHeight)/CGFloat(editingAreas.count - 1)
  1110. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  1111. areasCopyArray.sorted(by: { obj1, obj2 in
  1112. let area1 = obj1
  1113. let area2 = obj2
  1114. if area1.bounds.origin.x < area2.bounds.origin.x {
  1115. return true
  1116. } else {
  1117. return false
  1118. }
  1119. })
  1120. if let index = areasCopyArray.firstIndex(of: topestArea) {
  1121. areasCopyArray.remove(at: index)
  1122. }
  1123. if let index = areasCopyArray.firstIndex(of: bottomestArea) {
  1124. areasCopyArray.remove(at: index)
  1125. }
  1126. var bottomStartY = bottomestRect.maxY + gap
  1127. for i in 0 ... areasCopyArray.count-1 {
  1128. let areas = areasCopyArray[i]
  1129. var bounds = areas.bounds
  1130. bounds.origin.y = bottomStartY
  1131. newBoundsArray.append(NSStringFromRect(bounds))
  1132. bottomStartY = bottomStartY + bounds.height + gap
  1133. }
  1134. resultAreasArray = areasCopyArray
  1135. }
  1136. var oldBounds : [String] = []
  1137. for i in 0 ... resultAreasArray.count-1 {
  1138. let area : CPDFEditArea = resultAreasArray[i] as! CPDFEditArea
  1139. oldBounds.append(NSStringFromRect(area.bounds))
  1140. self.listView?.setBoundsEditArea(area, withBounds: NSRectFromString(newBoundsArray[i]))
  1141. }
  1142. self.listView?.setNeedsDisplayForVisiblePages()
  1143. }
  1144. }
  1145. }
  1146. // MARK: - CPDFViewDelegate
  1147. extension KMEditPDfHanddler: CPDFViewDelegate {
  1148. // 编辑区块已经改变
  1149. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  1150. let isEdited = self.listView?.isEdited() ?? false
  1151. if isEdited {
  1152. // 记录编辑状态
  1153. self.viewC?.recordIsPDFDocumentEdited(type: .editText)
  1154. }
  1155. if annotationType != .addText {
  1156. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView?.document)
  1157. }
  1158. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  1159. if areas.isEmpty {
  1160. self.hiddenPopWindow()
  1161. self.hiddenCropComfirmWindow()
  1162. let toolMode = self.listView?.toolMode ?? .none
  1163. let annotationType = self.annotationType
  1164. if toolMode == .editPDFToolMode {
  1165. if annotationType == .addImage || annotationType == .addText {
  1166. if self.isEditImage {
  1167. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1168. } else {
  1169. // if self.listView.annotationType == .addImage {
  1170. // self.closeRightPane()
  1171. // }
  1172. if annotationType == .addImage {
  1173. if self.rightViewC?.eidtPDFImageProperty != nil {
  1174. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1175. }
  1176. }
  1177. // self.openRightPane()
  1178. }
  1179. } else {
  1180. // self.viewC?.closeRightPane()
  1181. self.rightViewC?.isHidden = true
  1182. }
  1183. } else {
  1184. self.rightViewC?.isHidden = true
  1185. self.viewC?.closeRightPane()
  1186. if self.subViewType == .EditPDFAddText && annotationType == .addText {
  1187. self.rightViewC?.eidtPDFTextProperty.initData()
  1188. }
  1189. }
  1190. return
  1191. }
  1192. self.hiddenCropComfirmWindow()
  1193. self.viewC?.model.isPDFTextImageEdited = true
  1194. let subViewType = self.rightViewC?.subViewType ?? .None
  1195. if self.annotationType == .addImage {
  1196. var isImageArea = false
  1197. for i in 0 ..< areas.count {
  1198. if areas[i] is CPDFEditImageArea {
  1199. isImageArea = true
  1200. }
  1201. }
  1202. if isImageArea {
  1203. self.rightViewC?.isHidden = false
  1204. if self.subViewType == .EditPDFAddImage {
  1205. self.rightViewC?.subViewType = .EditPDFAddImage
  1206. self.rightViewC?.eidtPDFImageProperty.handdler = self
  1207. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1208. }
  1209. self.openRightPane()
  1210. } else {
  1211. self.rightViewC?.isHidden = true
  1212. // self.viewC?.closeRightPane()
  1213. }
  1214. } else if self.subViewType == .EditPDFAddText && annotationType == .addText {
  1215. self.rightViewC?.isHidden = false
  1216. let count = self.listView?.editingSelectionString()?.count ?? 0
  1217. if count != 0 {
  1218. self.rightViewC?.eidtPDFTextProperty.reloadData()
  1219. } else {
  1220. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  1221. }
  1222. self.openRightPane()
  1223. } else {
  1224. var textsAreas : [CPDFEditTextArea] = []
  1225. var imagesAreas : [CPDFEditImageArea] = []
  1226. let count = self.listView?.editingAreas()?.count ?? 0
  1227. if count < 1 {
  1228. return
  1229. }
  1230. for i in 0 ..< areas.count {
  1231. if areas[i] is CPDFEditTextArea {
  1232. textsAreas.append(areas[i] as! CPDFEditTextArea)
  1233. }
  1234. if areas[i] is CPDFEditImageArea {
  1235. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  1236. }
  1237. }
  1238. if textsAreas.count > 0 && textsAreas.count == areas.count {
  1239. self.rightViewC?.isHidden = false
  1240. self.rightViewC?.subViewType = .EditPDFAddText
  1241. self.rightViewC?.eidtPDFTextProperty.handdler = self
  1242. self.rightViewC?.eidtPDFTextProperty?.reloadData()
  1243. self.openRightPane()
  1244. } else if imagesAreas.count > 0 {
  1245. self.rightViewC?.isHidden = false
  1246. self.rightViewC?.subViewType = .EditPDFAddImage
  1247. self.rightViewC?.eidtPDFImageProperty.handdler = self
  1248. self.rightViewC?.eidtPDFImageProperty?.reloadData()
  1249. self.openRightPane()
  1250. }
  1251. }
  1252. var flag: CPDFEditArea?
  1253. for area in areas {
  1254. if flag == nil {
  1255. flag = area
  1256. continue
  1257. }
  1258. if let data = flag, data.bounds.maxY < area.bounds.maxY {
  1259. flag = area
  1260. }
  1261. }
  1262. if let data = flag {
  1263. self.showPopWindow(positionRect: data.bounds)
  1264. }
  1265. }
  1266. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  1267. if editArea != nil && (editArea is CPDFEditImageArea){
  1268. self.listView?.cropAreas = editArea as? CPDFEditImageArea
  1269. }
  1270. }
  1271. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1272. if self.isEditImage {
  1273. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1274. } else {
  1275. let window = KMEditPDFPopToolBarWindow.shared
  1276. if (window.isVisible) {
  1277. self.listView?.updateEditing([])
  1278. self.hiddenPopWindow()
  1279. self.hiddenCropComfirmWindow()
  1280. return
  1281. }
  1282. let panel = NSOpenPanel()
  1283. panel.allowsMultipleSelection = false
  1284. panel.allowedFileTypes = ["png","jpg"]
  1285. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1286. if response == .OK {
  1287. var filePath = panel.url?.path
  1288. var image = NSImage.init(contentsOf: panel.url!)
  1289. //图片自适应范围
  1290. if image != nil {
  1291. var imageRect = rect
  1292. let imageSize = image!.size
  1293. var previewSize = rect.size
  1294. var isChangeSize = false
  1295. if previewSize.width == 0 && previewSize.height == 0 {
  1296. previewSize = CGSize(width: 500, height: 500)
  1297. isChangeSize = true
  1298. }
  1299. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  1300. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  1301. if isChangeSize {
  1302. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  1303. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  1304. } else {
  1305. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  1306. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  1307. }
  1308. imageRect.size = newSize
  1309. let limitWidth = 1920.0
  1310. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  1311. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  1312. targetSize: CGSize(width: limitWidth, height: limitWidth),
  1313. maxSizeInBytes: 1024 * 1024 * 5,
  1314. targetCompression: 1.0)
  1315. }
  1316. //自适应page
  1317. let pageRect = self.listView?.currentPage().bounds ?? .zero
  1318. if imageRect.width > pageRect.width ||
  1319. imageRect.height > pageRect.height {
  1320. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  1321. imageRect = CGRect(x: imageRect.origin.x,
  1322. y: imageRect.origin.y,
  1323. width: imageRect.width * pageScale,
  1324. height: imageRect.height * pageScale)
  1325. }
  1326. if imageRect.origin.x < 0 {
  1327. imageRect.origin.x = 5
  1328. }
  1329. if imageRect.origin.y < 0 {
  1330. imageRect.origin.y = 5
  1331. }
  1332. if imageRect.origin.x + imageRect.width > pageRect.width ||
  1333. imageRect.origin.y + imageRect.height > pageRect.height {
  1334. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  1335. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  1336. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  1337. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  1338. }
  1339. DispatchQueue.main.async {
  1340. self.listView?.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  1341. self.viewC?.model.isPDFTextImageEdited = true
  1342. self.viewC?.recordIsPDFDocumentEdited(type: .editImage)
  1343. self.showPopWindow(positionRect: imageRect)
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }
  1349. }
  1350. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1351. let window = KMEditPDFPopToolBarWindow.shared
  1352. if (window.isVisible) {
  1353. self.hiddenPopWindow()
  1354. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  1355. if let area = areas.last {
  1356. if let data = area as? CPDFEditTextArea {
  1357. if let str = data.editTextAreaString(), str.isEmpty {
  1358. self.listView?.remove(with: [area])
  1359. }
  1360. }
  1361. }
  1362. return
  1363. }
  1364. var newRect = rect
  1365. if CGSizeEqualToSize(rect.size, .zero) {
  1366. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  1367. } else {
  1368. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  1369. }
  1370. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1371. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  1372. let fontSize = model.fontSize
  1373. let fontColor = model.color
  1374. let fontAlign = model.alignment
  1375. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  1376. NSColorPanel.shared.color = fontColor
  1377. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  1378. let attri = CEditAttributes()
  1379. attri.font = font
  1380. attri.fontColor = fontColor
  1381. attri.alignment = fontAlign
  1382. self.listView?.createStringBounds(newRect, with: attri, page: page)
  1383. // self.rightViewC != nil &&
  1384. if self.subViewType == .EditPDFAddText && self.annotationType == .addText {
  1385. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  1386. }
  1387. self.showPopWindow(positionRect: newRect)
  1388. }
  1389. // 文本区块 选中文本已经变化
  1390. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1391. // self.viewC?.rightSideViewController != nil &&
  1392. if self.subViewType == .EditPDFAddText {
  1393. self.rightViewC?.eidtPDFTextProperty.reloadData()
  1394. self.rightViewC?.eidtPDFTextProperty.updateTextTextPresuppositionState()
  1395. self.showPopWindow(positionRect: .zero)
  1396. }
  1397. }
  1398. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  1399. let areas = self.editingAreas
  1400. if areas.count == 1 {
  1401. if let data = areas.first as? CPDFEditImageArea {
  1402. let updating = self.listView?.editAreaBoundUpdating ?? false
  1403. if updating {
  1404. self.listView?.editAreaBoundUpdating = false
  1405. } else {
  1406. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1407. }
  1408. }
  1409. }
  1410. }
  1411. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  1412. }
  1413. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1414. self.hiddenPopWindow()
  1415. }
  1416. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1417. }
  1418. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1419. self.showPopWindow(positionRect: .zero)
  1420. }
  1421. }