Revision: 14170
          http://sourceforge.net/p/skim-app/code/14170
Author:   hofman
Date:     2024-04-03 15:00:44 +0000 (Wed, 03 Apr 2024)
Log Message:
-----------
implement sync lookup methods in appropriate data classes

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

Modified: trunk/SKPDFSynchronizer.m
===================================================================
--- trunk/SKPDFSynchronizer.m   2024-04-03 14:31:08 UTC (rev 14169)
+++ trunk/SKPDFSynchronizer.m   2024-04-03 15:00:44 UTC (rev 14170)
@@ -59,9 +59,15 @@
     synctex_scanner_p scanner;
     NSMapTable *filenames;
 }
+
 - (instancetype)initWithFileName:(NSString *)fileName;
+
 @property (nonatomic, readonly) synctex_scanner_p scanner 
NS_RETURNS_INNER_POINTER;
 @property (nonatomic, readonly) NSMapTable *filenames;
+
+- (BOOL)findFileLine:(NSInteger *)linePtr file:(out NSString * __autoreleasing 
*)filePtr forLocation:(NSPoint)point inRect:(NSRect)rect 
pageBounds:(NSRect)bounds atPageIndex:(NSUInteger)pageIndex;
+- (BOOL)findPage:(NSUInteger *)pageIndexPtr location:(NSPoint *)pointPtr 
forLine:(NSInteger)line inFile:(NSString *)file;
+
 @end
 
 #pragma mark -
@@ -70,8 +76,13 @@
     NSMutableArray<NSMutableArray<SKPDFSyncRecord *> *> *pages;
     NSMapTable *lines;
 }
+
 @property (nonatomic, readonly) NSMutableArray<NSMutableArray<SKPDFSyncRecord 
*> *> *pages;
 @property (nonatomic, readonly) NSMapTable *lines;
+
+- (BOOL)findFileLine:(NSInteger *)linePtr file:(out NSString * __autoreleasing 
*)filePtr forLocation:(NSPoint)point inRect:(NSRect)rect 
pageBounds:(NSRect)bounds atPageIndex:(NSUInteger)pageIndex;
+- (BOOL)findPage:(NSUInteger *)pageIndexPtr location:(NSPoint *)pointPtr 
forLine:(NSInteger)line inFile:(NSString *)file;
+
 @end
 
 #pragma mark -
@@ -318,122 +329,9 @@
     return rv;
 }
 
-- (BOOL)pdfsyncFindFileLine:(NSInteger *)linePtr file:(out NSString * 
__autoreleasing *)filePtr forLocation:(NSPoint)point inRect:(NSRect)rect 
pageBounds:(NSRect)bounds atPageIndex:(NSUInteger)pageIndex {
-    BOOL rv = NO;
-    if (pageIndex < [[pdfsync pages] count]) {
-        
-        SKPDFSyncRecord *record = nil;
-        SKPDFSyncRecord *beforeRecord = nil;
-        SKPDFSyncRecord *afterRecord = nil;
-        NSMutableDictionary *atRecords = [NSMutableDictionary dictionary];
-        
-        for (record in [[pdfsync pages] objectAtIndex:pageIndex]) {
-            if ([record line] == 0)
-                continue;
-            NSPoint p = [record point];
-            if (p.y > NSMaxY(rect)) {
-                beforeRecord = record;
-            } else if (p.y < NSMinY(rect)) {
-                afterRecord = record;
-                break;
-            } else if (p.x < NSMinX(rect)) {
-                beforeRecord = record;
-            } else if (p.x > NSMaxX(rect)) {
-                afterRecord = record;
-                break;
-            } else {
-                [atRecords setObject:record forKey:[NSNumber 
numberWithDouble:fabs(p.x - point.x)]];
-            }
-        }
-        
-        record = nil;
-        if ([atRecords count]) {
-            NSNumber *nearest = [[[atRecords allKeys] 
sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:0];
-            record = [atRecords objectForKey:nearest];
-        } else if (beforeRecord && afterRecord) {
-            NSPoint beforePoint = [beforeRecord point];
-            NSPoint afterPoint = [afterRecord point];
-            if (beforePoint.y - point.y < point.y - afterPoint.y)
-                record = beforeRecord;
-            else if (beforePoint.y - point.y > point.y - afterPoint.y)
-                record = afterRecord;
-            else if (beforePoint.x - point.x < point.x - afterPoint.x)
-                record = beforeRecord;
-            else if (beforePoint.x - point.x > point.x - afterPoint.x)
-                record = afterRecord;
-            else
-                record = beforeRecord;
-        } else if (beforeRecord) {
-            record = beforeRecord;
-        } else if (afterRecord) {
-            record = afterRecord;
-        }
-        
-        if (record) {
-            *linePtr = [record line];
-            *filePtr = [record file];
-            rv = YES;
-        }
-    }
-    if (rv == NO)
-        NSLog(@"PDFSync was unable to find file and line.");
-    return rv;
-}
-
-- (BOOL)pdfsyncFindPage:(NSUInteger *)pageIndexPtr location:(NSPoint 
*)pointPtr forLine:(NSInteger)line inFile:(NSString *)file {
-    BOOL rv = NO;
-    NSArray *theLines = [[pdfsync lines] objectForKey:file];
-    if (theLines) {
-        
-        SKPDFSyncRecord *record = nil;
-        SKPDFSyncRecord *beforeRecord = nil;
-        SKPDFSyncRecord *afterRecord = nil;
-        SKPDFSyncRecord *atRecord = nil;
-        
-        for (record in theLines) {
-            if ([record pageIndex] == NSNotFound)
-                continue;
-            NSInteger l = [record line];
-            if (l < line) {
-                beforeRecord = record;
-            } else if (l > line) {
-                afterRecord = record;
-                break;
-            } else {
-                atRecord = record;
-                break;
-            }
-        }
-        
-        if (atRecord) {
-            record = atRecord;
-        } else if (beforeRecord && afterRecord) {
-            NSInteger beforeLine = [beforeRecord line];
-            NSInteger afterLine = [afterRecord line];
-            if (beforeLine - line > line - afterLine)
-                record = afterRecord;
-            else
-                record = beforeRecord;
-        } else if (beforeRecord) {
-            record = beforeRecord;
-        } else if (afterRecord) {
-            record = afterRecord;
-        }
-        
-        if (record) {
-            *pageIndexPtr = [record pageIndex];
-            *pointPtr = [record point];
-            rv = YES;
-        }
-    }
-    if (rv == NO)
-        NSLog(@"PDFSync was unable to find location and page.");
-    return rv;
-}
-
 #pragma mark SyncTeX
 
-- (BOOL)loadSynctexFileForFile:(NSString *)theFileName {
+- (BOOL)loadSynctexFile:(NSString *)theFileName {
     BOOL rv = NO;
     synctex = [[SKSyncTeX alloc] initWithFileName:theFileName];
     if (synctex) {
@@ -451,51 +349,6 @@
     return rv;
 }
 
-- (BOOL)synctexFindFileLine:(NSInteger *)linePtr file:(out NSString * 
__autoreleasing *)filePtr forLocation:(NSPoint)point inRect:(NSRect)rect 
pageBounds:(NSRect)bounds atPageIndex:(NSUInteger)pageIndex {
-    BOOL rv = NO;
-    if (synctex_edit_query([synctex scanner], (int)pageIndex + 1, point.x, 
NSMaxY(bounds) - point.y) > 0) {
-        synctex_node_p node;
-        const char *file;
-        while (rv == NO && (node = synctex_scanner_next_result([synctex 
scanner]))) {
-            if ((file = synctex_scanner_get_name([synctex scanner], 
synctex_node_tag(node)))) {
-                *linePtr = MAX(synctex_node_line(node), 1) - 1;
-                *filePtr = [self sourceFileForFileName:[NSString 
stringWithUTF8String:file] isTeX:YES removeQuotes:NO];
-                rv = YES;
-            }
-        }
-    }
-    if (rv == NO)
-        NSLog(@"SyncTeX was unable to find file and line.");
-    return rv;
-}
-
-- (BOOL)synctexFindPage:(NSUInteger *)pageIndexPtr location:(NSPoint 
*)pointPtr forLine:(NSInteger)line inFile:(NSString *)file {
-    BOOL rv = NO;
-    char *filename = (char *)NSMapGet([synctex filenames], (__bridge void 
*)file) ?: (char *)NSMapGet([synctex filenames], (__bridge void *)[[file 
stringByResolvingSymlinksInPath] stringByStandardizingPath]);
-    if (filename == NULL) {
-        for (NSString *fn in [synctex filenames]) {
-            if ([[fn lastPathComponent] caseInsensitiveCompare:[file 
lastPathComponent]] == NSOrderedSame) {
-                filename = (char *)NSMapGet([synctex filenames], (__bridge 
void *)file);
-                break;
-            }
-        }
-        if (filename == NULL)
-            filename = (char *)[[file lastPathComponent] UTF8String];
-    }
-    if (synctex_display_query([synctex scanner], filename, (int)line + 1, 0, 
-1) > 0) {
-        synctex_node_p node = synctex_scanner_next_result([synctex scanner]);
-        if (node) {
-            NSUInteger page = synctex_node_page(node);
-            *pageIndexPtr = MAX(page, 1ul) - 1;
-            *pointPtr = NSMakePoint(synctex_node_visible_h(node), 
synctex_node_visible_v(node));
-            rv = YES;
-        }
-    }
-    if (rv == NO)
-        NSLog(@"SyncTeX was unable to find location and page.");
-    return rv;
-}
-
 #pragma mark Generic
 
 - (BOOL)loadSyncFileIfNeeded {
@@ -515,9 +368,9 @@
             else if (pdfsync)
                 rv = [self loadPdfsyncFile:theSyncFileName];
             else
-                rv = [self loadSynctexFileForFile:theFileName];
+                rv = [self loadSynctexFile:theFileName];
         } else {
-            rv = [self loadSynctexFileForFile:theFileName];
+            rv = [self loadSynctexFile:theFileName];
             if (rv == NO) {
                 theSyncFileName = [[theFileName stringByDeletingPathExtension] 
stringByAppendingPathExtension:SKPDFSynchronizerPdfsyncExtension];
                 if ([fileManager fileExistsAtPath:theSyncFileName])
@@ -547,10 +400,13 @@
             NSString *foundFile = nil;
             BOOL success = NO;
             
-            if (pdfsync)
-                success = [self pdfsyncFindFileLine:&foundLine file:&foundFile 
forLocation:point inRect:rect pageBounds:bounds atPageIndex:pageIndex];
-            else
-                success = [self synctexFindFileLine:&foundLine file:&foundFile 
forLocation:point inRect:rect pageBounds:bounds atPageIndex:pageIndex];
+            if (pdfsync) {
+                success = [pdfsync findFileLine:&foundLine file:&foundFile 
forLocation:point inRect:rect pageBounds:bounds atPageIndex:pageIndex];
+            } else {
+                success = [synctex findFileLine:&foundLine file:&foundFile 
forLocation:point inRect:rect pageBounds:bounds atPageIndex:pageIndex];
+                if (success)
+                    foundFile = [self sourceFileForFileName:foundFile 
isTeX:YES removeQuotes:NO];
+            }
             
             if (success && [self shouldKeepRunning]) {
                 dispatch_async(dispatch_get_main_queue(), ^{
@@ -573,9 +429,9 @@
             NSString *fixedFile = [self sourceFileForFileName:file isTeX:YES 
removeQuotes:NO];
             
             if (pdfsync)
-                success = [self pdfsyncFindPage:&foundPageIndex 
location:&foundPoint forLine:line inFile:fixedFile];
+                success = [pdfsync findPage:&foundPageIndex 
location:&foundPoint forLine:line inFile:fixedFile];
             else
-                success = [self synctexFindPage:&foundPageIndex 
location:&foundPoint forLine:line inFile:fixedFile];
+                success = [synctex findPage:&foundPageIndex 
location:&foundPoint forLine:line inFile:fixedFile];
             
             if (success && [self shouldKeepRunning]) {
                 if (pdfsync)
@@ -618,7 +474,51 @@
     scanner = NULL;
 }
 
+- (BOOL)findFileLine:(NSInteger *)linePtr file:(out NSString * __autoreleasing 
*)filePtr forLocation:(NSPoint)point inRect:(NSRect)rect 
pageBounds:(NSRect)bounds atPageIndex:(NSUInteger)pageIndex {
+    BOOL rv = NO;
+    if (synctex_edit_query(scanner, (int)pageIndex + 1, point.x, 
NSMaxY(bounds) - point.y) > 0) {
+        synctex_node_p node;
+        const char *file;
+        while (rv == NO && (node = synctex_scanner_next_result(scanner))) {
+            if ((file = synctex_scanner_get_name(scanner, 
synctex_node_tag(node)))) {
+                *linePtr = MAX(synctex_node_line(node), 1) - 1;
+                *filePtr = [NSString stringWithUTF8String:file];
+                rv = YES;
+            }
+        }
+    }
+    if (rv == NO)
+        NSLog(@"SyncTeX was unable to find file and line.");
+    return rv;
+}
 
+- (BOOL)findPage:(NSUInteger *)pageIndexPtr location:(NSPoint *)pointPtr 
forLine:(NSInteger)line inFile:(NSString *)file {
+    BOOL rv = NO;
+    char *filename = (char *)NSMapGet(filenames, (__bridge void *)file) ?: 
(char *)NSMapGet(filenames, (__bridge void *)[[file 
stringByResolvingSymlinksInPath] stringByStandardizingPath]);
+    if (filename == NULL) {
+        for (NSString *fn in filenames) {
+            if ([[fn lastPathComponent] caseInsensitiveCompare:[file 
lastPathComponent]] == NSOrderedSame) {
+                filename = (char *)NSMapGet(filenames, (__bridge void *)file);
+                break;
+            }
+        }
+        if (filename == NULL)
+            filename = (char *)[[file lastPathComponent] UTF8String];
+    }
+    if (synctex_display_query(scanner, filename, (int)line + 1, 0, -1) > 0) {
+        synctex_node_p node = synctex_scanner_next_result(scanner);
+        if (node) {
+            NSUInteger page = synctex_node_page(node);
+            *pageIndexPtr = MAX(page, 1ul) - 1;
+            *pointPtr = NSMakePoint(synctex_node_visible_h(node), 
synctex_node_visible_v(node));
+            rv = YES;
+        }
+    }
+    if (rv == NO)
+        NSLog(@"SyncTeX was unable to find location and page.");
+    return rv;
+}
+
 @end
 
 #pragma mark -
@@ -636,4 +536,117 @@
     return self;
 }
 
+- (BOOL)findFileLine:(NSInteger *)linePtr file:(out NSString * __autoreleasing 
*)filePtr forLocation:(NSPoint)point inRect:(NSRect)rect 
pageBounds:(NSRect)bounds atPageIndex:(NSUInteger)pageIndex {
+    BOOL rv = NO;
+    if (pageIndex < [pages count]) {
+        
+        SKPDFSyncRecord *record = nil;
+        SKPDFSyncRecord *beforeRecord = nil;
+        SKPDFSyncRecord *afterRecord = nil;
+        NSMutableDictionary *atRecords = [NSMutableDictionary dictionary];
+        
+        for (record in [pages objectAtIndex:pageIndex]) {
+            if ([record line] == 0)
+                continue;
+            NSPoint p = [record point];
+            if (p.y > NSMaxY(rect)) {
+                beforeRecord = record;
+            } else if (p.y < NSMinY(rect)) {
+                afterRecord = record;
+                break;
+            } else if (p.x < NSMinX(rect)) {
+                beforeRecord = record;
+            } else if (p.x > NSMaxX(rect)) {
+                afterRecord = record;
+                break;
+            } else {
+                [atRecords setObject:record forKey:[NSNumber 
numberWithDouble:fabs(p.x - point.x)]];
+            }
+        }
+        
+        record = nil;
+        if ([atRecords count]) {
+            NSNumber *nearest = [[[atRecords allKeys] 
sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:0];
+            record = [atRecords objectForKey:nearest];
+        } else if (beforeRecord && afterRecord) {
+            NSPoint beforePoint = [beforeRecord point];
+            NSPoint afterPoint = [afterRecord point];
+            if (beforePoint.y - point.y < point.y - afterPoint.y)
+                record = beforeRecord;
+            else if (beforePoint.y - point.y > point.y - afterPoint.y)
+                record = afterRecord;
+            else if (beforePoint.x - point.x < point.x - afterPoint.x)
+                record = beforeRecord;
+            else if (beforePoint.x - point.x > point.x - afterPoint.x)
+                record = afterRecord;
+            else
+                record = beforeRecord;
+        } else if (beforeRecord) {
+            record = beforeRecord;
+        } else if (afterRecord) {
+            record = afterRecord;
+        }
+        
+        if (record) {
+            *linePtr = [record line];
+            *filePtr = [record file];
+            rv = YES;
+        }
+    }
+    if (rv == NO)
+        NSLog(@"PDFSync was unable to find file and line.");
+    return rv;
+}
+
+- (BOOL)findPage:(NSUInteger *)pageIndexPtr location:(NSPoint *)pointPtr 
forLine:(NSInteger)line inFile:(NSString *)file {
+    BOOL rv = NO;
+    NSArray *theLines = [lines objectForKey:file];
+    if (theLines) {
+        
+        SKPDFSyncRecord *record = nil;
+        SKPDFSyncRecord *beforeRecord = nil;
+        SKPDFSyncRecord *afterRecord = nil;
+        SKPDFSyncRecord *atRecord = nil;
+        
+        for (record in theLines) {
+            if ([record pageIndex] == NSNotFound)
+                continue;
+            NSInteger l = [record line];
+            if (l < line) {
+                beforeRecord = record;
+            } else if (l > line) {
+                afterRecord = record;
+                break;
+            } else {
+                atRecord = record;
+                break;
+            }
+        }
+        
+        if (atRecord) {
+            record = atRecord;
+        } else if (beforeRecord && afterRecord) {
+            NSInteger beforeLine = [beforeRecord line];
+            NSInteger afterLine = [afterRecord line];
+            if (beforeLine - line > line - afterLine)
+                record = afterRecord;
+            else
+                record = beforeRecord;
+        } else if (beforeRecord) {
+            record = beforeRecord;
+        } else if (afterRecord) {
+            record = afterRecord;
+        }
+        
+        if (record) {
+            *pageIndexPtr = [record pageIndex];
+            *pointPtr = [record point];
+            rv = YES;
+        }
+    }
+    if (rv == NO)
+        NSLog(@"PDFSync was unable to find location and page.");
+    return rv;
+}
+
 @end

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



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to