KMEditPDfHanddler.swift 65 KB

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