KMEditPDFTextPropertyViewController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. //
  2. // KMEditPDFTextPropertyViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/12/27.
  6. //
  7. import Cocoa
  8. enum CPDFActiveAreasAlignType : Int {
  9. case Left
  10. case Vertical
  11. case Right
  12. case Top
  13. case Horizontally
  14. case Bottom
  15. case DisHorizontally
  16. case DisVertical
  17. }
  18. class KMEditPDFTextPropertyViewController: NSViewController {
  19. @IBOutlet weak var contentView: NSClipView!
  20. @IBOutlet weak var headerBox: NSBox!
  21. @IBOutlet weak var imageContentView: NSView!
  22. @IBOutlet weak var imageViewHeightConstraint: NSLayoutConstraint!
  23. @IBOutlet weak var propertyTitle: NSTextField!
  24. @IBOutlet weak var preImageView: NSImageView!
  25. @IBOutlet weak var textPresuppositionContentView: NSView!
  26. @IBOutlet weak var textPresuppositionBox: NSBox!
  27. @IBOutlet weak var resetTextPresuppositionBox: NSBox!
  28. @IBOutlet weak var textPresuppositionTopContstraint: NSLayoutConstraint!
  29. @IBOutlet weak var textPresuppositionHeightContstraint: NSLayoutConstraint!
  30. @IBOutlet weak var fontContentView: NSView!
  31. @IBOutlet weak var fontTitleLabel: NSTextField!
  32. @IBOutlet weak var fontImageView: NSButton!
  33. @IBOutlet weak var fontContentViewHeightConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var fontContentViewTopConstraint: NSLayoutConstraint!
  35. @IBOutlet weak var fontNameBox: NSBox!
  36. @IBOutlet weak var fontStyleBox: NSBox!
  37. @IBOutlet weak var fontSizeBox: NSBox!
  38. @IBOutlet weak var leftAlignmentBox: NSBox!
  39. @IBOutlet weak var centerAlignmentBox: NSBox!
  40. @IBOutlet weak var rightAlignmentBox: NSBox!
  41. @IBOutlet weak var colorBox: NSBox!
  42. @IBOutlet weak var fontColorButton: NSButton!
  43. @IBOutlet weak var fontCustomColorButton: NSButton!
  44. @IBOutlet weak var alignmentTopConstraint: NSLayoutConstraint!
  45. @IBOutlet weak var alignmentView: KMEditPropertyAlignmentView!
  46. var textPresuppositionVC: KMDesignSelect?
  47. var textPresuppositionResetVC: KMDesignPropertySelector?
  48. var textPresuppositionResetString: String = ""
  49. var leftAlignmentVC: KMDesignPropertySelector?
  50. var centerAlignmentVC: KMDesignPropertySelector?
  51. var rightAlignmentVC: KMDesignPropertySelector?
  52. var fontNameVC: KMDesignSelect?
  53. var fontSizeVC: KMDesignSelect?
  54. var fontStyleVC: KMDesignSelect?
  55. var borderWidthVC: KMDesignSelect?
  56. var textsAreas : [CPDFEditTextArea] = []
  57. var listView : CPDFListView!
  58. var fonts: [NSDictionary] = []
  59. var currentColor : NSColor = NSColor.black
  60. var alignment : NSTextAlignment = .left
  61. var fontName: String = "Helvetica" {
  62. didSet {
  63. if self.fontNameVC != nil {
  64. if self.fontNameVC!.items.contains(fontName) {
  65. self.fontNameVC?.stringValue = fontName
  66. } else {
  67. self.fontNameVC?.stringValue = "Helvetica"
  68. }
  69. }
  70. }
  71. }
  72. var fontStyle: String = "Regular" {
  73. didSet {
  74. self.fontStyleVC?.stringValue = fontStyle
  75. }
  76. }
  77. var fontSize: CGFloat = 12 {
  78. didSet {
  79. self.fontSizeVC?.stringValue = Int(ceil(fontSize)).description + "pt"
  80. }
  81. }
  82. var selectAreas: [Any] {
  83. get {
  84. return self.listView.editingAreas() ?? []
  85. }
  86. }
  87. deinit {
  88. KMPrint("KMEditPDFTextPropertyViewController 已释放.")
  89. NSColorPanel.shared.setTarget(nil)
  90. NSColorPanel.shared.setAction(nil)
  91. NSColorPanel.shared.close()
  92. }
  93. override func viewDidLoad() {
  94. super.viewDidLoad()
  95. self.setup()
  96. self.initData()
  97. // self.reloadData()
  98. self.updateLanguage()
  99. self.updatePreviewImage()
  100. }
  101. func setup() {
  102. self.contentView.backgroundColor(NSColor(hex: "#F7F8FA"))
  103. self.propertyTitle.font = NSFont.SFProTextSemibold(14.0)
  104. self.propertyTitle.textColor = NSColor(hex: "#252629")
  105. self.fontTitleLabel.font = NSFont.SFProTextSemibold(12.0)
  106. self.fontTitleLabel.textColor = NSColor(hex: "#616469")
  107. self.headerBox.borderWidth = 1
  108. self.headerBox.borderColor = NSColor(hex: "#DFE1E5")
  109. self.headerBox.cornerRadius = 4
  110. self.headerBox.fillColor = NSColor(hex: "#FFFFFF")
  111. //
  112. self.fontColorButton.border(NSColor.clear, 0, 10)
  113. //alignment
  114. leftAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  115. leftAlignmentBox.contentView = leftAlignmentVC?.view
  116. leftAlignmentBox.fillColor = NSColor.clear
  117. leftAlignmentVC?.target = self
  118. leftAlignmentVC?.action = #selector(leftAlignmentAction)
  119. leftAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignLeft")!
  120. leftAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")!
  121. leftAlignmentVC?.updateUI()
  122. centerAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  123. centerAlignmentBox.contentView = centerAlignmentVC?.view
  124. centerAlignmentBox.fillColor = NSColor.clear
  125. centerAlignmentVC?.target = self
  126. centerAlignmentVC?.action = #selector(centerAlignmentAction)
  127. centerAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignCenter")!
  128. centerAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")!
  129. rightAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  130. rightAlignmentBox.contentView = rightAlignmentVC?.view
  131. rightAlignmentBox.fillColor = NSColor.clear
  132. rightAlignmentVC?.target = self
  133. rightAlignmentVC?.action = #selector(rightAlignmentAction)
  134. rightAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignRight")!
  135. rightAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignRightSelect")!
  136. //font
  137. var fontNameArray: [String] = []
  138. for font in CPDFAnnotationModel.supportFonts() {
  139. let fontName = (font as? NSDictionary)!.allKeys.first
  140. fontNameArray.append(fontName as! String)
  141. }
  142. fontNameVC = KMDesignSelect.init(withType: .PopButton)
  143. fontNameBox.contentView = fontNameVC?.view
  144. fontNameBox.fillColor = NSColor.clear
  145. fontNameVC?.addItems(withObjectValues: fontNameArray)
  146. fontNameVC?.selectItem(at: 0)
  147. fontNameVC?.delete = self
  148. let styleArray = KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: fontName)
  149. fontStyleVC = KMDesignSelect.init(withType: .PopButton)
  150. fontStyleBox.contentView = fontStyleVC?.view
  151. fontStyleBox.fillColor = NSColor.clear
  152. fontStyleVC?.addItems(withObjectValues: styleArray)
  153. fontStyleVC?.selectItem(at: 0)
  154. fontStyleVC?.delete = self
  155. fontSizeVC = KMDesignSelect.init(withType: .PopButton)
  156. fontSizeBox.contentView = fontSizeVC?.view
  157. fontSizeBox.fillColor = NSColor.clear
  158. fontSizeVC?.removeAllItems()
  159. fontSizeVC?.addItems(withObjectValues: self.supportFontSize())
  160. fontSizeVC?.selectItem(at: 0)
  161. fontSizeVC?.delete = self
  162. //textPresupposition
  163. textPresuppositionVC = KMDesignSelect.init(withType: .PopButton)
  164. textPresuppositionBox.contentView = textPresuppositionVC?.view
  165. textPresuppositionBox.fillColor = NSColor.clear
  166. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  167. textPresuppositionVC?.selectItem(at: 0)
  168. textPresuppositionVC?.delete = self
  169. textPresuppositionResetVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  170. resetTextPresuppositionBox.contentView = textPresuppositionResetVC?.view
  171. resetTextPresuppositionBox.fillColor = NSColor.clear
  172. textPresuppositionResetVC?.target = self
  173. textPresuppositionResetVC?.action = #selector(resetTextPresuppositionButtonAction)
  174. textPresuppositionResetVC?.image = NSImage(named: "KMImagePropertPanelTextDefaultMore")!
  175. self.colorBox.borderColor = NSColor(hex: "#DFE1E5")
  176. self.alignmentView.didChange = { [unowned self] view, areasArray, boundsArray in
  177. self.changeAreasAlign(areas: areasArray, newBounds: boundsArray)
  178. }
  179. }
  180. func initData() {
  181. self.updateTextPresupposition(type: .commonly, needChangeListView: false)
  182. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  183. self.currentColor = model.color
  184. DispatchQueue.main.async {
  185. NSColorPanel.shared.color = model.color
  186. }
  187. self.fontColorButton.image = self.swatchWithColor(color: self.currentColor, size: NSSize(width: 20, height: 20))
  188. self.fonts = CPDFAnnotationModel.supportFonts() as! [NSDictionary]
  189. }
  190. func updateUI() {
  191. }
  192. func updateLanguage() {
  193. let areas = self.selectAreas
  194. if textsAreas.count > 0 && textsAreas.count != areas.count { //多选图片跟文字
  195. self.propertyTitle.stringValue = NSLocalizedString("General Properties", comment: "")
  196. } else {
  197. self.propertyTitle.stringValue = NSLocalizedString("Text", comment: "")
  198. }
  199. self.fontTitleLabel.stringValue = NSLocalizedString("Font", comment: "")
  200. }
  201. func reloadData() {
  202. if self.selectAreas.count > 0 {
  203. textsAreas = []
  204. let areas = self.selectAreas
  205. self.alignmentView.editingAreas = areas
  206. for i in 0 ... areas.count-1 {
  207. if areas[i] is CPDFEditTextArea {
  208. textsAreas.append(areas[i] as! CPDFEditTextArea)
  209. }
  210. }
  211. if textsAreas.count == 1 && textsAreas.count == areas.count {
  212. self.headerBox.isHidden = false
  213. self.fontContentView.isHidden = false
  214. self.textPresuppositionContentView.isHidden = false
  215. self.imageViewHeightConstraint.constant = 88
  216. self.textPresuppositionTopContstraint.constant = 152
  217. self.fontContentViewHeightConstraint.constant = 153
  218. self.alignmentTopConstraint.constant = 16
  219. self.fontContentViewTopConstraint.constant = 8
  220. } else if textsAreas.count > 1 && textsAreas.count == areas.count {
  221. self.headerBox.isHidden = true
  222. self.fontContentView.isHidden = false
  223. self.textPresuppositionContentView.isHidden = false
  224. self.imageViewHeightConstraint.constant = 0
  225. self.textPresuppositionTopContstraint.constant = 50
  226. self.fontContentViewHeightConstraint.constant = 153
  227. self.alignmentTopConstraint.constant = 16
  228. self.fontContentViewTopConstraint.constant = 8
  229. } else if textsAreas.count > 0 && textsAreas.count != areas.count { //多选图片跟文字
  230. self.headerBox.isHidden = true
  231. self.fontContentView.isHidden = true
  232. self.textPresuppositionContentView.isHidden = true
  233. self.fontContentViewHeightConstraint.constant = 0
  234. self.textPresuppositionTopContstraint.constant = 16
  235. self.imageViewHeightConstraint.constant = 0
  236. self.alignmentTopConstraint.constant = 0
  237. self.fontContentViewTopConstraint.constant = 0
  238. }
  239. self.refreshSelectAreaProperty()
  240. self.updateLanguage()
  241. }
  242. }
  243. func refreshSelectAreaProperty(needDefaultData: Bool = false) {
  244. if self.selectAreas.count == 1 {
  245. let areas = self.selectAreas.first
  246. if areas is CPDFEditTextArea {
  247. var sizeString = "\(abs(self.listView.editingSelectionFontSize(with: areas as? CPDFEditTextArea)))"
  248. var fontName: String = self.listView.editingSelectionFontName(with: areas as? CPDFEditTextArea) ?? "Helvetica"
  249. let alignment = self.listView.editingSelectionAlignment(with: areas as? CPDFEditTextArea)
  250. let color = self.listView.editingSelectionFontColor(with: areas as? CPDFEditTextArea) ?? NSColor.black
  251. fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? [])
  252. //获取默认数据
  253. if needDefaultData {
  254. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  255. fontName = model.fontName
  256. sizeString = model.fontSize.description
  257. }
  258. self.updateTextPresupposition(fontName: fontName, size: CGFloat(Float(sizeString)!), needChangeListView: false)
  259. self.currentColor = color
  260. DispatchQueue.main.async {
  261. NSColorPanel.shared.color = color
  262. }
  263. self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20))
  264. self.alignment = alignment
  265. self.selectAlignment(alignment: alignment)
  266. self.updatePreviewImage()
  267. }
  268. }
  269. }
  270. override func mouseDown(with event: NSEvent) {
  271. }
  272. //MARK: ToolAction
  273. private func swatchWithColor(color:NSColor,size:NSSize) -> NSImage {
  274. let image = NSImage(size: size)
  275. image.lockFocus()
  276. color.drawSwatch(in: NSRect(x: 0, y: 0, width: size.width, height: size.height))
  277. image.unlockFocus()
  278. return image
  279. }
  280. private func changeAreasAlign(areas:[Any],newBounds:[String]) {
  281. var oldBounds : [String] = []
  282. for i in 0 ... areas.count-1 {
  283. let area : CPDFEditArea = areas[i] as! CPDFEditArea
  284. oldBounds.append(NSStringFromRect(area.bounds))
  285. self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i]))
  286. }
  287. self.listView.setNeedsDisplayForVisiblePages()
  288. }
  289. }
  290. //MARK: - Action
  291. extension KMEditPDFTextPropertyViewController {
  292. @IBAction func fontColorAction(_ sender: Any) {
  293. let color = self.listView.editingSelectionFontColor(with: self.selectAreas.first as? CPDFEditTextArea)
  294. let panel = NSColorPanel.shared
  295. panel.setTarget(self)
  296. panel.setAction(#selector(fontColorChangeAction))
  297. panel.orderFront(nil)
  298. panel.showsAlpha = false
  299. if color != nil {
  300. panel.color = color ?? NSColor.black
  301. }
  302. }
  303. @objc func fontColorChangeAction() {
  304. let color = NSColorPanel.shared.color
  305. self.currentColor = color
  306. self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20))
  307. if self.selectAreas.count > 0 {
  308. self.listView.setEditingSelectionFontColor(color, with: self.selectAreas.first as? CPDFEditTextArea)
  309. } else {
  310. KMEditPDFTextManager.manager.setCommonlyFontColor(color: self.currentColor)
  311. }
  312. self.updatePreviewImage()
  313. }
  314. @objc func leftAlignmentAction() {
  315. self.leftAlignmentVC?.state = .Sel
  316. self.rightAlignmentVC?.state = .Norm
  317. self.centerAlignmentVC?.state = .Norm
  318. self.updateAlignment(alignment: .left)
  319. }
  320. @objc func centerAlignmentAction() {
  321. self.leftAlignmentVC?.state = .Norm
  322. self.rightAlignmentVC?.state = .Norm
  323. self.centerAlignmentVC?.state = .Sel
  324. self.updateAlignment(alignment: .center)
  325. }
  326. @objc func rightAlignmentAction() {
  327. self.leftAlignmentVC?.state = .Norm
  328. self.rightAlignmentVC?.state = .Sel
  329. self.centerAlignmentVC?.state = .Norm
  330. self.updateAlignment(alignment: .right)
  331. }
  332. func selectAlignment(alignment: NSTextAlignment) {
  333. switch alignment {
  334. case .left:
  335. self.leftAlignmentAction()
  336. case .right:
  337. self.rightAlignmentAction()
  338. case .center:
  339. self.centerAlignmentAction()
  340. default:
  341. self.leftAlignmentAction()
  342. }
  343. }
  344. @objc func resetTextPresuppositionButtonAction(sender: NSButton) {
  345. var popViewDataArr: [String] = []
  346. for string in ["Redefine", "Reset"] {
  347. popViewDataArr.append(NSLocalizedString(string, comment: ""))
  348. }
  349. //调整保存参数
  350. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  351. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  352. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  353. var disItems: [String] = popViewDataArr
  354. if model.change || model.redefine {
  355. disItems.removeObject("Reset")
  356. }
  357. if model.change {
  358. disItems.removeObject("Redefine")
  359. }
  360. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr)
  361. vc.disItems = disItems
  362. let createFilePopover: NSPopover = NSPopover.init()
  363. createFilePopover.contentViewController = vc
  364. createFilePopover.animates = true
  365. createFilePopover.behavior = .semitransient
  366. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  367. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: -10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  368. // vc.customBoxWidthLayoutConstraint.constant = self.popWidth ?? sender.frame.width
  369. vc.downCallback = { [unowned self](downEntered: Bool, count: String) -> Void in
  370. if downEntered {
  371. if count == "Reset" {
  372. self.resetTextPresuppositionData()
  373. } else if count == "Redefine" {
  374. self.reDefineTextPresuppositionData()
  375. }
  376. createFilePopover.close()
  377. }
  378. }
  379. }
  380. func updateTextTextPresuppositionState() {
  381. let areas = self.selectAreas.first
  382. if areas is CPDFEditTextArea {
  383. var size: CGFloat = (abs(self.listView.editingSelectionFontSize(with: areas as? CPDFEditTextArea)))
  384. var fontName: String = self.listView.editingSelectionFontName(with: areas as? CPDFEditTextArea) ?? "Helvetica"
  385. fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? [])
  386. let models = KMEditPDFTextManager.manager.fetchAllUserDefaultData()
  387. var index = 0
  388. for i in 0...models.count - 1 {
  389. let model = models[i]
  390. if model.fontName == fontName && size == model.fontSize {
  391. index = i
  392. break
  393. }
  394. }
  395. //刷新样式
  396. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  397. textPresuppositionVC?.selectItem(at: index)
  398. }
  399. }
  400. }
  401. //MARK: - Update
  402. extension KMEditPDFTextPropertyViewController {
  403. func updateAlignment(alignment: NSTextAlignment) {
  404. if self.alignment != alignment {
  405. if self.selectAreas.count > 0 {
  406. self.listView.setCurrentSelectionAlignment(alignment, with: self.selectAreas.first as? CPDFEditTextArea)
  407. } else {
  408. KMEditPDFTextManager.manager.setFontAlignment(alignment: alignment)
  409. }
  410. self.alignment = alignment
  411. self.updatePreviewImage()
  412. }
  413. }
  414. func updateTextPresupposition(type: KMEditPDFTextFontType, needChangeListView: Bool = true) {
  415. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  416. let fontName: String = model.fontName
  417. let size: CGFloat = model.fontSize
  418. self.updateTextPresupposition(fontName: fontName, size: size, needChangeListView: needChangeListView)
  419. //刷新样式
  420. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  421. textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem)
  422. }
  423. }
  424. extension KMEditPDFTextPropertyViewController: KMSelectPopButtonDelegate {
  425. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  426. if obj == textPresuppositionVC {
  427. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[obj.indexOfSelectedItem])
  428. self.updateTextPresupposition(type: type)
  429. } else if obj == fontNameVC {
  430. self.updateFontNameAndStyle(name: obj.stringValue, style: fontStyle)
  431. self.updatePreviewImage()
  432. } else if obj == fontSizeVC {
  433. let size = CGFloat(Float((obj.stringValue.replacingOccurrences(of: "pt", with: "")))!)
  434. self.updateFontSize(size: size)
  435. self.updatePreviewImage()
  436. } else if obj == fontStyleVC {
  437. self.updateFontNameAndStyle(name: fontName, style: obj.stringValue)
  438. self.updatePreviewImage()
  439. }
  440. }
  441. }
  442. //MARK: - TextPresupposition
  443. extension KMEditPDFTextPropertyViewController {
  444. //MARK: 基本属性调整
  445. func updateTextPresupposition(fontName: String, size: CGFloat, needChangeListView: Bool = true) {
  446. let fontNameArray = fontName.components(separatedBy: "-")
  447. var name = ""
  448. var style = ""
  449. if fontNameArray.count > 0 {
  450. name = fontNameArray.first!
  451. } else {
  452. name = "Helvetica"
  453. }
  454. if fontNameArray.count == 2 {
  455. style = fontNameArray.last!
  456. } else {
  457. style = "Regular"
  458. }
  459. self.updateFontNameAndStyle(name: name, style: style, needChangeListView: needChangeListView, needSave: false)
  460. self.updateFontSize(size: size, needChangeListView: needChangeListView, needSave: false)
  461. self.updatePreviewImage()
  462. }
  463. func resetTextPresuppositionData() {
  464. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  465. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  466. KMEditPDFTextManager.manager.resetTextPresupposition(type: type)
  467. self.updateTextPresupposition(type: type)
  468. }
  469. func reDefineTextPresuppositionData(redefine: Bool = true) {
  470. let fontStyle = self.fontStyle
  471. let fontName = self.fontName + "-" + fontStyle
  472. let fontSize = self.fontSize
  473. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  474. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  475. KMEditPDFTextManager.manager.reDefineTextPresupposition(fontName: fontName,
  476. fontSize: fontSize,
  477. type: type)
  478. self.updateTextPresupposition(type: type)
  479. }
  480. }
  481. //MARK: - Private
  482. extension KMEditPDFTextPropertyViewController {
  483. func updateFontNameAndStyle(name: String, style: String, needChangeListView: Bool = true, needSave: Bool = true) {
  484. // if fontName != name || fontStyle != style {
  485. let styleArray = KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: name)
  486. var styleString = KMEditPDFTextManager.manager.checkFontStyle(style: style)
  487. if !styleArray.contains(style) {
  488. self.fontStyleVC?.addItems(withObjectValues: styleArray)
  489. self.fontStyleVC?.selectItem(at: 0)
  490. styleString = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC!.stringValue)
  491. } else {
  492. self.fontStyleVC?.selectItem(at: styleArray.firstIndex(of: styleString) ?? 0)
  493. }
  494. var fontName = "\(name)-\(styleString)"
  495. if styleString.count == 0 {
  496. fontName = name
  497. }
  498. //数据保存返回
  499. if self.selectAreas.count > 0 {
  500. if needChangeListView {
  501. self.listView.setEditingSelectionFontName(fontName, with: self.selectAreas.first as? CPDFEditTextArea)
  502. if styleString.contains("Bold") {
  503. self.listView.setCurrentSelectionIsBold(true, with: self.selectAreas.first as? CPDFEditTextArea)
  504. } else {
  505. self.listView.setCurrentSelectionIsBold(false, with: self.selectAreas.first as? CPDFEditTextArea)
  506. }
  507. if styleString.contains("Oblique") || styleString.contains("Italic") {
  508. self.listView.setCurrentSelectionIsItalic(true, with: self.selectAreas.first as? CPDFEditTextArea)
  509. } else {
  510. self.listView.setCurrentSelectionIsItalic(false, with: self.selectAreas.first as? CPDFEditTextArea)
  511. }
  512. }
  513. } else {
  514. if needSave {
  515. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  516. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: fontName, fontSize: model.fontSize, type: .commonly)
  517. }
  518. }
  519. self.fontName = name
  520. self.fontStyle = self.fontStyleVC?.stringValue ?? "Regular"
  521. if needSave {
  522. //调整保存参数
  523. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  524. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  525. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  526. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: fontName, fontSize: model.fontSize, type: type)
  527. }
  528. //刷新样式
  529. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  530. textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem)
  531. // }
  532. }
  533. func updateFontSize(size: CGFloat, needChangeListView: Bool = true, needSave: Bool = true) {
  534. // var fontSize = Float((self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: ""))!) ?? 8.0
  535. if fontSize != size {
  536. if self.selectAreas.count > 0 {
  537. if needChangeListView {
  538. self.listView.setEditingSelectionFontSize(size, with: self.selectAreas.first as? CPDFEditTextArea)
  539. }
  540. } else {
  541. if needSave {
  542. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  543. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontSize: size, type: .commonly)
  544. }
  545. }
  546. self.fontSize = size
  547. if needSave {
  548. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  549. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  550. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  551. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontSize: size, type: type)
  552. }
  553. //刷新样式
  554. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  555. textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem)
  556. }
  557. }
  558. //MARK: 刷新预览图片
  559. private func updatePreviewImage() {
  560. var alignment = self.alignment
  561. var fontSize = Float((self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: ""))!) ?? 1.0
  562. var fontName = self.fontNameVC?.stringValue ?? "Sample"
  563. let fontStyle = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC?.stringValue ?? "")
  564. let fontCurrentColor = self.currentColor
  565. fontName = KMEditPDFTextManager.manager.checkFontName(fontName: (fontName + "-" + fontStyle))
  566. fontSize = max(fontSize, 8)
  567. let editringareas = self.selectAreas
  568. let count = editringareas.count
  569. // let editingSelectionString = self.listView.editingSelectionString()
  570. let editingSelectionAlignment = self.listView.editingSelectionAlignment(with: editringareas.first as? CPDFEditTextArea)
  571. if count == 1 {
  572. let areas = editringareas.first
  573. if areas is CPDFEditTextArea {
  574. // string = editingSelectionString ?? NSLocalizedString("Sample", comment: "")
  575. alignment = editingSelectionAlignment
  576. }
  577. }
  578. let image = KMEditPDFTextManager.manager.fetchTextImage(alignment: .center,
  579. fontName: fontName,
  580. fontSize: CGFloat(fontSize),
  581. color: fontCurrentColor,
  582. imageSize: self.preImageView.frame.size)
  583. self.preImageView.image = image
  584. }
  585. func supportFontSize() -> [String] {
  586. return ["8pt", "9pt", "10pt", "11pt", "12pt", "14pt", "16pt", "18pt", "20pt", "22pt", "24pt", "26pt", "28pt", "36pt", "48pt", "72pt"]
  587. }
  588. }