KMOCToolClass.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // KMOCToolClass.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/9/26.
  6. //
  7. import Cocoa
  8. struct MwcFlags {
  9. var caseInsensitiveSearch: Int = 1
  10. var wholeWordSearch: Int = 1
  11. var settingUpWindow: Int = 1
  12. var isSwitchingFullScreen: Int = 0
  13. var wantsPresentation: Int = 1
  14. }
  15. @objc enum SKInteractionMode: Int {
  16. case SKNormalMode = 0
  17. case SKFullScreenMode
  18. case SKPresentationMode
  19. case SKLegacyFullScreenMode
  20. }
  21. class KMOCToolClass {
  22. class func filterAnnotation(annotations: [Any], types: [Any]) -> [Any] {
  23. let array = annotations
  24. let leftExpression = NSExpression(forKeyPath: "type")
  25. let rightExpression = NSExpression(forConstantValue: types)
  26. let predicate = NSComparisonPredicate(
  27. leftExpression: leftExpression,
  28. rightExpression: rightExpression,
  29. modifier: .direct,
  30. type: .in,
  31. options: .diacriticInsensitive
  32. )
  33. return (array as NSArray).filtered(using: predicate)
  34. }
  35. class func filterAnnotation(annotations: [Any], colors: [Any]) -> [Any] {
  36. var array = annotations
  37. if array.contains(where: { $0 is NSColor && ($0 as! NSColor) == NSColor.clear }) {
  38. array.removeAll { $0 is NSColor && ($0 as! NSColor) == NSColor.clear }
  39. }
  40. let leftExpression = NSExpression(forKeyPath: "color")
  41. let rightExpression = NSExpression(forConstantValue: colors)
  42. let predicate = NSComparisonPredicate(
  43. leftExpression: leftExpression,
  44. rightExpression: rightExpression,
  45. modifier: .direct,
  46. type: .in,
  47. options: .diacriticInsensitive
  48. )
  49. return (array as NSArray).filtered(using: predicate)
  50. }
  51. class func filterAnnotation(annotations: [Any], authors: [Any]) -> [Any] {
  52. let array = annotations
  53. let leftExpression = NSExpression(forKeyPath: "userName")
  54. let rightExpression = NSExpression(forConstantValue: authors)
  55. let predicate = NSComparisonPredicate(
  56. leftExpression: leftExpression,
  57. rightExpression: rightExpression,
  58. modifier: .direct,
  59. type: .in,
  60. options: .diacriticInsensitive
  61. )
  62. return (array as NSArray).filtered(using: predicate)
  63. }
  64. class func arrayContains(array: [Any]?, annotation item: Any?) -> Bool {
  65. return array?.contains { $0 as AnyObject === item as AnyObject } ?? false
  66. }
  67. class func arrayIndexOf<T: Equatable>(array: [T], item: T) -> Int? {
  68. guard let index = array.firstIndex(of: item) else {
  69. return nil // Return nil to indicate that the item was not found
  70. }
  71. return index
  72. }
  73. class func exproString(annotation: CPDFAnnotation) -> String {
  74. var exproString = ""
  75. if let markupAnnotation = annotation as? CPDFMarkupAnnotation,
  76. let page = markupAnnotation.page,
  77. let points = markupAnnotation.quadrilateralPoints as? [CGPoint], // Cast to [CGPoint]
  78. points.count % 4 == 0 {
  79. for i in stride(from: 0, to: points.count, by: 4) {
  80. let ptlt = points[i]
  81. let ptrt = points[i + 1]
  82. let ptlb = points[i + 2]
  83. let ptrb = points[i + 3]
  84. let rect = CGRect(x: ptlt.x, y: ptlt.y, width: ptrt.x - ptlt.x, height: ptrt.y - ptlt.y)
  85. if let string = page.string(for: rect), !string.isEmpty {
  86. if !exproString.isEmpty {
  87. exproString += "\n"
  88. }
  89. exproString += string
  90. }
  91. }
  92. }
  93. return exproString
  94. }
  95. class func getAttributedString(selection: CPDFSelection, keyword: String) -> NSMutableAttributedString {
  96. guard let currentPage = selection.page else {
  97. return NSMutableAttributedString()
  98. }
  99. let range = selection.range
  100. var startLocation = 0
  101. let maxLocation: UInt = 10
  102. let maxEndLocation: UInt = 50
  103. var keyLocation = 0
  104. if range.location > maxLocation {
  105. startLocation = range.location - Int(maxLocation)
  106. keyLocation = Int(maxLocation)
  107. } else {
  108. startLocation = 0
  109. keyLocation = range.location
  110. }
  111. var endLocation: UInt = 0
  112. if range.location + Int(maxEndLocation) > currentPage.numberOfCharacters {
  113. endLocation = UInt(currentPage.numberOfCharacters)
  114. } else {
  115. endLocation = UInt(range.location + Int(maxEndLocation))
  116. }
  117. var attributed: NSMutableAttributedString = NSMutableAttributedString()
  118. if endLocation > UInt(startLocation) {
  119. let string = currentPage.string(for: NSRange(location: startLocation, length: Int(endLocation) - startLocation))
  120. var currentString = "..."
  121. let stringArr = string?.components(separatedBy: CharacterSet.newlines) ?? []
  122. for s in stringArr {
  123. if s.isEmpty {
  124. currentString += " "
  125. } else {
  126. currentString += s
  127. }
  128. }
  129. let tRange = NSRange(location: keyLocation + 3, length: keyword.count)
  130. if tRange.location != NSNotFound {
  131. attributed = NSMutableAttributedString(string: currentString)
  132. let paragraphStyle = NSMutableParagraphStyle()
  133. paragraphStyle.firstLineHeadIndent = 10.0
  134. paragraphStyle.headIndent = 10.0
  135. paragraphStyle.lineBreakMode = .byCharWrapping
  136. paragraphStyle.lineHeightMultiple = 1.32
  137. let dic: [NSAttributedString.Key: Any] = [
  138. .font: NSFont(name: "SFProText-Regular", size: 14.0)!,
  139. .foregroundColor: NSColor(red: 0.145, green: 0.149, blue: 0.161, alpha: 1),
  140. .paragraphStyle: paragraphStyle
  141. ]
  142. let fullRange = NSRange(location: 0, length: attributed.length)
  143. attributed.setAttributes(dic, range: fullRange)
  144. // Highlight string
  145. let highlightDic: [NSAttributedString.Key: Any] = [
  146. .backgroundColor: NSColor(red: 1.0, green: 0.9, blue: 0.0, alpha: 1.0)
  147. ]
  148. if attributed.length > tRange.length + tRange.location {
  149. attributed.addAttributes(highlightDic, range: tRange)
  150. }
  151. }
  152. }
  153. return attributed
  154. }
  155. }