KMOCToolClass.swift 7.2 KB

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