Revision: 14165
          http://sourceforge.net/p/skim-app/code/14165
Author:   hofman
Date:     2024-04-02 14:41:20 +0000 (Tue, 02 Apr 2024)
Log Message:
-----------
embed data and scanner for synctex and pdfsync in objects, avoids the need to 
import the synctex header in the public header

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

Modified: trunk/SKPDFSynchronizer.h
===================================================================
--- trunk/SKPDFSynchronizer.h   2024-04-01 17:19:46 UTC (rev 14164)
+++ trunk/SKPDFSynchronizer.h   2024-04-02 14:41:20 UTC (rev 14165)
@@ -37,7 +37,6 @@
  */
 
 #import <Cocoa/Cocoa.h>
-#import "synctex_parser.h"
 #import <stdatomic.h>
 
 NS_ASSUME_NONNULL_BEGIN
@@ -50,7 +49,7 @@
 };
 
 @protocol SKPDFSynchronizerDelegate;
-@class SKPDFSyncRecord;
+@class SKSyncTeX, SKPDFSync;
 
 @interface SKPDFSynchronizer : NSObject {
     __weak id <SKPDFSynchronizerDelegate> delegate;
@@ -62,16 +61,12 @@
     NSString *fileName;
     NSString *syncFileName;
     NSDate *lastModDate;
-    BOOL isPdfsync;
     
     NSFileManager *fileManager;
     
-    NSMutableArray<NSMutableArray<SKPDFSyncRecord *> *> *pages;
-    NSMapTable *lines;
+    SKPDFSync *pdfsync;
+    SKSyncTeX *synctex;
     
-    NSMapTable *filenames;
-    synctex_scanner_p scanner;
-    
     _Atomic(BOOL) shouldKeepRunning;
 }
 

Modified: trunk/SKPDFSynchronizer.m
===================================================================
--- trunk/SKPDFSynchronizer.m   2024-04-01 17:19:46 UTC (rev 14164)
+++ trunk/SKPDFSynchronizer.m   2024-04-02 14:41:20 UTC (rev 14165)
@@ -37,6 +37,7 @@
  */
 
 #import "SKPDFSynchronizer.h"
+#import "synctex_parser.h"
 #import "SKPDFSyncRecord.h"
 #import "NSCharacterSet_SKExtensions.h"
 #import "NSScanner_SKExtensions.h"
@@ -54,6 +55,27 @@
 
 #pragma mark -
 
+@interface SKSyncTeX : NSObject {
+    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;
+@end
+
+#pragma mark -
+
+@interface SKPDFSync : NSObject {
+    NSMutableArray<NSMutableArray<SKPDFSyncRecord *> *> *pages;
+    NSMapTable *lines;
+}
+@property (nonatomic, readonly) NSMutableArray<NSMutableArray<SKPDFSyncRecord 
*> *> *pages;
+@property (nonatomic, readonly) NSMapTable *lines;
+@end
+
+#pragma mark -
+
 @implementation SKPDFSynchronizer
 
 @synthesize delegate;
@@ -74,14 +96,10 @@
         fileName = nil;
         syncFileName = nil;
         lastModDate = nil;
-        isPdfsync = YES;
         
-        pages = nil;
-        lines = nil;
+        pdfsync = nil;
+        synctex = nil;
         
-        filenames = nil;
-        scanner = NULL;
-        
         shouldKeepRunning = YES;
         
         // it is not safe to use the defaultManager on background threads
@@ -90,11 +108,6 @@
     return self;
 }
 
-- (void)dealloc {
-    if (scanner) synctex_scanner_free(scanner);
-    scanner = NULL;
-}
-
 - (void)terminate {
     // make sure we're not calling our delegate
     delegate = nil;
@@ -199,18 +212,9 @@
 
 - (BOOL)loadPdfsyncFile:(NSString *)theFileName {
 
-    if (pages)
-        [pages removeAllObjects];
-    else
-        pages = [[NSMutableArray alloc] init];
-    if (lines) {
-        [lines removeAllObjects];
-    } else {
-        lines = [[NSMapTable alloc] 
initWithKeyPointerFunctions:[NSPointerFunctions 
caseInsensitiveStringPointerFunctions] 
valuePointerFunctions:[NSPointerFunctions strongPointerFunctions] capacity:0];
-    }
+    pdfsync = [[SKPDFSync alloc] init];
     
     [self setSyncFileName:theFileName];
-    isPdfsync = YES;
     
     NSString *pdfsyncString = [NSString stringWithContentsOfFile:theFileName 
encoding:NSUTF8StringEncoding error:NULL];
     BOOL rv = NO;
@@ -237,7 +241,7 @@
             [files addObject:file];
             
             array = [[NSMutableArray alloc] init];
-            [lines setObject:array forKey:file];
+            [[pdfsync lines] setObject:array forKey:file];
             
             // we ignore the version
             if ([sc scanString:@"version" intoString:NULL] && [sc 
scanInteger:NULL]) {
@@ -254,7 +258,7 @@
                                 record = recordForIndex(records, recordIndex);
                                 [record setFile:file];
                                 [record setLine:line];
-                                [[lines objectForKey:file] addObject:record];
+                                [[[pdfsync lines] objectForKey:file] 
addObject:record];
                             }
                             break;
                         case 'p':
@@ -263,17 +267,17 @@
                                 [sc scanString:@"+" intoString:NULL];
                             if ([sc scanInteger:&recordIndex] && [sc 
scanDouble:&x] && [sc scanDouble:&y]) {
                                 record = recordForIndex(records, recordIndex);
-                                [record setPageIndex:[pages count] - 1];
+                                [record setPageIndex:[[pdfsync pages] count] - 
1];
                                 [record setPoint:NSMakePoint(PDFSYNC_TO_PDF(x) 
+ pdfOffset.x, PDFSYNC_TO_PDF(y) + pdfOffset.y)];
-                                [[pages lastObject] addObject:record];
+                                [[[pdfsync pages] lastObject] 
addObject:record];
                             }
                             break;
                         case 's':
-                            // start of a new page, the scanned integer should 
always equal [pages count]+1
-                            if ([sc scanInteger:&pageIndex] == NO) pageIndex = 
[pages count] + 1;
-                            while (pageIndex > (NSInteger)[pages count]) {
+                            // start of a new page, the scanned integer should 
always equal [[pdfsync pages] count]+1
+                            if ([sc scanInteger:&pageIndex] == NO) pageIndex = 
[[pdfsync pages] count] + 1;
+                            while (pageIndex > (NSInteger)[[pdfsync pages] 
count]) {
                                 array = [[NSMutableArray alloc] init];
-                                [pages addObject:array];
+                                [[pdfsync pages] addObject:array];
                             }
                             break;
                         case '(':
@@ -281,9 +285,9 @@
                             if ([sc scanUpToCharactersFromSet:newlines 
intoString:&file]) {
                                 file = [self sourceFileForFileName:file 
isTeX:YES removeQuotes:YES];
                                 [files addObject:file];
-                                if ([lines objectForKey:file] == nil) {
+                                if ([[pdfsync lines] objectForKey:file] == 
nil) {
                                     array = [[NSMutableArray alloc] init];
-                                    [lines setObject:array forKey:file];
+                                    [[pdfsync lines] setObject:array 
forKey:file];
                                 }
                             }
                             break;
@@ -308,9 +312,9 @@
                 NSSortDescriptor *ySortDescriptor = [[NSSortDescriptor alloc] 
initWithKey:@"y" ascending:NO];
                 NSArray *lineSortDescriptors = @[lineSortDescriptor];
                 
-                for (array in [lines objectEnumerator])
+                for (array in [[pdfsync lines] objectEnumerator])
                     [array sortUsingDescriptors:lineSortDescriptors];
-                [pages 
makeObjectsPerformSelector:@selector(sortUsingDescriptors:)
+                [[pdfsync pages] 
makeObjectsPerformSelector:@selector(sortUsingDescriptors:)
                                        withObject:@[ySortDescriptor, 
xSortDescriptor]];
                 
                 rv = [self shouldKeepRunning];
@@ -323,7 +327,7 @@
 
 - (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 < [pages count]) {
+    if (pageIndex < [[pdfsync pages] count]) {
         
         SKPDFSyncRecord *record = nil;
         SKPDFSyncRecord *beforeRecord = nil;
@@ -330,7 +334,7 @@
         SKPDFSyncRecord *afterRecord = nil;
         NSMutableDictionary *atRecords = [NSMutableDictionary dictionary];
         
-        for (record in [pages objectAtIndex:pageIndex]) {
+        for (record in [[pdfsync pages] objectAtIndex:pageIndex]) {
             if ([record line] == 0)
                 continue;
             NSPoint p = [record point];
@@ -385,7 +389,7 @@
 
 - (BOOL)pdfsyncFindPage:(NSUInteger *)pageIndexPtr location:(NSPoint 
*)pointPtr forLine:(NSInteger)line inFile:(NSString *)file {
     BOOL rv = NO;
-    NSArray *theLines = [lines objectForKey:file];
+    NSArray *theLines = [[pdfsync lines] objectForKey:file];
     if (theLines) {
         
         SKPDFSyncRecord *record = nil;
@@ -438,26 +442,16 @@
 
 - (BOOL)loadSynctexFileForFile:(NSString *)theFileName {
     BOOL rv = NO;
-    if (scanner)
-        synctex_scanner_free(scanner);
-    scanner = synctex_scanner_new_with_output_file([theFileName UTF8String], 
NULL, 1);
-    if (scanner) {
-        const char *fileRep = synctex_scanner_get_synctex(scanner);
+    synctex = [[SKSyncTeX alloc] initWithFileName:theFileName];
+    if (synctex) {
+        const char *fileRep = synctex_scanner_get_synctex([synctex scanner]);
         [self setSyncFileName:[self sourceFileForFileName:[NSString 
stringWithUTF8String:fileRep] isTeX:NO removeQuotes:NO]];
-        if (filenames) {
-            [filenames removeAllObjects];
-        } else {
-            NSPointerFunctions *keyPointerFunctions = [NSPointerFunctions 
caseInsensitiveStringPointerFunctions];
-            NSPointerFunctions *valuePointerFunctions = [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsMallocMemory | 
NSPointerFunctionsCStringPersonality | NSPointerFunctionsCopyIn];
-            filenames = [[NSMapTable alloc] 
initWithKeyPointerFunctions:keyPointerFunctions 
valuePointerFunctions:valuePointerFunctions capacity:0];
-        }
-        synctex_node_p node = synctex_scanner_input(scanner);
+        synctex_node_p node = synctex_scanner_input([synctex scanner]);
         do {
-            if ((fileRep = synctex_scanner_get_name(scanner, 
synctex_node_tag(node)))) {
-                NSMapInsert(filenames, (__bridge void *)[self 
sourceFileForFileName:[NSString stringWithUTF8String:fileRep] isTeX:YES 
removeQuotes:NO], (void *)fileRep);
+            if ((fileRep = synctex_scanner_get_name([synctex scanner], 
synctex_node_tag(node)))) {
+                NSMapInsert([synctex filenames], (__bridge void *)[self 
sourceFileForFileName:[NSString stringWithUTF8String:fileRep] isTeX:YES 
removeQuotes:NO], (void *)fileRep);
             }
         } while ((node = synctex_node_next(node)));
-        isPdfsync = NO;
         rv = [self shouldKeepRunning];
     }
     return rv;
@@ -465,11 +459,11 @@
 
 - (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(scanner, (int)pageIndex + 1, point.x, 
NSMaxY(bounds) - point.y) > 0) {
+    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(scanner))) {
-            if ((file = synctex_scanner_get_name(scanner, 
synctex_node_tag(node)))) {
+        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;
@@ -483,11 +477,11 @@
 
 - (BOOL)synctexFindPage:(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]);
+    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 filenames) {
+        for (NSString *fn in [synctex filenames]) {
             if ([[fn lastPathComponent] caseInsensitiveCompare:[file 
lastPathComponent]] == NSOrderedSame) {
-                filename = (char *)NSMapGet(filenames, (__bridge void *)file);
+                filename = (char *)NSMapGet([synctex filenames], (__bridge 
void *)file);
                 break;
             }
         }
@@ -494,8 +488,8 @@
         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 (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;
@@ -524,7 +518,7 @@
         
             if (currentModDate && [modDate compare:currentModDate] != 
NSOrderedDescending)
                 rv = YES;
-            else if (isPdfsync)
+            else if (pdfsync)
                 rv = [self loadPdfsyncFile:theSyncFileName];
             else
                 rv = [self loadSynctexFileForFile:theFileName];
@@ -559,7 +553,7 @@
             NSString *foundFile = nil;
             BOOL success = NO;
             
-            if (isPdfsync)
+            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];
@@ -584,13 +578,13 @@
             BOOL success = NO;
             NSString *fixedFile = [self sourceFileForFileName:file isTeX:YES 
removeQuotes:NO];
             
-            if (isPdfsync)
+            if (pdfsync)
                 success = [self pdfsyncFindPage:&foundPageIndex 
location:&foundPoint forLine:line inFile:fixedFile];
             else
                 success = [self synctexFindPage:&foundPageIndex 
location:&foundPoint forLine:line inFile:fixedFile];
             
             if (success && [self shouldKeepRunning]) {
-                if (isPdfsync)
+                if (pdfsync)
                     foundOptions &= ~SKPDFSynchronizerFlippedMask;
                 else
                     foundOptions |= SKPDFSynchronizerFlippedMask;
@@ -603,3 +597,49 @@
 }
 
 @end
+
+#pragma mark -
+
+@implementation SKSyncTeX
+
+@synthesize scanner, filenames;
+
+- (instancetype)initWithFileName:(NSString *)fileName {
+    self = [super init];
+    if (self) {
+        scanner = synctex_scanner_new_with_output_file([fileName UTF8String], 
NULL, 1);
+        if (scanner) {
+            NSPointerFunctions *keyPointerFunctions = [NSPointerFunctions 
caseInsensitiveStringPointerFunctions];
+            NSPointerFunctions *valuePointerFunctions = [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsMallocMemory | 
NSPointerFunctionsCStringPersonality | NSPointerFunctionsCopyIn];
+            filenames = [[NSMapTable alloc] 
initWithKeyPointerFunctions:keyPointerFunctions 
valuePointerFunctions:valuePointerFunctions capacity:0];
+        } else {
+            self = nil;
+        }
+    }
+    return self;
+}
+
+- (void)dealloc {
+    if (scanner) synctex_scanner_free(scanner);
+    scanner = NULL;
+}
+
+
+@end
+
+#pragma mark -
+
+@implementation SKPDFSync
+
+@synthesize pages, lines;
+
+- (instancetype)init {
+    self = [super init];
+    if (self) {
+        pages = [[NSMutableArray alloc] init];
+        lines = [[NSMapTable alloc] 
initWithKeyPointerFunctions:[NSPointerFunctions 
caseInsensitiveStringPointerFunctions] 
valuePointerFunctions:[NSPointerFunctions strongPointerFunctions] capacity:0];
+    }
+    return self;
+}
+
+@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