KMEditPDfHanddler.swift 58 KB

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