KMEditPDfHanddler.swift 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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 editingAreas = self.editingAreas
  712. for area in editingAreas {
  713. if let data = area as? CPDFEditTextArea {
  714. self.listView?.setEditingSelectionFontColor(theColor, with: data)
  715. }
  716. }
  717. }
  718. func fontStyleAction(fontName: String?) {
  719. guard let font = CPDFFont.mappingFont(withFontString: fontName) else {
  720. return
  721. }
  722. let editingAreas = self.editingAreas
  723. for area in editingAreas {
  724. if let data = area as? CPDFEditTextArea {
  725. self.listView?.setEditSelectionCFont(font, with: data)
  726. }
  727. }
  728. }
  729. func fontAddAction() {
  730. let editingAreas = self.editingAreas
  731. if editingAreas.isEmpty {
  732. return
  733. }
  734. guard let area = editingAreas.last else {
  735. return
  736. }
  737. if area.isTextArea() == false {
  738. return
  739. }
  740. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  741. self.listView?.setEditingSelectionFontSize(fontSize+1, with: area as! CPDFEditTextArea, isAutoSize: false)
  742. }
  743. }
  744. func fontReduceAction() {
  745. let editingAreas = self.editingAreas
  746. if editingAreas.isEmpty {
  747. return
  748. }
  749. guard let area = editingAreas.last else {
  750. return
  751. }
  752. if area.isTextArea() == false {
  753. return
  754. }
  755. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  756. self.listView?.setEditingSelectionFontSize(fontSize-1, with: area as! CPDFEditTextArea, isAutoSize: false)
  757. }
  758. }
  759. func fontBoldAction() {
  760. let editingAreas = self.editingAreas
  761. if editingAreas.isEmpty {
  762. return
  763. }
  764. guard let area = editingAreas.last else {
  765. return
  766. }
  767. if area.isTextArea() == false {
  768. return
  769. }
  770. self.listView?.setCurrentSelectionIsBold(true, with: area as! CPDFEditTextArea)
  771. }
  772. func fontItalicAction() {
  773. let editingAreas = self.editingAreas
  774. if editingAreas.isEmpty {
  775. return
  776. }
  777. guard let area = editingAreas.last else {
  778. return
  779. }
  780. if area.isTextArea() == false {
  781. return
  782. }
  783. self.listView?.setCurrentSelectionIsItalic(true, with: area as! CPDFEditTextArea)
  784. }
  785. func textAlignmentAction(align: NSTextAlignment) {
  786. let editingAreas = self.editingAreas
  787. for area in editingAreas {
  788. if let data = area as? CPDFEditTextArea {
  789. self.listView?.setCurrentSelectionAlignment(align, with: data)
  790. }
  791. }
  792. }
  793. func leftRotateAction() {
  794. let editingAreas = self.editingAreas
  795. if editingAreas.isEmpty {
  796. return
  797. }
  798. guard let area = editingAreas.last as? CPDFEditImageArea else {
  799. return
  800. }
  801. // if let ang = self.listView?.getRotateWith(area) {
  802. // self.listView?.rotate(with: area, rotate: ang-90)
  803. // }
  804. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Rotate"])
  805. self.listView?.rotate(with: area, rotate: -90)
  806. // var frame = area.bounds
  807. // frame.size.width += 100
  808. // self.listView?.setBoundsEditArea(area, withBounds: frame)
  809. // 文档拖拽、tooltip、多选场景、暗黑场景
  810. // if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  811. // self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  812. // }
  813. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  814. // self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  815. }
  816. func rightRotateAction() {
  817. let editingAreas = self.editingAreas
  818. if editingAreas.isEmpty {
  819. return
  820. }
  821. guard let area = editingAreas.last as? CPDFEditImageArea else {
  822. return
  823. }
  824. self.listView?.rotate(with: area, rotate: 90)
  825. }
  826. func reverseXAction() {
  827. let editingAreas = self.editingAreas
  828. if editingAreas.isEmpty {
  829. return
  830. }
  831. for area in editingAreas {
  832. if let data = area as? CPDFEditImageArea {
  833. self.listView?.horizontalMirror(with: data)
  834. }
  835. }
  836. }
  837. func reverseYAction() {
  838. let editingAreas = self.editingAreas
  839. if editingAreas.isEmpty {
  840. return
  841. }
  842. for area in editingAreas {
  843. if let data = area as? CPDFEditImageArea {
  844. self.listView?.verticalMirror(with: data)
  845. }
  846. }
  847. }
  848. func cropAction() {
  849. let editingAreas = self.editingAreas
  850. if editingAreas.isEmpty {
  851. return
  852. }
  853. self.listView?.isEditImage = true
  854. for area in editingAreas {
  855. if let data = area as? CPDFEditImageArea {
  856. self.listView?.enterCrop(with: data)
  857. }
  858. }
  859. self.hiddenPopWindow()
  860. Task { @MainActor in
  861. self.showCropComfirmWindow()
  862. }
  863. self.rightViewC?.eidtPDFImageProperty.updateButtonState(hidden: false)
  864. }
  865. func cropCancelAction() {
  866. self.hiddenCropComfirmWindow()
  867. let areas = self.editingAreas
  868. if areas.isEmpty {
  869. return
  870. }
  871. for area in areas {
  872. if let data = area as? CPDFEditImageArea {
  873. self.listView?.exitCrop(with: data)
  874. }
  875. }
  876. self.listView?.cropAreas = nil
  877. self.listView?.isEditImage = false
  878. self.rightViewC?.eidtPDFImageProperty.updateButtonState(hidden: true)
  879. Task { @MainActor [weak self] in
  880. if let data = self?.listView?.selectImageAreas {
  881. self?.showPopWindow(positionRect:data.bounds)
  882. }
  883. }
  884. }
  885. func cropComfirmAction() {
  886. guard let selectImageAreas = self.listView?.selectImageAreas else {
  887. self.cropCancelAction()
  888. return
  889. }
  890. self.listView?.cropEditImageArea(selectImageAreas, withBounds: self.listView?.cropAreas.cropRect ?? .zero)
  891. self.cropCancelAction()
  892. }
  893. func replaceAction() {
  894. let editingAreas = self.editingAreas
  895. if editingAreas.isEmpty {
  896. return
  897. }
  898. let panel = NSOpenPanel()
  899. panel.allowsMultipleSelection = false
  900. panel.allowedFileTypes = ["png","jpg"]
  901. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  902. if response == .OK {
  903. let openPath = panel.url?.path
  904. for area in editingAreas {
  905. if let data = area as? CPDFEditImageArea {
  906. // , rect: data.bounds
  907. self.listView?.replace(data, imagePath: openPath!)
  908. }
  909. }
  910. }
  911. }
  912. }
  913. func exportAction() {
  914. let editingAreas = self.editingAreas
  915. if editingAreas.isEmpty {
  916. return
  917. }
  918. var imagesAreas: [CPDFEditImageArea] = []
  919. for area in editingAreas {
  920. if let data = area as? CPDFEditImageArea {
  921. imagesAreas.append(data)
  922. }
  923. }
  924. if imagesAreas.count == 1 {
  925. let panel = NSSavePanel()
  926. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  927. panel.isExtensionHidden = true
  928. let response = panel.runModal()
  929. if response == .OK {
  930. let url = panel.url
  931. if FileManager.default.fileExists(atPath: url!.path) {
  932. try?FileManager.default.removeItem(atPath: url!.path)
  933. }
  934. let result = self.listView?.extractImage(with: imagesAreas.first, toImagePath: url!.path) ?? false
  935. if result {
  936. NSWorkspace.shared.activateFileViewerSelecting([url!])
  937. }
  938. }
  939. } else if imagesAreas.count > 1 {
  940. let panel = NSOpenPanel()
  941. panel.canChooseFiles = false
  942. panel.canChooseDirectories = true
  943. panel.canCreateDirectories = true
  944. panel.allowsMultipleSelection = false
  945. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  946. if response == .OK {
  947. let outputURL = panel.url
  948. let s = self.listView?.document?.documentURL.lastPathComponent
  949. let folderPath = (self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? "") + "_extract"
  950. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  951. var i = 1
  952. let testFilePath = filePath
  953. while FileManager.default.fileExists(atPath: filePath!) {
  954. filePath = testFilePath! + "\(i)"
  955. i = i + 1
  956. }
  957. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  958. var saveURLs : [URL] = []
  959. for j in 0 ... imagesAreas.count-1 {
  960. let documentFileName = self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  961. var outPath = filePath!
  962. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  963. outPath = outPath + "page \(j+1)"
  964. outPath = outPath.stringByAppendingPathExtension("jpg")
  965. let result = self.listView?.extractImage(with: imagesAreas[j], toImagePath: outPath) ?? false
  966. if result {
  967. saveURLs.append(URL(fileURLWithPath: outPath))
  968. }
  969. }
  970. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  971. }
  972. }
  973. }
  974. }
  975. func alignmentAction(align: CPDFActiveAreasAlignType) {
  976. KMPrint("updateFormAearsAlignMangent")
  977. let stype = align
  978. let editingAreas = self.editingAreas
  979. if editingAreas.count >= 2 {
  980. var zeroRect = NSRect.null
  981. var highestRect = NSZeroRect
  982. var widthestRect = NSZeroRect
  983. let fristArea : CPDFEditArea = editingAreas.first as! CPDFEditArea
  984. var leftestRect = fristArea.bounds
  985. var rightestRect = fristArea.bounds
  986. var topestRect = fristArea.bounds
  987. var bottomestRect = fristArea.bounds
  988. var leftestArea : CPDFEditArea = fristArea
  989. var rightestArea : CPDFEditArea = fristArea
  990. var topestArea : CPDFEditArea = fristArea
  991. var bottomestArea : CPDFEditArea = fristArea
  992. var totalWidth = 0.0
  993. var totalHeight = 0.0
  994. for i in 0 ... editingAreas.count-1 {
  995. let area : CPDFEditArea = editingAreas[i] as! CPDFEditArea
  996. zeroRect = zeroRect.union(area.bounds)
  997. totalWidth = totalWidth + area.bounds.width
  998. totalHeight = totalHeight + area.bounds.height
  999. if area.bounds.height > highestRect.height {
  1000. highestRect = area.bounds
  1001. }
  1002. if area.bounds.width > widthestRect.width {
  1003. widthestRect = area.bounds
  1004. }
  1005. if leftestRect.minX > area.bounds.minX {
  1006. leftestRect = area.bounds
  1007. leftestArea = area
  1008. }
  1009. if area.bounds.maxX > rightestRect.maxX {
  1010. rightestRect = area.bounds
  1011. rightestArea = area
  1012. }
  1013. if area.bounds.maxY > topestRect.maxY {
  1014. topestRect = area.bounds
  1015. topestArea = area
  1016. }
  1017. if bottomestRect.minY > area.bounds.minY {
  1018. bottomestRect = area.bounds
  1019. bottomestArea = area
  1020. }
  1021. }
  1022. var resultAreasArray: [Any] = []
  1023. var newBoundsArray: [String] = []
  1024. if stype == .Left {
  1025. for i in 0 ... editingAreas.count-1 {
  1026. let areas = editingAreas[i] as! CPDFEditArea
  1027. var bounds = areas.bounds
  1028. bounds.origin.x = zeroRect.origin.x
  1029. newBoundsArray.append(NSStringFromRect(bounds))
  1030. }
  1031. resultAreasArray = editingAreas
  1032. } else if stype == .Right {
  1033. for i in 0 ... editingAreas.count-1 {
  1034. let areas = editingAreas[i] as! CPDFEditArea
  1035. var bounds = areas.bounds
  1036. bounds.origin.x = zeroRect.maxX - bounds.size.width
  1037. newBoundsArray.append(NSStringFromRect(bounds))
  1038. }
  1039. resultAreasArray = editingAreas
  1040. } else if stype == .Top {
  1041. for i in 0 ... editingAreas.count-1 {
  1042. let areas = editingAreas[i] as! CPDFEditArea
  1043. var bounds = areas.bounds
  1044. bounds.origin.y = zeroRect.maxY - bounds.size.height
  1045. newBoundsArray.append(NSStringFromRect(bounds))
  1046. }
  1047. resultAreasArray = editingAreas
  1048. } else if stype == .Bottom {
  1049. for i in 0 ... editingAreas.count-1 {
  1050. let areas = editingAreas[i] as! CPDFEditArea
  1051. var bounds = areas.bounds
  1052. bounds.origin.y = zeroRect.minY
  1053. newBoundsArray.append(NSStringFromRect(bounds))
  1054. }
  1055. resultAreasArray = editingAreas
  1056. } else if stype == .Horizontally {
  1057. for i in 0 ... editingAreas.count-1 {
  1058. let areas = editingAreas[i] as! CPDFEditArea
  1059. var bounds = areas.bounds
  1060. bounds.origin.y = highestRect.midY - bounds.height/2
  1061. newBoundsArray.append(NSStringFromRect(bounds))
  1062. }
  1063. resultAreasArray = editingAreas
  1064. } else if stype == .Vertical {
  1065. for i in 0 ... editingAreas.count-1 {
  1066. let areas = editingAreas[i] as! CPDFEditArea
  1067. var bounds = areas.bounds
  1068. bounds.origin.x = widthestRect.midX - bounds.width/2
  1069. newBoundsArray.append(NSStringFromRect(bounds))
  1070. }
  1071. resultAreasArray = editingAreas
  1072. } else if stype == .DisHorizontally {
  1073. let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
  1074. let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
  1075. let gap = (middleGap - otherAreasTotalWidth)/CGFloat(editingAreas.count - 1)
  1076. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  1077. areasCopyArray.sorted(by: { obj1, obj2 in
  1078. let area1 = obj1
  1079. let area2 = obj2
  1080. if area1.bounds.origin.x < area2.bounds.origin.x {
  1081. return true
  1082. } else {
  1083. return false
  1084. }
  1085. })
  1086. if let index = areasCopyArray.firstIndex(of: leftestArea) {
  1087. areasCopyArray.remove(at: index)
  1088. }
  1089. if let index = areasCopyArray.firstIndex(of: rightestArea) {
  1090. areasCopyArray.remove(at: index)
  1091. }
  1092. var leftStartX = leftestRect.maxX + gap
  1093. for i in 0 ... areasCopyArray.count-1 {
  1094. let areas = areasCopyArray[i]
  1095. var bounds = areas.bounds
  1096. bounds.origin.x = leftStartX
  1097. newBoundsArray.append(NSStringFromRect(bounds))
  1098. leftStartX = leftStartX + bounds.width + gap
  1099. }
  1100. resultAreasArray = areasCopyArray
  1101. } else if stype == .DisVertical {
  1102. let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
  1103. let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
  1104. let gap = (middleGap - otherAreasTotalHeight)/CGFloat(editingAreas.count - 1)
  1105. var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
  1106. areasCopyArray.sorted(by: { obj1, obj2 in
  1107. let area1 = obj1
  1108. let area2 = obj2
  1109. if area1.bounds.origin.x < area2.bounds.origin.x {
  1110. return true
  1111. } else {
  1112. return false
  1113. }
  1114. })
  1115. if let index = areasCopyArray.firstIndex(of: topestArea) {
  1116. areasCopyArray.remove(at: index)
  1117. }
  1118. if let index = areasCopyArray.firstIndex(of: bottomestArea) {
  1119. areasCopyArray.remove(at: index)
  1120. }
  1121. var bottomStartY = bottomestRect.maxY + gap
  1122. for i in 0 ... areasCopyArray.count-1 {
  1123. let areas = areasCopyArray[i]
  1124. var bounds = areas.bounds
  1125. bounds.origin.y = bottomStartY
  1126. newBoundsArray.append(NSStringFromRect(bounds))
  1127. bottomStartY = bottomStartY + bounds.height + gap
  1128. }
  1129. resultAreasArray = areasCopyArray
  1130. }
  1131. var oldBounds : [String] = []
  1132. for i in 0 ... resultAreasArray.count-1 {
  1133. let area : CPDFEditArea = resultAreasArray[i] as! CPDFEditArea
  1134. oldBounds.append(NSStringFromRect(area.bounds))
  1135. self.listView?.setBoundsEditArea(area, withBounds: NSRectFromString(newBoundsArray[i]))
  1136. }
  1137. self.listView?.setNeedsDisplayForVisiblePages()
  1138. }
  1139. }
  1140. }
  1141. // MARK: - CPDFViewDelegate
  1142. extension KMEditPDfHanddler: CPDFViewDelegate {
  1143. // 编辑区块已经改变
  1144. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  1145. let isEdited = self.listView?.isEdited() ?? false
  1146. if isEdited {
  1147. // 记录编辑状态
  1148. self.viewC?.recordIsPDFDocumentEdited(type: .editText)
  1149. }
  1150. if annotationType != .addText {
  1151. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView?.document)
  1152. }
  1153. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  1154. if areas.isEmpty {
  1155. self.hiddenPopWindow()
  1156. let toolMode = self.listView?.toolMode ?? .none
  1157. let annotationType = self.annotationType
  1158. if toolMode == .editPDFToolMode {
  1159. if annotationType == .addImage || annotationType == .addText {
  1160. if self.isEditImage {
  1161. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1162. } else {
  1163. // if self.listView.annotationType == .addImage {
  1164. // self.closeRightPane()
  1165. // }
  1166. if annotationType == .addImage {
  1167. if self.rightViewC?.eidtPDFImageProperty != nil {
  1168. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1169. }
  1170. }
  1171. // self.openRightPane()
  1172. }
  1173. } else {
  1174. // self.viewC?.closeRightPane()
  1175. self.rightViewC?.isHidden = true
  1176. }
  1177. } else {
  1178. self.rightViewC?.isHidden = true
  1179. self.viewC?.closeRightPane()
  1180. if self.subViewType == .EditPDFAddText && annotationType == .addText {
  1181. self.rightViewC?.eidtPDFTextProperty.initData()
  1182. }
  1183. }
  1184. return
  1185. }
  1186. self.viewC?.model.isPDFTextImageEdited = true
  1187. let subViewType = self.rightViewC?.subViewType ?? .None
  1188. if self.annotationType == .addImage {
  1189. var isImageArea = false
  1190. for i in 0 ..< areas.count {
  1191. if areas[i] is CPDFEditImageArea {
  1192. isImageArea = true
  1193. }
  1194. }
  1195. if isImageArea {
  1196. self.rightViewC?.isHidden = false
  1197. if self.subViewType == .EditPDFAddImage {
  1198. self.rightViewC?.subViewType = .EditPDFAddImage
  1199. self.rightViewC?.eidtPDFImageProperty.handdler = self
  1200. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1201. }
  1202. self.openRightPane()
  1203. } else {
  1204. self.rightViewC?.isHidden = true
  1205. // self.viewC?.closeRightPane()
  1206. }
  1207. } else if self.subViewType == .EditPDFAddText && annotationType == .addText {
  1208. self.rightViewC?.isHidden = false
  1209. let count = self.listView?.editingSelectionString()?.count ?? 0
  1210. if count != 0 {
  1211. self.rightViewC?.eidtPDFTextProperty.reloadData()
  1212. } else {
  1213. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  1214. }
  1215. self.openRightPane()
  1216. } else {
  1217. var textsAreas : [CPDFEditTextArea] = []
  1218. var imagesAreas : [CPDFEditImageArea] = []
  1219. let count = self.listView?.editingAreas()?.count ?? 0
  1220. if count < 1 {
  1221. return
  1222. }
  1223. for i in 0 ..< areas.count {
  1224. if areas[i] is CPDFEditTextArea {
  1225. textsAreas.append(areas[i] as! CPDFEditTextArea)
  1226. }
  1227. if areas[i] is CPDFEditImageArea {
  1228. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  1229. }
  1230. }
  1231. if textsAreas.count > 0 && textsAreas.count == areas.count {
  1232. self.rightViewC?.isHidden = false
  1233. self.rightViewC?.subViewType = .EditPDFAddText
  1234. self.rightViewC?.eidtPDFTextProperty.handdler = self
  1235. self.rightViewC?.eidtPDFTextProperty?.reloadData()
  1236. self.openRightPane()
  1237. } else if imagesAreas.count > 0 {
  1238. self.rightViewC?.isHidden = false
  1239. self.rightViewC?.subViewType = .EditPDFAddImage
  1240. self.rightViewC?.eidtPDFImageProperty.handdler = self
  1241. self.rightViewC?.eidtPDFImageProperty?.reloadData()
  1242. self.openRightPane()
  1243. }
  1244. }
  1245. var flag: CPDFEditArea?
  1246. for area in areas {
  1247. if flag == nil {
  1248. flag = area
  1249. continue
  1250. }
  1251. if let data = flag, data.bounds.maxY < area.bounds.maxY {
  1252. flag = area
  1253. }
  1254. }
  1255. if let data = flag {
  1256. self.showPopWindow(positionRect: data.bounds)
  1257. }
  1258. }
  1259. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  1260. if editArea != nil && (editArea is CPDFEditImageArea){
  1261. self.listView?.cropAreas = editArea as? CPDFEditImageArea
  1262. }
  1263. }
  1264. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1265. if self.isEditImage {
  1266. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1267. } else {
  1268. let panel = NSOpenPanel()
  1269. panel.allowsMultipleSelection = false
  1270. panel.allowedFileTypes = ["png","jpg"]
  1271. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1272. if response == .OK {
  1273. var filePath = panel.url?.path
  1274. var image = NSImage.init(contentsOf: panel.url!)
  1275. //图片自适应范围
  1276. if image != nil {
  1277. var imageRect = rect
  1278. let imageSize = image!.size
  1279. var previewSize = rect.size
  1280. var isChangeSize = false
  1281. if previewSize.width == 0 && previewSize.height == 0 {
  1282. previewSize = CGSize(width: 500, height: 500)
  1283. isChangeSize = true
  1284. }
  1285. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  1286. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  1287. if isChangeSize {
  1288. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  1289. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  1290. } else {
  1291. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  1292. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  1293. }
  1294. imageRect.size = newSize
  1295. let limitWidth = 1920.0
  1296. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  1297. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  1298. targetSize: CGSize(width: limitWidth, height: limitWidth),
  1299. maxSizeInBytes: 1024 * 1024 * 5,
  1300. targetCompression: 1.0)
  1301. }
  1302. //自适应page
  1303. let pageRect = self.listView?.currentPage().bounds ?? .zero
  1304. if imageRect.width > pageRect.width ||
  1305. imageRect.height > pageRect.height {
  1306. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  1307. imageRect = CGRect(x: imageRect.origin.x,
  1308. y: imageRect.origin.y,
  1309. width: imageRect.width * pageScale,
  1310. height: imageRect.height * pageScale)
  1311. }
  1312. if imageRect.origin.x < 0 {
  1313. imageRect.origin.x = 5
  1314. }
  1315. if imageRect.origin.y < 0 {
  1316. imageRect.origin.y = 5
  1317. }
  1318. if imageRect.origin.x + imageRect.width > pageRect.width ||
  1319. imageRect.origin.y + imageRect.height > pageRect.height {
  1320. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  1321. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  1322. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  1323. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  1324. }
  1325. DispatchQueue.main.async {
  1326. self.listView?.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  1327. self.viewC?.model.isPDFTextImageEdited = true
  1328. self.viewC?.recordIsPDFDocumentEdited(type: .editImage)
  1329. self.showPopWindow(positionRect: imageRect)
  1330. }
  1331. }
  1332. }
  1333. }
  1334. }
  1335. }
  1336. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1337. let window = KMEditPDFPopToolBarWindow.shared
  1338. if (window.isVisible) {
  1339. self.hiddenPopWindow()
  1340. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  1341. if let area = areas.last {
  1342. if let data = area as? CPDFEditTextArea {
  1343. if let str = data.editTextAreaString(), str.isEmpty {
  1344. self.listView?.remove(with: [area])
  1345. }
  1346. }
  1347. }
  1348. return
  1349. }
  1350. var newRect = rect
  1351. if CGSizeEqualToSize(rect.size, .zero) {
  1352. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  1353. } else {
  1354. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  1355. }
  1356. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1357. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  1358. let fontSize = model.fontSize
  1359. let fontColor = model.color
  1360. let fontAlign = model.alignment
  1361. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  1362. NSColorPanel.shared.color = fontColor
  1363. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  1364. let attri = CEditAttributes()
  1365. attri.font = font
  1366. attri.fontColor = fontColor
  1367. attri.alignment = fontAlign
  1368. self.listView?.createStringBounds(newRect, with: attri, page: page)
  1369. // self.rightViewC != nil &&
  1370. if self.subViewType == .EditPDFAddText && self.annotationType == .addText {
  1371. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  1372. }
  1373. self.showPopWindow(positionRect: newRect)
  1374. }
  1375. // 文本区块 选中文本已经变化
  1376. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1377. // self.viewC?.rightSideViewController != nil &&
  1378. if self.subViewType == .EditPDFAddText {
  1379. self.rightViewC?.eidtPDFTextProperty.reloadData()
  1380. self.rightViewC?.eidtPDFTextProperty.updateTextTextPresuppositionState()
  1381. }
  1382. }
  1383. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  1384. let areas = self.editingAreas
  1385. if areas.count == 1 {
  1386. if let data = areas.first as? CPDFEditImageArea {
  1387. let updating = self.listView?.editAreaBoundUpdating ?? false
  1388. if updating {
  1389. self.listView?.editAreaBoundUpdating = false
  1390. } else {
  1391. self.rightViewC?.eidtPDFImageProperty.reloadData()
  1392. }
  1393. }
  1394. }
  1395. }
  1396. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  1397. }
  1398. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1399. self.hiddenPopWindow()
  1400. }
  1401. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1402. }
  1403. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1404. self.showPopWindow(positionRect: .zero)
  1405. }
  1406. }