KMEditPDfHanddler.swift 54 KB

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