KMEditPDFTextPropertyViewController.swift 74 KB

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