KMOCToolClass.swift 7.3 KB

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