From: <ho...@us...> - 2009-08-30 10:17:09
|
Revision: 5601 http://skim-app.svn.sourceforge.net/skim-app/?rev=5601&view=rev Author: hofman Date: 2009-08-30 10:16:56 +0000 (Sun, 30 Aug 2009) Log Message: ----------- Get all character ranges for a selection at once which can be more efficient. Or just get the first or last character index when that's all we need. Modified Paths: -------------- trunk/PDFAnnotationMarkup_SKExtensions.m trunk/PDFDocument_SKExtensions.m trunk/PDFPage_SKExtensions.m trunk/PDFSelection_SKExtensions.h trunk/PDFSelection_SKExtensions.m trunk/SKIndexCommand.m trunk/SKJoinCommand.m trunk/SKPDFView.m Modified: trunk/PDFAnnotationMarkup_SKExtensions.m =================================================================== --- trunk/PDFAnnotationMarkup_SKExtensions.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/PDFAnnotationMarkup_SKExtensions.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -218,12 +218,14 @@ newBounds = NSUnionRect(lineRect, newBounds); } } - } else if (iMax = [selection safeNumberOfRangesOnPage:page]) { + } else { NSString *string = [page string]; NSRect charRect = NSZeroRect; NSRect lastCharRect = NSZeroRect; - for (i = 0; i < iMax; i++) { - NSRange range = [selection safeRangeAtIndex:i onPage:page]; + NSEnumerator *rangeEnum = [[selection safeRangesOnPage:page] objectEnumerator]; + NSValue *value; + while (value = [rangeEnum nextObject]) { + NSRange range = [value rangeValue]; NSUInteger j, jMax = NSMaxRange(range); for (j = range.location; j < jMax; j++) { lastCharRect = charRect; Modified: trunk/PDFDocument_SKExtensions.m =================================================================== --- trunk/PDFDocument_SKExtensions.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/PDFDocument_SKExtensions.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -85,9 +85,8 @@ NSUInteger pageIndex = [self indexForPage:page]; NSUInteger firstPageIndex = [self indexForPage:firstPage]; NSUInteger lastPageIndex = [self indexForPage:lastPage]; - NSUInteger n = [selection safeNumberOfRangesOnPage:lastPage]; - NSUInteger firstChar = n ? [selection safeRangeAtIndex:0 onPage:firstPage].location : NSNotFound; - NSUInteger lastChar = n ? NSMaxRange([selection safeRangeAtIndex:n - 1 onPage:lastPage]) : NSNotFound; + NSUInteger firstChar = [selection safeIndexOfFirstCharacterOnPage:firstPage]; + NSUInteger lastChar = [selection safeIndexOfLastCharacterOnPage:lastPage]; NSRect firstRect, lastRect; if (firstChar != NSNotFound) { Modified: trunk/PDFPage_SKExtensions.m =================================================================== --- trunk/PDFPage_SKExtensions.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/PDFPage_SKExtensions.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -391,9 +391,10 @@ NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; NSString *string = [self string]; NSRange stringRange = NSMakeRange(0, [string length]); - iMax = [sel safeNumberOfRangesOnPage:self]; - for (i = 0; i < iMax; i++) { - NSRange range = [sel safeRangeAtIndex:i onPage:self]; + NSEnumerator *rangeEnum = [[sel safeRangesOnPage:self] objectEnumerator]; + NSValue *value; + while (value = [rangeEnum nextObject]) { + NSRange range = [value rangeValue]; NSUInteger j, jMax = NSMaxRange(range); for (j = range.location; j < jMax; j++) { @@ -402,11 +403,11 @@ NSRect r = [self characterBoundsAtIndex:j]; PDFSelection *s = [self selectionForLineAtPoint:SKCenterPoint(r)]; - NSUInteger k, kMax = [s safeNumberOfRangesOnPage:self]; BOOL notEmpty = NO; - - for (k = 0; k < kMax; k++) { - NSRange selRange = [s safeRangeAtIndex:k onPage:self]; + NSEnumerator *rEnum = [[s safeRangesOnPage:self] objectEnumerator]; + NSValue *val; + while (val = [rEnum nextObject]) { + NSRange selRange = [val rangeValue]; if (selRange.location != NSNotFound) { [indexes addIndexesInRange:selRange]; // due to a bug in PDFKit, the range of the selection can sometimes lie partly outside the range of the string Modified: trunk/PDFSelection_SKExtensions.h =================================================================== --- trunk/PDFSelection_SKExtensions.h 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/PDFSelection_SKExtensions.h 2009-08-30 10:16:56 UTC (rev 5601) @@ -48,8 +48,9 @@ - (PDFDestination *)destination; -- (NSInteger)safeNumberOfRangesOnPage:(PDFPage *)page; -- (NSRange)safeRangeAtIndex:(NSInteger)index onPage:(PDFPage *)page; +- (NSUInteger)safeIndexOfFirstCharacterOnPage:(PDFPage *)page; +- (NSUInteger)safeIndexOfLastCharacterOnPage:(PDFPage *)page; +- (NSArray *)safeRangesOnPage:(PDFPage *)page; + (id)selectionWithSpecifier:(id)specifier; + (id)selectionWithSpecifier:(id)specifier onPage:(PDFPage *)aPage; Modified: trunk/PDFSelection_SKExtensions.m =================================================================== --- trunk/PDFSelection_SKExtensions.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/PDFSelection_SKExtensions.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -122,47 +122,61 @@ return destination; } -- (NSInteger)safeNumberOfRangesOnPage:(PDFPage *)page { +- (NSUInteger)safeIndexOfFirstCharacterOnPage:(PDFPage *)page { if ([self respondsToSelector:@selector(indexOfCharactersOnPage:)]) { - NSIndexSet *chars = [self indexOfCharactersOnPage:page]; - NSUInteger idx = [chars firstIndex]; - NSUInteger prevIdx = NSNotFound; - NSInteger count = 0; - while (idx != NSNotFound) { - if (prevIdx == NSNotFound || idx != prevIdx + 1) count++; - prevIdx = idx; - idx = [chars indexGreaterThanIndex:idx]; - } - return count; - } else if ([self respondsToSelector:@selector(numberOfRangesOnPage:)]) - return [self numberOfRangesOnPage:page]; - return 0; + NSIndexSet *indexes = [self indexOfCharactersOnPage:page]; + if (indexes) + return [indexes firstIndex]; + } else if ([self respondsToSelector:@selector(numberOfRangesOnPage:)] && [self respondsToSelector:@selector(rangeAtIndex:onPage:)]) { + NSInteger n = [self numberOfRangesOnPage:page]; + if (n) + return [self rangeAtIndex:0 onPage:page].location; + } + return NSNotFound; } -- (NSRange)safeRangeAtIndex:(NSInteger)anIndex onPage:(PDFPage *)page { +- (NSUInteger)safeIndexOfLastCharacterOnPage:(PDFPage *)page { if ([self respondsToSelector:@selector(indexOfCharactersOnPage:)]) { - NSIndexSet *chars = [self indexOfCharactersOnPage:page]; - NSUInteger idx = [chars firstIndex]; + NSIndexSet *indexes = [self indexOfCharactersOnPage:page]; + if (indexes) + return [indexes lastIndex]; + } else if ([self respondsToSelector:@selector(numberOfRangesOnPage:)] && [self respondsToSelector:@selector(rangeAtIndex:onPage:)]) { + NSInteger n = [self numberOfRangesOnPage:page]; + if (n) + return NSMaxRange([self rangeAtIndex:n - 1 onPage:page]); + } + return NSNotFound; +} + +- (NSArray *)safeRangesOnPage:(PDFPage *)page { + if ([self respondsToSelector:@selector(indexOfCharactersOnPage:)]) { + NSIndexSet *indexes = [self indexOfCharactersOnPage:page]; + NSUInteger idx = [indexes firstIndex]; NSUInteger prevIdx = NSNotFound; NSRange range = NSMakeRange(NSNotFound, 0); - NSInteger count = 0; + NSMutableArray *ranges = [NSMutableArray array]; while (idx != NSNotFound) { if (prevIdx == NSNotFound || idx != prevIdx + 1) { - if (count > anIndex) - return range; - count++; + if (range.length) + [ranges addObject:[NSValue valueWithRange:range]]; range = NSMakeRange(idx, 1); } else { range.length++; } prevIdx = idx; - idx = [chars indexGreaterThanIndex:idx]; + idx = [indexes indexGreaterThanIndex:idx]; } - if (count > anIndex) - return range; - } else if ([self respondsToSelector:@selector(rangeAtIndex:onPage:)]) - return [self rangeAtIndex:anIndex onPage:page]; - return NSMakeRange(NSNotFound, 0); + if (range.length) + [ranges addObject:[NSValue valueWithRange:range]]; + return ranges; + } else if ([self respondsToSelector:@selector(numberOfRangesOnPage:)] && [self respondsToSelector:@selector(rangeAtIndex:onPage:)]) { + NSInteger i, iMax = [self numberOfRangesOnPage:page]; + NSMutableArray *ranges = [NSMutableArray array]; + for (i = 0; i < iMax; i++) + [ranges addObject:[NSValue valueWithRange:[self rangeAtIndex:i onPage:page]]]; + return ranges; + } + return nil; } static inline NSRange rangeOfSubstringOfStringAtIndex(NSString *string, NSArray *substrings, NSUInteger anIndex) { @@ -506,10 +520,11 @@ NSEnumerator *pageEnum = [[self pages] objectEnumerator]; PDFPage *page; while (page = [pageEnum nextObject]) { - NSUInteger i, iMax = [self safeNumberOfRangesOnPage:page]; + NSEnumerator *rangeEnum = [[self safeRangesOnPage:page] objectEnumerator]; + NSValue *value; NSRange lastRange = NSMakeRange(0, 0); - for (i = 0; i < iMax; i++) { - NSRange range = [self safeRangeAtIndex:i onPage:page]; + while (value = [rangeEnum nextObject]) { + NSRange range = [value rangeValue]; if (range.length == 0) { } else if (lastRange.length == 0) { lastRange = range; Modified: trunk/SKIndexCommand.m =================================================================== --- trunk/SKIndexCommand.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/SKIndexCommand.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -65,15 +65,8 @@ } else { PDFSelection *selection = [PDFSelection selectionWithSpecifier:dP onPage:page]; NSArray *pages = [selection pages]; - if ([pages count] && (page = [pages objectAtIndex:last ? [pages count] - 1 : 0])) { - NSUInteger count = [selection safeNumberOfRangesOnPage:page]; - if (count > 0) { - NSRange range = [selection safeRangeAtIndex:last ? count - 1 : 0 onPage:page]; - if (range.length) { - idx = last ? NSMaxRange(range) - 1 : range.location; - } - } - } + if ([pages count] && (page = [pages objectAtIndex:last ? [pages count] - 1 : 0])) + idx = last ? [selection safeIndexOfLastCharacterOnPage:page] : [selection safeIndexOfFirstCharacterOnPage:page]; } return [NSNumber numberWithInt:idx == NSNotFound ? 0 : (NSInteger)idx + 1]; Modified: trunk/SKJoinCommand.m =================================================================== --- trunk/SKJoinCommand.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/SKJoinCommand.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -60,10 +60,8 @@ NSArray *pages = [selection pages]; PDFPage *firstPage = [pages objectAtIndex:0]; PDFPage *lastPage = [pages lastObject]; - - NSUInteger numRanges = [selection safeNumberOfRangesOnPage:lastPage]; - NSUInteger firstIndex = numRanges ? [selection safeRangeAtIndex:0 onPage:firstPage].location : NSNotFound; - NSUInteger lastIndex = numRanges ? NSMaxRange([selection safeRangeAtIndex:numRanges - 1 onPage:lastPage]) : NSNotFound; + NSUInteger firstIndex = [selection safeIndexOfFirstCharacterOnPage:firstPage]; + NSUInteger lastIndex = [selection safeIndexOfLastCharacterOnPage:lastPage]; if (firstIndex != NSNotFound && lastIndex != NSNotFound) selection = [[firstPage document] selectionFromPage:firstPage atCharacterIndex:firstIndex toPage:lastPage atCharacterIndex:lastIndex - 1]; } Modified: trunk/SKPDFView.m =================================================================== --- trunk/SKPDFView.m 2009-08-30 09:39:05 UTC (rev 5600) +++ trunk/SKPDFView.m 2009-08-30 10:16:56 UTC (rev 5601) @@ -1607,15 +1607,12 @@ - (void)checkSpelling:(id)sender { PDFSelection *selection = [self currentSelection]; PDFPage *page = [self currentPage]; - NSUInteger numRanges, idx = 0; + NSUInteger idx = 0; if ([[selection string] length]) { page = [[selection pages] lastObject]; - numRanges = [selection safeNumberOfRangesOnPage:page]; - if (numRanges > 0) { - idx = NSMaxRange([selection safeRangeAtIndex:numRanges - 1 onPage:page]); - if (idx == NSNotFound) - idx = 0; - } + idx = [selection safeIndexOfLastCharacterOnPage:page]; + if (idx == NSNotFound) + idx = 0; } [self checkSpellingStartingAtIndex:idx onPage:page]; } @@ -1626,11 +1623,9 @@ NSUInteger idx = 0; if ([[selection string] length]) { page = [[selection pages] objectAtIndex:0]; - if ([selection safeNumberOfRangesOnPage:page] > 0) { - idx = [selection safeRangeAtIndex:0 onPage:page].location; - if (idx == NSNotFound) - idx = 0; - } + idx = [selection safeIndexOfFirstCharacterOnPage:page]; + if (idx == NSNotFound) + idx = 0; } [self checkSpellingStartingAtIndex:idx onPage:page]; [[[NSSpellChecker sharedSpellChecker] spellingPanel] orderFront:self]; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |