KMEditPDFTextPropertyViewController.swift 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. //
  2. // KMEditPDFTextPropertyViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/12/27.
  6. //
  7. import Cocoa
  8. class KMSliderItemView: NSView {
  9. var titleLabel: NSTextField {
  10. get {
  11. return self.titleLabel_
  12. }
  13. }
  14. private lazy var titleLabel_: NSTextField = {
  15. let view = NSTextField(labelWithString: "")
  16. return view
  17. }()
  18. var slider: NSSlider {
  19. get {
  20. return self.slider_
  21. }
  22. }
  23. private lazy var slider_: NSSlider = {
  24. let view = NSSlider()
  25. view.minValue = 0
  26. view.maxValue = 1
  27. view.target = self
  28. view.action = #selector(sliderValueDidChange)
  29. return view
  30. }()
  31. var box: NSBox {
  32. get {
  33. return self.box_
  34. }
  35. }
  36. private lazy var box_: NSBox = {
  37. let view = NSBox()
  38. view.boxType = .custom
  39. view.titlePosition = .noTitle
  40. view.contentViewMargins = .zero
  41. view.borderWidth = 0
  42. return view
  43. }()
  44. var minValue: Double = 0 {
  45. didSet {
  46. self.slider.minValue = self.minValue
  47. }
  48. }
  49. var maxValue: Double = 1 {
  50. didSet {
  51. self.slider.maxValue = self.maxValue
  52. }
  53. }
  54. var doubleValue: Double = 1 {
  55. didSet {
  56. self.slider.doubleValue = self.doubleValue
  57. }
  58. }
  59. var valueChange: ((Double)->Void)?
  60. convenience init() {
  61. self.init(frame: .zero)
  62. self.initSubViews()
  63. }
  64. func initSubViews() {
  65. self.addSubview(self.titleLabel_)
  66. self.addSubview(self.slider_)
  67. self.addSubview(self.box_)
  68. self.titleLabel_.km_add_top_constraint(constant: 0)
  69. self.titleLabel_.km_add_left_constraint(constant: 4)
  70. self.slider_.km_add_left_constraint(constant: 4)
  71. self.slider_.km_add_top_constraint(equalTo: self.titleLabel_, attribute: .bottom, constant: 4)
  72. self.slider_.km_add_height_constraint(constant: 26)
  73. self.slider_.km_add_right_constraint(equalTo: self.box_, attribute: .left, constant: -8)
  74. self.box_.km_add_right_constraint(constant: 0)
  75. self.box_.km_add_bottom_constraint(constant: -4)
  76. }
  77. @objc func sliderValueDidChange(_ sender: NSSlider) {
  78. self.valueChange?(sender.doubleValue)
  79. }
  80. }
  81. enum CPDFActiveAreasAlignType : Int {
  82. case Left
  83. case Vertical
  84. case Right
  85. case Top
  86. case Horizontally
  87. case Bottom
  88. case DisHorizontally
  89. case DisVertical
  90. }
  91. class KMEditPDFTextPropertyViewController: NSViewController {
  92. @IBOutlet weak var contentView: NSClipView!
  93. @IBOutlet weak var backgroundView: NSView!
  94. @IBOutlet weak var headerBox: NSBox!
  95. @IBOutlet weak var imageContentView: NSView!
  96. @IBOutlet weak var imageViewHeightConstraint: NSLayoutConstraint!
  97. @IBOutlet weak var propertyTitle: NSTextField!
  98. @IBOutlet weak var preImageView: NSImageView!
  99. @IBOutlet weak var textPresuppositionContentView: NSView!
  100. @IBOutlet weak var textPresuppositionBox: NSBox!
  101. @IBOutlet weak var resetTextPresuppositionBox: NSBox!
  102. @IBOutlet weak var textPresuppositionTopContstraint: NSLayoutConstraint!
  103. @IBOutlet weak var textPresuppositionHeightContstraint: NSLayoutConstraint!
  104. @IBOutlet weak var fontContentView: NSView!
  105. @IBOutlet weak var fontTitleLabel: NSTextField!
  106. @IBOutlet weak var fontImageView: NSButton!
  107. @IBOutlet weak var fontContentViewHeightConstraint: NSLayoutConstraint!
  108. @IBOutlet weak var fontContentViewTopConstraint: NSLayoutConstraint!
  109. @IBOutlet weak var fontNameBox: NSBox!
  110. @IBOutlet weak var fontStyleBox: NSBox!
  111. @IBOutlet weak var fontSizeBox: NSBox!
  112. @IBOutlet weak var leftAlignmentBox: NSBox!
  113. @IBOutlet weak var centerAlignmentBox: NSBox!
  114. @IBOutlet weak var rightAlignmentBox: NSBox!
  115. @IBOutlet weak var colorBox: NSBox!
  116. @IBOutlet weak var fontColorButton: NSButton!
  117. @IBOutlet weak var fontCustomColorButton: NSButton!
  118. @IBOutlet weak var alignmentTopConstraint: NSLayoutConstraint!
  119. @IBOutlet weak var alignmentView: KMEditPropertyAlignmentView!
  120. var textPresuppositionVC: KMDesignSelect?
  121. var textPresuppositionResetVC: KMDesignPropertySelector?
  122. var textPresuppositionResetString: String = ""
  123. var leftAlignmentVC: KMDesignPropertySelector?
  124. var centerAlignmentVC: KMDesignPropertySelector?
  125. var rightAlignmentVC: KMDesignPropertySelector?
  126. var fontNameVC: KMDesignSelect?
  127. var fontSizeVC: KMDesignSelect?
  128. var fontStyleVC: KMDesignSelect?
  129. var borderWidthVC: KMDesignSelect?
  130. @IBOutlet weak var fontBoldBox: NSBox!
  131. @IBOutlet weak var fontItalicBox: NSBox!
  132. var fontBoldVC: KMDesignButton?
  133. var fontItalicVC: KMDesignButton?
  134. @IBOutlet weak var fontAddBox: NSBox!
  135. @IBOutlet weak var fontReduceBox: NSBox!
  136. var fontAddVC: KMDesignButton?
  137. var fontReduceVC: KMDesignButton?
  138. @IBOutlet weak var opacityBox: NSBox!
  139. var opacitVC: KMDesignSelect?
  140. var sliderItemView: KMSliderItemView?
  141. var textsAreas : [CPDFEditTextArea] = []
  142. var listView : CPDFListView!
  143. var fonts: [NSDictionary] = []
  144. var currentColor : NSColor = NSColor.black
  145. var alignment : NSTextAlignment = .left
  146. var fontName: String = "Helvetica" {
  147. didSet {
  148. if self.fontNameVC != nil {
  149. if self.fontNameVC!.items.contains(fontName) {
  150. let areas = self.handdler?.editingTextAreas ?? []
  151. if areas.count == 1 {
  152. let cfont = self.listView.editingSelectionCFont(byRangeEdit: areas.first)
  153. self.fontNameVC?.stringValue = cfont == nil ? "--" : fontName
  154. } else {
  155. self.fontNameVC?.stringValue = fontName
  156. }
  157. } else {
  158. // self.fontNameVC?.stringValue = "Helvetica"
  159. let areas = self.handdler?.editingTextAreas ?? []
  160. if areas.count == 1 {
  161. let cfont = self.listView.editingSelectionCFont(byRangeEdit: areas.first)
  162. self.fontNameVC?.stringValue = cfont == nil ? "--" : fontName
  163. } else {
  164. self.fontNameVC?.stringValue = fontName
  165. }
  166. }
  167. }
  168. }
  169. }
  170. var fontStyle: String = "Regular" {
  171. didSet {
  172. self.fontStyleVC?.stringValue = fontStyle
  173. }
  174. }
  175. var fontSize: CGFloat = 12 {
  176. didSet {
  177. if self.fontSize == -1 {
  178. self.fontSizeVC?.stringValue = "--"
  179. self.fontAddVC?.state = .Disabled
  180. self.fontReduceVC?.state = .Disabled
  181. } else {
  182. self.fontSizeVC?.stringValue = Int(ceil(fontSize)).description + "pt"
  183. self.fontAddVC?.state = .Norm
  184. self.fontReduceVC?.state = .Norm
  185. }
  186. }
  187. }
  188. var selectAreas: [Any] {
  189. get {
  190. return self.listView.editingAreas() ?? []
  191. }
  192. }
  193. weak var handdler: KMEditPDfHanddler?
  194. deinit {
  195. KMPrint("KMEditPDFTextPropertyViewController 已释放.")
  196. NSColorPanel.shared.setTarget(nil)
  197. NSColorPanel.shared.setAction(nil)
  198. NSColorPanel.shared.close()
  199. self.removeNotification()
  200. }
  201. func addNotification() {
  202. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  203. }
  204. func removeNotification() {
  205. NotificationCenter.default.removeObserver(self)
  206. DistributedNotificationCenter.default().removeObserver(self)
  207. }
  208. @objc func changeEffectiveAppearance() {
  209. print("changeEffectiveAppearance")
  210. let isDarkModel = NSView.isDarkModel()
  211. if isDarkModel {
  212. self.view.appearance = NSAppearance(named: .darkAqua)
  213. } else {
  214. self.view.appearance = NSAppearance(named: .aqua)
  215. }
  216. self.updateUI()
  217. }
  218. // func updateUI() {
  219. // let isDarkModel = NSView.isDarkModel()
  220. // if isDarkModel {
  221. // self.backgroundView.backgroundColor(NSColor.km_init(hex: "#252526"))
  222. // } else {
  223. // self.backgroundView.backgroundColor(NSColor.km_init(hex: "#FAFAFA"))
  224. // }
  225. // }
  226. override func viewDidLoad() {
  227. super.viewDidLoad()
  228. self.addNotification()
  229. self.setup()
  230. self.initData()
  231. // self.reloadData()
  232. self.updateLanguage()
  233. // self.updatePreviewImage()
  234. self.updateUI()
  235. self.headerBox.isHidden = true
  236. self.imageViewHeightConstraint.constant = 0
  237. self.textPresuppositionTopContstraint.constant = 50
  238. self.alignmentTopConstraint.constant = 16+50
  239. }
  240. func setup() {
  241. // self.backgroundView.backgroundColor(KMAppearance.Layout.bgDrakColor())
  242. self.propertyTitle.font = NSFont.SFProTextSemiboldFont(14.0)
  243. self.propertyTitle.textColor = KMAppearance.Layout.h0Color()
  244. self.fontTitleLabel.font = NSFont.SFProTextRegularFont(12.0)
  245. self.fontTitleLabel.textColor = KMAppearance.Layout.h0Color()
  246. self.headerBox.borderWidth = 1
  247. self.headerBox.borderColor = KMAppearance.Interactive.s0Color()
  248. self.headerBox.cornerRadius = 4
  249. self.headerBox.fillColor = KMAppearance.Layout.l1Color()
  250. //
  251. self.fontColorButton.border(NSColor.clear, 0, 10)
  252. self.fontColorButton.target = self
  253. self.fontColorButton.action = #selector(fontColorAction)
  254. //alignment
  255. leftAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  256. leftAlignmentBox.contentView = leftAlignmentVC?.view
  257. leftAlignmentBox.fillColor = NSColor.clear
  258. leftAlignmentVC?.target = self
  259. leftAlignmentVC?.action = #selector(leftAlignmentAction)
  260. leftAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignLeft")!
  261. leftAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")!
  262. leftAlignmentVC?.background_hov = KMAppearance.Layout.l1Color()
  263. leftAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color()
  264. leftAlignmentVC?.updateUI()
  265. centerAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  266. centerAlignmentBox.contentView = centerAlignmentVC?.view
  267. centerAlignmentBox.fillColor = NSColor.clear
  268. centerAlignmentVC?.target = self
  269. centerAlignmentVC?.action = #selector(centerAlignmentAction)
  270. centerAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignCenter")!
  271. centerAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")!
  272. centerAlignmentVC?.background_hov = KMAppearance.Layout.l1Color()
  273. centerAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color()
  274. centerAlignmentVC?.updateUI()
  275. rightAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  276. rightAlignmentBox.contentView = rightAlignmentVC?.view
  277. rightAlignmentBox.fillColor = NSColor.clear
  278. rightAlignmentVC?.target = self
  279. rightAlignmentVC?.action = #selector(rightAlignmentAction)
  280. rightAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignRight")!
  281. rightAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignRightSelect")!
  282. rightAlignmentVC?.background_hov = KMAppearance.Layout.l1Color()
  283. rightAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color()
  284. rightAlignmentVC?.updateUI()
  285. let fontNameArray = KMEditPDFTextManager.manager.fetchTextFontNames()
  286. let familyNames = CPDFFont.familyNames
  287. fontNameVC = KMDesignSelect.init(withType: .PopButton)
  288. // fontNameVC?.isScrollPop = true
  289. fontNameBox.contentView = fontNameVC?.view
  290. fontNameBox.fillColor = NSColor.clear
  291. fontNameVC?.addItems(withObjectValues: familyNames)
  292. fontNameVC?.selectItem(at: 0)
  293. fontNameVC?.delete = self
  294. fontNameVC?.borderColor = KMAppearance.Interactive.s0Color()
  295. fontNameVC?.background = KMAppearance.Layout.l1Color()
  296. fontNameVC?.background_hov = KMAppearance.Layout.l1Color()
  297. fontNameVC?.background_focus = KMAppearance.Layout.l1Color()
  298. fontNameVC?.textColor = KMAppearance.Layout.h1Color()
  299. fontNameVC?.textColor_hov = KMAppearance.Layout.h1Color()
  300. fontNameVC?.textColor_focus = KMAppearance.Layout.h1Color()
  301. fontNameVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  302. fontNameVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  303. fontNameVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  304. fontNameVC?.updateUI()
  305. self.fontNameVC?.isScrollPop = true
  306. self.fontNameVC?.showVerticalScroller = true
  307. let styleArray = defaultFontStyles //KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: fontName)
  308. let fontStyleNames = CPDFFont.fontNames(forFamilyName: "Helvetica")
  309. fontStyleVC = KMDesignSelect.init(withType: .PopButton)
  310. fontStyleBox.contentView = fontStyleVC?.view
  311. fontStyleBox.fillColor = NSColor.clear
  312. fontStyleVC?.addItems(withObjectValues: fontStyleNames)
  313. fontStyleVC?.selectItem(at: 0)
  314. fontStyleVC?.delete = self
  315. fontStyleVC?.borderColor = KMAppearance.Interactive.s0Color()
  316. fontStyleVC?.background = KMAppearance.Layout.l1Color()
  317. fontStyleVC?.background_hov = KMAppearance.Layout.l1Color()
  318. fontStyleVC?.background_focus = KMAppearance.Layout.l1Color()
  319. fontStyleVC?.textColor = KMAppearance.Layout.h1Color()
  320. fontStyleVC?.textColor_hov = KMAppearance.Layout.h1Color()
  321. fontStyleVC?.textColor_focus = KMAppearance.Layout.h1Color()
  322. fontStyleVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  323. fontStyleVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  324. fontStyleVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  325. fontStyleVC?.updateUI()
  326. self.fontBoldVC = KMDesignButton(withType: .Image)
  327. self.fontBoldBox.contentView = self.fontBoldVC?.view
  328. self.fontBoldBox.borderWidth = 0
  329. self.fontBoldVC?.target = self
  330. self.fontBoldVC?.action = #selector(fontBoldAction)
  331. self.fontBoldVC?.image = NSImage(named: "KMImageNameEditPDFFontBold")!
  332. self.fontBoldVC?.pagination()
  333. self.fontBoldVC?.initDefaultValue()
  334. self.fontItalicVC = KMDesignButton(withType: .Image)
  335. self.fontItalicBox.contentView = self.fontItalicVC?.view
  336. self.fontItalicBox.borderWidth = 0
  337. self.fontItalicVC?.target = self
  338. self.fontItalicVC?.action = #selector(fontItalicAction)
  339. self.fontItalicVC?.image = NSImage(named: "KMImageNameEditPDFFontItalic")!
  340. self.fontItalicVC?.pagination()
  341. self.fontItalicVC?.initDefaultValue()
  342. fontSizeVC = KMDesignSelect.init(withType: .Combox)
  343. fontSizeBox.contentView = fontSizeVC?.view
  344. fontSizeBox.fillColor = NSColor.clear
  345. fontSizeVC?.removeAllItems()
  346. fontSizeVC?.addItems(withObjectValues: self.supportFontSize())
  347. fontSizeVC?.selectItem(at: 0)
  348. fontSizeVC?.delete = self
  349. fontSizeVC?.borderColor = KMAppearance.Interactive.s0Color()
  350. fontSizeVC?.background = KMAppearance.Layout.l1Color()
  351. fontSizeVC?.background_hov = KMAppearance.Layout.l1Color()
  352. fontSizeVC?.background_focus = KMAppearance.Layout.l1Color()
  353. fontSizeVC?.textColor = KMAppearance.Layout.h1Color()
  354. fontSizeVC?.textColor_hov = KMAppearance.Layout.h1Color()
  355. fontSizeVC?.textColor_focus = KMAppearance.Layout.h1Color()
  356. fontSizeVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  357. fontSizeVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  358. fontSizeVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  359. fontSizeVC?.updateUI()
  360. self.fontSizeVC?.editable = true
  361. self.fontAddVC = KMDesignButton(withType: .Image)
  362. self.fontAddBox.contentView = self.fontAddVC?.view
  363. self.fontAddBox.borderWidth = 0
  364. self.fontAddVC?.target = self
  365. self.fontAddVC?.action = #selector(fontAddAction)
  366. self.fontAddVC?.image = NSImage(named: "KMImageNameEditPDFFontAdd")!
  367. self.fontAddVC?.pagination()
  368. self.fontAddVC?.initDefaultValue()
  369. self.fontReduceVC = KMDesignButton(withType: .Image)
  370. self.fontReduceBox.contentView = self.fontReduceVC?.view
  371. self.fontReduceBox.borderWidth = 0
  372. self.fontReduceVC?.target = self
  373. self.fontReduceVC?.action = #selector(fontReduceAction)
  374. self.fontReduceVC?.image = NSImage(named: "KMImageNameEditPDFFontReduce")!
  375. self.fontReduceVC?.pagination()
  376. self.fontReduceVC?.initDefaultValue()
  377. //textPresupposition
  378. textPresuppositionVC = KMDesignSelect.init(withType: .PopButton)
  379. textPresuppositionBox.contentView = textPresuppositionVC?.view
  380. textPresuppositionBox.fillColor = NSColor.clear
  381. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  382. textPresuppositionVC?.selectItem(at: 0)
  383. textPresuppositionVC?.delete = self
  384. textPresuppositionVC?.borderColor = KMAppearance.Interactive.s0Color()
  385. textPresuppositionVC?.background = KMAppearance.Layout.l1Color()
  386. textPresuppositionVC?.background_hov = KMAppearance.Layout.l1Color()
  387. textPresuppositionVC?.background_focus = KMAppearance.Layout.l1Color()
  388. textPresuppositionVC?.textColor = KMAppearance.Layout.h1Color()
  389. textPresuppositionVC?.textColor_hov = KMAppearance.Layout.h1Color()
  390. textPresuppositionVC?.textColor_focus = KMAppearance.Layout.h1Color()
  391. textPresuppositionVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  392. textPresuppositionVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  393. textPresuppositionVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  394. textPresuppositionVC?.updateUI()
  395. textPresuppositionResetVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  396. resetTextPresuppositionBox.contentView = textPresuppositionResetVC?.view
  397. resetTextPresuppositionBox.fillColor = NSColor.clear
  398. textPresuppositionResetVC?.borderWidth = 1
  399. textPresuppositionResetVC?.borderWidth_hov = 1
  400. textPresuppositionResetVC?.borderWidth_sel = 1
  401. textPresuppositionResetVC?.target = self
  402. textPresuppositionResetVC?.action = #selector(resetTextPresuppositionButtonAction)
  403. textPresuppositionResetVC?.image = NSImage(named: "KMImagePropertPanelTextDefaultMore")!
  404. textPresuppositionResetVC?._image_disabled = NSImage(named: "KMImagePropertPanelTextDefaultMore")!
  405. textPresuppositionResetVC?.background_hov = KMAppearance.Layout.l1Color()
  406. textPresuppositionResetVC?.background_disabled = KMAppearance.view_bg_dis_color()
  407. textPresuppositionResetVC?.cornerRadius = 4
  408. textPresuppositionResetVC?.cornerRadius_hov = 4
  409. textPresuppositionResetVC?.cornerRadius_disabled = 4
  410. textPresuppositionResetVC?.borderWidth_disabled = 1
  411. textPresuppositionResetVC?.borderColor_disabled = KMAppearance.view_border_dis_color()
  412. textPresuppositionResetVC?.updateUI()
  413. self.colorBox.borderColor = KMAppearance.Interactive.s0Color()
  414. let sliderItemView = KMSliderItemView()
  415. self.sliderItemView = sliderItemView
  416. self.opacityBox.contentView = sliderItemView
  417. self.opacityBox.borderWidth = 0
  418. sliderItemView.titleLabel.font = NSFont.SFProTextRegularFont(12)
  419. sliderItemView.titleLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  420. sliderItemView.minValue = 0.0001
  421. sliderItemView.maxValue = 1
  422. sliderItemView.valueChange = { [weak self] value in
  423. self?.opacitVC?.stringValue = String(format: "%0.f%%", value*100)
  424. if let data = self?.opacitVC {
  425. self?.km_comboBoxSelectionDidChange(data)
  426. }
  427. }
  428. opacitVC = KMDesignSelect.init(withType: .PopButton)
  429. sliderItemView.box.contentView = opacitVC?.view
  430. opacitVC?.addItems(withObjectValues: ["25%", "50%", "75%", "100%"])
  431. opacitVC?.selectItem(at: 0)
  432. opacitVC?.delete = self
  433. opacitVC?.borderColor = KMAppearance.Interactive.s0Color()
  434. opacitVC?.background = KMAppearance.Layout.l1Color()
  435. opacitVC?.background_hov = KMAppearance.Layout.l1Color()
  436. opacitVC?.background_focus = KMAppearance.Layout.l1Color()
  437. opacitVC?.textColor = KMAppearance.Layout.h1Color()
  438. opacitVC?.textColor_hov = KMAppearance.Layout.h1Color()
  439. opacitVC?.textColor_focus = KMAppearance.Layout.h1Color()
  440. opacitVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  441. opacitVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  442. opacitVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  443. opacitVC?.updateUI()
  444. self.alignmentView.didChange = { [unowned self] view, areasArray, boundsArray in
  445. self.changeAreasAlign(areas: areasArray, newBounds: boundsArray)
  446. self._trackEvent()
  447. let textAreas = self.handdler?.editingTextAreas ?? []
  448. let imageAreas = self.handdler?.editingImageAreas ?? []
  449. if textAreas.count > 0 && imageAreas.count > 0 {
  450. self._trackAlignEvent()
  451. }
  452. }
  453. self.changeEffectiveAppearance()
  454. }
  455. func initData() {
  456. self.updateTextPresupposition(type: .commonly, needChangeListView: false)
  457. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  458. self.currentColor = model.color
  459. DispatchQueue.main.async {
  460. NSColorPanel.shared.color = model.color
  461. }
  462. self.fontColorButton.image = self.swatchWithColor(color: self.currentColor, size: NSSize(width: 20, height: 20))
  463. self.fonts = CPDFAnnotationModel.supportFonts() as! [NSDictionary]
  464. }
  465. func updateLanguage() {
  466. let areas = self.selectAreas
  467. if textsAreas.count > 0 && textsAreas.count != areas.count { //多选图片跟文字
  468. self.propertyTitle.stringValue = NSLocalizedString("General Properties", comment: "")
  469. } else {
  470. self.propertyTitle.stringValue = NSLocalizedString("Text", comment: "")
  471. }
  472. self.fontTitleLabel.stringValue = NSLocalizedString("Font", comment: "")
  473. }
  474. // func addNotification() {
  475. // NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  476. // }
  477. //
  478. // func removeNotification() {
  479. // NotificationCenter.default.removeObserver(self)
  480. // DistributedNotificationCenter.default().removeObserver(self)
  481. // }
  482. // @objc func changeEffectiveAppearance() {
  483. // let isDarkModel = KMAdvertisementConfig.isDarkModel()
  484. // if isDarkModel {
  485. // self.view.appearance = NSAppearance(named: .darkAqua)
  486. // } else {
  487. // self.view.appearance = NSAppearance(named: .aqua)
  488. // }
  489. // self.updateUI()
  490. // }
  491. func updateUI() {
  492. let isDarkModel = KMAppearance.isDarkMode()
  493. if isDarkModel {
  494. self.backgroundView.backgroundColor(NSColor.km_init(hex: "#252526"))
  495. self.fontTitleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  496. self.sliderItemView?.titleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  497. self.alignmentView.alignmentLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  498. self.textPresuppositionResetVC?.background_disabled = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  499. self.textPresuppositionResetVC?.borderColor = KMAppearance.Layout.w15Color()
  500. self.textPresuppositionResetVC?.borderColor_hov = KMAppearance.Interactive.a0Color()
  501. self.textPresuppositionResetVC?.borderColor_sel = KMAppearance.Layout.w15Color()
  502. self.textPresuppositionResetVC?.background = KMAppearance.Layout.l1Color()
  503. self.textPresuppositionResetVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  504. self.fontBoldVC?.background_act = KMAppearance.Interactive.m1Color()
  505. self.fontBoldVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  506. self.fontBoldVC?.borderColor_hov = KMAppearance.Interactive.a0Color()
  507. self.fontBoldVC?.borderWidth_act = 0
  508. self.fontItalicVC?.background_act = KMAppearance.Interactive.m1Color()
  509. self.fontItalicVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  510. self.fontItalicVC?.borderColor_hov = KMAppearance.Interactive.a0Color()
  511. self.fontItalicVC?.borderWidth_act = 0
  512. self.fontAddVC?.ignoreDisabledImage = false
  513. self.fontAddVC?.initDefaultValue()
  514. self.fontAddVC?.background_act = KMAppearance.Interactive.m1Color()
  515. self.fontAddVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  516. self.fontAddVC?.borderColor_hov = KMAppearance.Interactive.a0Color()
  517. self.fontAddVC?.image_disabled = NSImage(named: "KMImageNameEditPDFFontAddDisabled")!
  518. self.fontAddVC?.background_disabled = NSColor(white: 96/255.0, alpha: 1.0)
  519. self.fontReduceVC?.ignoreDisabledImage = false
  520. self.fontReduceVC?.initDefaultValue()
  521. self.fontReduceVC?.background_act = KMAppearance.Interactive.m1Color()
  522. self.fontReduceVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  523. self.fontReduceVC?.borderColor_hov = KMAppearance.Interactive.a0Color()
  524. // self.fontAddVC?.background_act = KMAppearance.Interactive.m1Color()
  525. // self.fontAddVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  526. // self.fontAddVC?.borderColor_hov = KMAppearance.Interactive.a0Color()
  527. self.fontReduceVC?.image_disabled = NSImage(named: "KMImageNameEditPDFFontReduceDisabled")!
  528. self.fontReduceVC?.background_disabled = NSColor(white: 96/255.0, alpha: 1.0)
  529. self.leftAlignmentVC?.background_sel = KMAppearance.Interactive.m1Color()
  530. self.centerAlignmentVC?.background_sel = KMAppearance.Interactive.m1Color()
  531. self.rightAlignmentVC?.background_sel = KMAppearance.Interactive.m1Color()
  532. let color = NSColor.km_init(hex: "#0E1114")
  533. // fontNameVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  534. fontNameVC?.popViewControllerBackground = color
  535. } else {
  536. self.backgroundView.backgroundColor(NSColor.km_init(hex: "#FAFAFA"))
  537. self.propertyTitle.textColor = KMAppearance.Layout.h0Color()
  538. // self.fontTitleLabel.textColor = KMAppearance.Layout.h0Color()
  539. self.fontTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  540. self.sliderItemView?.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  541. self.alignmentView.alignmentLabel.textColor = NSColor.km_init(hex: "#616469")
  542. self.headerBox.borderColor = KMAppearance.Interactive.s0Color()
  543. self.headerBox.fillColor = KMAppearance.Layout.l1Color()
  544. leftAlignmentBox.fillColor = NSColor.clear
  545. leftAlignmentVC?.background_hov = KMAppearance.Layout.l1Color()
  546. leftAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color()
  547. centerAlignmentBox.fillColor = NSColor.clear
  548. centerAlignmentVC?.background_hov = KMAppearance.Layout.l1Color()
  549. centerAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color()
  550. rightAlignmentBox.fillColor = NSColor.clear
  551. rightAlignmentVC?.background_hov = KMAppearance.Layout.l1Color()
  552. rightAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color()
  553. fontNameVC?.borderColor = KMAppearance.Interactive.s0Color()
  554. fontNameVC?.background = KMAppearance.Layout.l1Color()
  555. fontNameVC?.background_hov = KMAppearance.Layout.l1Color()
  556. fontNameVC?.background_focus = KMAppearance.Layout.l1Color()
  557. fontNameVC?.textColor = KMAppearance.Layout.h1Color()
  558. fontNameVC?.textColor_hov = KMAppearance.Layout.h1Color()
  559. fontNameVC?.textColor_focus = KMAppearance.Layout.h1Color()
  560. fontNameVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  561. fontNameVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  562. fontNameVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  563. fontStyleVC?.borderColor = KMAppearance.Interactive.s0Color()
  564. fontStyleVC?.background = KMAppearance.Layout.l1Color()
  565. fontStyleVC?.background_hov = KMAppearance.Layout.l1Color()
  566. fontStyleVC?.background_focus = KMAppearance.Layout.l1Color()
  567. fontStyleVC?.textColor = KMAppearance.Layout.h1Color()
  568. fontStyleVC?.textColor_hov = KMAppearance.Layout.h1Color()
  569. fontStyleVC?.textColor_focus = KMAppearance.Layout.h1Color()
  570. fontStyleVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  571. fontStyleVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  572. fontStyleVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  573. fontSizeVC?.borderColor = KMAppearance.Interactive.s0Color()
  574. fontSizeVC?.background = KMAppearance.Layout.l1Color()
  575. fontSizeVC?.background_hov = KMAppearance.Layout.l1Color()
  576. fontSizeVC?.background_focus = KMAppearance.Layout.l1Color()
  577. fontSizeVC?.textColor = KMAppearance.Layout.h1Color()
  578. fontSizeVC?.textColor_hov = KMAppearance.Layout.h1Color()
  579. fontSizeVC?.textColor_focus = KMAppearance.Layout.h1Color()
  580. fontSizeVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  581. fontSizeVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  582. fontSizeVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  583. textPresuppositionBox.fillColor = NSColor.clear
  584. textPresuppositionVC?.borderColor = KMAppearance.Interactive.s0Color()
  585. textPresuppositionVC?.background = KMAppearance.Layout.l1Color()
  586. textPresuppositionVC?.background_hov = KMAppearance.Layout.l1Color()
  587. textPresuppositionVC?.background_focus = KMAppearance.Layout.l1Color()
  588. textPresuppositionVC?.textColor = KMAppearance.Layout.h1Color()
  589. textPresuppositionVC?.textColor_hov = KMAppearance.Layout.h1Color()
  590. textPresuppositionVC?.textColor_focus = KMAppearance.Layout.h1Color()
  591. textPresuppositionVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  592. textPresuppositionVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  593. textPresuppositionVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  594. textPresuppositionResetVC?.borderColor = NSColor(red: 223/255, green: 225/255, blue: 229/255, alpha: 1)
  595. textPresuppositionResetVC?.borderColor_hov = NSColor.clear
  596. textPresuppositionResetVC?.borderColor_sel = KMAppearance.Layout.w15Color()
  597. textPresuppositionResetVC?.borderColor_act = KMAppearance.Layout.w15Color()
  598. textPresuppositionResetVC?.borderColor_disabled = KMAppearance.Layout.w15Color()
  599. textPresuppositionResetVC?.background = NSColor.white
  600. textPresuppositionResetVC?.background_hov = KMAppearance.view_bg_dis_color()
  601. textPresuppositionResetVC?.background_disabled = KMAppearance.view_bg_dis_color()
  602. textPresuppositionResetVC?.borderColor_disabled = KMAppearance.view_border_dis_color()
  603. textPresuppositionResetVC?.borderWidth = 1
  604. opacitVC?.borderColor = KMAppearance.Interactive.s0Color()
  605. opacitVC?.background = KMAppearance.Layout.l1Color()
  606. opacitVC?.background_hov = KMAppearance.Layout.l1Color()
  607. opacitVC?.background_focus = KMAppearance.Layout.l1Color()
  608. opacitVC?.textColor = KMAppearance.Layout.h1Color()
  609. opacitVC?.textColor_hov = KMAppearance.Layout.h1Color()
  610. opacitVC?.textColor_focus = KMAppearance.Layout.h1Color()
  611. opacitVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  612. opacitVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  613. opacitVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  614. self.fontBoldVC?.initDefaultValue()
  615. self.fontItalicVC?.initDefaultValue()
  616. // self.fontAddVC?.pagination()
  617. self.fontAddVC?.ignoreDisabledImage = true
  618. self.fontAddVC?.initDefaultValue()
  619. self.fontAddVC?.image = NSImage(named: "KMImageNameEditPDFFontAdd")!
  620. // self.fontAddVC?.image_disabled = NSImage(named: "KMImageNameEditPDFFontAddDisabled")!
  621. // self.fontAddVC?.background_disabled = NSColor.red
  622. self.fontAddVC?.background_disabled = KMAppearance.buttonDisabledColor()
  623. self.fontReduceVC?.ignoreDisabledImage = true
  624. self.fontReduceVC?.initDefaultValue()
  625. self.fontReduceVC?.image = NSImage(named: "KMImageNameEditPDFFontReduce")!
  626. // self.fontAddVC?.image_disabled = NSImage(named: "KMImageNameEditPDFFontAddDisabled")!
  627. // self.fontAddVC?.background_disabled = NSColor.red
  628. self.fontReduceVC?.background_disabled = KMAppearance.buttonDisabledColor()
  629. }
  630. }
  631. func reloadData() {
  632. if self.selectAreas.count > 0 {
  633. textsAreas = []
  634. let areas = self.selectAreas
  635. self.alignmentView.editingAreas = areas
  636. for i in 0 ... areas.count-1 {
  637. if areas[i] is CPDFEditTextArea {
  638. textsAreas.append(areas[i] as! CPDFEditTextArea)
  639. }
  640. }
  641. if textsAreas.count == 1 && textsAreas.count == areas.count {
  642. self.headerBox.isHidden = true
  643. self.fontContentView.isHidden = false
  644. self.textPresuppositionContentView.isHidden = false
  645. // 88
  646. self.imageViewHeightConstraint.constant = 0
  647. // 152
  648. self.textPresuppositionTopContstraint.constant = 50
  649. self.fontContentViewHeightConstraint.constant = 193
  650. self.alignmentTopConstraint.constant = 16+50
  651. self.fontContentViewTopConstraint.constant = 8
  652. } else if textsAreas.count > 1 && textsAreas.count == areas.count {
  653. self.headerBox.isHidden = true
  654. self.fontContentView.isHidden = false
  655. self.textPresuppositionContentView.isHidden = false
  656. self.imageViewHeightConstraint.constant = 0
  657. self.textPresuppositionTopContstraint.constant = 50
  658. self.fontContentViewHeightConstraint.constant = 193
  659. self.alignmentTopConstraint.constant = 16+50
  660. self.fontContentViewTopConstraint.constant = 8
  661. } else if textsAreas.count > 0 && textsAreas.count != areas.count { //多选图片跟文字
  662. self.headerBox.isHidden = true
  663. self.fontContentView.isHidden = true
  664. self.textPresuppositionContentView.isHidden = true
  665. self.fontContentViewHeightConstraint.constant = 0
  666. self.textPresuppositionTopContstraint.constant = 16
  667. self.imageViewHeightConstraint.constant = 0
  668. self.alignmentTopConstraint.constant = 0
  669. self.fontContentViewTopConstraint.constant = 0
  670. }
  671. self.refreshSelectAreaProperty()
  672. self.updateLanguage()
  673. }
  674. }
  675. func refreshSelectAreaProperty(needDefaultData: Bool = false) {
  676. if self.selectAreas.count == 1 {
  677. let areas = self.selectAreas.first
  678. if areas is CPDFEditTextArea {
  679. let area = areas as! CPDFEditTextArea
  680. let cFont = self.listView.editingSelectionCFont(byRangeEdit: area)
  681. // let cFont = self.listView.editingSelectionCFont(with: area)
  682. var sizeString = "\(self.listView.editingSelectionFontSize(byRangeEdit: area))"
  683. var fontName: String = cFont?.familyName ?? "Helvetica"
  684. if fontName.count == 0 {
  685. fontName = "Helvetica"
  686. }
  687. var fontStyle: String = cFont?.styleName ?? "Regular"
  688. if fontStyle.count == 0 {
  689. fontStyle = "Regular"
  690. }
  691. let alignment = self.listView.currentSelectionAlignment(byRangeEdit: area)
  692. let color = self.listView.editingSelectionFontColor(byRangeEdit: area) ?? NSColor.black
  693. // fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? [])
  694. self.fontNameVC?.stringValue = cFont?.familyName == nil ? "--" : fontName
  695. // if let data = self.listView?.isBoldCurrentSelection(byRangeEdit: area), data {
  696. // fontName += "-Bold"
  697. // }
  698. // if let data = self.listView?.isItalicCurrentSelection(byRangeEdit: area), data {
  699. // fontName += "-Italic"
  700. // }
  701. //获取默认数据
  702. if needDefaultData {
  703. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  704. fontName = model.fontName
  705. fontStyle = model.fontStyle
  706. sizeString = model.fontSize.description
  707. }
  708. self.updateTextTextPresuppositionState()
  709. // var fontN = fontName
  710. // if let data = cFont {
  711. // fontN = CPDFFont.convertAppleFont(data) ?? fontName
  712. // }
  713. self.updateTextPresupposition(fontName: fontName, style: fontStyle, size: CGFloat(Float(sizeString)!), needChangeListView: false)
  714. self.currentColor = color
  715. // DispatchQueue.main.async {
  716. // NSColorPanel.shared.color = color
  717. // }
  718. self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20))
  719. self.alignment = alignment
  720. self.selectAlignment(alignment: alignment)
  721. // self.updatePreviewImage()
  722. let opacity = self.listView.opacity(for: areas as? CPDFEditArea)
  723. let sliderView = self.opacityBox.contentView as? KMSliderItemView
  724. sliderView?.doubleValue = Double(opacity)
  725. self.opacitVC?.stringValue = String(format: "%0.f%%", opacity*100)
  726. if let value = self.listView?.isBoldCurrentSelection(byRangeEdit: area) {
  727. self.fontBoldVC?.state = value ? .Act : .Norm
  728. }
  729. if let value = self.listView?.isItalicCurrentSelection(byRangeEdit: area) {
  730. self.fontItalicVC?.state = value ? .Act : .Norm
  731. }
  732. }
  733. } else if self.selectAreas.count > 1 {
  734. if let data = self.handdler?.editAreasFontNameIsEqual(), data {
  735. if let area = self.handdler?.editingTextAreas.first {
  736. let font = self.listView.editingSelectionCFont(byRangeEdit: area)
  737. // let font = self.listView.editingSelectionCFont(with: area)
  738. self.fontNameVC?.stringValue = font?.familyName ?? ""
  739. }
  740. } else {
  741. self.fontNameVC?.stringValue = "--"
  742. }
  743. if let data = self.handdler?.editAreasFontStyleIsEqual(), data {
  744. if let area = self.handdler?.editingTextAreas.first {
  745. // if let styleName = self.listView.editingSelectionCFont(with: area)?.styleName, styleName.isEmpty == false {
  746. if let styleName = self.listView.editingSelectionCFont(byRangeEdit: area)?.styleName, styleName.isEmpty == false {
  747. self.fontStyleVC?.stringValue = styleName
  748. } else {
  749. let styleNames = CPDFFont.fontNames(forFamilyName: self.listView.editingSelectionCFont(with: area)?.familyName ?? "")
  750. if let styleN = styleNames.first, styleN.isEmpty == false {
  751. self.fontStyleVC?.stringValue = styleN
  752. } else {
  753. self.fontStyleVC?.stringValue = "Regular"
  754. }
  755. }
  756. }
  757. } else {
  758. self.fontStyleVC?.stringValue = "--"
  759. }
  760. if let data = self.handdler?.editAreasFontBoldIsEqual(), data {
  761. if let area = self.handdler?.editingTextAreas.first {
  762. let value = self.listView.isBoldCurrentSelection(byRangeEdit: area)
  763. self.fontBoldVC?.state = value ? .Act : .Norm
  764. } else {
  765. self.fontBoldVC?.state = .Disabled
  766. }
  767. } else {
  768. self.fontBoldVC?.state = .Disabled
  769. }
  770. if let data = self.handdler?.editAreasFontItalicIsEqual(), data {
  771. if let area = self.handdler?.editingTextAreas.first {
  772. let value = self.listView.isItalicCurrentSelection(byRangeEdit: area)
  773. self.fontItalicVC?.state = value ? .Act : .Norm
  774. } else {
  775. self.fontItalicVC?.state = .Disabled
  776. }
  777. } else {
  778. self.fontItalicVC?.state = .Disabled
  779. }
  780. if let data = self.handdler?.editAreasFontSizeIsEqual(), data {
  781. if let area = self.handdler?.editingTextAreas.first {
  782. let data = self.listView.editingSelectionFontSize(with: area)
  783. self.fontSizeVC?.stringValue = String(format: "%.0fpt", data)
  784. }
  785. self.fontAddVC?.state = .Norm
  786. self.fontReduceVC?.state = .Norm
  787. self.fontAddVC?.button.isEnabled = true
  788. self.fontReduceVC?.button.isEnabled = true
  789. } else {
  790. self.fontSizeVC?.stringValue = "--"
  791. self.fontAddVC?.state = .Disabled
  792. self.fontReduceVC?.state = .Disabled
  793. self.fontAddVC?.button.isEnabled = false
  794. self.fontReduceVC?.button.isEnabled = false
  795. }
  796. if let data = self.handdler?.editAreasTextAlignmentIsEqual(), data {
  797. } else {
  798. self.leftAlignmentVC?.state = .Disabled
  799. self.centerAlignmentVC?.state = .Disabled
  800. self.rightAlignmentVC?.state = .Disabled
  801. }
  802. if let data = self.handdler?.editAreasFontColorIsEqual(), data {
  803. if let area = self.handdler?.editingTextAreas.first {
  804. let color = self.listView?.editingSelectionFontColor(byRangeEdit: area) ?? NSColor.black
  805. self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20))
  806. } else {
  807. self.fontColorButton.image = NSImage(named: "KMImageNameEditPDFColorDisabled")
  808. }
  809. } else {
  810. self.fontColorButton.image = NSImage(named: "KMImageNameEditPDFColorDisabled")
  811. }
  812. }
  813. }
  814. override func mouseDown(with event: NSEvent) {
  815. }
  816. //MARK: ToolAction
  817. private func swatchWithColor(color:NSColor,size:NSSize) -> NSImage {
  818. let image = NSImage(size: size)
  819. image.lockFocus()
  820. color.drawSwatch(in: NSRect(x: 0, y: 0, width: size.width, height: size.height))
  821. image.unlockFocus()
  822. return image
  823. }
  824. private func changeAreasAlign(areas:[Any],newBounds:[String]) {
  825. var oldBounds : [String] = []
  826. for i in 0 ... areas.count-1 {
  827. let area : CPDFEditArea = areas[i] as! CPDFEditArea
  828. oldBounds.append(NSStringFromRect(area.bounds))
  829. self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i]))
  830. }
  831. self.listView.setNeedsDisplayForVisiblePages()
  832. }
  833. }
  834. //MARK: - Action
  835. extension KMEditPDFTextPropertyViewController {
  836. @IBAction func fontColorAction(_ sender: Any) {
  837. self._trackEvent()
  838. let color = self.listView.editingSelectionFontColor(with: self.selectAreas.first as? CPDFEditTextArea)
  839. let panel = NSColorPanel.shared
  840. panel.setTarget(self)
  841. panel.setAction(#selector(fontColorChangeAction))
  842. panel.orderFront(nil)
  843. panel.showsAlpha = false
  844. if color != nil {
  845. if let cnt = self.handdler?.editingTextAreas.count, cnt <= 1 {
  846. panel.color = color ?? NSColor.black
  847. }
  848. }
  849. }
  850. @objc func fontColorChangeAction() {
  851. let color = NSColorPanel.shared.color
  852. self.currentColor = color
  853. self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20))
  854. let areas = self.handdler?.editingTextAreas ?? []
  855. if areas.count > 0 {
  856. for area in areas {
  857. self.listView.setEditingSelectionFontColor(color, with: area)
  858. }
  859. KMEditPDFPopToolBarWindow.shared.updateFontColor(fontColor: color)
  860. } else {
  861. KMEditPDFTextManager.manager.setCommonlyFontColor(color: self.currentColor)
  862. }
  863. // self.updatePreviewImage()
  864. }
  865. @objc func fontBoldAction(_ sender: AnyObject) {
  866. let state = self.fontBoldVC?.state ?? .None
  867. if state == .Disabled {
  868. return
  869. }
  870. var boldV = false
  871. if state == .Act {
  872. self.fontBoldVC?.state = .Norm
  873. boldV = false
  874. } else {
  875. self.fontBoldVC?.state = .Act
  876. boldV = true
  877. }
  878. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  879. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  880. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  881. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontStyle: model.fontStyle, fontSize: model.fontSize, type: type, bold: boldV, italic: nil)
  882. let model_com = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  883. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model_com.fontName, fontStyle: model_com.fontStyle, fontSize: model_com.fontSize, type: .commonly, bold: boldV, italic: nil)
  884. self.handdler?.fontBoldAction()
  885. self._trackEvent()
  886. }
  887. @objc func fontItalicAction(_ sender: AnyObject) {
  888. let state = self.fontItalicVC?.state ?? .None
  889. if state == .Disabled {
  890. return
  891. }
  892. var italicV = false
  893. if state == .Act {
  894. self.fontItalicVC?.state = .Norm
  895. italicV = false
  896. } else {
  897. self.fontItalicVC?.state = .Act
  898. italicV = true
  899. }
  900. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  901. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  902. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  903. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName,fontStyle: model.fontStyle, fontSize: model.fontSize, type: type, bold: nil, italic: italicV)
  904. let model_com = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  905. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model_com.fontName, fontStyle: model_com.fontStyle, fontSize: model_com.fontSize, type: .commonly, bold: nil, italic: italicV)
  906. self.handdler?.fontItalicAction()
  907. self._trackEvent()
  908. }
  909. @objc func fontAddAction(_ sender: AnyObject) {
  910. let state = self.fontAddVC?.state ?? .None
  911. if state == .Disabled {
  912. return
  913. }
  914. let size = Float(self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: "") ?? "12") ?? 1
  915. self.fontSizeVC?.stringValue = String(format: "%.0fpt", size+1)
  916. self.km_comboBoxSelectionDidChange(self.fontSizeVC!)
  917. self._trackEvent()
  918. }
  919. @objc func fontReduceAction(_ sender: AnyObject) {
  920. let state = self.fontReduceVC?.state ?? .None
  921. if state == .Disabled {
  922. return
  923. }
  924. let size = Float(self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: "") ?? "12") ?? 1
  925. if size < 1 {
  926. return
  927. }
  928. self.fontSizeVC?.stringValue = String(format: "%.0fpt", size-1)
  929. self.km_comboBoxSelectionDidChange(self.fontSizeVC!)
  930. self._trackEvent()
  931. }
  932. @objc func leftAlignmentAction(sender: NSButton?) {
  933. self.leftAlignmentVC?.state = .Sel
  934. self.rightAlignmentVC?.state = .Norm
  935. self.centerAlignmentVC?.state = .Norm
  936. self.updateAlignment(alignment: .left)
  937. if let _ = sender {
  938. KMEditPDFPopToolBarWindow.shared.updateTextAlign(align: .left)
  939. self._trackEvent()
  940. }
  941. }
  942. @objc func centerAlignmentAction(sender: NSButton?) {
  943. self.leftAlignmentVC?.state = .Norm
  944. self.rightAlignmentVC?.state = .Norm
  945. self.centerAlignmentVC?.state = .Sel
  946. self.updateAlignment(alignment: .center)
  947. if let _ = sender {
  948. KMEditPDFPopToolBarWindow.shared.updateTextAlign(align: .center)
  949. }
  950. self._trackEvent()
  951. }
  952. @objc func rightAlignmentAction(sender: NSButton?) {
  953. self.leftAlignmentVC?.state = .Norm
  954. self.rightAlignmentVC?.state = .Sel
  955. self.centerAlignmentVC?.state = .Norm
  956. self.updateAlignment(alignment: .right)
  957. if let _ = sender {
  958. KMEditPDFPopToolBarWindow.shared.updateTextAlign(align: .right)
  959. }
  960. self._trackEvent()
  961. }
  962. func selectAlignment(alignment: NSTextAlignment) {
  963. switch alignment {
  964. case .left:
  965. self.leftAlignmentAction(sender: nil)
  966. case .right:
  967. self.rightAlignmentAction(sender: nil)
  968. case .center:
  969. self.centerAlignmentAction(sender: nil)
  970. default:
  971. self.leftAlignmentAction(sender: nil)
  972. }
  973. }
  974. @objc func resetTextPresuppositionButtonAction(sender: NSButton) {
  975. let state = self.textPresuppositionResetVC?.state ?? .None
  976. if state == .Disabled {
  977. return
  978. }
  979. var popViewDataArr: [String] = []
  980. var dataArray = [NSLocalizedString("Redefine", comment: ""), NSLocalizedString("Reset", comment: "")]
  981. for string in dataArray {
  982. popViewDataArr.append(string)
  983. }
  984. //调整保存参数
  985. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  986. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  987. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  988. var disItems: [String] = dataArray
  989. if model.change || model.redefine {
  990. disItems.removeObject(NSLocalizedString("Reset", comment: ""))
  991. }
  992. if model.change {
  993. disItems.removeObject(NSLocalizedString("Redefine", comment: ""))
  994. }
  995. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr)
  996. vc.background = KMAppearance.Layout.bgColor()
  997. vc.textColor = KMAppearance.Layout.h0Color()
  998. vc.enterFillColor = KMAppearance.Interactive.s0Color()
  999. vc.disItems = disItems
  1000. let createFilePopover: NSPopover = NSPopover.init()
  1001. createFilePopover.contentViewController = vc
  1002. createFilePopover.animates = true
  1003. createFilePopover.behavior = .semitransient
  1004. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  1005. 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)
  1006. // vc.customBoxWidthLayoutConstraint.constant = self.popWidth ?? sender.frame.width
  1007. vc.downCallback = { [unowned self](downEntered: Bool, count: String) -> Void in
  1008. if downEntered {
  1009. if count == NSLocalizedString("Reset", comment: "") {
  1010. self.resetTextPresuppositionData()
  1011. } else if count == NSLocalizedString("Redefine", comment: "") {
  1012. self.reDefineTextPresuppositionData()
  1013. }
  1014. createFilePopover.close()
  1015. }
  1016. }
  1017. }
  1018. func updateTextTextPresuppositionState() {
  1019. let areas = self.selectAreas.first
  1020. if areas is CPDFEditTextArea {
  1021. var size: CGFloat = (abs(self.listView.editingSelectionFontSize(byRangeEdit: areas as? CPDFEditTextArea)))
  1022. let cfont = self.listView.editingSelectionCFont(byRangeEdit: areas as? CPDFEditTextArea)
  1023. // let cfont = self.listView.editingSelectionCFont(with: areas as? CPDFEditTextArea)
  1024. var fontName = cfont?.familyName ?? "Helvetica"
  1025. // fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? [])
  1026. var isBold = false
  1027. if let data = self.listView?.isBoldCurrentSelection(byRangeEdit: areas as? CPDFEditTextArea) {
  1028. // fontName += "-Bold"
  1029. isBold = data
  1030. }
  1031. var isItalic = false
  1032. if let data = self.listView?.isItalicCurrentSelection(byRangeEdit: areas as? CPDFEditTextArea) {
  1033. // fontName += "-Italic"
  1034. isItalic = data
  1035. }
  1036. // var fontN = fontName
  1037. // if let data = cfont {
  1038. // fontN = CPDFFont.convertAppleFont(data) ?? fontName
  1039. // }
  1040. let models = KMEditPDFTextManager.manager.fetchAllUserDefaultData()
  1041. var index = 0
  1042. // && model.bold == isBold && model.italic == isItalic
  1043. for i in 0...models.count - 1 {
  1044. let model = models[i]
  1045. if model.fontName == fontName && model.fontStyle == cfont?.styleName && size == model.fontSize {
  1046. index = i
  1047. }
  1048. }
  1049. //刷新样式
  1050. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  1051. textPresuppositionVC?.selectItem(at: index)
  1052. }
  1053. }
  1054. }
  1055. //MARK: - Update
  1056. extension KMEditPDFTextPropertyViewController {
  1057. func updateAlignment(alignment: NSTextAlignment) {
  1058. if self.alignment != alignment {
  1059. let areas = self.handdler?.editingTextAreas ?? []
  1060. if areas.count > 0 {
  1061. for area in areas {
  1062. self.listView.setCurrentSelectionAlignment(alignment, with: area)
  1063. }
  1064. } else {
  1065. KMEditPDFTextManager.manager.setFontAlignment(alignment: alignment)
  1066. }
  1067. self.alignment = alignment
  1068. // self.updatePreviewImage()
  1069. }
  1070. }
  1071. // 更新字体预设
  1072. func updateTextPresupposition(type: KMEditPDFTextFontType, needChangeListView: Bool = true) {
  1073. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  1074. let fontName: String = model.fontName
  1075. let size: CGFloat = model.fontSize
  1076. self.updateTextPresupposition(fontName: fontName, style: model.fontStyle, size: size, needChangeListView: needChangeListView)
  1077. //刷新样式
  1078. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  1079. textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem)
  1080. if type == .customize || type == .commonly {
  1081. self.textPresuppositionResetVC?.state = .Disabled
  1082. } else {
  1083. self.textPresuppositionResetVC?.state = .Norm
  1084. }
  1085. if needChangeListView {
  1086. let areas = self.handdler?.editingTextAreas ?? []
  1087. for area in areas {
  1088. self.listView.setCurrentSelectionIsBold(model.bold, with: area)
  1089. self.listView.setCurrentSelectionIsItalic(model.italic, with: area)
  1090. }
  1091. self._boldButtonStateUpdate(fontStyle: "")
  1092. }
  1093. }
  1094. }
  1095. extension KMEditPDFTextPropertyViewController: KMSelectPopButtonDelegate {
  1096. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  1097. if obj == textPresuppositionVC {
  1098. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[obj.indexOfSelectedItem])
  1099. self.updateTextPresupposition(type: type)
  1100. } else if obj == fontNameVC {
  1101. self.updateFontNameAndStyle(name: obj.stringValue, style: fontStyle)
  1102. // self.updatePreviewImage()
  1103. let win = KMEditPDFPopToolBarWindow.shared
  1104. if win.isVisible {
  1105. (win.contentViewController as? KMEditPDFPopToolBarController)?.toolbarView?.reloadData()
  1106. }
  1107. } else if obj == fontSizeVC {
  1108. let size = CGFloat(Float((obj.stringValue.replacingOccurrences(of: "pt", with: "")))!)
  1109. self.updateFontSize(size: size)
  1110. // self.updatePreviewImage()
  1111. KMEditPDFPopToolBarWindow.shared.updateFontSizeButtons(enable: true)
  1112. } else if obj == fontStyleVC {
  1113. self.updateFontNameAndStyle(name: fontName, style: obj.stringValue)
  1114. // self.updatePreviewImage()
  1115. } else if obj == self.opacitVC {
  1116. let stringValue = self.opacitVC?.stringValue ?? ""
  1117. let value = (stringValue.components(separatedBy: "%").first?.stringToCGFloat() ?? 0) / 100
  1118. let view = self.opacityBox.contentView as? KMSliderItemView
  1119. view?.doubleValue = Double(value)
  1120. let areas = self.listView.editingAreas() as? [CPDFEditArea] ?? []
  1121. for area in areas {
  1122. self.listView.setOpacityEditArea(area, opacity: value)
  1123. }
  1124. }
  1125. }
  1126. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  1127. if obj == self.fontSizeVC {
  1128. let format = NumberFormatter()
  1129. format.numberStyle = .decimal
  1130. var string = obj.stringValue.replacingOccurrences(of: "pt", with: "")
  1131. string = string.replacingOccurrences(of: "p", with: "")
  1132. let size = max(Float(string) ?? 1, 1)
  1133. self.fontSizeVC?.stringValue = format.string(from: NSNumber(value: size)) ?? ""
  1134. }
  1135. }
  1136. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  1137. if obj == self.fontSizeVC {
  1138. var string = obj.stringValue.replacingOccurrences(of: "pt", with: "")
  1139. string = string.replacingOccurrences(of: ",", with: "")
  1140. let size = Float(string) ?? 1
  1141. let sizeValue = max(size, 1)
  1142. self.fontSizeVC?.stringValue = String(format: "%.0fpt", sizeValue)
  1143. self.updateFontSize(size: size.cgFloat)
  1144. }
  1145. }
  1146. func km_cellViewWillShow(_ obj: KMDesignSelect, _ cellView: KMBox, _ index: Int) {
  1147. if obj.isEqual(to: self.textPresuppositionVC) == false {
  1148. return
  1149. }
  1150. if let label = cellView.contentView?.subviews.first as? NSTextField {
  1151. /* 14.0 "SFProText-Regular" */
  1152. let allValues = KMEditPDFTextFontType.allValues()
  1153. let idx = allValues.count-index-1
  1154. if idx == 0 { // custom
  1155. return
  1156. }
  1157. guard let value = allValues.safe_element(for: idx) as? String else {
  1158. return
  1159. }
  1160. let type = KMEditPDFTextFontType.typeOfRawValue(value: value)
  1161. var fontSize: CGFloat = 0
  1162. if type == .h1 {
  1163. fontSize = 20
  1164. } else if type == .h2 {
  1165. fontSize = 18
  1166. } else if type == .h3 {
  1167. fontSize = 16
  1168. } else if type == .b1 {
  1169. fontSize = 14
  1170. } else if type == .b2 {
  1171. fontSize = 12
  1172. } else if type == .b3 {
  1173. fontSize = 10
  1174. }
  1175. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  1176. let fontName: String = model.fontName
  1177. // let size: CGFloat = model.fontSize
  1178. var fontN: String = fontName
  1179. if let cfont = CPDFFont.mappingFont(withFontString: fontName) {
  1180. fontN = CPDFFont.convertAppleFont(cfont) ?? fontName
  1181. }
  1182. var bold = fontName.contains("Bold")
  1183. var italic = fontName.contains("Oblique") || fontName.contains("Italic")
  1184. if bold == false {
  1185. bold = model.bold
  1186. }
  1187. if italic == false {
  1188. italic = model.italic
  1189. }
  1190. // if let data = NSFont(name: fontN, size: fontSidze) {
  1191. // let attri = NSMutableAttributedString(attributedString: label.attributedStringValue)
  1192. // attri.addAttribute(.font, value: data, range: NSMakeRange(0, attri.string.count))
  1193. // label.attributedStringValue = attri
  1194. // } else {
  1195. let attri = NSMutableAttributedString(attributedString: label.attributedStringValue)
  1196. if bold {
  1197. attri.addAttribute(.font, value: NSFont.boldSystemFont(ofSize: fontSize), range: NSMakeRange(0, attri.string.count))
  1198. } else {
  1199. attri.addAttribute(.font, value: NSFont.systemFont(ofSize: fontSize), range: NSMakeRange(0, attri.string.count))
  1200. }
  1201. if italic {
  1202. attri.addAttribute(.obliqueness, value: 0.3, range: NSMakeRange(0, attri.string.count))
  1203. }
  1204. label.attributedStringValue = attri
  1205. // }
  1206. }
  1207. }
  1208. func km_SelectPopoverWillShow(_ obj: KMDesignSelect) {
  1209. if obj.isEqual(to: self.fontNameVC) {
  1210. self._trackEvent()
  1211. } else if obj.isEqual(to: self.fontStyleVC) {
  1212. self._trackEvent()
  1213. } else if obj.isEqual(to: self.fontSizeVC) {
  1214. self._trackEvent()
  1215. }
  1216. }
  1217. }
  1218. //MARK: - TextPresupposition
  1219. extension KMEditPDFTextPropertyViewController {
  1220. //MARK: 基本属性调整
  1221. func updateTextPresupposition(fontName: String, style: String, size: CGFloat, needChangeListView: Bool = true) {
  1222. // let fontNameArray = fontName.components(separatedBy: "-")
  1223. // var name = ""
  1224. // var style = ""
  1225. // if fontNameArray.count > 0 {
  1226. // name = fontNameArray.first!
  1227. // } else {
  1228. // name = "Helvetica"
  1229. // }
  1230. //
  1231. // if fontNameArray.count == 4 {
  1232. // style = "\(fontNameArray[2]) \(fontNameArray[3])"
  1233. // } else if fontNameArray.count == 3 {
  1234. // style = "\(fontNameArray[1]) \(fontNameArray[2])"
  1235. // } else if fontNameArray.count == 2 {
  1236. // style = fontNameArray.last!
  1237. // } else {
  1238. // style = "Regular"
  1239. // }
  1240. self.updateFontNameAndStyle(name: fontName, style: style, needChangeListView: needChangeListView, needSave: false)
  1241. self.updateFontSize(size: size, needChangeListView: needChangeListView, needSave: false)
  1242. // self.updatePreviewImage()
  1243. }
  1244. func resetTextPresuppositionData() {
  1245. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  1246. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  1247. KMEditPDFTextManager.manager.resetTextPresupposition(type: type)
  1248. self.updateTextPresupposition(type: type)
  1249. }
  1250. func reDefineTextPresuppositionData(redefine: Bool = true) {
  1251. let fontStyle = self.fontStyle
  1252. let fontName = self.fontName + "-" + fontStyle
  1253. let fontSize = self.fontSize
  1254. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  1255. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  1256. KMEditPDFTextManager.manager.reDefineTextPresupposition(fontName: self.fontName,fontStyle: self.fontStyle,
  1257. fontSize: fontSize,
  1258. type: type)
  1259. self.updateTextPresupposition(type: type)
  1260. }
  1261. }
  1262. //MARK: - Private
  1263. // 更新字体名称+字重
  1264. extension KMEditPDFTextPropertyViewController {
  1265. // 更新字体名称 和 字重
  1266. func updateFontNameAndStyle(name: String, style: String, needChangeListView: Bool = true, needSave: Bool = true) {
  1267. // if fontName != name || fontStyle != style {
  1268. // let styleArray = defaultFontStyles //KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: name)
  1269. // var styleString = KMEditPDFTextManager.manager.checkFontStyle(style: style)
  1270. var styleString = style
  1271. let fontStyleNames = CPDFFont.fontNames(forFamilyName: name)
  1272. if !fontStyleNames.contains(style) {
  1273. if fontStyleNames.isEmpty {
  1274. self.fontStyleVC?.addItems(withObjectValues: [style])
  1275. self.fontStyleVC?.selectItem(at: 0)
  1276. // styleString = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC!.stringValue)
  1277. styleString = style
  1278. } else {
  1279. self.fontStyleVC?.addItems(withObjectValues: fontStyleNames)
  1280. self.fontStyleVC?.selectItem(at: 0)
  1281. // styleString = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC!.stringValue)
  1282. styleString = fontStyleNames.first ?? ""
  1283. }
  1284. } else {
  1285. self.fontStyleVC?.addItems(withObjectValues: fontStyleNames)
  1286. self.fontStyleVC?.selectItem(at: fontStyleNames.firstIndex(of: style) ?? 0)
  1287. }
  1288. // var fontName = "\(name)-\(styleString)"
  1289. // if styleString.count == 0 {
  1290. // fontName = name
  1291. // }
  1292. //数据保存返回
  1293. if self.selectAreas.count > 0 {
  1294. if needChangeListView {
  1295. let areas = self.handdler?.editingTextAreas ?? []
  1296. for area in areas {
  1297. // self.listView.setEditingSelectionFontName(name, with: area)
  1298. let font = CPDFFont(familyName: name, fontStyle: styleString)
  1299. self.listView.setEditSelectionCFont(font, with: area)
  1300. // if styleString.contains("Bold") {
  1301. // self.listView.setCurrentSelectionIsBold(true, with: area)
  1302. // } else {
  1303. // self.listView.setCurrentSelectionIsBold(false, with: area)
  1304. // }
  1305. //
  1306. // if styleString.contains("Oblique") || styleString.contains("Italic") {
  1307. // self.listView.setCurrentSelectionIsItalic(true, with: area)
  1308. // } else {
  1309. // self.listView.setCurrentSelectionIsItalic(false, with: area)
  1310. // }
  1311. }
  1312. }
  1313. } else {
  1314. if needSave {
  1315. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1316. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: name, fontStyle: styleString, fontSize: model.fontSize, type: .commonly, bold: nil, italic: nil)
  1317. }
  1318. }
  1319. self.fontName = name
  1320. // self.fontStyle = self.fontStyleVC?.stringValue ?? "Regular"
  1321. self.fontStyle = styleString
  1322. if needSave {
  1323. //调整保存参数
  1324. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  1325. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  1326. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  1327. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: name, fontStyle: styleString, fontSize: model.fontSize, type: type, bold: nil, italic: nil)
  1328. }
  1329. //刷新样式
  1330. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  1331. textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem)
  1332. // }
  1333. self._boldButtonStateUpdate(fontStyle: styleString)
  1334. }
  1335. func updateFontSize(size: CGFloat, needChangeListView: Bool = true, needSave: Bool = true) {
  1336. // var fontSize = Float((self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: ""))!) ?? 8.0
  1337. if fontSize != size {
  1338. let areas = self.handdler?.editingTextAreas ?? []
  1339. if areas.count > 0 {
  1340. if needChangeListView {
  1341. for area in areas {
  1342. self.listView.setEditingSelectionFontSize(size, with: area, isAutoSize: false)
  1343. }
  1344. }
  1345. } else {
  1346. if needSave {
  1347. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1348. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontStyle: model.fontStyle,fontSize: size, type: .commonly, bold: nil, italic: nil)
  1349. }
  1350. }
  1351. self.fontSize = size
  1352. if needSave {
  1353. let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0
  1354. let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index])
  1355. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  1356. KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontStyle: model.fontStyle, fontSize: size, type: type, bold: nil, italic: nil)
  1357. }
  1358. //刷新样式
  1359. textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray())
  1360. textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem)
  1361. }
  1362. }
  1363. // Bold
  1364. private func _boldButtonStateUpdate(fontStyle fStyle: String) {
  1365. if self.selectAreas.count == 1 {
  1366. let areas = self.selectAreas.first
  1367. if areas is CPDFEditTextArea {
  1368. let area = areas as! CPDFEditTextArea
  1369. if let value = self.listView?.isBoldCurrentSelection(byRangeEdit: area) {
  1370. self.fontBoldVC?.state = value ? .Act : .Norm
  1371. }
  1372. if let value = self.listView?.isItalicCurrentSelection(byRangeEdit: area) {
  1373. self.fontItalicVC?.state = value ? .Act : .Norm
  1374. }
  1375. }
  1376. } else if self.selectAreas.count > 1 {
  1377. if let data = self.handdler?.editAreasFontBoldIsEqual(), data {
  1378. if let area = self.handdler?.editingTextAreas.first {
  1379. let value = self.listView.isBoldCurrentSelection(byRangeEdit: area)
  1380. self.fontBoldVC?.state = value ? .Act : .Norm
  1381. } else {
  1382. self.fontBoldVC?.state = .Disabled
  1383. }
  1384. } else {
  1385. self.fontBoldVC?.state = .Disabled
  1386. }
  1387. if let data = self.handdler?.editAreasFontItalicIsEqual(), data {
  1388. if let area = self.handdler?.editingTextAreas.first {
  1389. let value = self.listView.isItalicCurrentSelection(byRangeEdit: area)
  1390. self.fontItalicVC?.state = value ? .Act : .Norm
  1391. } else {
  1392. self.fontItalicVC?.state = .Disabled
  1393. }
  1394. } else {
  1395. self.fontItalicVC?.state = .Disabled
  1396. }
  1397. } else {
  1398. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1399. self.fontBoldVC?.state = model.bold ? .Act : .Norm
  1400. self.fontItalicVC?.state = model.italic ? .Act : .Norm
  1401. }
  1402. }
  1403. //MARK: 刷新预览图片
  1404. private func updatePreviewImage() {
  1405. var alignment = self.alignment
  1406. var fontSize = Float((self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: ""))!) ?? 1.0
  1407. var fontName = self.fontNameVC?.stringValue ?? "Sample"
  1408. let fontStyle = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC?.stringValue ?? "")
  1409. let fontCurrentColor = self.currentColor
  1410. fontName = KMEditPDFTextManager.manager.checkFontName(fontName: (fontName + "-" + fontStyle))
  1411. fontSize = max(fontSize, 8)
  1412. let editringareas = self.selectAreas
  1413. let count = editringareas.count
  1414. // let editingSelectionString = self.listView.editingSelectionString()
  1415. let editingSelectionAlignment = self.listView.editingSelectionAlignment(with: editringareas.first as? CPDFEditTextArea)
  1416. if count == 1 {
  1417. let areas = editringareas.first
  1418. if areas is CPDFEditTextArea {
  1419. // string = editingSelectionString ?? NSLocalizedString("Sample", comment: "")
  1420. alignment = editingSelectionAlignment
  1421. }
  1422. }
  1423. let image = KMEditPDFTextManager.manager.fetchTextImage(alignment: .center,
  1424. fontName: fontName,
  1425. fontSize: CGFloat(fontSize),
  1426. color: fontCurrentColor,
  1427. imageSize: self.preImageView.frame.size)
  1428. self.preImageView.image = image
  1429. }
  1430. func supportFontSize() -> [String] {
  1431. return ["8pt", "9pt", "10pt", "11pt", "12pt", "14pt", "16pt", "18pt", "20pt", "22pt", "24pt", "26pt", "28pt", "36pt", "48pt", "72pt"]
  1432. }
  1433. private func _trackEvent() {
  1434. // kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditText":"SubTbr_EditText_PropertiesPanel_All"])
  1435. }
  1436. private func _trackAlignEvent() {
  1437. // kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditText":"SubTbr_EditContent_AlignContent"])
  1438. }
  1439. }