KMEditPDFTextPropertyViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 alignmentController: KMNAlignmentController?
  28. private var familyNames = CPDFFont.familyNames
  29. private let fontStyle_boldItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_bold"), identifier: "fontStyle_bold")
  30. private let fontStyle_ItalityItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Itality"), identifier: "fontStyle_Itality")
  31. private let fontStyle_CenterItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Center"), identifier: "fontStyle_Center")
  32. private let fontStyle_Underline: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Underline"), identifier: "fontStyle_Underline")
  33. private let fontAlign_leftItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_left"), identifier: "fontAlign_left")
  34. private let fontAlign_centerItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_center"), identifier: "fontAlign_center")
  35. private let fontAlign_rightItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_right"), identifier: "fontAlign_right")
  36. private let fontAlign_justifyItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_justify"), identifier: "fontAlign_justify")
  37. var pdfView: CPDFListView?
  38. var currentArea: CPDFEditTextArea?
  39. override func viewDidAppear() {
  40. super.viewDidAppear()
  41. colorSlider.reloadData()
  42. }
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. setupProperty()
  46. }
  47. func setupProperty() {
  48. //Font
  49. fontLabel.stringValue = KMLocalizedString("Font")
  50. fontLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  51. fontLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  52. fontTitleSelect.properties = ComponentSelectProperties(size: .s,
  53. state: .normal,
  54. text: "")
  55. if true {
  56. var menuItemArr: [ComponentMenuitemProperty] = []
  57. for string in KMEditPDFTextFontTypeWrapper.allValues() {
  58. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  59. menuItemArr.append(item)
  60. }
  61. fontTitleSelect.updateMenuItemsArr(menuItemArr)
  62. }
  63. fontTitleSelect.delegate = self
  64. fontTitleButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "toolbar_more"))
  65. fontTitleButton.setTarget(self, action: #selector(buttonClicked(_:)))
  66. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  67. state: .normal,
  68. text: "")
  69. if true {
  70. var menuItemArr: [ComponentMenuitemProperty] = []
  71. for string in familyNames {
  72. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  73. menuItemArr.append(item)
  74. }
  75. fontNameSelect.updateMenuItemsArr(menuItemArr)
  76. }
  77. fontNameSelect.delegate = self
  78. fontStyleSelect.properties = ComponentSelectProperties(size: .s,
  79. state: .normal,
  80. text: "")
  81. fontStyleSelect.delegate = self
  82. fontSizeSelect.properties = ComponentSelectProperties(size: .s,
  83. state: .normal,
  84. creatable: true,
  85. text: "12 pt",
  86. textUnit: " pt",
  87. regexString: "0123456789")
  88. if true {
  89. var sizeItemArr: [ComponentMenuitemProperty] = []
  90. for string in KMHeaderFooterManager.getFontSize() {
  91. let item = ComponentMenuitemProperty(type: .normal, text: string + " pt", identifier: string)
  92. sizeItemArr.append(item)
  93. }
  94. fontSizeSelect.updateMenuItemsArr(sizeItemArr)
  95. }
  96. fontSizeSelect.delegate = self
  97. if true {
  98. let itemArr: [ComponentCSelectorProperty] = [fontStyle_boldItem, fontStyle_ItalityItem, fontStyle_CenterItem, fontStyle_Underline]
  99. fontStyleGroup.updateItemProperty(itemArr)
  100. fontStyleGroup.delegate = self
  101. }
  102. if true {
  103. let itemArr: [ComponentCSelectorProperty] = [fontAlign_leftItem, fontAlign_centerItem, fontAlign_rightItem, fontAlign_justifyItem]
  104. fontAlignmentGroup.updateItemProperty(itemArr)
  105. fontAlignmentGroup.delegate = self
  106. }
  107. //Color
  108. colorLabel.stringValue = KMLocalizedString("Font")
  109. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  110. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  111. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  112. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  113. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  114. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  115. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  116. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  117. colorGroup.delegate = self
  118. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  119. colorSlider.delegate = self
  120. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  121. state: .normal,
  122. creatable: true,
  123. text: "100%",
  124. regexString: "0123456789%")
  125. if true {
  126. var opacityItems: [ComponentMenuitemProperty] = []
  127. for string in ["25%", "50%", "75%", "100%"] {
  128. let item = ComponentMenuitemProperty(type: .normal, text: string)
  129. opacityItems.append(item)
  130. }
  131. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  132. }
  133. colorOpacitySelect.delegate = self
  134. if alignmentController == nil {
  135. alignmentController = KMNAlignmentController.init()
  136. }
  137. alignmentController?.view.frame = alignmentBGView.bounds
  138. alignmentController?.view.autoresizingMask = [.width, .height]
  139. alignmentController?.delegate = self
  140. alignmentBGView.addSubview(alignmentController!.view)
  141. }
  142. func reloadData() {
  143. let areas = pdfView?.km_editingTextAreas() ?? []
  144. if areas.count == 0 {
  145. currentArea = nil
  146. } else if areas.count == 1 {
  147. currentArea = areas.first
  148. alignmentBGView.isHidden = true
  149. } else if areas.count > 1 {
  150. alignmentBGView.isHidden = false
  151. if areas.count == 2 {
  152. alignmentController?.updateMulti(false)
  153. } else {
  154. alignmentController?.updateMulti(true)
  155. }
  156. currentArea = nil
  157. }
  158. if let area = currentArea {
  159. if let fontNames = pdfView?.km_editTextAreasFontNames([area]) {
  160. if let fontName = fontNames.first {
  161. fontNameSelect.properties.text = fontName
  162. fontNameSelect.reloadData()
  163. }
  164. }
  165. if let fontStyles = pdfView?.km_editTextAreasFontStyles([area]) {
  166. var styles: [String] = []
  167. if let fontStyle = fontStyles.first, fontStyle.isEmpty == false {
  168. styles = fontStyles
  169. fontStyleSelect.properties.text = fontStyle
  170. } else {
  171. if let name = fontNameSelect.properties.text {
  172. styles = CPDFFont.fontNames(forFamilyName: name)
  173. if styles.isEmpty == true {
  174. styles.append("Regular")
  175. fontStyleSelect.properties.text = "Regular"
  176. }
  177. }
  178. }
  179. fontStyleSelect.reloadData()
  180. var menuItemArr: [ComponentMenuitemProperty] = []
  181. for string in styles {
  182. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  183. menuItemArr.append(item)
  184. }
  185. fontStyleSelect.updateMenuItemsArr(menuItemArr)
  186. }
  187. if let fontSizes = pdfView?.km_editTextAreasFontSizes([area]) {
  188. if let fontSize = fontSizes.first {
  189. fontSizeSelect.properties.text = String(format: "%.0f", fontSize)
  190. fontSizeSelect.reloadData()
  191. }
  192. }
  193. if let bolds = pdfView?.km_editTextAreasFontBolds([area]) {
  194. if let bold = bolds.first, bold == true {
  195. fontStyle_boldItem.state = .pressed
  196. } else {
  197. fontStyle_boldItem.state = .normal
  198. }
  199. }
  200. if let italitys = pdfView?.km_editTextAreasFontItalics([area]) {
  201. if let itality = italitys.first, itality == true {
  202. fontStyle_ItalityItem.state = .pressed
  203. } else {
  204. fontStyle_ItalityItem.state = .normal
  205. }
  206. }
  207. fontStyle_CenterItem.state = .normal
  208. fontStyle_Underline.state = .normal
  209. fontStyleGroup.reloadData()
  210. if let alignments = pdfView?.km_editTextAreasTextAlignments([area]) {
  211. if let alignment = alignments.first {
  212. fontAlign_leftItem.state = .normal
  213. fontAlign_centerItem.state = .normal
  214. fontAlign_rightItem.state = .normal
  215. fontAlign_justifyItem.state = .normal
  216. if alignment == .left {
  217. fontAlign_leftItem.state = .pressed
  218. } else if alignment == .center {
  219. fontAlign_centerItem.state = .pressed
  220. } else if alignment == .right {
  221. fontAlign_rightItem.state = .pressed
  222. } else if alignment == .justified {
  223. fontAlign_justifyItem.state = .pressed
  224. }
  225. fontAlignmentGroup.reloadData()
  226. }
  227. }
  228. if let colors = pdfView?.km_editTextAreasFontColors([area]) {
  229. if let color = colors.first {
  230. colorGroup.currentColor = color
  231. } else {
  232. colorGroup.currentColor = NSColor.clear
  233. }
  234. colorGroup.refreshUI()
  235. }
  236. if let opacitys = pdfView?.km_editAreasOpacitys([area]) {
  237. if var opacity = opacitys.first {
  238. opacity = min(1, opacity)
  239. opacity = max(0, opacity)
  240. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  241. colorOpacitySelect.reloadData()
  242. colorSlider.properties.percent = opacity
  243. colorSlider.reloadData()
  244. }
  245. }
  246. }
  247. }
  248. //MARK: - Action
  249. @objc func buttonClicked(_ sender: ComponentButton) {
  250. }
  251. override func mouseDown(with event: NSEvent) {
  252. super.mouseDown(with: event)
  253. view.window?.makeFirstResponder(nil)
  254. }
  255. }
  256. //MARK: - ComponentSelectDelegate
  257. extension KMEditPDFTextPropertyViewController: ComponentSelectDelegate {
  258. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  259. if view == fontTitleSelect {
  260. } else if view == fontNameSelect {
  261. if let fontName = menuItemProperty?.text {
  262. var cFont = CPDFFont.mappingFont(withFontString: fontName)
  263. var styles = CPDFFont.fontNames(forFamilyName: fontName)
  264. if styles.isEmpty == false, let style = styles.first {
  265. cFont = CPDFFont.init(familyName: fontName, fontStyle: style)
  266. }
  267. if let cFont = cFont {
  268. pdfView?.setEditingTextarea_font(font: cFont)
  269. }
  270. }
  271. } else if view == fontStyleSelect {
  272. } else if view == fontSizeSelect {
  273. if let text = menuItemProperty?.text {
  274. let result = text.stringByDeleteCharString(" pt")
  275. let fontSize = result.stringToCGFloat()
  276. pdfView?.setEditingTextarea_FontSize(size: fontSize)
  277. }
  278. }
  279. reloadData()
  280. }
  281. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  282. if view == fontSizeSelect {
  283. if let string = view.properties.text {
  284. let result = string.stringByDeleteCharString(" pt")
  285. let size = result.stringToCGFloat()
  286. let fontSize = max(size, 100)
  287. pdfView?.setEditingTextarea_FontSize(size: fontSize)
  288. }
  289. }
  290. reloadData()
  291. }
  292. }
  293. //MARK: - ComponentCColorDelegate
  294. extension KMEditPDFTextPropertyViewController: ComponentCColorDelegate {
  295. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  296. if view == colorGroup {
  297. pdfView?.changeEditingTextarea_Color(color: color)
  298. }
  299. }
  300. }
  301. //MARK: - ComponentSliderDelegate
  302. extension KMEditPDFTextPropertyViewController: ComponentSliderDelegate {
  303. func componentSliderDidUpdate(_ view: ComponentSlider) {
  304. let percent = view.properties.percent
  305. self.pdfView?.setEditingAreasOpacity(percent)
  306. reloadData()
  307. }
  308. }
  309. //MARK: - ComponentCSelectorGroupDelegate
  310. extension KMEditPDFTextPropertyViewController: ComponentCSelectorGroupDelegate {
  311. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  312. if view == fontStyleGroup {
  313. if item.properties.identifier == "fontStyle_bold" {
  314. pdfView?.setEditingTextarea_Bold()
  315. } else if item.properties.identifier == "fontStyle_Itality" {
  316. pdfView?.setEditingTextarea_Italic()
  317. } else if item.properties.identifier == "fontStyle_Center" {
  318. } else if item.properties.identifier == "fontStyle_Underline" {
  319. }
  320. } else if view == fontAlignmentGroup {
  321. if item.properties.identifier == "fontAlign_left" {
  322. pdfView?.setEditingTextarea_Alignment(align: .left)
  323. } else if item.properties.identifier == "fontAlign_center" {
  324. pdfView?.setEditingTextarea_Alignment(align: .center)
  325. } else if item.properties.identifier == "fontAlign_right" {
  326. pdfView?.setEditingTextarea_Alignment(align: .right)
  327. } else if item.properties.identifier == "fontAlign_justify" {
  328. pdfView?.setEditingTextarea_Alignment(align: .justified)
  329. }
  330. }
  331. reloadData()
  332. }
  333. }
  334. //MARK: - KMNAlignmentControllerDelegate
  335. extension KMEditPDFTextPropertyViewController: KMNAlignmentControllerDelegate {
  336. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  337. pdfView?.changeEditingAreas(alignmentType)
  338. }
  339. }