Revision: 4861
          http://skim-app.svn.sourceforge.net/skim-app/?rev=4861&view=rev
Author:   hofman
Date:     2008-10-24 15:17:49 +0000 (Fri, 24 Oct 2008)

Log Message:
-----------
Create selection ranges in document in one go

Modified Paths:
--------------
    trunk/PDFSelection_SKExtensions.m

Modified: trunk/PDFSelection_SKExtensions.m
===================================================================
--- trunk/PDFSelection_SKExtensions.m   2008-10-24 14:41:42 UTC (rev 4860)
+++ trunk/PDFSelection_SKExtensions.m   2008-10-24 15:17:49 UTC (rev 4861)
@@ -412,35 +412,47 @@
                 
                 PDFDocument *document = [container pdfDocument];
                 unsigned int i, numPages = [document pageCount];
-                NSRange *pageRanges = NSZoneMalloc(NSDefaultMallocZone(), 
numPages * sizeof(NSRange));
+                unsigned int *pageLengths = 
NSZoneMalloc(NSDefaultMallocZone(), numPages * sizeof(unsigned int));
                 unsigned aPageIndex = aPage ? [aPage pageIndex] : NSNotFound;
                 
                 for (i = 0; i < numPages; i++)
-                    pageRanges[i] = NSMakeRange(NSNotFound, 0);
+                    pageLengths[i] = NSNotFound;
                 
                 while (value = [rangeEnum nextObject]) {
                     NSRange range = [value rangeValue];
+                    unsigned int pageStart = 0;
+                    unsigned int startPage = NSNotFound, endPage = NSNotFound, 
startIndex = NSNotFound, endIndex = NSNotFound;
                     for (i = 0; i < numPages; i++) {
-                        if (pageRanges[i].location == NSNotFound)
-                            pageRanges[i] = NSMakeRange(i == 0 ? 0 : 
NSMaxRange(pageRanges[i - 1]), [[[document pageAtIndex:i] string] length]);
-                        if ((aPageIndex == NSNotFound || i == aPageIndex) && 
pageRanges[i].length && range.location < NSMaxRange(pageRanges[i])) {
-                            PDFSelection *sel;
-                            NSRange r = NSIntersectionRange(pageRanges[i], 
range);
-                            r.location -= pageRanges[i].location;
-                            if (sel = [[document pageAtIndex:i] 
selectionForRange:r]) {
-                                doc = document;
-                                if (selection == nil)
-                                    selection = sel;
-                                else
-                                    [selection addSelection:sel];
+                        if (pageLengths[i] == NSNotFound)
+                            pageLengths[i] = [[[document pageAtIndex:i] 
string] length];
+                        if ((aPageIndex == NSNotFound || i == aPageIndex) && 
pageLengths[i] && range.location < pageStart + pageLengths[i]) {
+                            if (startPage == NSNotFound && startIndex == 
NSNotFound) {
+                                startPage = i;
+                                startIndex = range.location < pageStart ? 0 : 
range.location - pageStart;
                             }
+                            if (startPage != NSNotFound && startIndex != 
NSNotFound) {
+                                endPage = i;
+                                endIndex = MIN(NSMaxRange(range) - pageStart, 
pageLengths[i] - 1);
+                            }
                         }
-                        if (NSMaxRange(range) <= NSMaxRange(pageRanges[i]))
+                        pageStart += pageLengths[i];
+                        if (pageStart >= NSMaxRange(range))
                             break;
                     }
+                    
+                    if (startPage != NSNotFound && startIndex != NSNotFound && 
endPage != NSNotFound && endIndex != NSNotFound) {
+                        PDFSelection *sel = [document selectionFromPage:[doc 
pageAtIndex:startPage] atCharacterIndex:startIndex toPage:[doc 
pageAtIndex:endPage] atCharacterIndex:endIndex];
+                        if (sel) {
+                            doc = document;
+                            if (selection == nil)
+                                selection = sel;
+                            else
+                                [selection addSelection:sel];
+                        }
+                    }
                 }
                 
-                NSZoneFree(NSDefaultMallocZone(), pageRanges);
+                NSZoneFree(NSDefaultMallocZone(), pageLengths);
                 
             } else if ([container isKindOfClass:[PDFPage class]] && (aPage == 
nil || [aPage isEqual:container]) && (doc == nil || [doc isEqual:[container 
document]])) {
                 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to