// // KMPDFAnnotationButtonWidgetSub.swift // PDF Reader Pro // // Created by wanjun on 2024/1/31. // import Cocoa class KMPDFAnnotationButtonWidgetSub: CPDFButtonWidgetAnnotation { var formType: CAnnotationType = .unkown override func draw(with box: CPDFDisplayBox, in context: CGContext!) { guard shouldDisplay() else { return } if controlType() == .checkBoxControl { if hasAppearanceStream() { super.draw(with: box, in: context) } else { super.draw(with: box, in: context) NSGraphicsContext.saveGraphicsState() transformContext(for: page, displayBox: box) context.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1.0) context.setLineWidth(1.0) context.addRect(CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)) context.strokePath() NSGraphicsContext.restoreGraphicsState() } } else if controlType() == .pushButtonControl { if hasAppearanceStream() { super.draw(with: box, in: context) } else { NSGraphicsContext.saveGraphicsState() let stabilizeHeight: CGFloat = 25 let actualRect = NSRect(x: bounds.origin.x, y: bounds.minY + (bounds.height - stabilizeHeight) / 2, width: bounds.size.width, height: stabilizeHeight) let bezierPath = NSBezierPath(roundedRect: actualRect, xRadius: 2, yRadius: 2) NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1).set() bezierPath.fill() var attributes = [NSAttributedString.Key: Any]() if let font = self.font { attributes[.font] = font } if let fontColor = self.fontColor { attributes[.foregroundColor] = fontColor } let stringRect = caption()!.boundingRect(with: CGSize(width: actualRect.size.width, height: actualRect.size.height), options: .usesLineFragmentOrigin, attributes: attributes) caption().draw(in: CGRect(x: (actualRect.size.width - stringRect.size.width)/2 + actualRect.origin.x, y: (actualRect.size.height - stringRect.size.height)/2 + actualRect.origin.y, width: stringRect.size.width, height: stringRect.size.height), withAttributes: attributes) NSGraphicsContext.restoreGraphicsState() } } else { super.draw(with: box, in: context) } } func transformContext(for page: CPDFPage, displayBox box: CPDFDisplayBox) { var transform = NSAffineTransform() let boxRect = page.bounds(for: box) let rotation = page.rotation // Handle rotation. switch rotation { case 90: transform.rotate(byDegrees: -90) transform.translateX(by: -boxRect.size.width, yBy: 0.0) case 180: transform.rotate(byDegrees: 180) transform.translateX(by: -boxRect.size.width, yBy: -boxRect.size.height) case 270: transform.rotate(byDegrees: 90) transform.translateX(by: 0.0, yBy: -boxRect.size.height) default: break } // Origin. transform.translateX(by: -boxRect.origin.x, yBy: -boxRect.origin.y) // Concatenate. transform.concat() } func keysForValuesToObserveForUndo() -> Set { return [] } }