KMEditPDFTextPropertyViewController.swift 29 KB

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