KMEditPDFTextPropertyViewController.swift 36 KB

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