KMEditPDFTextPropertyViewController.swift 32 KB

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