KMPDFAnnotationButtonWidgetSub.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // KMPDFAnnotationButtonWidgetSub.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/1/31.
  6. //
  7. import Cocoa
  8. class KMPDFAnnotationButtonWidgetSub: CPDFButtonWidgetAnnotation {
  9. var formType: CAnnotationType = .unkown
  10. override func draw(with box: CPDFDisplayBox, in context: CGContext!) {
  11. guard shouldDisplay() else {
  12. return
  13. }
  14. if controlType() == .checkBoxControl {
  15. if hasAppearanceStream() {
  16. super.draw(with: box, in: context)
  17. } else {
  18. super.draw(with: box, in: context)
  19. NSGraphicsContext.saveGraphicsState()
  20. transformContext(for: page, displayBox: box)
  21. context.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1.0)
  22. context.setLineWidth(1.0)
  23. context.addRect(CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height))
  24. context.strokePath()
  25. NSGraphicsContext.restoreGraphicsState()
  26. }
  27. } else if controlType() == .pushButtonControl {
  28. if hasAppearanceStream() {
  29. super.draw(with: box, in: context)
  30. } else {
  31. NSGraphicsContext.saveGraphicsState()
  32. let stabilizeHeight: CGFloat = 25
  33. let actualRect = NSRect(x: bounds.origin.x, y: bounds.minY + (bounds.height - stabilizeHeight) / 2, width: bounds.size.width, height: stabilizeHeight)
  34. let bezierPath = NSBezierPath(roundedRect: actualRect, xRadius: 2, yRadius: 2)
  35. NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1).set()
  36. bezierPath.fill()
  37. var attributes = [NSAttributedString.Key: Any]()
  38. if let font = self.font {
  39. attributes[.font] = font
  40. }
  41. if let fontColor = self.fontColor {
  42. attributes[.foregroundColor] = fontColor
  43. }
  44. let stringRect = caption()!.boundingRect(with: CGSize(width: actualRect.size.width, height: actualRect.size.height), options: .usesLineFragmentOrigin, attributes: attributes)
  45. caption().draw(in: CGRect(x: (actualRect.size.width - stringRect.size.width)/2 + actualRect.origin.x,
  46. y: (actualRect.size.height - stringRect.size.height)/2 + actualRect.origin.y,
  47. width: stringRect.size.width, height: stringRect.size.height), withAttributes: attributes)
  48. NSGraphicsContext.restoreGraphicsState()
  49. }
  50. } else {
  51. super.draw(with: box, in: context)
  52. }
  53. }
  54. func transformContext(for page: CPDFPage, displayBox box: CPDFDisplayBox) {
  55. var transform = NSAffineTransform()
  56. let boxRect = page.bounds(for: box)
  57. let rotation = page.rotation
  58. // Handle rotation.
  59. switch rotation {
  60. case 90:
  61. transform.rotate(byDegrees: -90)
  62. transform.translateX(by: -boxRect.size.width, yBy: 0.0)
  63. case 180:
  64. transform.rotate(byDegrees: 180)
  65. transform.translateX(by: -boxRect.size.width, yBy: -boxRect.size.height)
  66. case 270:
  67. transform.rotate(byDegrees: 90)
  68. transform.translateX(by: 0.0, yBy: -boxRect.size.height)
  69. default:
  70. break
  71. }
  72. // Origin.
  73. transform.translateX(by: -boxRect.origin.x, yBy: -boxRect.origin.y)
  74. // Concatenate.
  75. transform.concat()
  76. }
  77. func keysForValuesToObserveForUndo() -> Set<String> {
  78. return []
  79. }
  80. }