CAnnotationManage.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // CAnnotationManage.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import ComPDFKit
  14. public class CAnnotationManage: NSObject {
  15. public weak var pdfListView:CPDFListView?
  16. public var annotation:CPDFAnnotation?
  17. public var annotStyle:CAnnotStyle?
  18. public init(pdfListView: CPDFListView) {
  19. self.pdfListView = pdfListView
  20. super.init()
  21. }
  22. public func setAnnotStyle(from annotations: [CPDFAnnotation]) {
  23. annotStyle = CAnnotStyle(annotionMode: .CPDFViewAnnotationModenone, annotations: annotations)
  24. }
  25. public func refreshPage(with annotations: [CPDFAnnotation]) {
  26. var pages = [CPDFPage]()
  27. for annotation in annotations {
  28. if annotation.page != nil {
  29. let page:CPDFPage = annotation.page!
  30. if !pages.contains(page) {
  31. pages.append(page)
  32. }
  33. }
  34. }
  35. for page in pages {
  36. self.pdfListView?.setNeedsDisplayFor(page)
  37. }
  38. }
  39. public func setAnnotStyle(from annotationMode: CPDFViewAnnotationMode) {
  40. self.annotStyle = CAnnotStyle(annotionMode: annotationMode, annotations: [])
  41. }
  42. static func highlightAnnotationColor() -> UIColor? {
  43. let annotStyle = CAnnotStyle(annotionMode: .highlight, annotations: [])
  44. let colorComponents = annotStyle.color?.cgColor.components ?? [0, 0, 0, 0]
  45. let red = CGFloat(colorComponents[0])
  46. let green = CGFloat(colorComponents[1])
  47. let blue = CGFloat(colorComponents[2])
  48. let alpha = annotStyle.opacity
  49. return UIColor(red: red, green: green, blue: blue, alpha: alpha)
  50. }
  51. static func underlineAnnotationColor() -> UIColor? {
  52. let annotStyle = CAnnotStyle(annotionMode: .underline, annotations: [])
  53. let colorComponents = annotStyle.color?.cgColor.components ?? [0, 0, 0, 0]
  54. let red = CGFloat(colorComponents[0])
  55. let green = CGFloat(colorComponents[1])
  56. let blue = CGFloat(colorComponents[2])
  57. let alpha = annotStyle.opacity
  58. return UIColor(red: red, green: green, blue: blue, alpha: alpha)
  59. }
  60. static func strikeoutAnnotationColor() -> UIColor? {
  61. let annotStyle = CAnnotStyle(annotionMode: .strikeout, annotations: [])
  62. let colorComponents = annotStyle.color?.cgColor.components ?? [0, 0, 0, 0]
  63. let red = CGFloat(colorComponents[0])
  64. let green = CGFloat(colorComponents[1])
  65. let blue = CGFloat(colorComponents[2])
  66. let alpha = annotStyle.opacity
  67. return UIColor(red: red, green: green, blue: blue, alpha: alpha)
  68. }
  69. static func squigglyAnnotationColor() -> UIColor? {
  70. let annotStyle = CAnnotStyle(annotionMode: .squiggly, annotations: [])
  71. let colorComponents = annotStyle.color?.cgColor.components ?? [0, 0, 0, 0]
  72. let red = CGFloat(colorComponents[0])
  73. let green = CGFloat(colorComponents[1])
  74. let blue = CGFloat(colorComponents[2])
  75. let alpha = annotStyle.opacity
  76. return UIColor(red: red, green: green, blue: blue, alpha: alpha)
  77. }
  78. static func freehandAnnotationColor() -> UIColor? {
  79. let annotStyle = CAnnotStyle(annotionMode: .ink, annotations: [])
  80. let colorComponents = annotStyle.color?.cgColor.components ?? [0, 0, 0, 0]
  81. let red = CGFloat(colorComponents[0])
  82. let green = CGFloat(colorComponents[1])
  83. let blue = CGFloat(colorComponents[2])
  84. let alpha = annotStyle.opacity
  85. return UIColor(red: red, green: green, blue: blue, alpha: alpha)
  86. }
  87. }