// // KMMeasureController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/12/7. // import Cocoa import KMComponentLibrary class KMMeasureController: NSViewController { @IBOutlet var typeSegmented: ComponentSegmented! @IBOutlet var infoBGView: NSView! @IBOutlet var infoTopConst: NSLayoutConstraint! //Color @IBOutlet var colorBGView: NSView! @IBOutlet var colorLabel: NSTextField! @IBOutlet var fontColorGroup: ComponentCColorGroup! @IBOutlet var ColorGroup: ComponentCColorGroup! @IBOutlet var fillColorGroup: ComponentCColorGroup! @IBOutlet var fillColorImage: NSImageView! @IBOutlet var opacitySlider: ComponentSlider! @IBOutlet var opacitySelect: ComponentSelect! @IBOutlet var colorBGViewHeightConst: NSLayoutConstraint! @IBOutlet var opacityViewTopConst: NSLayoutConstraint! //Line @IBOutlet var lineBGView: NSView! @IBOutlet var lineLabel: NSTextField! @IBOutlet var lineTypeSelector: ComponentCSelectorGroup! @IBOutlet var lineWidthSlider: ComponentSlider! @IBOutlet var lineWidthSelect: ComponentSelect! @IBOutlet var linedashInfoView: NSView! @IBOutlet var lineDashSlider: ComponentSlider! @IBOutlet var lineDashSelect: ComponentSelect! @IBOutlet var lineBGViewHeightConst: NSLayoutConstraint! @IBOutlet var arrowTypeView: NSView! @IBOutlet var arrowTypeTopConst: NSLayoutConstraint! @IBOutlet var arrow_leftTypeSelect: ComponentSelect! @IBOutlet var arrow_RightTypeSelect: ComponentSelect! //Font @IBOutlet var fontBGView: NSView! @IBOutlet var fontLabel: NSTextField! @IBOutlet var fontNameSelect: ComponentSelect! @IBOutlet var fontStyleSelect: ComponentSelect! @IBOutlet var fontSizeSelect: ComponentSelect! //Display Label @IBOutlet var displayBGView: NSView! @IBOutlet var displayLabel: NSTextField! @IBOutlet var displayLengthCheckBox: ComponentCheckBox! @IBOutlet var displayArea_CheckBox: ComponentCheckBox! private let line_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_line_gray"), iconImage_active: NSImage(named: "measure_line")) private let multiLine_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_multiLine_gray"), iconImage_active: NSImage(named: "measure_multiLine")) private let polygon_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_polygon_gray"), iconImage_active: NSImage(named: "measure_polygon")) private let rectangle_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_rectangle_gray"), iconImage_active: NSImage(named: "measure_rectangle")) private var familyNames = CPDFFont.familyNames private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid")) private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash")) private var annotations: [CPDFAnnotation] = [] var annotationType: CAnnotationType = .measureLine var pdfView: CPDFListView? var viewManager: KMPDFViewManager? //MARK: - func override func viewDidAppear() { super.viewDidAppear() opacitySlider.reloadData() lineWidthSlider.reloadData() lineDashSlider.reloadData() } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. annotationType = CPDFMeasureDefaultInfo.default_measureType() setupProperty() reloadData() } func setupProperty() { if true { var itemArr: [ComponentSegmentedProperty] = [] itemArr.append(line_Property) itemArr.append(multiLine_Property) itemArr.append(polygon_Property) itemArr.append(rectangle_Property) typeSegmented.updateItemProperty(itemArr) } typeSegmented.delegate = self //Color colorLabel.stringValue = KMLocalizedString("Color") colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") fontColorGroup.delegate = self ColorGroup.delegate = self fillColorGroup.delegate = self opacitySlider.properties = ComponentSliderProperty(size: .m, percent: 1) opacitySlider.delegate = self opacitySelect.properties = ComponentSelectProperties(size: .s, state: .normal, creatable: true, text: "100%", textUnit: "%", regexString: "0123456789%") if true { var opacityItems: [ComponentMenuitemProperty] = [] for string in ["25%", "50%", "75%", "100%"] { let item = ComponentMenuitemProperty(type: .normal, text: string) opacityItems.append(item) } opacitySelect.updateMenuItemsArr(opacityItems) } opacitySelect.delegate = self //Line lineLabel.stringValue = KMLocalizedString("Line") lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") lineTypeSelector.updateItemProperty([solidProperty, dashProperty]) lineTypeSelector.delegate = self lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1) lineWidthSlider.delegate = self lineWidthSelect.properties = ComponentSelectProperties(size: .s, state: .normal, creatable: true, text: "2", textUnit: " pt", regexString: "0123456789 pt") if true { var opacityItems: [ComponentMenuitemProperty] = [] for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] { let item = ComponentMenuitemProperty(type: .normal, text: string) opacityItems.append(item) } lineWidthSelect.updateMenuItemsArr(opacityItems) } lineWidthSelect.delegate = self lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1) lineDashSlider.delegate = self lineDashSelect.properties = ComponentSelectProperties(size: .s, state: .normal, creatable: true, text: "2", textUnit: " pt", regexString: "0123456789 pt") if true { var opacityItems: [ComponentMenuitemProperty] = [] for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] { let item = ComponentMenuitemProperty(type: .normal, text: string) opacityItems.append(item) } lineDashSelect.updateMenuItemsArr(opacityItems) } lineDashSelect.delegate = self //ArrowType arrow_leftTypeSelect.properties = ComponentSelectProperties(size: .s, state: .normal, creatable: false) if true { let stringItems = ["None", "Arrow Open", "Arrow Close", "Square", "Oval", "Diamond", "Butt", "Slash", "R Open Arrow", "R Close Arrow"] let imageNames = ["arrowType_line", "arrowType_left_Arrow_open", "arrowType_left_Arrow_close", "arrowType_left_Square", "arrowType_left_Oval", "arrowType_left_Diamond", "arrowType_left_Butt", "arrowType_left_Slash", "arrowType_left_Open_arrow", "arrowType_left_Close_arrow"] var opacityItems: [ComponentMenuitemProperty] = [] for i in 0...stringItems.count-1 { let string = stringItems[i] let imageName = imageNames[i] let item = ComponentMenuitemProperty(type: .normal, lefticon: NSImage(named: imageName), text: string, identifier: string) opacityItems.append(item) } arrow_leftTypeSelect.updateMenuItemsArr(opacityItems) } arrow_leftTypeSelect.delegate = self arrow_RightTypeSelect.properties = ComponentSelectProperties(size: .s, state: .normal, creatable: false) if true { let stringItems = ["None", "Arrow Open", "Arrow Close", "Square", "Oval", "Diamond", "Butt", "Slash", "R Open Arrow", "R Close Arrow"] let imageNames = ["arrowType_line", "arrowType_right_Arrow_open", "arrowType_right_Arrow_close", "arrowType_right_Square", "arrowType_right_Oval", "arrowType_right_Diamond", "arrowType_right_Butt", "arrowType_right_Slash", "arrowType_right_Open_arrow", "arrowType_right_Close_arrow"] var opacityItems: [ComponentMenuitemProperty] = [] for i in 0...stringItems.count-1 { let string = stringItems[i] let imageName = imageNames[i] let item = ComponentMenuitemProperty(type: .normal, lefticon: NSImage(named: imageName), text: string, identifier: string) opacityItems.append(item) } arrow_RightTypeSelect.updateMenuItemsArr(opacityItems) } arrow_RightTypeSelect.delegate = self //Font 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 //Display Label displayLabel.stringValue = KMLocalizedString("Display Label") displayLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") displayLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") displayLengthCheckBox.properties = ComponentCheckBoxProperty(size: .m, text: KMLocalizedString("Length")) displayLengthCheckBox.setTarget(self, action: #selector(checkBoxClicked(_:))) displayArea_CheckBox.properties = ComponentCheckBoxProperty(size: .m, text: KMLocalizedString("Area")) displayArea_CheckBox.setTarget(self, action: #selector(checkBoxClicked(_:))) } func reloadData() { guard let pdfView = self.pdfView else { return } self.annotations.removeAll() let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? [] for annotation in allAnnotations { if annotation is CPDFLineAnnotation { if let _ = (annotation as! CPDFLineAnnotation).measureInfo { annotations.append((annotation as! CPDFLineAnnotation)) } } else if annotation is CPDFPolylineAnnotation { annotations.append((annotation as! CPDFPolylineAnnotation)) } else if annotation is CPDFPolygonAnnotation { annotations.append((annotation as! CPDFPolygonAnnotation)) } } var firstAnnotation: CPDFAnnotation? if annotations.count > 0 { firstAnnotation = annotations.first } if viewManager?.subToolMode == .Measure { infoTopConst.constant = 56 typeSegmented.isHidden = false } else { infoTopConst.constant = 0 typeSegmented.isHidden = true } line_Property.active = false multiLine_Property.active = false polygon_Property.active = false rectangle_Property.active = false if annotationType == .measureLine { line_Property.active = true } else if annotationType == .measurePolyLine { multiLine_Property.active = true } else if annotationType == .measurePolyGon { polygon_Property.active = true } else if annotationType == .measureSquare { rectangle_Property.active = true } typeSegmented.reloadData() colorBGViewHeightConst.constant = 192 fillColorGroup.isHidden = false fillColorImage.isHidden = false opacityViewTopConst.constant = 48 displayBGView.isHidden = false arrowTypeView.isHidden = false if annotationType == .measureLine || annotationType == .measurePolyLine { colorBGViewHeightConst.constant = 152 fillColorGroup.isHidden = true fillColorImage.isHidden = true opacityViewTopConst.constant = 8 displayBGView.isHidden = true if annotationType == .measurePolyLine { arrowTypeView.isHidden = true } } else if annotationType == .measurePolyGon || annotationType == .measureSquare { arrowTypeView.isHidden = true } if true { let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.measureFontColors if colors.count > 4 { let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4]) fontColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty) } } if true { let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.measure_Border_Colors if colors.count > 4 { let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4]) ColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty) } } if true { let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.measure_Fill_Colors if colors.count > 4 { let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4]) fillColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty) } } //Color var fontColor: NSColor? var borderColor: NSColor? var fillColor: NSColor? var opacity: CGFloat = 0 //Line var lineBorderStyle: CPDFBorderStyle? var lineBorderWidth: CGFloat? var dashPattern: CGFloat? var startLineStyle: CPDFLineStyle = .none var endLineStyle: CPDFLineStyle = .none //Font var fontNameString: String? var fontStyleString: String? var fontSize: CGFloat? //Display Label var displayLength: Bool = false var displayArea: Bool = false if annotations.count == 0 { //Color fontColor = CPDFMeasureDefaultInfo.default_measure_FontColor(annotationType: self.annotationType) borderColor = CPDFMeasureDefaultInfo.default_measure_BorderColor(annotationType: self.annotationType) fillColor = CPDFMeasureDefaultInfo.default_measure_FillColor(annotationType: self.annotationType) opacity = CPDFMeasureDefaultInfo.default_measure_Opacity(annotationType: self.annotationType) //Line lineBorderStyle = CPDFMeasureDefaultInfo.default_measure_LineStyle(annotationType: self.annotationType) lineBorderWidth = CPDFMeasureDefaultInfo.default_measure_LineWidth(annotationType: self.annotationType) var dash = 1.0 for dashPattern in CPDFMeasureDefaultInfo.default_measure_DashPattern(annotationType: self.annotationType) { if let value = dashPattern as? CGFloat { dash = value break } } dashPattern = dash startLineStyle = CPDFMeasureDefaultInfo.default_measure_Arrow_StartLineStyle(annotationType: self.annotationType) endLineStyle = CPDFMeasureDefaultInfo.default_measure_Arrow_EndLineStyle(annotationType: self.annotationType) //Font fontNameString = CPDFMeasureDefaultInfo.default_measure_FontName(annotationType: self.annotationType) fontStyleString = CPDFMeasureDefaultInfo.default_measure_FontStyle(annotationType: self.annotationType) fontSize = CPDFMeasureDefaultInfo.default_measure_FontSize(annotationType: self.annotationType) displayLength = CPDFMeasureDefaultInfo.default_measure_DisplayLength(annotationType: self.annotationType) displayArea = CPDFMeasureDefaultInfo.default_measure_DisplayArea(annotationType: self.annotationType) } else if let annotation = firstAnnotation { if let lineAnnotation = annotation as? CPDFLineAnnotation, let _ = lineAnnotation.measureInfo { fontColor = lineAnnotation.fontColor borderColor = lineAnnotation.color opacity = lineAnnotation.opacity lineBorderStyle = lineAnnotation.borderStyle() lineBorderWidth = lineAnnotation.borderWidth var dash = 1.0 for dashPattern in lineAnnotation.dashPattern() { if let value = dashPattern as? CGFloat { dash = value break } } dashPattern = dash startLineStyle = lineAnnotation.startLineStyle endLineStyle = lineAnnotation.endLineStyle fontNameString = lineAnnotation.cFont.familyName fontStyleString = lineAnnotation.cFont.styleName fontSize = lineAnnotation.fontSize } else if let polylineAnnot = annotation as? CPDFPolylineAnnotation { fontColor = polylineAnnot.fontColor borderColor = polylineAnnot.color opacity = polylineAnnot.opacity lineBorderStyle = polylineAnnot.borderStyle() lineBorderWidth = polylineAnnot.borderWidth var dash = 1.0 for dashPattern in polylineAnnot.dashPattern() { if let value = dashPattern as? CGFloat { dash = value break } } dashPattern = dash fontNameString = polylineAnnot.cFont.familyName fontStyleString = polylineAnnot.cFont.styleName fontSize = polylineAnnot.fontSize } else if let polylineAnnot = annotation as? CPDFPolygonAnnotation { fontColor = polylineAnnot.fontColor borderColor = polylineAnnot.color fillColor = polylineAnnot.interiorColor opacity = polylineAnnot.opacity lineBorderStyle = polylineAnnot.borderStyle() lineBorderWidth = polylineAnnot.borderWidth var dash = 1.0 for dashPattern in polylineAnnot.dashPattern() { if let value = dashPattern as? CGFloat { dash = value break } } dashPattern = dash fontNameString = polylineAnnot.cFont.familyName fontStyleString = polylineAnnot.cFont.styleName fontSize = polylineAnnot.fontSize if let me = polylineAnnot.measureInfo { if me.captionType.contains(.length) { displayLength = true } if me.captionType.contains(.area) { displayArea = true } } } } //Color fontColorGroup.currentColor = fontColor fontColorGroup.refreshUI() ColorGroup.currentColor = borderColor ColorGroup.refreshUI() fillColorGroup.currentColor = fillColor fillColorGroup.refreshUI() opacitySlider.properties.percent = opacity opacitySlider.reloadData() opacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%") opacitySelect.reloadData() //Line dashProperty.state = .normal solidProperty.state = .normal if lineBorderStyle == .dashed { dashProperty.state = .pressed } else if lineBorderStyle == .solid { solidProperty.state = .pressed } lineTypeSelector.reloadData() var percent: CGFloat = 0 if let value = lineBorderWidth, value > 0 { percent = value/18 lineWidthSlider.properties.percent = percent lineWidthSelect.properties.text = String(format: "%.0f%@", value, " pt") } else { lineWidthSlider.properties.percent = 0 lineWidthSelect.properties.text = "0 pt" } lineWidthSlider.reloadData() lineWidthSelect.reloadData() linedashInfoView.isHidden = true arrowTypeTopConst.constant = 8 lineBGViewHeightConst.constant = 112 if arrowTypeView.isHidden { lineBGViewHeightConst.constant = 72 } if lineBorderStyle == .dashed { linedashInfoView.isHidden = false arrowTypeTopConst.constant = 48 lineBGViewHeightConst.constant = 152 if arrowTypeView.isHidden { lineBGViewHeightConst.constant = 112 } var dash = 1.0 if let value = dashPattern { dash = value } let percent: CGFloat = CGFloat(dash)/18 lineDashSlider.properties.percent = percent lineDashSlider.reloadData() lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt") lineDashSelect.reloadData() } arrow_leftTypeSelect.selectItemAtIndex(startLineStyle.rawValue) arrow_RightTypeSelect.selectItemAtIndex(endLineStyle.rawValue) //Font if let familyName = fontNameString, familyName != "-" { var styleNames = CPDFFont.fontNames(forFamilyName: familyName) var menuItemArr: [ComponentMenuitemProperty] = [] if styleNames.count == 0 { styleNames.append("Regular") } for string in styleNames { let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string) menuItemArr.append(item) } fontStyleSelect.updateMenuItemsArr(menuItemArr) } else { fontStyleSelect.updateMenuItemsArr([]) } if let value = fontNameString { fontNameSelect.properties.text = value } else { fontNameSelect.properties.text = "-" } fontNameSelect.reloadData() if let value = fontStyleString { fontStyleSelect.properties.text = value } else { fontStyleSelect.properties.text = "-" } fontStyleSelect.reloadData() if let value = fontSize { fontSizeSelect.properties.text = String(format: "%.0f", value) } else { fontSizeSelect.properties.text = "-" } fontSizeSelect.reloadData() displayLengthCheckBox.properties.checkboxType = displayLength ? .selected : .normal displayLengthCheckBox.reloadData() displayArea_CheckBox.properties.checkboxType = displayArea ? .selected : .normal displayArea_CheckBox.reloadData() } //MARK: - Action @objc func checkBoxClicked(_ sender: ComponentCheckBox) { if sender == displayLengthCheckBox || sender == displayArea_CheckBox { let isLength = displayLengthCheckBox.properties.checkboxType == .selected let isArea = displayArea_CheckBox.properties.checkboxType == .selected var captionType: CPDFCaptionType = CPDFCaptionType(rawValue: 0) if isLength && isArea { captionType = [.length, .area] } else if isLength { captionType = .length } else if isArea { captionType = .area } CPDFAnnotation.updateAnnotations(annotations, newcaptionType: captionType, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_CaptionType(annotationType, isLength, isArea) } } //MARK: - Mouse override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) view.window?.makeFirstResponder(nil) } } //MARK: - ComponentCColorDelegate extension KMMeasureController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { if view == fontColorGroup { CPDFAnnotation.updateAnnotations(annotations, newFontColor: color, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Color(annotationType: self.annotationType, color) } else if view == ColorGroup { CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_BorderColor(annotationType: self.annotationType, color) } else if view == fillColorGroup { CPDFAnnotation.updateAnnotations(annotations, newInteriorColor: color, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_FillColor(annotationType: self.annotationType, color) } reloadData() } } //MARK: - ComponentSliderDelegate extension KMMeasureController: ComponentSliderDelegate { func componentSliderDidUpdate(_ view: ComponentSlider) { if view == opacitySlider { let value = view.properties.percent CPDFAnnotation.updateAnnotations(annotations, newInteriorOpacity: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Opacity(annotationType: self.annotationType, value) } else if view == lineWidthSlider { let value = view.properties.percent * 18 CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_LineWidth(annotationType: self.annotationType, value) } else if view == lineDashSlider { let value = view.properties.percent * 18 CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_DashPattern(annotationType: self.annotationType, value) } reloadData() } } //MARK: - ComponentSelectDelegate extension KMMeasureController: ComponentSelectDelegate { func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) { if let result = text { if view == opacitySelect { let value = max(0, min(1, result.stringToCGFloat()/100)) CPDFAnnotation.updateAnnotations(annotations, newInteriorOpacity: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Opacity(annotationType: self.annotationType, value) } else if view == lineWidthSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_LineWidth(annotationType: self.annotationType, value) } else if view == lineDashSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_DashPattern(annotationType: self.annotationType, value) } else if view == fontSizeSelect { let size = result.stringToCGFloat() CPDFAnnotation.updateAnnotations(annotations, newFontSize: size, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_FontSize(self.annotationType, size) } reloadData() } } func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) { if var result = menuItemProperty?.text { if let textUnit = view?.properties.textUnit { result = result.stringByDeleteCharString(textUnit) } if view == opacitySelect { let value = max(0, min(1, result.stringToCGFloat()/100)) CPDFAnnotation.updateAnnotations(annotations, newInteriorOpacity: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Opacity(annotationType: self.annotationType, value) } else if view == lineWidthSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_LineWidth(annotationType: self.annotationType, value) } else if view == lineDashSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_DashPattern(annotationType: self.annotationType, value) } else if view == arrow_leftTypeSelect { let idx = view?.indexOfSelect() ?? 0 CPDFAnnotation.updateAnnotations(annotations, newStartLineStyle: CPDFLineStyle(rawValue: idx) ?? .none, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_StartLineStyle(self.annotationType, CPDFLineStyle(rawValue: idx) ?? .none) } else if view == arrow_RightTypeSelect { let idx = view?.indexOfSelect() ?? 0 CPDFAnnotation.updateAnnotations(annotations, newEndLineStyle: CPDFLineStyle(rawValue: idx) ?? .none, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_EndLineStyle(self.annotationType, CPDFLineStyle(rawValue: idx) ?? .none) }else if view == fontNameSelect { var styleName = "Regular" let styleNames = CPDFFont.fontNames(forFamilyName: result) if let first = styleNames.first { styleName = first } CPDFAnnotation.updateAnnotations(annotations, newCFont: CPDFFont(familyName: result, fontStyle: styleName), withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Font(self.annotationType, CPDFFont(familyName: result, fontStyle: styleName)) } else if view == fontStyleSelect { let familyName = fontNameSelect.properties.text ?? "Helvetica" CPDFAnnotation.updateAnnotations(annotations, newCFont: CPDFFont(familyName: familyName, fontStyle: result), withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Font(self.annotationType, CPDFFont(familyName: familyName, fontStyle: result)) } else if view == fontSizeSelect { let size = result.stringToCGFloat() CPDFAnnotation.updateAnnotations(annotations, newFontSize: size, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_FontSize(self.annotationType, size) } reloadData() } } } //MARK: - ComponentCSelectorGroupDelegate extension KMMeasureController: ComponentCSelectorGroupDelegate { func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) { if view == lineTypeSelector { if item.properties == solidProperty { CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .solid, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Style(annotationType: self.annotationType, .solid) } else if item.properties == dashProperty { CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .dashed, withPDFView: pdfView) CPDFMeasureDefaultInfo.update_default_measure_Style(annotationType: self.annotationType, .dashed) } } reloadData() } } //MARK: - ComponentSegmentedDelegate extension KMMeasureController: ComponentSegmentedDelegate { func componentSegmentedDidSelected(_ view: ComponentSegmented, item: ComponentSegmentedItem) { if item.properties == line_Property { annotationType = .measureLine } else if item.properties == multiLine_Property { annotationType = .measurePolyLine } else if item.properties == polygon_Property { annotationType = .measurePolyGon } else if item.properties == rectangle_Property { annotationType = .measureSquare } CPDFMeasureDefaultInfo.update_default_measureType(annotationType: annotationType) self.pdfView?.annotationType = annotationType reloadData() } }