KMEditPDFTextPropertyViewController.swift 31 KB

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