KMEditPDfHanddler.swift 55 KB

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