KMEditPDfHanddler.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. //
  2. // KMEditPDfHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/16.
  6. //
  7. import Cocoa
  8. // EditPDF处理对象
  9. class KMEditPDfHanddler: NSObject {
  10. weak var viewC: KMMainViewController?
  11. weak var listView: CPDFListView? {
  12. get {
  13. return self.viewC?.listView
  14. }
  15. }
  16. var annotationType: CAnnotationType {
  17. get {
  18. return self.listView?.annotationType ?? .unkown
  19. }
  20. }
  21. weak var rightViewC: KMRightSideViewController? {
  22. get {
  23. return self.viewC?.rightSideViewController
  24. }
  25. }
  26. var subViewType: RightSubViewType {
  27. get {
  28. return self.rightViewC?.subViewType ?? .None
  29. }
  30. }
  31. // var toolMode: CToolMode {
  32. // get {
  33. // return
  34. // }
  35. // }
  36. var isEditImage: Bool {
  37. get {
  38. return self.listView?.isEditImage ?? false
  39. }
  40. }
  41. var isEditing: Bool {
  42. get {
  43. return self.listView?.isEditing() ?? false
  44. }
  45. }
  46. var editingConfig: CPDFEditingConfig? {
  47. get {
  48. return self.listView?.editingConfig()
  49. }
  50. }
  51. var editingAreas: [CPDFEditArea] {
  52. get {
  53. return self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  54. }
  55. }
  56. func enterEditPDF() {
  57. self.listView?.updateActiveAnnotations([])
  58. self.listView?.setNeedsDisplayForVisiblePages()
  59. self.listView?.commitEditFormText()
  60. self.listView?.annotationType = .editTextImage
  61. // 设置边框颜色
  62. self.editingConfig?.editingBorderColor = .clear
  63. // 设置边框宽度
  64. // self.editingConfig?.editingBorderWidth = 10
  65. // 内容与边框的间距
  66. // self.editingConfig?.editAreaMargin = .init(floatLiteral: 5)
  67. // 设置选中块边框颜色
  68. // self.editingConfig?.editingSelectionBorderColor = .red
  69. // 显示hover边框
  70. self.editingConfig?.isShowMouseAreaHover = true
  71. // hover
  72. // 边框宽度
  73. // self.editingConfig?.mouseHoverBorderWidth = 1
  74. // 边框颜色
  75. self.editingConfig?.mouseHoverBorderColor = .purple
  76. // 边框虚线设置
  77. self.editingConfig?.mouseHoverBorderDashPattern = [3,3,3]
  78. // 块填充颜色(拖拽中)
  79. // self.editingConfig?.editAreaMoveFillColor = .cyan
  80. // 是否显示位置辅助线
  81. self.editingConfig?.isShowEditingAreaHover = true
  82. // 辅助线颜色
  83. // self.editingConfig?.editingHoverBorderColor = .brown
  84. // 支持多选
  85. self.editingConfig?.isSupportMultipleSelectEditingArea = true
  86. // 图片是否显示8个操作点
  87. self.editingConfig?.isDrawRectWithDot = true
  88. }
  89. func fontAddAction() {
  90. let editingAreas = self.editingAreas
  91. if editingAreas.isEmpty {
  92. return
  93. }
  94. guard let area = editingAreas.last else {
  95. return
  96. }
  97. if area.isTextArea() == false {
  98. return
  99. }
  100. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  101. self.listView?.setEditingSelectionFontSize(fontSize+1, with: area as! CPDFEditTextArea, isAutoSize: false)
  102. }
  103. }
  104. func fontReduceAction() {
  105. let editingAreas = self.editingAreas
  106. if editingAreas.isEmpty {
  107. return
  108. }
  109. guard let area = editingAreas.last else {
  110. return
  111. }
  112. if area.isTextArea() == false {
  113. return
  114. }
  115. if let fontSize = self.listView?.editingSelectionFontSize(with: area as! CPDFEditTextArea) {
  116. self.listView?.setEditingSelectionFontSize(fontSize-1, with: area as! CPDFEditTextArea, isAutoSize: false)
  117. }
  118. }
  119. func fontBoldAction() {
  120. let editingAreas = self.editingAreas
  121. if editingAreas.isEmpty {
  122. return
  123. }
  124. guard let area = editingAreas.last else {
  125. return
  126. }
  127. if area.isTextArea() == false {
  128. return
  129. }
  130. self.listView?.setCurrentSelectionIsBold(true, with: area as! CPDFEditTextArea)
  131. }
  132. func fontItalicAction() {
  133. let editingAreas = self.editingAreas
  134. if editingAreas.isEmpty {
  135. return
  136. }
  137. guard let area = editingAreas.last else {
  138. return
  139. }
  140. if area.isTextArea() == false {
  141. return
  142. }
  143. self.listView?.setCurrentSelectionIsItalic(true, with: area as! CPDFEditTextArea)
  144. }
  145. func textAlignmentAction() {
  146. KMPrint("textAlignmentAction")
  147. }
  148. func leftRotateAction() {
  149. let editingAreas = self.editingAreas
  150. if editingAreas.isEmpty {
  151. return
  152. }
  153. guard let area = editingAreas.last as? CPDFEditImageArea else {
  154. return
  155. }
  156. // if let ang = self.listView?.getRotateWith(area) {
  157. // self.listView?.rotate(with: area, rotate: ang-90)
  158. // }
  159. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Rotate"])
  160. self.listView?.rotate(with: area, rotate: -90)
  161. // if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  162. // self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  163. // }
  164. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  165. // self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  166. }
  167. func rightRotateAction() {
  168. let editingAreas = self.editingAreas
  169. if editingAreas.isEmpty {
  170. return
  171. }
  172. guard let area = editingAreas.last as? CPDFEditImageArea else {
  173. return
  174. }
  175. self.listView?.rotate(with: area, rotate: 90)
  176. }
  177. func reverseXAction() {
  178. let editingAreas = self.editingAreas
  179. if editingAreas.isEmpty {
  180. return
  181. }
  182. for area in editingAreas {
  183. if let data = area as? CPDFEditImageArea {
  184. self.listView?.horizontalMirror(with: data)
  185. }
  186. }
  187. }
  188. func reverseYAction() {
  189. let editingAreas = self.editingAreas
  190. if editingAreas.isEmpty {
  191. return
  192. }
  193. for area in editingAreas {
  194. if let data = area as? CPDFEditImageArea {
  195. self.listView?.verticalMirror(with: data)
  196. }
  197. }
  198. }
  199. func cropAction() {
  200. self.listView?.isEditImage = true
  201. let editingAreas = self.editingAreas
  202. if editingAreas.isEmpty {
  203. return
  204. }
  205. for area in editingAreas {
  206. if let data = area as? CPDFEditImageArea {
  207. self.listView?.enterCrop(with: data)
  208. }
  209. }
  210. }
  211. func replaceAction() {
  212. let editingAreas = self.editingAreas
  213. if editingAreas.isEmpty {
  214. return
  215. }
  216. let panel = NSOpenPanel()
  217. panel.allowsMultipleSelection = false
  218. panel.allowedFileTypes = ["png","jpg"]
  219. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  220. if response == .OK {
  221. let openPath = panel.url?.path
  222. for area in editingAreas {
  223. if let data = area as? CPDFEditImageArea {
  224. // self.listView?.replace(data, imagePath: openPath!)
  225. self.listView?.replace(data, imagePath: openPath!, rect: data.bounds)
  226. }
  227. }
  228. }
  229. }
  230. }
  231. func exportAction() {
  232. let editingAreas = self.editingAreas
  233. if editingAreas.isEmpty {
  234. return
  235. }
  236. var imagesAreas: [CPDFEditImageArea] = []
  237. for area in editingAreas {
  238. if let data = area as? CPDFEditImageArea {
  239. imagesAreas.append(data)
  240. }
  241. }
  242. if imagesAreas.count == 1 {
  243. let panel = NSSavePanel()
  244. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  245. panel.isExtensionHidden = true
  246. let response = panel.runModal()
  247. if response == .OK {
  248. let url = panel.url
  249. if FileManager.default.fileExists(atPath: url!.path) {
  250. try?FileManager.default.removeItem(atPath: url!.path)
  251. }
  252. let result = self.listView?.extractImage(with: imagesAreas.first, toImagePath: url!.path) ?? false
  253. if result {
  254. NSWorkspace.shared.activateFileViewerSelecting([url!])
  255. }
  256. }
  257. } else if imagesAreas.count > 1 {
  258. let panel = NSOpenPanel()
  259. panel.canChooseFiles = false
  260. panel.canChooseDirectories = true
  261. panel.canCreateDirectories = true
  262. panel.allowsMultipleSelection = false
  263. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  264. if response == .OK {
  265. let outputURL = panel.url
  266. let s = self.listView?.document?.documentURL.lastPathComponent
  267. let folderPath = (self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? "") + "_extract"
  268. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  269. var i = 1
  270. let testFilePath = filePath
  271. while FileManager.default.fileExists(atPath: filePath!) {
  272. filePath = testFilePath! + "\(i)"
  273. i = i + 1
  274. }
  275. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  276. var saveURLs : [URL] = []
  277. for j in 0 ... imagesAreas.count-1 {
  278. let documentFileName = self.listView?.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  279. var outPath = filePath!
  280. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  281. outPath = outPath + "page \(j+1)"
  282. outPath = outPath.stringByAppendingPathExtension("jpg")
  283. let result = self.listView?.extractImage(with: imagesAreas[j], toImagePath: outPath) ?? false
  284. if result {
  285. saveURLs.append(URL(fileURLWithPath: outPath))
  286. }
  287. }
  288. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  289. }
  290. }
  291. }
  292. }
  293. func alignmentAction() {
  294. KMPrint("alignmentAction")
  295. }
  296. func showPopWindow(positionRect: NSRect) {
  297. let win = KMEditPDFPopToolBarWindow.shared
  298. let areas = self.editingAreas
  299. win.isMultiple = areas.count > 1
  300. var hasText = false
  301. var hasImage = false
  302. for area in areas {
  303. if area is CPDFEditTextArea {
  304. hasText = true
  305. }
  306. if area is CPDFEditImageArea {
  307. hasImage = true
  308. }
  309. }
  310. var style: KMEditPDFToolbarStyle = []
  311. if hasText {
  312. style.insert(.text)
  313. }
  314. if hasImage {
  315. style.insert(.image)
  316. }
  317. win.style = style
  318. win.show(relativeTo: positionRect, of: self.viewC!.listView, preferredEdge: .maxY)
  319. self.viewC?.view.window?.addChildWindow(win, ordered: .above)
  320. win.itemClick = { [weak self] itemKey, obj in
  321. if itemKey == .fontAdd {
  322. self?.fontAddAction()
  323. } else if itemKey == .fontReduce {
  324. self?.fontReduceAction()
  325. } else if itemKey == .fontBold {
  326. self?.fontBoldAction()
  327. } else if itemKey == .fontItalic {
  328. self?.fontItalicAction()
  329. } else if itemKey == .textAlignment {
  330. self?.textAlignmentAction()
  331. }
  332. // 图片
  333. else if itemKey == .leftRotate {
  334. self?.leftRotateAction()
  335. } else if itemKey == .rightRotate {
  336. self?.rightRotateAction()
  337. } else if itemKey == .reverseX {
  338. self?.reverseXAction()
  339. } else if itemKey == .reverseY {
  340. self?.reverseYAction()
  341. } else if itemKey == .crop {
  342. self?.cropAction()
  343. } else if itemKey == .replace {
  344. self?.replaceAction()
  345. } else if itemKey == .export {
  346. self?.exportAction()
  347. }
  348. // 对齐
  349. else if itemKey == .alignmentLeft {
  350. self?.alignmentAction()
  351. } else if itemKey == .alignmentCenterX {
  352. self?.alignmentAction()
  353. } else if itemKey == .alignmentRight {
  354. self?.alignmentAction()
  355. } else if itemKey == .alignmentjustifiedX {
  356. self?.alignmentAction()
  357. } else if itemKey == .alignmentTop {
  358. self?.alignmentAction()
  359. } else if itemKey == .alignmentCenterY {
  360. self?.alignmentAction()
  361. } else if itemKey == .alignmentBottom {
  362. self?.alignmentAction()
  363. } else if itemKey == .alignmentjustifiedY {
  364. self?.alignmentAction()
  365. }
  366. }
  367. // 显示新手引导
  368. if let toolbarView = (win.contentViewController as? KMEditPDFPopToolBarController)?.toolbarView {
  369. if let view = toolbarView.subviews.last {
  370. self.showGuideView(view)
  371. }
  372. }
  373. }
  374. func showGuideView(_ view: NSView) {
  375. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  376. if KMGuideInfoWindowController.availableShow(.editPDFPopWindow) {
  377. let guideWC = KMGuideInfoWindowController.currentWC()
  378. // guard let guideWC = guideInfoWindowController else { return }
  379. guideWC.type = .editPDFPopWindow
  380. var viewFrame = view.superview?.convert(view.frame, to: view.window?.contentView) ?? .zero
  381. viewFrame.origin.x += (view.window?.frame.origin.x ?? 0)
  382. viewFrame.origin.x -= (self.viewC?.view.window?.frame.origin.x ?? 0)
  383. viewFrame.origin.y += (view.window?.frame.origin.y ?? 0)
  384. viewFrame.origin.x -= (self.viewC?.view.window?.frame.origin.y ?? 0)
  385. viewFrame.origin.x -= KMEditPDFPopGuideView.kWidth
  386. viewFrame.origin.y -= KMEditPDFPopGuideView.kHeight
  387. viewFrame.origin.y += 26
  388. guideWC.digitalBoxRect = viewFrame
  389. var beh = view.window?.collectionBehavior ?? []
  390. beh.insert(.canJoinAllSpaces)
  391. guideWC.window?.collectionBehavior = beh
  392. let view_ = self.viewC?.view
  393. var rect = view_?.window?.frame ?? .zero
  394. rect.size.height -= 20
  395. guideWC.window?.setFrame(rect, display: false)
  396. guideWC.window?.minSize = rect.size
  397. guideWC.window?.maxSize = rect.size
  398. view_?.window?.addChildWindow(guideWC.window!, ordered: .above)
  399. guideWC.show()
  400. guideWC.settingCallback = {
  401. KMPreferenceController.shared.showWindow(nil)
  402. }
  403. }
  404. }
  405. }
  406. }
  407. extension KMEditPDfHanddler: CPDFViewDelegate {
  408. // 编辑区块已经改变
  409. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  410. let isEdited = self.listView?.isEdited() ?? false
  411. if isEdited {
  412. // 记录编辑状态
  413. self.viewC?.recordIsPDFDocumentEdited(type: .editText)
  414. }
  415. if annotationType != .addText {
  416. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView?.document)
  417. }
  418. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  419. if areas.isEmpty {
  420. let toolMode = self.listView?.toolMode ?? .none
  421. let annotationType = self.annotationType
  422. if toolMode == .editPDFToolMode {
  423. if annotationType == .addImage || annotationType == .addText {
  424. if self.isEditImage {
  425. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  426. } else {
  427. // if self.listView.annotationType == .addImage {
  428. // self.closeRightPane()
  429. // }
  430. if annotationType == .addImage {
  431. if self.rightViewC?.eidtPDFImageProperty != nil {
  432. self.rightViewC?.eidtPDFImageProperty.reloadData()
  433. }
  434. }
  435. // self.openRightPane()
  436. }
  437. } else {
  438. self.viewC?.closeRightPane()
  439. }
  440. } else {
  441. self.rightViewC?.isHidden = true
  442. self.viewC?.closeRightPane()
  443. if self.subViewType == .EditPDFAddText && annotationType == .addText {
  444. self.rightViewC?.eidtPDFTextProperty.initData()
  445. }
  446. }
  447. return
  448. }
  449. self.viewC?.model.isPDFTextImageEdited = true
  450. let subViewType = self.rightViewC?.subViewType ?? .None
  451. if self.annotationType == .addImage {
  452. var isImageArea = false
  453. for i in 0 ..< areas.count {
  454. if areas[i] is CPDFEditImageArea {
  455. isImageArea = true
  456. }
  457. }
  458. if isImageArea {
  459. self.rightViewC?.isHidden = false
  460. if self.subViewType == .EditPDFAddImage {
  461. self.rightViewC?.subViewType = .EditPDFAddImage
  462. self.rightViewC?.eidtPDFImageProperty.reloadData()
  463. }
  464. self.viewC?.openRightPane()
  465. } else {
  466. self.rightViewC?.isHidden = true
  467. self.viewC?.closeRightPane()
  468. }
  469. } else if self.subViewType == .EditPDFAddText && annotationType == .addText {
  470. self.rightViewC?.isHidden = false
  471. let count = self.listView?.editingSelectionString()?.count ?? 0
  472. if count != 0 {
  473. self.rightViewC?.eidtPDFTextProperty.reloadData()
  474. } else {
  475. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  476. }
  477. self.viewC?.openRightPane()
  478. } else {
  479. var textsAreas : [CPDFEditTextArea] = []
  480. var imagesAreas : [CPDFEditImageArea] = []
  481. let count = self.listView?.editingAreas()?.count ?? 0
  482. if count < 1 {
  483. return
  484. }
  485. for i in 0 ..< areas.count {
  486. if areas[i] is CPDFEditTextArea {
  487. textsAreas.append(areas[i] as! CPDFEditTextArea)
  488. }
  489. if areas[i] is CPDFEditImageArea {
  490. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  491. }
  492. }
  493. if textsAreas.count > 0 && textsAreas.count == areas.count {
  494. self.rightViewC?.isHidden = false
  495. self.rightViewC?.subViewType = .EditPDFAddText
  496. self.rightViewC?.eidtPDFTextProperty?.reloadData()
  497. self.viewC?.openRightPane()
  498. } else if imagesAreas.count > 0 {
  499. self.rightViewC?.isHidden = false
  500. self.rightViewC?.subViewType = .EditPDFAddImage
  501. self.rightViewC?.eidtPDFImageProperty?.reloadData()
  502. self.viewC?.openRightPane()
  503. }
  504. }
  505. var flag: CPDFEditArea?
  506. for area in areas {
  507. if flag == nil {
  508. flag = area
  509. continue
  510. }
  511. if let data = flag, data.bounds.maxY < area.bounds.maxY {
  512. flag = area
  513. }
  514. }
  515. if let data = flag {
  516. self.showPopWindow(positionRect: data.bounds)
  517. }
  518. }
  519. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  520. if editArea != nil && (editArea is CPDFEditImageArea){
  521. self.listView?.cropAreas = editArea as? CPDFEditImageArea
  522. }
  523. }
  524. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  525. if self.isEditImage {
  526. self.viewC?.menuItemEditingClick_CropImage(sender: NSMenuItem())
  527. } else {
  528. let panel = NSOpenPanel()
  529. panel.allowsMultipleSelection = false
  530. panel.allowedFileTypes = ["png","jpg"]
  531. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  532. if response == .OK {
  533. var filePath = panel.url?.path
  534. var image = NSImage.init(contentsOf: panel.url!)
  535. //图片自适应范围
  536. if image != nil {
  537. var imageRect = rect
  538. let imageSize = image!.size
  539. var previewSize = rect.size
  540. var isChangeSize = false
  541. if previewSize.width == 0 && previewSize.height == 0 {
  542. previewSize = CGSize(width: 500, height: 500)
  543. isChangeSize = true
  544. }
  545. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  546. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  547. if isChangeSize {
  548. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  549. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  550. } else {
  551. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  552. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  553. }
  554. imageRect.size = newSize
  555. let limitWidth = 1920.0
  556. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  557. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  558. targetSize: CGSize(width: limitWidth, height: limitWidth),
  559. maxSizeInBytes: 1024 * 1024 * 5,
  560. targetCompression: 1.0)
  561. }
  562. //自适应page
  563. let pageRect = self.listView?.currentPage().bounds ?? .zero
  564. if imageRect.width > pageRect.width ||
  565. imageRect.height > pageRect.height {
  566. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  567. imageRect = CGRect(x: imageRect.origin.x,
  568. y: imageRect.origin.y,
  569. width: imageRect.width * pageScale,
  570. height: imageRect.height * pageScale)
  571. }
  572. if imageRect.origin.x < 0 {
  573. imageRect.origin.x = 5
  574. }
  575. if imageRect.origin.y < 0 {
  576. imageRect.origin.y = 5
  577. }
  578. if imageRect.origin.x + imageRect.width > pageRect.width ||
  579. imageRect.origin.y + imageRect.height > pageRect.height {
  580. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  581. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  582. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  583. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  584. }
  585. DispatchQueue.main.async {
  586. self.listView?.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  587. self.viewC?.model.isPDFTextImageEdited = true
  588. self.viewC?.recordIsPDFDocumentEdited(type: .editImage)
  589. self.showPopWindow(positionRect: imageRect)
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  597. let window = KMEditPDFPopToolBarWindow.shared
  598. if (window.isVisible) {
  599. window.orderOut(nil)
  600. let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  601. if let area = areas.last {
  602. if let data = area as? CPDFEditTextArea {
  603. if let str = data.editTextAreaString(), str.isEmpty {
  604. self.listView?.remove(with: [area])
  605. }
  606. }
  607. }
  608. return
  609. }
  610. var newRect = rect
  611. if CGSizeEqualToSize(rect.size, .zero) {
  612. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  613. } else {
  614. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  615. }
  616. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  617. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  618. let fontSize = model.fontSize
  619. let fontColor = model.color
  620. let fontAlign = model.alignment
  621. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  622. NSColorPanel.shared.color = fontColor
  623. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  624. let attri = CEditAttributes()
  625. attri.font = font
  626. attri.fontColor = fontColor
  627. attri.alignment = fontAlign
  628. self.listView?.createStringBounds(newRect, with: attri, page: page)
  629. // self.rightViewC != nil &&
  630. if self.subViewType == .EditPDFAddText && self.annotationType == .addText {
  631. self.rightViewC?.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  632. }
  633. self.showPopWindow(positionRect: newRect)
  634. }
  635. // 文本区块 选中文本已经变化
  636. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  637. // self.viewC?.rightSideViewController != nil &&
  638. if self.subViewType == .EditPDFAddText {
  639. self.rightViewC?.eidtPDFTextProperty.reloadData()
  640. self.rightViewC?.eidtPDFTextProperty.updateTextTextPresuppositionState()
  641. }
  642. }
  643. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  644. KMPrint("pdfViewMobileEditingBegan")
  645. }
  646. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  647. KMPrint("pdfViewMobileEditingMove")
  648. }
  649. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  650. KMPrint("pdfViewMobileEditingEnd")
  651. }
  652. }