KMOCToolClass.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // KMOCToolClass.m
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/11/18.
  6. //
  7. #import "KMOCToolClass.h"
  8. @implementation KMOCToolClass
  9. + (NSArray *)filterAnnotation:(NSArray *)annotations types:(NSArray *)types {
  10. NSMutableArray *array = [NSMutableArray arrayWithArray:annotations];
  11. NSExpression *leftExpression = [NSExpression expressionForKeyPath:@"type"];
  12. NSExpression *rightExpression = [NSExpression expressionForConstantValue:types];
  13. NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
  14. rightExpression:rightExpression
  15. modifier:NSDirectPredicateModifier
  16. type:NSInPredicateOperatorType
  17. options:NSDiacriticInsensitivePredicateOption];
  18. return [array filteredArrayUsingPredicate:predicate];
  19. }
  20. + (NSArray *)filterAnnotation:(NSArray *)annotations colors:(NSArray *)colors {
  21. NSMutableArray *array = [NSMutableArray arrayWithArray:annotations];
  22. if ([array containsObject:[NSColor clearColor]]) {
  23. [array removeObject:[NSColor clearColor]];
  24. }
  25. NSExpression *leftExpression = [NSExpression expressionForKeyPath:@"color"];
  26. NSExpression *rightExpression = [NSExpression expressionForConstantValue:colors];
  27. NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
  28. rightExpression:rightExpression
  29. modifier:NSDirectPredicateModifier
  30. type:NSInPredicateOperatorType
  31. options:NSDiacriticInsensitivePredicateOption];
  32. return [array filteredArrayUsingPredicate:predicate];
  33. }
  34. + (NSArray *)filterAnnotation:(NSArray *)annotations authors:(NSArray *)authors {
  35. NSMutableArray *array = [NSMutableArray arrayWithArray:annotations];
  36. NSExpression *leftExpression = [NSExpression expressionForKeyPath:@"userName"];
  37. NSExpression *rightExpression = [NSExpression expressionForConstantValue:authors];
  38. NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
  39. rightExpression:rightExpression
  40. modifier:NSDirectPredicateModifier
  41. type:NSInPredicateOperatorType
  42. options:NSDiacriticInsensitivePredicateOption];
  43. return [array filteredArrayUsingPredicate:predicate];
  44. }
  45. + (BOOL)arrayContains:(NSArray *)array annotation:(id)item {
  46. if (!array || array.count == 0 || !item) {
  47. return NO;
  48. }
  49. return [array containsObject:item];
  50. }
  51. + (NSInteger)arrayIndexOf:(NSArray *)array annotation:(id)item {
  52. if (!array || array.count == 0 || !item) {
  53. return 0;
  54. }
  55. return [array indexOfObject:item];
  56. }
  57. + (NSString *)exproString:(CPDFAnnotation *)annotation {
  58. NSString *exproString = @"";
  59. if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  60. CPDFPage *page = annotation.page;
  61. NSArray *points = [(CPDFMarkupAnnotation *)annotation quadrilateralPoints];
  62. NSInteger count = 4;
  63. for (NSUInteger i=0; i+count <= points.count;) {
  64. CGPoint ptlt = [points[i++] pointValue];
  65. CGPoint ptrt = [points[i++] pointValue];
  66. CGPoint ptlb = [points[i++] pointValue];
  67. CGPoint ptrb = [points[i++] pointValue];
  68. CGRect rect = CGRectMake(ptlb.x, ptlb.y, ptrt.x - ptlb.x, ptrt.y - ptlb.y);
  69. NSString *string = [page stringForRect:rect];
  70. if (string && string.length >0) {
  71. if (exproString) {
  72. exproString = [exproString stringByAppendingString:@"\n"];
  73. exproString = [exproString stringByAppendingString:string];
  74. } else {
  75. exproString = string;
  76. }
  77. }
  78. }
  79. }
  80. return exproString;
  81. }
  82. + (NSMutableArray *)scannerCharaterString:(NSString *)string {
  83. NSScanner *scanner = [NSScanner scannerWithString:string];
  84. NSMutableArray *words = [NSMutableArray array];
  85. NSString *word;
  86. [scanner setCharactersToBeSkipped:nil];
  87. while ([scanner isAtEnd] == NO) {
  88. if ('"' == [[scanner string] characterAtIndex:[scanner scanLocation]]) {
  89. [scanner setScanLocation:[scanner scanLocation] + 1];
  90. if ([scanner scanUpToString:@"\"" intoString:&word])
  91. [words addObject:word];
  92. if ([scanner isAtEnd] == NO)
  93. [scanner setScanLocation:[scanner scanLocation] + 1];
  94. } else if ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&word]) {
  95. [words addObject:word];
  96. }
  97. [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
  98. }
  99. return words;
  100. }
  101. + (NSMutableAttributedString *)getAttributedStringWithSelection:(CPDFSelection *)selection
  102. keyword:(NSString *)keyword
  103. {
  104. CPDFPage * currentPage = selection.page;
  105. NSRange range = selection.range;
  106. NSUInteger startLocation = 0;
  107. NSUInteger maxLocation = 10;
  108. NSUInteger maxEndLocation = 50;
  109. NSInteger keyLocation = 0;
  110. if (range.location > maxLocation) {
  111. startLocation = range.location - maxLocation;
  112. keyLocation = maxLocation;
  113. } else {
  114. startLocation = 0;
  115. keyLocation = range.location;
  116. }
  117. NSUInteger endLocation = 0;
  118. if (range.location + maxEndLocation > currentPage.numberOfCharacters) {
  119. endLocation = currentPage.numberOfCharacters;
  120. } else {
  121. endLocation = range.location + maxEndLocation;
  122. }
  123. NSMutableAttributedString * attributed = nil;
  124. if (endLocation> startLocation) {
  125. NSString * string = [currentPage stringForRange:NSMakeRange(startLocation, endLocation - startLocation)];
  126. NSString *currentString = @"...";
  127. NSArray *stringArr = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
  128. for (NSString *s in stringArr) {
  129. if (s.length == 0)
  130. currentString = [currentString stringByAppendingString:@" "];
  131. else
  132. currentString = [currentString stringByAppendingString:s];
  133. }
  134. NSRange tRange = NSMakeRange(keyLocation+3, keyword.length);
  135. if (tRange.location != NSNotFound) {
  136. attributed = [[NSMutableAttributedString alloc] initWithString:currentString];
  137. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  138. paragraphStyle.firstLineHeadIndent = 10.0;
  139. paragraphStyle.headIndent = 10.0;
  140. paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
  141. paragraphStyle.lineHeightMultiple = 1.32;
  142. NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"SFProText-Regular" size:14.0],NSFontAttributeName,[NSColor colorWithRed:0.145 green:0.149 blue:0.161 alpha:1],NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];
  143. NSRange range = [[attributed string] rangeOfString:[attributed string]];
  144. [attributed setAttributes:dic range:range];
  145. //hightlight string
  146. dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor colorWithRed:1.0 green:0.9 blue:0.0 alpha:1.0],NSBackgroundColorAttributeName,nil];
  147. if (attributed.length > tRange.length + tRange.location) {
  148. [attributed addAttributes:dic range:tRange];
  149. }
  150. }
  151. }
  152. return attributed;
  153. }
  154. + (BOOL)wholeWordWithSelection:(CPDFSelection *)selection
  155. keyword:(NSString *)keyword {
  156. CPDFPage * currentPage = selection.page;
  157. NSRange range = selection.range;
  158. NSUInteger startLocation = 0;
  159. NSUInteger maxLocation = 10;
  160. NSUInteger maxEndLocation = 80;
  161. NSInteger keyLocation = 0;
  162. if (range.location > maxLocation) {
  163. startLocation = range.location - maxLocation;
  164. keyLocation = maxLocation;
  165. } else {
  166. startLocation = 0;
  167. keyLocation = range.location;
  168. }
  169. NSUInteger endLocation = 0;
  170. if (range.location + maxEndLocation > currentPage.numberOfCharacters) {
  171. endLocation = currentPage.numberOfCharacters;
  172. } else {
  173. endLocation = range.location + maxEndLocation;
  174. }
  175. if (endLocation> startLocation) {
  176. NSString * string = [currentPage stringForRange:NSMakeRange(startLocation, endLocation - startLocation)];
  177. NSString *currentString = @"";
  178. NSArray *stringArr = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
  179. for (NSString *s in stringArr) {
  180. if (s.length == 0)
  181. currentString = [currentString stringByAppendingString:@" "];
  182. else
  183. currentString = [currentString stringByAppendingString:s];
  184. }
  185. char leftChar = [currentString characterAtIndex:keyLocation == 0 ? 0 : keyLocation-1];
  186. char rightChar = [currentString characterAtIndex:(keyLocation+keyword.length)];
  187. if (leftChar == 32 && rightChar == 32) {
  188. return YES;
  189. }
  190. }
  191. return NO;
  192. }
  193. @end