|
@@ -0,0 +1,663 @@
|
|
|
+//
|
|
|
+// KMAnnotationButtonWidgetAppearanceViewController.swift
|
|
|
+// PDF Master
|
|
|
+//
|
|
|
+// Created by wanjun on 2024/1/15.
|
|
|
+//
|
|
|
+
|
|
|
+import Cocoa
|
|
|
+
|
|
|
+class KMAnnotationButtonWidgetAppearanceViewController: NSViewController {
|
|
|
+
|
|
|
+ private var _annotations: [CPDFButtonWidgetAnnotation] = []
|
|
|
+ private var _formMode: CAnnotationType = .radioButton
|
|
|
+ var pdfView: CPDFListView?
|
|
|
+ var annotationModel: CPDFAnnotationModel?
|
|
|
+
|
|
|
+ @IBOutlet private var backgroundColorLabel: NSTextField!
|
|
|
+ @IBOutlet private var backgroundColorPickerView: KMColorPickerView!
|
|
|
+ @IBOutlet private var appearanceAddTextBtn: NSButton!
|
|
|
+ @IBOutlet private var appearanceFontLabel: NSTextField!
|
|
|
+ @IBOutlet private var fontPopUpButton: KMPopUpButton!
|
|
|
+ @IBOutlet private var fontStylePopUpButton: KMPopUpButton!
|
|
|
+ @IBOutlet private var fontSizeComboBox: KMComboBox!
|
|
|
+ @IBOutlet private var fontButton: NSButton!
|
|
|
+ @IBOutlet private var fontView: NSView!
|
|
|
+ @IBOutlet private var textColorPickerView: KMColorPickerView!
|
|
|
+ private var annotation: CPDFButtonWidgetAnnotation?
|
|
|
+ private var isFromMode: Bool = false
|
|
|
+ private var annotationFreeText: CPDFFreeTextAnnotation?
|
|
|
+ private var annotationFont: NSFont?
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ NotificationCenter.default.removeObserver(self)
|
|
|
+ annotation = nil
|
|
|
+ annotationFreeText = nil
|
|
|
+ annotationFont = nil
|
|
|
+ backgroundColorPickerView.action = nil
|
|
|
+ backgroundColorPickerView.target = nil
|
|
|
+ textColorPickerView.action = nil
|
|
|
+ textColorPickerView.target = nil
|
|
|
+ NSFontManager.shared.target = nil
|
|
|
+ }
|
|
|
+
|
|
|
+ override func loadView() {
|
|
|
+ super.loadView()
|
|
|
+
|
|
|
+ backgroundColorLabel.stringValue = NSLocalizedString("Colors", comment: "")
|
|
|
+ backgroundColorLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+ appearanceAddTextBtn.title = NSLocalizedString("Add Text", comment: "")
|
|
|
+
|
|
|
+ appearanceFontLabel.stringValue = NSLocalizedString("Fonts", comment: "")
|
|
|
+ appearanceFontLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+
|
|
|
+ backgroundColorPickerView.noContentString = true
|
|
|
+ backgroundColorPickerView.annotationTypeString = NSLocalizedString("Background Color", comment: "")
|
|
|
+ backgroundColorPickerView.annotationType = .fromFillColors
|
|
|
+ backgroundColorPickerView.isCallColorPanelAction = true
|
|
|
+
|
|
|
+ textColorPickerView.noContentString = true
|
|
|
+ textColorPickerView.annotationTypeString = NSLocalizedString("Text Color", comment: "")
|
|
|
+ textColorPickerView.annotationType = .fromColors
|
|
|
+ textColorPickerView.isCallColorPanelAction = true
|
|
|
+
|
|
|
+ fontStylePopUpButton.type = .arrowDown
|
|
|
+ fontSizeComboBox.type = .none
|
|
|
+ fontSizeComboBox.comboxRect = fontSizeComboBox.bounds
|
|
|
+ fontPopUpButton.type = .arrowUpDown
|
|
|
+
|
|
|
+ [fontPopUpButton, fontStylePopUpButton, fontSizeComboBox].forEach { view in
|
|
|
+ view?.wantsLayer = true
|
|
|
+ view?.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
|
|
|
+ view?.layer?.borderWidth = 1.0
|
|
|
+ view?.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
|
|
|
+ view?.layer?.cornerRadius = 1.0
|
|
|
+ }
|
|
|
+
|
|
|
+ var fieldString = annotation?.fieldName
|
|
|
+ if annotation?.controlType() == .radioButtonControl, #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ if pdfView?.currentPage() != nil {
|
|
|
+ let currentPage: CPDFPage = (pdfView?.currentPage())!
|
|
|
+ if currentPage.annotations != nil {
|
|
|
+ for annotation in currentPage.annotations {
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+
|
|
|
+ if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation,
|
|
|
+ freeTextAnnotation.userName() == annotationField {
|
|
|
+ self.annotationFreeText = freeTextAnnotation
|
|
|
+ appearanceAddTextBtn.state = .on
|
|
|
+ fontView.isHidden = false
|
|
|
+ } else {
|
|
|
+ appearanceAddTextBtn.state = .off
|
|
|
+ fontView.isHidden = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if annotationFreeText == nil {
|
|
|
+ annotationFreeText = CPDFFreeTextAnnotation(PDFListViewNoteWith: pdfView!.document!)
|
|
|
+ annotationFreeText?.setUserName("\(annotation!.fieldName)_AddText")
|
|
|
+
|
|
|
+ let defaults = UserDefaults.standard
|
|
|
+ var formFont: NSFont? = nil
|
|
|
+ var formColor: NSColor = NSColor.black
|
|
|
+
|
|
|
+ if formMode == .radioButton {
|
|
|
+ formFont = defaults.font(forNameKey: SKAnnotationRadioButtonWidgetFontNameKey,
|
|
|
+ sizeKey: SKAnnotationRadioButtonWidgetFontSizeKey)
|
|
|
+ formColor = defaults.color(forKey: SKAnnotationRadioButtonWidgetFontColorKey) ?? NSColor.black
|
|
|
+ } else if formMode == .checkBox {
|
|
|
+ formFont = defaults.font(forNameKey: SKAnnotationCheckBoxWidgetFontNameKey,
|
|
|
+ sizeKey: SKAnnotationCheckBoxWidgetFontSizeKey)
|
|
|
+ formColor = defaults.color(forKey: SKAnnotationCheckBoxWidgetFontColorKey) ?? NSColor.black
|
|
|
+ } else {
|
|
|
+ formFont = NSFont.systemFont(ofSize: 9.0)
|
|
|
+ }
|
|
|
+
|
|
|
+ if formFont == nil {
|
|
|
+ formFont = NSFont.systemFont(ofSize: 12.0)
|
|
|
+ }
|
|
|
+
|
|
|
+ annotationFreeText?.font = formFont!
|
|
|
+ annotationFreeText?.fontColor = formColor
|
|
|
+ annotationFreeText?.contents = NSLocalizedString("Text", comment: "")
|
|
|
+ }
|
|
|
+
|
|
|
+ reloadData()
|
|
|
+ }
|
|
|
+
|
|
|
+ override func viewDidAppear() {
|
|
|
+ super.viewDidAppear()
|
|
|
+
|
|
|
+ let showConvertDetails = KMPropertiesViewPopController.showChangeColorDetails()
|
|
|
+ if showConvertDetails, self.view.window?.windowController?.document?.fileURL != nil {
|
|
|
+ KMPropertiesViewPopController.defaultManager().showChangeColorDetailsView(self.textColorPickerView.firstButton)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func reloadData() {
|
|
|
+ var opacity: CGFloat = 1
|
|
|
+ if let color = annotation!.backgroundColor {
|
|
|
+ color.usingColorSpaceName(.calibratedRGB)?.getRed(nil, green:nil, blue: nil, alpha: &opacity)
|
|
|
+ }
|
|
|
+
|
|
|
+ annotationFont = annotation?.font
|
|
|
+ if let buttonWidget = annotation {
|
|
|
+ backgroundColorPickerView.color = buttonWidget.backgroundColor
|
|
|
+ }
|
|
|
+
|
|
|
+ let fontManager = NSFontManager.shared
|
|
|
+ fontManager.target = self
|
|
|
+ fontManager.action = #selector(changeFont(_:))
|
|
|
+
|
|
|
+ if annotations.count > 1 {
|
|
|
+ appearanceAddTextBtn.isEnabled = false
|
|
|
+ }
|
|
|
+
|
|
|
+ if let annotationFreeText = annotationFreeText {
|
|
|
+ if let fontColor = annotationFreeText.fontColor {
|
|
|
+ textColorPickerView.color = fontColor
|
|
|
+ }
|
|
|
+
|
|
|
+ fontSizeComboBox.stringValue = "\(annotationFreeText.font.pointSize) pt"
|
|
|
+ DispatchQueue.global(qos: .default).async {
|
|
|
+ let fonts = NSFontManager.shared.availableFontFamilies
|
|
|
+ var selectedIndex = 0
|
|
|
+ let family = annotationFreeText.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String
|
|
|
+ let style = annotationFreeText.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String
|
|
|
+
|
|
|
+ var menuItems: [NSMenuItem] = []
|
|
|
+
|
|
|
+ for (index, fontName) in fonts.enumerated() {
|
|
|
+ if let font = NSFont(name: fontName, size: 12.0) {
|
|
|
+ let attrited = [NSAttributedString.Key.font: font]
|
|
|
+ let string = NSAttributedString(string: fontName, attributes: attrited)
|
|
|
+ let item = NSMenuItem()
|
|
|
+ item.attributedTitle = string
|
|
|
+ menuItems.append(item)
|
|
|
+
|
|
|
+ if annotationFreeText.font.fontName == font.fontName {
|
|
|
+ selectedIndex = index
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.main.async { [self] in
|
|
|
+ fontPopUpButton.menu?.items = menuItems
|
|
|
+ fontPopUpButton.selectItem(at: selectedIndex)
|
|
|
+ let selectedStyleIndex = setFontStyle(fontName: family!, currentStyle: style)
|
|
|
+ fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ if appearanceAddTextBtn.state == .on {
|
|
|
+ switch self.formMode {
|
|
|
+ case .radioButton:
|
|
|
+ userDefaults.set(true, forKey: SKAnnotationRadioButtonWidgetAddTextKey)
|
|
|
+ case .checkBox:
|
|
|
+ userDefaults.set(true, forKey: SKAnnotationCheckBoxWidgetAddTextKey)
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ switch self.formMode {
|
|
|
+ case .radioButton:
|
|
|
+ userDefaults.set(false, forKey: SKAnnotationRadioButtonWidgetAddTextKey)
|
|
|
+ case .checkBox:
|
|
|
+ userDefaults.set(false, forKey: SKAnnotationCheckBoxWidgetAddTextKey)
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Set & Get
|
|
|
+ var formMode: CAnnotationType {
|
|
|
+ get {
|
|
|
+ return _formMode
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _formMode = newValue
|
|
|
+
|
|
|
+ isFromMode = true
|
|
|
+
|
|
|
+ let defaults = UserDefaults.standard
|
|
|
+ var annotation: CPDFButtonWidgetAnnotation?
|
|
|
+ let bounds = NSMakeRect(0, 0, 60, 25)
|
|
|
+ var backgroundColor: NSColor?
|
|
|
+
|
|
|
+ if formMode == .checkBox {
|
|
|
+ annotation = CPDFButtonWidgetAnnotation(PDFListViewNoteWith: pdfView!.document!)
|
|
|
+ backgroundColor = defaults.color(forKey: SKAnnotationCheckBoxWidgetBackgroundColorKey)
|
|
|
+ } else if formMode == .radioButton {
|
|
|
+ annotation = CPDFButtonWidgetAnnotation(PDFListViewNoteWith: pdfView!.document!, controlType: .radioButtonControl)
|
|
|
+ backgroundColor = defaults.color(forKey: SKAnnotationRadioButtonWidgetBackgroundColorKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ annotation?.backgroundColor = backgroundColor ?? NSColor.clear
|
|
|
+
|
|
|
+ if let annotation = annotation {
|
|
|
+ annotations = [annotation]
|
|
|
+ }
|
|
|
+
|
|
|
+ self.annotation = annotation
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var annotations: [CPDFButtonWidgetAnnotation] {
|
|
|
+ get {
|
|
|
+ return _annotations
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _annotations = newValue
|
|
|
+ annotation = _annotations.first
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func updateAnnotationMode() {
|
|
|
+ let defaults = UserDefaults.standard
|
|
|
+ let annotation = self.annotation
|
|
|
+ let annotationMode = self.formMode
|
|
|
+
|
|
|
+ if annotationMode == .radioButton {
|
|
|
+ defaults.setColor(annotation!.backgroundColor!, forKey: SKAnnotationRadioButtonWidgetBackgroundColorKey)
|
|
|
+ } else if annotationMode == .checkBox {
|
|
|
+ defaults.setColor(annotation!.backgroundColor!, forKey: SKAnnotationCheckBoxWidgetBackgroundColorKey)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Private Method
|
|
|
+
|
|
|
+ func setFontStyle(fontName: String, currentStyle style: String?) -> UInt {
|
|
|
+ var selectIndex: UInt = 0
|
|
|
+ let menu = NSMenu()
|
|
|
+ if let fontFamily = NSFontManager.shared.availableMembers(ofFontFamily: fontName) {
|
|
|
+ for (index, array) in fontFamily.enumerated() {
|
|
|
+ let styleName = array[1] as! String
|
|
|
+ if style == styleName {
|
|
|
+ selectIndex = UInt(index)
|
|
|
+ }
|
|
|
+
|
|
|
+ let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [
|
|
|
+ NSFontDescriptor.AttributeName.family: fontName,
|
|
|
+ NSFontDescriptor.AttributeName.face: styleName
|
|
|
+ ])
|
|
|
+ if let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0) {
|
|
|
+ let attrited: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font]
|
|
|
+ let string = NSAttributedString(string: styleName, attributes: attrited)
|
|
|
+ let item = NSMenuItem()
|
|
|
+ item.attributedTitle = string
|
|
|
+ menu.addItem(item)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if style == nil {
|
|
|
+ selectIndex = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ fontStylePopUpButton.menu = menu
|
|
|
+
|
|
|
+ return selectIndex
|
|
|
+ }
|
|
|
+
|
|
|
+ func colorWithCGColor(_ CGColor: CGColor?) -> NSColor? {
|
|
|
+ guard let cgColor = CGColor else { return nil }
|
|
|
+ return NSColor(cgColor: cgColor)
|
|
|
+ }
|
|
|
+
|
|
|
+ private func updateAnnotation() {
|
|
|
+ if annotationModel?.annotation != nil {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: UI Action
|
|
|
+
|
|
|
+ @IBAction func backgroundColorViewAction(_ sender: Any) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ tAnnotation.updateAppearanceStream()
|
|
|
+ tAnnotation.backgroundColor = backgroundColorPickerView.color
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func textColorPickerViewAction(_ sender: Any) {
|
|
|
+ if self.annotationFreeText == nil {
|
|
|
+ var fieldString = annotation!.fieldName
|
|
|
+ if annotation?.controlType() == .radioButtonControl,
|
|
|
+ #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ if let page = annotation?.page {
|
|
|
+ for annotation in page.annotations {
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+ if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation,
|
|
|
+ freeTextAnnotation.userName() == annotationField {
|
|
|
+ self.annotationFreeText = freeTextAnnotation
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ if formMode == .radioButton {
|
|
|
+ userDefaults.setColor(textColorPickerView.color!, forKey: SKAnnotationRadioButtonWidgetFontColorKey)
|
|
|
+ } else if formMode == .checkBox {
|
|
|
+ userDefaults.setColor(textColorPickerView.color!, forKey: SKAnnotationCheckBoxWidgetFontColorKey)
|
|
|
+ }
|
|
|
+ self.annotationFreeText?.fontColor = self.textColorPickerView.color
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func addTextButtonAction(_ sender: NSButton) {
|
|
|
+ guard let PDFView = pdfView else { return }
|
|
|
+
|
|
|
+ let sud = UserDefaults.standard
|
|
|
+
|
|
|
+ if sender.state == .on {
|
|
|
+ self.fontView.isHidden = false
|
|
|
+
|
|
|
+ if !annotation!.fieldName().hasPrefix("button") {
|
|
|
+ var fieldString = annotation!.fieldName
|
|
|
+ if annotation!.controlType() == .radioButtonControl, #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+// if PDFView.addWidgetRelatedAnnotationFreeText(annotationField) {
|
|
|
+// reloadData()
|
|
|
+//
|
|
|
+// for annotation in PDFView.currentSelectionPage().annotations {
|
|
|
+// if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation,
|
|
|
+// annotationField == freeTextAnnotation.userName() {
|
|
|
+// self.annotationFreeText = freeTextAnnotation
|
|
|
+// break
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ let annotationMode = self.formMode
|
|
|
+ var formFontColor: NSColor = .black
|
|
|
+ var formFontSize: CGFloat = 9.0
|
|
|
+ var formFontName: String? = nil
|
|
|
+
|
|
|
+ if annotationMode == .radioButton {
|
|
|
+ formFontColor = sud.color(forKey: SKAnnotationRadioButtonWidgetFontColorKey) ?? .black
|
|
|
+ formFontSize = CGFloat(sud.float(forKey: SKAnnotationRadioButtonWidgetFontSizeKey))
|
|
|
+ formFontName = sud.string(forKey: SKAnnotationRadioButtonWidgetFontNameKey)
|
|
|
+ } else if annotationMode == .checkBox {
|
|
|
+ formFontColor = sud.color(forKey: SKAnnotationCheckBoxWidgetFontColorKey) ?? .black
|
|
|
+ formFontSize = CGFloat(sud.float(forKey: SKAnnotationCheckBoxWidgetFontSizeKey))
|
|
|
+ formFontName = sud.string(forKey: SKAnnotationCheckBoxWidgetFontNameKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ if formFontColor == nil {
|
|
|
+ formFontColor = .black
|
|
|
+ }
|
|
|
+
|
|
|
+ textColorPickerView.color = formFontColor
|
|
|
+
|
|
|
+ if formFontSize == 0.0 {
|
|
|
+ formFontSize = 9.0
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.global(qos: .default).async {
|
|
|
+ let fonts = NSFontManager.shared.availableFontFamilies
|
|
|
+ let menu = NSMenu()
|
|
|
+ var selectedIndex = 0
|
|
|
+
|
|
|
+ if let family = self.annotationFreeText?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
|
|
|
+ let style = self.annotationFreeText?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
|
|
|
+
|
|
|
+ for (index, fontName) in fonts.enumerated() {
|
|
|
+ if let font = NSFont(name: fontName, size: 12.0) {
|
|
|
+ let attributes: [NSAttributedString.Key: Any] = [.font: font]
|
|
|
+ let string = NSAttributedString(string: fontName, attributes: attributes)
|
|
|
+
|
|
|
+ let item = NSMenuItem()
|
|
|
+ item.attributedTitle = string
|
|
|
+ menu.addItem(item)
|
|
|
+
|
|
|
+ if formFontName == font.fontName {
|
|
|
+ selectedIndex = index
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.fontPopUpButton.menu = menu
|
|
|
+ self.fontPopUpButton.selectItem(at: selectedIndex)
|
|
|
+
|
|
|
+ if let family = self.annotationFreeText?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
|
|
|
+ let style = self.annotationFreeText?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
|
|
|
+ let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
|
|
|
+ self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if annotationMode == .radioButton {
|
|
|
+ sud.set(true, forKey: SKAnnotationRadioButtonWidgetAddTextKey)
|
|
|
+ } else if annotationMode == .checkBox {
|
|
|
+ sud.set(true, forKey: SKAnnotationCheckBoxWidgetAddTextKey)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fontView.isHidden = true
|
|
|
+
|
|
|
+ var fieldString = annotation!.fieldName
|
|
|
+ if annotation?.controlType() == .radioButtonControl, #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+// PDFView.removeWidgetRelatedAnnotationFreeText(annotationField)
|
|
|
+
|
|
|
+ let annotationMode = self.formMode
|
|
|
+ if annotationMode == .radioButton {
|
|
|
+ sud.set(false, forKey: SKAnnotationRadioButtonWidgetAddTextKey)
|
|
|
+ } else if annotationMode == .checkBox {
|
|
|
+ sud.set(false, forKey: SKAnnotationCheckBoxWidgetAddTextKey)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
|
|
|
+ guard let familyString = fontPopUpButton.selectedItem?.attributedTitle?.string else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
|
|
|
+ if let styleString = fontStylePopUpButton.selectedItem?.title {
|
|
|
+ fontStylePopUpButton.selectItem(at: Int(selectIndex))
|
|
|
+
|
|
|
+ if annotationFreeText == nil {
|
|
|
+ if let page = annotation?.page {
|
|
|
+ var fieldString = annotation!.fieldName
|
|
|
+ if annotation?.controlType() == .radioButtonControl,
|
|
|
+ #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+ for tAnnotation in page.annotations {
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+ if let tAnnotation = tAnnotation as? CPDFFreeTextAnnotation,
|
|
|
+ tAnnotation.userName() == annotationField {
|
|
|
+ annotationFreeText = tAnnotation
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ if formMode == .radioButton {
|
|
|
+ userDefaults.set(familyString, forKey: SKAnnotationRadioButtonWidgetFontNameKey)
|
|
|
+ } else if formMode == .checkBox {
|
|
|
+ userDefaults.set(familyString, forKey: SKAnnotationCheckBoxWidgetFontNameKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ if let fontSize = annotationFreeText!.font!.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? NSNumber {
|
|
|
+ let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [
|
|
|
+ NSFontDescriptor.AttributeName.family: familyString,
|
|
|
+ NSFontDescriptor.AttributeName.face: styleString
|
|
|
+ ])
|
|
|
+ if let font = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize.floatValue)) {
|
|
|
+ annotationFreeText?.font = font
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontSizeComboBox(_ sender: NSComboBox) {
|
|
|
+ if annotationFreeText == nil {
|
|
|
+ var fieldString = annotation!.fieldName
|
|
|
+ if annotation?.controlType() == .radioButtonControl,
|
|
|
+ #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ if let page = annotation?.page {
|
|
|
+ for tAnnotation in page.annotations {
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+ if let tFreeTextAnnotation = tAnnotation as? CPDFFreeTextAnnotation,
|
|
|
+ tFreeTextAnnotation.userName() == annotationField {
|
|
|
+ annotationFreeText = tFreeTextAnnotation
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ var fontSizeKey: String = ""
|
|
|
+ if formMode == .radioButton {
|
|
|
+ fontSizeKey = SKAnnotationRadioButtonWidgetFontSizeKey
|
|
|
+ } else if formMode == .checkBox {
|
|
|
+ fontSizeKey = SKAnnotationCheckBoxWidgetFontSizeKey
|
|
|
+ }
|
|
|
+
|
|
|
+ userDefaults.set(Float(sender.floatValue), forKey: fontSizeKey)
|
|
|
+
|
|
|
+ if let font = annotationFreeText?.font {
|
|
|
+ let newFont = NSFont(name: font.fontName, size: CGFloat(sender.floatValue))
|
|
|
+ annotationFreeText?.font = newFont
|
|
|
+ }
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
|
|
|
+ guard let styleString = self.fontStylePopUpButton.selectedItem?.title else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if self.annotationFreeText == nil {
|
|
|
+ var fieldString = annotation!.fieldName
|
|
|
+ if self.annotation?.controlType() == .radioButtonControl, #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation!.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ if let page = annotation!.page {
|
|
|
+ for annotation in page.annotations {
|
|
|
+ let annotationField = "\(fieldString)_AddText"
|
|
|
+ if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation, freeTextAnnotation.userName() == annotationField {
|
|
|
+ self.annotationFreeText = freeTextAnnotation
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if let font = self.annotationFreeText?.font,
|
|
|
+ let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size),
|
|
|
+ let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) {
|
|
|
+ let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
|
|
|
+ if let updatedFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat((fontSize as! NSString).floatValue)) {
|
|
|
+// self.annotationFreeText?.removeAllAppearanceStreams()
|
|
|
+ self.annotationFreeText?.font = updatedFont
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontButtonAction(_ sender: NSButton) {
|
|
|
+ weak var weakSelf = self
|
|
|
+// let fontWindowController = KMAnnotationFontWindowController.sharedAnnotationFont()
|
|
|
+// let window = fontWindowController.window
|
|
|
+// fontWindowController.annotations = [self.annotationFreeText] ?? []
|
|
|
+//
|
|
|
+// fontWindowController.annotationAlignCallback = { selectedCount in
|
|
|
+// weakSelf?.annotationFreeText.removeAllAppearanceStreams()
|
|
|
+//
|
|
|
+// switch selectedCount {
|
|
|
+// case 0:
|
|
|
+// weakSelf?.annotationFreeText.setAlignment(.left)
|
|
|
+// case 2:
|
|
|
+// weakSelf?.annotationFreeText.setAlignment(.center)
|
|
|
+// case 1:
|
|
|
+// weakSelf?.annotationFreeText.setAlignment(.right)
|
|
|
+// case 3:
|
|
|
+// weakSelf?.annotationFreeText.setAlignment(.justified)
|
|
|
+// default:
|
|
|
+// break
|
|
|
+// }
|
|
|
+//
|
|
|
+// weakSelf?.updateAnnotation()
|
|
|
+// }
|
|
|
+
|
|
|
+// fontWindowController.annotationCallback = { annotation in
|
|
|
+// weakSelf?.annotationFreeText.fontColor = annotation.fontColor
|
|
|
+// weakSelf?.annotationFreeText.font = annotation.font
|
|
|
+// weakSelf?.reloadData()
|
|
|
+// weakSelf?.updateAnnotation()
|
|
|
+// }
|
|
|
+
|
|
|
+// window?.orderFront(sender)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func changeFont(_ sender: Any) {
|
|
|
+ if annotationFreeText == nil {
|
|
|
+ var fieldString = annotation?.fieldName
|
|
|
+ if annotation?.controlType() == .radioButtonControl, #available(macOS 10.13, *) {
|
|
|
+ fieldString = annotation?.buttonWidgetStateString
|
|
|
+ }
|
|
|
+
|
|
|
+ if let page = annotation?.page {
|
|
|
+ for pageAnnotation in page.annotations {
|
|
|
+ if let freeTextAnnotation = pageAnnotation as? CPDFFreeTextAnnotation,
|
|
|
+ freeTextAnnotation.userName() == "\(fieldString)_AddText" {
|
|
|
+ annotationFreeText = freeTextAnnotation
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+
|
|
|
+ if formMode == .radioButton {
|
|
|
+ userDefaults.set(annotationFont?.fontName, forKey: SKAnnotationRadioButtonWidgetFontNameKey)
|
|
|
+ userDefaults.set(annotationFont?.pointSize, forKey: SKAnnotationRadioButtonWidgetFontSizeKey)
|
|
|
+ } else if formMode == .checkBox {
|
|
|
+ userDefaults.set(annotationFont?.fontName, forKey: SKAnnotationCheckBoxWidgetFontNameKey)
|
|
|
+ userDefaults.set(annotationFont?.pointSize, forKey: SKAnnotationCheckBoxWidgetFontSizeKey)
|
|
|
+ }
|
|
|
+
|
|
|
+// annotationFont = (sender as AnyObject).convert(annotationFont!)
|
|
|
+ annotationFreeText?.font = NSFont(name: annotationFont!.fontName, size: annotationFont!.pointSize)
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+}
|