123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- //
- // KMEditPDFTextPropertyViewController.swift
- // PDF Reader Pro
- //
- // Created by lxy on 2022/12/27.
- //
- import Cocoa
- import KMComponentLibrary
-
- class KMEditPDFTextPropertyViewController: NSViewController {
- @IBOutlet weak var contentView: NSClipView!
- @IBOutlet weak var backgroundView: NSView!
-
- @IBOutlet var fontBGView: NSView!
- @IBOutlet var fontLabel: NSTextField!
- @IBOutlet var fontTitleSelect: ComponentSelect!
- @IBOutlet var fontTitleButton: ComponentButton!
- @IBOutlet var fontNameSelect: ComponentSelect!
- @IBOutlet var fontStyleSelect: ComponentSelect!
- @IBOutlet var fontSizeSelect: ComponentSelect!
- @IBOutlet var fontStyleGroup: ComponentCSelectorGroup!
- @IBOutlet var fontAlignmentGroup: ComponentCSelectorGroup!
-
- @IBOutlet var colorBGView: NSView!
- @IBOutlet var colorLabel: NSTextField!
- @IBOutlet var colorGroup: ComponentCColorGroup!
- @IBOutlet var colorSlider: ComponentSlider!
- @IBOutlet var colorOpacitySelect: ComponentSelect!
-
- @IBOutlet var alignmentBGView: NSView!
-
- private var alignmentController: KMNAlignmentController?
-
- private var familyNames = CPDFFont.familyNames
-
- private let fontStyle_boldItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_bold"), identifier: "fontStyle_bold")
- private let fontStyle_ItalityItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Itality"), identifier: "fontStyle_Itality")
- private let fontStyle_CenterItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Center"), identifier: "fontStyle_Center")
- private let fontStyle_Underline: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontStyle_Underline"), identifier: "fontStyle_Underline")
-
- private let fontAlign_leftItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_left"), identifier: "fontAlign_left")
- private let fontAlign_centerItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_center"), identifier: "fontAlign_center")
- private let fontAlign_rightItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_right"), identifier: "fontAlign_right")
- private let fontAlign_justifyItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_justify"), identifier: "fontAlign_justify")
-
- var pdfView: CPDFListView?
-
- var currentArea: CPDFEditTextArea?
-
-
- override func viewDidAppear() {
- super.viewDidAppear()
-
- colorSlider.reloadData()
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- setupProperty()
- }
-
- func setupProperty() {
-
- //Font
- fontLabel.stringValue = KMLocalizedString("Font")
- fontLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- fontLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
- fontTitleSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- text: "")
- if true {
- var menuItemArr: [ComponentMenuitemProperty] = []
- for string in KMEditPDFTextFontTypeWrapper.allValues() {
- let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
- menuItemArr.append(item)
- }
- fontTitleSelect.updateMenuItemsArr(menuItemArr)
- }
- fontTitleSelect.delegate = self
- fontTitleButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "toolbar_more"))
- fontTitleButton.setTarget(self, action: #selector(buttonClicked(_:)))
-
- fontNameSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- text: "")
- if true {
- var menuItemArr: [ComponentMenuitemProperty] = []
- for string in familyNames {
- let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
- menuItemArr.append(item)
- }
- fontNameSelect.updateMenuItemsArr(menuItemArr)
- }
- fontNameSelect.delegate = self
-
- fontStyleSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- text: "")
- fontStyleSelect.delegate = self
-
- fontSizeSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- creatable: true,
- text: "12 pt",
- textUnit: " pt",
- regexString: "0123456789")
- if true {
- var sizeItemArr: [ComponentMenuitemProperty] = []
- for string in KMHeaderFooterManager.getFontSize() {
- let item = ComponentMenuitemProperty(type: .normal, text: string + " pt", identifier: string)
- sizeItemArr.append(item)
- }
- fontSizeSelect.updateMenuItemsArr(sizeItemArr)
- }
- fontSizeSelect.delegate = self
-
- if true {
- let itemArr: [ComponentCSelectorProperty] = [fontStyle_boldItem, fontStyle_ItalityItem, fontStyle_CenterItem, fontStyle_Underline]
- fontStyleGroup.updateItemProperty(itemArr)
- fontStyleGroup.delegate = self
- }
-
- if true {
- let itemArr: [ComponentCSelectorProperty] = [fontAlign_leftItem, fontAlign_centerItem, fontAlign_rightItem, fontAlign_justifyItem]
- fontAlignmentGroup.updateItemProperty(itemArr)
- fontAlignmentGroup.delegate = self
- }
-
- //Color
- colorLabel.stringValue = KMLocalizedString("Font")
- colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
- let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
- let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
- let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
- let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
- let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
- colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
- colorGroup.delegate = self
-
- colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
- colorSlider.delegate = self
-
- colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- creatable: true,
- text: "100%",
- regexString: "0123456789%")
- if true {
- var opacityItems: [ComponentMenuitemProperty] = []
- for string in ["25%", "50%", "75%", "100%"] {
- let item = ComponentMenuitemProperty(type: .normal, text: string)
- opacityItems.append(item)
- }
- colorOpacitySelect.updateMenuItemsArr(opacityItems)
- }
- colorOpacitySelect.delegate = self
-
-
- if alignmentController == nil {
- alignmentController = KMNAlignmentController.init()
- }
- alignmentController?.view.frame = alignmentBGView.bounds
- alignmentController?.view.autoresizingMask = [.width, .height]
- alignmentController?.delegate = self
- alignmentBGView.addSubview(alignmentController!.view)
- }
-
- func reloadData() {
-
- let areas = pdfView?.km_editingTextAreas() ?? []
- if areas.count == 0 {
- currentArea = nil
- } else if areas.count == 1 {
- currentArea = areas.first
-
- alignmentBGView.isHidden = true
- } else if areas.count > 1 {
- alignmentBGView.isHidden = false
- if areas.count == 2 {
- alignmentController?.updateMulti(false)
- } else {
- alignmentController?.updateMulti(true)
- }
-
- currentArea = nil
- }
-
- if let area = currentArea {
- if let fontNames = pdfView?.km_editTextAreasFontNames([area]) {
- if let fontName = fontNames.first {
- fontNameSelect.properties.text = fontName
- fontNameSelect.reloadData()
- }
- }
-
- if let fontStyles = pdfView?.km_editTextAreasFontStyles([area]) {
- var styles: [String] = []
- if let fontStyle = fontStyles.first, fontStyle.isEmpty == false {
- styles = fontStyles
-
- fontStyleSelect.properties.text = fontStyle
- } else {
- if let name = fontNameSelect.properties.text {
- styles = CPDFFont.fontNames(forFamilyName: name)
- if styles.isEmpty == true {
- styles.append("Regular")
- fontStyleSelect.properties.text = "Regular"
- }
- }
- }
- fontStyleSelect.reloadData()
-
- var menuItemArr: [ComponentMenuitemProperty] = []
- for string in styles {
- let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
- menuItemArr.append(item)
- }
- fontStyleSelect.updateMenuItemsArr(menuItemArr)
-
-
- }
-
- if let fontSizes = pdfView?.km_editTextAreasFontSizes([area]) {
- if let fontSize = fontSizes.first {
- fontSizeSelect.properties.text = String(format: "%.0f", fontSize)
- fontSizeSelect.reloadData()
- }
- }
-
- if let bolds = pdfView?.km_editTextAreasFontBolds([area]) {
- if let bold = bolds.first, bold == true {
- fontStyle_boldItem.state = .pressed
- } else {
- fontStyle_boldItem.state = .normal
- }
- }
-
- if let italitys = pdfView?.km_editTextAreasFontItalics([area]) {
- if let itality = italitys.first, itality == true {
- fontStyle_ItalityItem.state = .pressed
- } else {
- fontStyle_ItalityItem.state = .normal
- }
- }
- fontStyle_CenterItem.state = .normal
- fontStyle_Underline.state = .normal
-
- fontStyleGroup.reloadData()
-
- if let alignments = pdfView?.km_editTextAreasTextAlignments([area]) {
- if let alignment = alignments.first {
-
- fontAlign_leftItem.state = .normal
- fontAlign_centerItem.state = .normal
- fontAlign_rightItem.state = .normal
- fontAlign_justifyItem.state = .normal
-
- if alignment == .left {
- fontAlign_leftItem.state = .pressed
- } else if alignment == .center {
- fontAlign_centerItem.state = .pressed
- } else if alignment == .right {
- fontAlign_rightItem.state = .pressed
- } else if alignment == .justified {
- fontAlign_justifyItem.state = .pressed
- }
- fontAlignmentGroup.reloadData()
- }
- }
-
- if let colors = pdfView?.km_editTextAreasFontColors([area]) {
- if let color = colors.first {
- colorGroup.currentColor = color
- } else {
- colorGroup.currentColor = NSColor.clear
- }
- colorGroup.refreshUI()
- }
-
- if let opacitys = pdfView?.km_editAreasOpacitys([area]) {
- if var opacity = opacitys.first {
- opacity = min(1, opacity)
- opacity = max(0, opacity)
-
- colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
- colorOpacitySelect.reloadData()
-
- colorSlider.properties.percent = opacity
- colorSlider.reloadData()
- }
- }
- }
-
-
- }
-
- //MARK: - Action
- @objc func buttonClicked(_ sender: ComponentButton) {
-
- }
-
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- view.window?.makeFirstResponder(nil)
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMEditPDFTextPropertyViewController: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- if view == fontTitleSelect {
-
- } else if view == fontNameSelect {
- if let fontName = menuItemProperty?.text {
- var cFont = CPDFFont.mappingFont(withFontString: fontName)
- var styles = CPDFFont.fontNames(forFamilyName: fontName)
- if styles.isEmpty == false, let style = styles.first {
- cFont = CPDFFont.init(familyName: fontName, fontStyle: style)
- }
- if let cFont = cFont {
- pdfView?.setEditingTextarea_font(font: cFont)
- }
- }
- } else if view == fontStyleSelect {
-
- } else if view == fontSizeSelect {
- if let text = menuItemProperty?.text {
- let result = text.stringByDeleteCharString(" pt")
- let fontSize = result.stringToCGFloat()
- pdfView?.setEditingTextarea_FontSize(size: fontSize)
- }
- }
- reloadData()
-
- }
-
- func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
- if view == fontSizeSelect {
- if let string = view.properties.text {
- let result = string.stringByDeleteCharString(" pt")
- let size = result.stringToCGFloat()
- let fontSize = max(size, 100)
-
- pdfView?.setEditingTextarea_FontSize(size: fontSize)
-
- }
- }
- reloadData()
-
- }
- }
- //MARK: - ComponentCColorDelegate
- extension KMEditPDFTextPropertyViewController: ComponentCColorDelegate {
- func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
- if view == colorGroup {
- pdfView?.changeEditingTextarea_Color(color: color)
- }
- }
- }
- //MARK: - ComponentSliderDelegate
- extension KMEditPDFTextPropertyViewController: ComponentSliderDelegate {
- func componentSliderDidUpdate(_ view: ComponentSlider) {
- let percent = view.properties.percent
-
- self.pdfView?.setEditingAreasOpacity(percent)
-
- reloadData()
-
- }
- }
- //MARK: - ComponentCSelectorGroupDelegate
- extension KMEditPDFTextPropertyViewController: ComponentCSelectorGroupDelegate {
- func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
- if view == fontStyleGroup {
- if item.properties.identifier == "fontStyle_bold" {
- pdfView?.setEditingTextarea_Bold()
- } else if item.properties.identifier == "fontStyle_Itality" {
- pdfView?.setEditingTextarea_Italic()
- } else if item.properties.identifier == "fontStyle_Center" {
-
- } else if item.properties.identifier == "fontStyle_Underline" {
-
- }
- } else if view == fontAlignmentGroup {
- if item.properties.identifier == "fontAlign_left" {
- pdfView?.setEditingTextarea_Alignment(align: .left)
- } else if item.properties.identifier == "fontAlign_center" {
- pdfView?.setEditingTextarea_Alignment(align: .center)
- } else if item.properties.identifier == "fontAlign_right" {
- pdfView?.setEditingTextarea_Alignment(align: .right)
- } else if item.properties.identifier == "fontAlign_justify" {
- pdfView?.setEditingTextarea_Alignment(align: .justified)
- }
- }
-
- reloadData()
-
- }
- }
- //MARK: - KMNAlignmentControllerDelegate
- extension KMEditPDFTextPropertyViewController: KMNAlignmentControllerDelegate {
- func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
- pdfView?.changeEditingAreas(alignmentType)
-
- }
- }
|