KMEditPDFTextPropertyViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. //
  2. // KMEditPDFTextPropertyViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/12/27.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMEditPDFTextPropertyViewController: NSViewController {
  10. @IBOutlet weak var contentView: NSClipView!
  11. @IBOutlet weak var backgroundView: NSView!
  12. @IBOutlet var fontBGView: NSView!
  13. @IBOutlet var fontLabel: NSTextField!
  14. @IBOutlet var fontTitleSelect: ComponentSelect!
  15. @IBOutlet var fontTitleButton: ComponentButton!
  16. @IBOutlet var fontNameSelect: ComponentSelect!
  17. @IBOutlet var fontStyleSelect: ComponentSelect!
  18. @IBOutlet var fontSizeSelect: ComponentSelect!
  19. @IBOutlet var fontStyleGroup: ComponentCSelectorGroup!
  20. @IBOutlet var fontAlignmentGroup: ComponentCSelectorGroup!
  21. @IBOutlet var colorBGView: NSView!
  22. @IBOutlet var colorLabel: NSTextField!
  23. @IBOutlet var colorGroup: ComponentCColorGroup!
  24. @IBOutlet var colorSlider: ComponentSlider!
  25. @IBOutlet var colorOpacitySelect: ComponentSelect!
  26. @IBOutlet var alignmentBGView: NSView!
  27. private var fontTitleGroup: ComponentGroup?
  28. private var alignmentController: KMNAlignmentController?
  29. private var familyNames = CPDFFont.familyNames
  30. private let fontStyle_boldItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_bold"), identifier: "fontStyle_bold")
  31. private let fontStyle_ItalityItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Itality"), identifier: "fontStyle_Itality")
  32. private let fontStyle_CenterItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Center"), identifier: "fontStyle_Center")
  33. private let fontStyle_Underline: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Underline"), identifier: "fontStyle_Underline")
  34. private let fontAlign_leftItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_left"), identifier: "fontAlign_left")
  35. private let fontAlign_centerItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_center"), identifier: "fontAlign_center")
  36. private let fontAlign_rightItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_right"), identifier: "fontAlign_right")
  37. private let fontAlign_justifyItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_justify"), identifier: "fontAlign_justify")
  38. var pdfView: CPDFListView?
  39. private var currentArea: CPDFEditTextArea?
  40. //自定义
  41. private var customTextModel: KMEditPDFTextFontModel = KMEditPDFTextFontModel()
  42. private var selCustomFontType: KMEditPDFTextFontType = .b1
  43. //MARK: - func
  44. override func viewDidAppear() {
  45. super.viewDidAppear()
  46. colorSlider.reloadData()
  47. }
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. setupProperty()
  51. }
  52. func setupProperty() {
  53. if let scrollview = self.view as? NSScrollView {
  54. scrollview.backgroundColor = ComponentLibrary.shared.backgroundColor(forToken: "colorBg/layout-middle") ?? NSColor.clear
  55. }
  56. //Font
  57. fontLabel.stringValue = KMLocalizedString("Font")
  58. fontLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  59. fontLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  60. fontTitleSelect.properties = ComponentSelectProperties(size: .s,
  61. state: .normal,
  62. text: "")
  63. if true {
  64. var menuItemArr: [ComponentMenuitemProperty] = []
  65. var ids = [Customize_USERDEFAULTS, H1_USERDEFAULTS,
  66. H2_USERDEFAULTS, H3_USERDEFAULTS,
  67. B1_USERDEFAULTS, B2_USERDEFAULTS,
  68. B3_USERDEFAULTS]
  69. var strings = KMEditPDFTextFontTypeWrapper.allValues()
  70. for i in 0...strings.count - 1 {
  71. let string = strings[i]
  72. var id = string
  73. if i < ids.count {
  74. id = ids[i]
  75. }
  76. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: id)
  77. menuItemArr.append(item)
  78. }
  79. fontTitleSelect.updateMenuItemsArr(menuItemArr)
  80. }
  81. fontTitleSelect.delegate = self
  82. fontTitleButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "toolbar_more"), keepPressState: false)
  83. fontTitleButton.setTarget(self, action: #selector(buttonClicked(_:)))
  84. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  85. state: .normal,
  86. text: "")
  87. if true {
  88. var menuItemArr: [ComponentMenuitemProperty] = []
  89. for string in familyNames {
  90. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  91. menuItemArr.append(item)
  92. }
  93. fontNameSelect.updateMenuItemsArr(menuItemArr)
  94. }
  95. fontNameSelect.delegate = self
  96. fontStyleSelect.properties = ComponentSelectProperties(size: .s,
  97. state: .normal,
  98. text: "")
  99. fontStyleSelect.delegate = self
  100. fontSizeSelect.properties = ComponentSelectProperties(size: .s,
  101. state: .normal,
  102. creatable: true,
  103. text: "12 pt",
  104. textUnit: " pt",
  105. regexString: "0123456789")
  106. if true {
  107. var sizeItemArr: [ComponentMenuitemProperty] = []
  108. for string in KMHeaderFooterManager.getFontSize() {
  109. let item = ComponentMenuitemProperty(type: .normal, text: string + " pt", identifier: string)
  110. sizeItemArr.append(item)
  111. }
  112. fontSizeSelect.updateMenuItemsArr(sizeItemArr)
  113. }
  114. fontSizeSelect.delegate = self
  115. if true {
  116. let itemArr: [ComponentCSelectorProperty] = [fontStyle_boldItem, fontStyle_ItalityItem]
  117. fontStyleGroup.updateItemProperty(itemArr)
  118. fontStyleGroup.delegate = self
  119. }
  120. if true {
  121. let itemArr: [ComponentCSelectorProperty] = [fontAlign_leftItem, fontAlign_centerItem, fontAlign_rightItem, fontAlign_justifyItem]
  122. fontAlignmentGroup.updateItemProperty(itemArr)
  123. fontAlignmentGroup.delegate = self
  124. }
  125. //Color
  126. colorLabel.stringValue = KMLocalizedString("Font")
  127. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  128. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  129. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.edit_Text_Colors[0])
  130. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.edit_Text_Colors[1])
  131. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.edit_Text_Colors[2])
  132. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.edit_Text_Colors[3])
  133. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMAnnotationPropertiesColorManager.manager.edit_Text_Colors[4])
  134. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  135. colorGroup.delegate = self
  136. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  137. colorSlider.delegate = self
  138. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  139. state: .normal,
  140. creatable: true,
  141. text: "100%",
  142. textUnit: "%",
  143. regexString: "0123456789%")
  144. if true {
  145. var opacityItems: [ComponentMenuitemProperty] = []
  146. for string in ["25%", "50%", "75%", "100%"] {
  147. let item = ComponentMenuitemProperty(type: .normal, text: string)
  148. opacityItems.append(item)
  149. }
  150. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  151. }
  152. colorOpacitySelect.delegate = self
  153. if alignmentController == nil {
  154. alignmentController = KMNAlignmentController.init()
  155. }
  156. alignmentController?.view.frame = alignmentBGView.bounds
  157. alignmentController?.view.autoresizingMask = [.width, .height]
  158. alignmentController?.delegate = self
  159. alignmentBGView.addSubview(alignmentController!.view)
  160. }
  161. //文字块变化时调用
  162. func currentAreaChanged() {
  163. fontTitleSelect.selectItemAtIndex(0)
  164. fontTitleButton.properties.isDisabled = true
  165. fontTitleButton.reloadData()
  166. selCustomFontType = .customize
  167. }
  168. func reloadData() {
  169. let areas = pdfView?.km_editingTextAreas() ?? []
  170. if areas.count == 0 {
  171. if let _ = currentArea {
  172. currentArea = nil
  173. currentAreaChanged()
  174. }
  175. alignmentBGView.isHidden = true
  176. } else if areas.count == 1 {
  177. if let area = areas.first, currentArea != area {
  178. currentArea = area
  179. currentAreaChanged()
  180. }
  181. alignmentBGView.isHidden = true
  182. } else if areas.count > 1 {
  183. alignmentBGView.isHidden = false
  184. if areas.count == 2 {
  185. alignmentController?.updateMulti(false)
  186. } else {
  187. alignmentController?.updateMulti(true)
  188. }
  189. if let area = areas.first, currentArea != area {
  190. currentArea = area
  191. currentAreaChanged()
  192. }
  193. }
  194. var fontName: String?
  195. var fontStyle: String?
  196. var fontSize: CGFloat?
  197. var isBold: Bool = false
  198. var isItality: Bool = false
  199. var alignment: NSTextAlignment?
  200. var color: NSColor?
  201. var opacity: CGFloat?
  202. if let currentArea = self.currentArea {
  203. //Font
  204. if let value = pdfView?.editingSelectionCFont(with: currentArea)?.familyName {
  205. fontName = value
  206. if let value = pdfView?.editingSelectionCFont(with: currentArea)?.styleName, value.count > 0 {
  207. fontStyle = value
  208. }
  209. }
  210. if let value = pdfView?.editingSelectionFontSize(byRangeEdit: currentArea) {
  211. fontSize = value
  212. }
  213. isBold = pdfView?.isBoldCurrentSelection(byRangeEdit: currentArea) ?? false
  214. isItality = pdfView?.isItalicCurrentSelection(byRangeEdit: currentArea) ?? false
  215. if let value = pdfView?.currentSelectionAlignment(byRangeEdit: currentArea) {
  216. alignment = value
  217. }
  218. //Color
  219. if let colors = pdfView?.km_editTextAreasFontColors([currentArea]) {
  220. color = colors.first
  221. }
  222. if let opacitys = pdfView?.km_editAreasOpacitys([currentArea]) {
  223. if var value = opacitys.first {
  224. value = min(1, value)
  225. value = max(0, value)
  226. opacity = value
  227. }
  228. }
  229. } else {
  230. fontName = CPDFEditTextArea.defaultFontName()
  231. fontStyle = CPDFEditTextArea.defaultFontStyle()
  232. fontSize = CPDFEditTextArea.defaultFontSize()
  233. isBold = CPDFEditTextArea.defaultIsBold()
  234. isItality = CPDFEditTextArea.defaultIsItality()
  235. alignment = CPDFEditTextArea.defaultFontAlignment()
  236. color = CPDFEditTextArea.defaultColor()
  237. opacity = CPDFEditTextArea.defaultOpacity()
  238. }
  239. if areas.count > 1 {
  240. if CPDFEditArea.isMultiFontFamilyNames(areas, inPDFView: pdfView) {
  241. fontName = nil
  242. }
  243. if CPDFEditArea.isMultiFontStyles(areas, inPDFView: pdfView) {
  244. fontStyle = nil
  245. }
  246. if CPDFEditArea.isMultiFontSizes(areas, inPDFView: pdfView) {
  247. fontSize = nil
  248. }
  249. if CPDFEditArea.isMultiFontBolds(areas, inPDFView: pdfView) {
  250. isBold = false
  251. }
  252. if CPDFEditArea.isMultiFontItalitys(areas, inPDFView: pdfView) {
  253. isItality = false
  254. }
  255. if CPDFEditArea.isMultiFontAlignment(areas, inPDFView: pdfView) {
  256. alignment = nil
  257. }
  258. if CPDFEditArea.isMultiColors(areas, inPDFView: pdfView) {
  259. color = nil
  260. }
  261. if CPDFEditArea.isMultiOpacity(areas, inPDFView: pdfView) {
  262. opacity = nil
  263. }
  264. }
  265. if selCustomFontType == .customize {
  266. fontTitleSelect.selectItemAtIndex(0)
  267. }
  268. //Font
  269. if let value = fontName {
  270. fontNameSelect.properties.text = value
  271. var styles: [String] = CPDFFont.fontNames(forFamilyName: value)
  272. if styles.isEmpty == true {
  273. styles.append("Regular")
  274. }
  275. var menuItemArr: [ComponentMenuitemProperty] = []
  276. for string in styles {
  277. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  278. menuItemArr.append(item)
  279. }
  280. fontStyleSelect.updateMenuItemsArr(menuItemArr)
  281. } else {
  282. fontNameSelect.properties.text = "-"
  283. fontStyleSelect.updateMenuItemsArr([])
  284. }
  285. fontNameSelect.reloadData()
  286. if let value = fontStyle {
  287. fontStyleSelect.properties.text = value
  288. } else {
  289. fontStyleSelect.properties.text = "-"
  290. fontStyleSelect.updateMenuItemsArr([])
  291. }
  292. fontStyleSelect.reloadData()
  293. if let value = fontSize {
  294. fontSizeSelect.properties.text = String(format: "%.0f", value)
  295. } else {
  296. fontSizeSelect.properties.text = "-"
  297. }
  298. fontSizeSelect.reloadData()
  299. fontStyle_boldItem.state = isBold ? .pressed : .normal
  300. fontStyle_ItalityItem.state = isItality ? .pressed : .normal
  301. fontStyle_CenterItem.state = .normal
  302. fontStyle_Underline.state = .normal
  303. fontStyleGroup.reloadData()
  304. fontAlign_leftItem.state = .normal
  305. fontAlign_centerItem.state = .normal
  306. fontAlign_rightItem.state = .normal
  307. fontAlign_justifyItem.state = .normal
  308. if let value = alignment {
  309. if value == .left {
  310. fontAlign_leftItem.state = .pressed
  311. } else if value == .center {
  312. fontAlign_centerItem.state = .pressed
  313. } else if value == .right {
  314. fontAlign_rightItem.state = .pressed
  315. } else if value == .justified {
  316. fontAlign_justifyItem.state = .pressed
  317. }
  318. }
  319. fontAlignmentGroup.reloadData()
  320. //Color
  321. colorGroup.currentColor = color
  322. colorGroup.refreshUI()
  323. if var value = opacity {
  324. value = min(1, value)
  325. value = max(0, value)
  326. colorOpacitySelect.properties.text = String(format: "%.0f", value*100)
  327. colorSlider.properties.percent = value
  328. } else {
  329. colorSlider.properties.percent = 0
  330. colorOpacitySelect.properties.text = "-"
  331. }
  332. colorSlider.reloadData()
  333. colorOpacitySelect.reloadData()
  334. self.checkFontTitleInfo()
  335. }
  336. func checkFontTitleInfo() {
  337. if self.fontTitleSelect.indexOfSelect() == 0 {
  338. return
  339. }
  340. var type: KMEditPDFTextFontType = .customize
  341. if self.fontTitleSelect.indexOfSelect() == 1 {
  342. type = .h1
  343. } else if self.fontTitleSelect.indexOfSelect() == 2 {
  344. type = .h2
  345. } else if self.fontTitleSelect.indexOfSelect() == 3 {
  346. type = .h3
  347. } else if self.fontTitleSelect.indexOfSelect() == 4 {
  348. type = .b1
  349. } else if self.fontTitleSelect.indexOfSelect() == 5 {
  350. type = .b2
  351. } else if self.fontTitleSelect.indexOfSelect() == 6 {
  352. type = .b3
  353. }
  354. if type == .customize {
  355. return
  356. }
  357. let fontName = fontNameSelect.properties.text ?? ""
  358. let fontStyle = fontStyleSelect.properties.text ?? ""
  359. var fontSize = 12.0
  360. let bold = fontStyle_boldItem.state == .pressed
  361. let italic = fontStyle_ItalityItem.state == .pressed
  362. var alignment: NSTextAlignment = .left
  363. if fontAlign_centerItem.state == .pressed {
  364. alignment = .center
  365. } else if fontAlign_rightItem.state == .pressed {
  366. alignment = .right
  367. } else if fontAlign_justifyItem.state == .pressed {
  368. alignment = .justified
  369. }
  370. if let text = fontSizeSelect.properties.text, let textUnit = fontSizeSelect.properties.textUnit {
  371. let result = text.stringByDeleteCharString(textUnit)
  372. fontSize = result.stringToCGFloat()
  373. }
  374. let isChanged = KMEditPDFTextManager.manager.isModelChanged(type: type,
  375. fontName: fontName,
  376. fontStyle: fontStyle,
  377. fontSize: fontSize,
  378. bold: bold,
  379. italic: italic,
  380. alignment: alignment)
  381. var fontTitleText = fontTitleSelect.properties.text ?? ""
  382. if isChanged {
  383. if fontTitleText.hasPrefix("* ") == false {
  384. fontTitleText = "* " + fontTitleText
  385. }
  386. } else {
  387. if fontTitleText.hasPrefix("* ") == true {
  388. fontTitleText = fontTitleText.substring(form: 2)
  389. }
  390. }
  391. fontTitleSelect.properties.text = fontTitleText
  392. fontTitleSelect.reloadData()
  393. }
  394. func updateAreaInfoWithCustomType(type: KMEditPDFTextFontType) {
  395. var selModel: KMEditPDFTextFontModel?
  396. if type != .customize {
  397. selModel = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type)
  398. }
  399. if let model = selModel {
  400. let fontName = model.fontName
  401. let fontStyle = model.fontStyle
  402. let fontSize = model.fontSize
  403. let bold = model.bold
  404. let italic = model.italic
  405. let alignment = model.alignment
  406. let font = CPDFFont(familyName: fontName, fontStyle: fontStyle)
  407. pdfView?.setEditingTextarea_font(font: font)
  408. pdfView?.setEditingTextarea_FontSize(size: fontSize)
  409. pdfView?.setEditingTextarea(isBold: bold)
  410. pdfView?.setEditingTextarea(isItalic: italic)
  411. pdfView?.setEditingTextarea_Alignment(align: alignment)
  412. CPDFEditTextArea.update_DefaultFont_Info(font)
  413. CPDFEditTextArea.update_DefaultFontSize_Info(fontSize)
  414. CPDFEditTextArea.update_DefaultFontBold(bold)
  415. CPDFEditTextArea.update_DefaultFontItality(italic)
  416. CPDFEditTextArea.update_DefaultFontAlignment_Info(alignment)
  417. }
  418. }
  419. //MARK: - Action
  420. @objc func buttonClicked(_ sender: ComponentButton) {
  421. if sender == fontTitleButton {
  422. var viewHeight: CGFloat = 8
  423. var menuItemArr: [ComponentMenuitemProperty] = []
  424. for i in ["Redefine", "Reset"] {
  425. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  426. itemSelected: false,
  427. isDisabled: false,
  428. keyEquivalent: nil,
  429. text: KMLocalizedString(i),
  430. identifier: i)
  431. menuItemArr.append(properties_Menuitem)
  432. viewHeight += 36
  433. }
  434. if fontTitleGroup == nil {
  435. fontTitleGroup = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  436. }
  437. fontTitleGroup?.groupDelegate = self
  438. fontTitleGroup?.frame = CGRectMake(310, 0, 140, viewHeight)
  439. fontTitleGroup?.updateGroupInfo(menuItemArr)
  440. var point = fontTitleButton.superview?.convert(fontTitleButton.frame.origin, to: nil) ?? CGPointZero
  441. point.y -= viewHeight
  442. fontTitleGroup?.showWithPoint(point, relativeTo: fontTitleButton)
  443. }
  444. }
  445. override func mouseDown(with event: NSEvent) {
  446. super.mouseDown(with: event)
  447. view.window?.makeFirstResponder(nil)
  448. }
  449. }
  450. //MARK: - ComponentSelectDelegate
  451. extension KMEditPDFTextPropertyViewController: ComponentSelectDelegate {
  452. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  453. if var result = menuItemProperty?.text {
  454. if let textUnit = view?.properties.textUnit {
  455. result = result.stringByDeleteCharString(textUnit)
  456. }
  457. if view == fontTitleSelect {
  458. if menuItemProperty?.identifier == Customize_USERDEFAULTS {
  459. fontTitleButton.properties.isDisabled = true
  460. selCustomFontType = .customize
  461. } else {
  462. fontTitleButton.properties.isDisabled = false
  463. var type: KMEditPDFTextFontType = .customize
  464. if menuItemProperty?.identifier == H1_USERDEFAULTS {
  465. type = .h1
  466. } else if menuItemProperty?.identifier == H2_USERDEFAULTS {
  467. type = .h2
  468. } else if menuItemProperty?.identifier == H3_USERDEFAULTS {
  469. type = .h3
  470. } else if menuItemProperty?.identifier == B1_USERDEFAULTS {
  471. type = .b1
  472. } else if menuItemProperty?.identifier == B2_USERDEFAULTS {
  473. type = .b2
  474. } else if menuItemProperty?.identifier == B3_USERDEFAULTS {
  475. type = .b3
  476. }
  477. selCustomFontType = type
  478. self.updateAreaInfoWithCustomType(type: type)
  479. }
  480. fontTitleButton.reloadData()
  481. } else if view == fontNameSelect {
  482. var styleName = "Regular"
  483. let styleNames = CPDFFont.fontNames(forFamilyName: result)
  484. if let first = styleNames.first {
  485. styleName = first
  486. }
  487. let cFont = CPDFFont(familyName: result, fontStyle: styleName)
  488. pdfView?.setEditingTextarea_font(font: cFont)
  489. CPDFEditTextArea.update_DefaultFont_Info(cFont)
  490. } else if view == fontStyleSelect {
  491. let fontName = fontNameSelect.properties.text ?? "Helvetica"
  492. let cFont = CPDFFont(familyName: fontName, fontStyle: result)
  493. pdfView?.setEditingTextarea_font(font: cFont)
  494. CPDFEditTextArea.update_DefaultFont_Info(cFont)
  495. } else if view == fontSizeSelect {
  496. if let _ = menuItemProperty?.text {
  497. let fontSize = result.stringToCGFloat()
  498. pdfView?.setEditingTextarea_FontSize(size: fontSize)
  499. CPDFEditTextArea.update_DefaultFontSize_Info(fontSize)
  500. }
  501. } else if view == colorOpacitySelect {
  502. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  503. pdfView?.setEditingAreasOpacity(opacity)
  504. CPDFEditTextArea.updateDefault_Opacity(opacity)
  505. }
  506. }
  507. reloadData()
  508. }
  509. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  510. if var result = view.properties.text {
  511. if let textUnit = view.properties.textUnit {
  512. result = result.stringByDeleteCharString(textUnit)
  513. }
  514. if view == fontSizeSelect {
  515. let size = result.stringToCGFloat()
  516. let fontSize = min(size, 288)
  517. pdfView?.setEditingTextarea_FontSize(size: fontSize)
  518. CPDFEditTextArea.update_DefaultFontSize_Info(fontSize)
  519. } else if view == colorOpacitySelect {
  520. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  521. pdfView?.setEditingAreasOpacity(opacity)
  522. CPDFEditTextArea.updateDefault_Opacity(opacity)
  523. }
  524. }
  525. reloadData()
  526. }
  527. }
  528. //MARK: - ComponentCColorDelegate
  529. extension KMEditPDFTextPropertyViewController: ComponentCColorDelegate {
  530. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  531. if view == colorGroup {
  532. pdfView?.changeEditingTextarea_Color(color: color)
  533. CPDFEditTextArea.updateDefault_Color(color)
  534. }
  535. }
  536. }
  537. //MARK: - ComponentSliderDelegate
  538. extension KMEditPDFTextPropertyViewController: ComponentSliderDelegate {
  539. func componentSliderDidUpdate(_ view: ComponentSlider) {
  540. let percent = view.properties.percent
  541. self.pdfView?.setEditingAreasOpacity(percent)
  542. CPDFEditTextArea.updateDefault_Opacity(percent)
  543. reloadData()
  544. }
  545. }
  546. //MARK: - ComponentCSelectorGroupDelegate
  547. extension KMEditPDFTextPropertyViewController: ComponentCSelectorGroupDelegate {
  548. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  549. if view == fontStyleGroup {
  550. if item.properties.identifier == "fontStyle_bold" {
  551. pdfView?.setEditingTextarea_Bold()
  552. CPDFEditTextArea.update_DefaultFontBold(fontStyle_boldItem.state == .pressed ? true : false)
  553. } else if item.properties.identifier == "fontStyle_Itality" {
  554. pdfView?.setEditingTextarea_Italic()
  555. CPDFEditTextArea.update_DefaultFontItality(fontStyle_ItalityItem.state == .pressed ? true : false)
  556. } else if item.properties.identifier == "fontStyle_Center" {
  557. } else if item.properties.identifier == "fontStyle_Underline" {
  558. }
  559. } else if view == fontAlignmentGroup {
  560. if item.properties.identifier == "fontAlign_left" {
  561. pdfView?.setEditingTextarea_Alignment(align: .left)
  562. CPDFEditTextArea.update_DefaultFontAlignment_Info(.left)
  563. } else if item.properties.identifier == "fontAlign_center" {
  564. pdfView?.setEditingTextarea_Alignment(align: .center)
  565. CPDFEditTextArea.update_DefaultFontAlignment_Info(.center)
  566. } else if item.properties.identifier == "fontAlign_right" {
  567. pdfView?.setEditingTextarea_Alignment(align: .right)
  568. CPDFEditTextArea.update_DefaultFontAlignment_Info(.right)
  569. } else if item.properties.identifier == "fontAlign_justify" {
  570. pdfView?.setEditingTextarea_Alignment(align: .justified)
  571. CPDFEditTextArea.update_DefaultFontAlignment_Info(.justified)
  572. }
  573. }
  574. reloadData()
  575. }
  576. }
  577. //MARK: - KMNAlignmentControllerDelegate
  578. extension KMEditPDFTextPropertyViewController: KMNAlignmentControllerDelegate {
  579. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  580. pdfView?.changeEditingAreas(alignmentType)
  581. }
  582. }
  583. //MARK: - ComponentGroupDelegate
  584. extension KMEditPDFTextPropertyViewController: ComponentGroupDelegate {
  585. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  586. if menuItemProperty?.identifier == "Redefine" {
  587. self.redefineCustomFontType()
  588. } else if menuItemProperty?.identifier == "Reset" {
  589. if selCustomFontType != .customize {
  590. KMEditPDFTextManager.manager.resetTextPresupposition(type: selCustomFontType)
  591. self.updateAreaInfoWithCustomType(type: selCustomFontType)
  592. }
  593. }
  594. reloadData()
  595. }
  596. func redefineCustomFontType() {
  597. if selCustomFontType == .customize {
  598. return
  599. }
  600. let fontName = fontNameSelect.properties.text ?? ""
  601. let fontStyle = fontStyleSelect.properties.text ?? ""
  602. var fontSize = 12.0
  603. let bold = fontStyle_boldItem.state == .pressed
  604. let italic = fontStyle_ItalityItem.state == .pressed
  605. var alignment: NSTextAlignment = .left
  606. if fontAlign_centerItem.state == .pressed {
  607. alignment = .center
  608. } else if fontAlign_rightItem.state == .pressed {
  609. alignment = .right
  610. } else if fontAlign_justifyItem.state == .pressed {
  611. alignment = .justified
  612. }
  613. if let text = fontSizeSelect.properties.text, let textUnit = fontSizeSelect.properties.textUnit {
  614. let result = text.stringByDeleteCharString(textUnit)
  615. fontSize = result.stringToCGFloat()
  616. }
  617. KMEditPDFTextManager.manager.reDefineTextPresupposition(type: selCustomFontType, fontName: fontName, fontStyle: fontStyle, fontSize: fontSize, bold: bold, italic: italic, alignment: alignment)
  618. }
  619. }