123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- //
- // KMOCToolClass.m
- // PDF Master
- //
- // Created by lxy on 2022/11/18.
- //
- #import "KMOCToolClass.h"
- @implementation KMOCToolClass
- + (NSArray *)filterAnnotation:(NSArray *)annotations types:(NSArray *)types {
- NSMutableArray *array = [NSMutableArray arrayWithArray:annotations];
- NSExpression *leftExpression = [NSExpression expressionForKeyPath:@"type"];
- NSExpression *rightExpression = [NSExpression expressionForConstantValue:types];
- NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
- rightExpression:rightExpression
- modifier:NSDirectPredicateModifier
- type:NSInPredicateOperatorType
- options:NSDiacriticInsensitivePredicateOption];
- return [array filteredArrayUsingPredicate:predicate];
- }
- + (NSArray *)filterAnnotation:(NSArray *)annotations colors:(NSArray *)colors {
- NSMutableArray *array = [NSMutableArray arrayWithArray:annotations];
- if ([array containsObject:[NSColor clearColor]]) {
- [array removeObject:[NSColor clearColor]];
- }
- NSExpression *leftExpression = [NSExpression expressionForKeyPath:@"color"];
- NSExpression *rightExpression = [NSExpression expressionForConstantValue:colors];
- NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
- rightExpression:rightExpression
- modifier:NSDirectPredicateModifier
- type:NSInPredicateOperatorType
- options:NSDiacriticInsensitivePredicateOption];
- return [array filteredArrayUsingPredicate:predicate];
- }
- + (NSArray *)filterAnnotation:(NSArray *)annotations authors:(NSArray *)authors {
- NSMutableArray *array = [NSMutableArray arrayWithArray:annotations];
- NSExpression *leftExpression = [NSExpression expressionForKeyPath:@"userName"];
- NSExpression *rightExpression = [NSExpression expressionForConstantValue:authors];
- NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
- rightExpression:rightExpression
- modifier:NSDirectPredicateModifier
- type:NSInPredicateOperatorType
- options:NSDiacriticInsensitivePredicateOption];
- return [array filteredArrayUsingPredicate:predicate];
- }
- + (BOOL)arrayContains:(NSArray *)array annotation:(id)item {
- if (!array || array.count == 0 || !item) {
- return NO;
- }
- return [array containsObject:item];
- }
- + (NSInteger)arrayIndexOf:(NSArray *)array annotation:(id)item {
- if (!array || array.count == 0 || !item) {
- return 0;
- }
- return [array indexOfObject:item];
- }
- + (NSString *)exproString:(CPDFAnnotation *)annotation {
- NSString *exproString = @"";
- if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
- CPDFPage *page = annotation.page;
- NSArray *points = [(CPDFMarkupAnnotation *)annotation quadrilateralPoints];
- NSInteger count = 4;
- for (NSUInteger i=0; i+count <= points.count;) {
- CGPoint ptlt = [points[i++] pointValue];
- CGPoint ptrt = [points[i++] pointValue];
- CGPoint ptlb = [points[i++] pointValue];
- CGPoint ptrb = [points[i++] pointValue];
- CGRect rect = CGRectMake(ptlb.x, ptlb.y, ptrt.x - ptlb.x, ptrt.y - ptlb.y);
- NSString *string = [page stringForRect:rect];
- if (string && string.length >0) {
- if (exproString) {
- exproString = [exproString stringByAppendingString:@"\n"];
- exproString = [exproString stringByAppendingString:string];
- } else {
- exproString = string;
- }
- }
- }
- }
- return exproString;
- }
- + (NSMutableArray *)scannerCharaterString:(NSString *)string {
- NSScanner *scanner = [NSScanner scannerWithString:string];
- NSMutableArray *words = [NSMutableArray array];
- NSString *word;
- [scanner setCharactersToBeSkipped:nil];
- while ([scanner isAtEnd] == NO) {
- if ('"' == [[scanner string] characterAtIndex:[scanner scanLocation]]) {
- [scanner setScanLocation:[scanner scanLocation] + 1];
- if ([scanner scanUpToString:@"\"" intoString:&word])
- [words addObject:word];
- if ([scanner isAtEnd] == NO)
- [scanner setScanLocation:[scanner scanLocation] + 1];
- } else if ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&word]) {
- [words addObject:word];
- }
- [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
- }
- return words;
- }
- + (NSMutableAttributedString *)getAttributedStringWithSelection:(CPDFSelection *)selection
- keyword:(NSString *)keyword
- {
- CPDFPage * currentPage = selection.page;
- NSRange range = selection.range;
- NSUInteger startLocation = 0;
- NSUInteger maxLocation = 10;
- NSUInteger maxEndLocation = 50;
- NSInteger keyLocation = 0;
- if (range.location > maxLocation) {
- startLocation = range.location - maxLocation;
- keyLocation = maxLocation;
- } else {
- startLocation = 0;
- keyLocation = range.location;
- }
- NSUInteger endLocation = 0;
- if (range.location + maxEndLocation > currentPage.numberOfCharacters) {
- endLocation = currentPage.numberOfCharacters;
- } else {
- endLocation = range.location + maxEndLocation;
- }
-
- NSMutableAttributedString * attributed = nil;
- if (endLocation> startLocation) {
- NSString * string = [currentPage stringForRange:NSMakeRange(startLocation, endLocation - startLocation)];
- NSString *currentString = @"...";
- NSArray *stringArr = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
- for (NSString *s in stringArr) {
- if (s.length == 0)
- currentString = [currentString stringByAppendingString:@" "];
- else
- currentString = [currentString stringByAppendingString:s];
- }
- NSRange tRange = NSMakeRange(keyLocation+3, keyword.length);
- if (tRange.location != NSNotFound) {
- attributed = [[NSMutableAttributedString alloc] initWithString:currentString];
-
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.firstLineHeadIndent = 10.0;
- paragraphStyle.headIndent = 10.0;
- paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
- paragraphStyle.lineHeightMultiple = 1.32;
- 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];
-
- NSRange range = [[attributed string] rangeOfString:[attributed string]];
- [attributed setAttributes:dic range:range];
-
- //hightlight string
- dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor colorWithRed:1.0 green:0.9 blue:0.0 alpha:1.0],NSBackgroundColorAttributeName,nil];
- if (attributed.length > tRange.length + tRange.location) {
- [attributed addAttributes:dic range:tRange];
- }
- }
- }
- return attributed;
- }
- + (BOOL)wholeWordWithSelection:(CPDFSelection *)selection
- keyword:(NSString *)keyword {
- CPDFPage * currentPage = selection.page;
- NSRange range = selection.range;
- NSUInteger startLocation = 0;
- NSUInteger maxLocation = 10;
- NSUInteger maxEndLocation = 80;
- NSInteger keyLocation = 0;
- if (range.location > maxLocation) {
- startLocation = range.location - maxLocation;
- keyLocation = maxLocation;
- } else {
- startLocation = 0;
- keyLocation = range.location;
- }
- NSUInteger endLocation = 0;
- if (range.location + maxEndLocation > currentPage.numberOfCharacters) {
- endLocation = currentPage.numberOfCharacters;
- } else {
- endLocation = range.location + maxEndLocation;
- }
- if (endLocation> startLocation) {
- NSString * string = [currentPage stringForRange:NSMakeRange(startLocation, endLocation - startLocation)];
- NSString *currentString = @"";
- NSArray *stringArr = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
- for (NSString *s in stringArr) {
- if (s.length == 0)
- currentString = [currentString stringByAppendingString:@" "];
- else
- currentString = [currentString stringByAppendingString:s];
- }
-
- char leftChar = [currentString characterAtIndex:keyLocation == 0 ? 0 : keyLocation-1];
- char rightChar = [currentString characterAtIndex:(keyLocation+keyword.length)];
- if (leftChar == 32 && rightChar == 32) {
- return YES;
- }
- }
- return NO;
- }
- @end
|