KMPDFSynchronizer.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //
  2. // KMPDFSynchronizer.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/2/28.
  6. //
  7. import Cocoa
  8. enum KMPDFSynchronizerType: Int {
  9. case defaultOptions = 0
  10. case showReadingBarMask = 1
  11. case flippedMask = 2
  12. }
  13. let PDFSYNC_TO_PDF: (CGFloat) -> CGFloat = { coord in
  14. return CGFloat(coord) / 65536.0
  15. }
  16. let SKPDFSynchronizerPdfsyncExtension = "pdfsync"
  17. var SKPDFSynchronizerTexExtensions: [String] = ["tex", "ltx", "latex", "ctx", "lyx", "rnw"]
  18. let STACK_BUFFER_SIZE = 256
  19. func caseInsensitiveStringEqual(_ item1: UnsafeRawPointer, _ item2: UnsafeRawPointer, _ size: ((UnsafeRawPointer) -> Int)?) -> Bool {
  20. return CFStringCompare(item1 as! CFString, item2 as! CFString, CFStringCompareFlags.compareCaseInsensitive) == CFComparisonResult.compareEqualTo
  21. }
  22. //func caseInsensitiveStringHash(item: UnsafeRawPointer, size: @escaping (UnsafeRawPointer) -> UInt) -> UInt {
  23. // guard let item = item else { return 0 }
  24. //
  25. // var hash: UInt = 0
  26. // let allocator = CFGetAllocator(item)
  27. // let len = CFStringGetLength(item as! CFString)
  28. //
  29. // let STACK_BUFFER_SIZE = 1024 // 设置STACK_BUFFER_SIZE的值,以便与C代码匹配
  30. //
  31. // var buffer = [UniChar](repeating: 0, count: STACK_BUFFER_SIZE)
  32. // let bufferPtr: UnsafeMutablePointer<UniChar>
  33. // if len + 10 >= STACK_BUFFER_SIZE {
  34. // bufferPtr = UnsafeMutablePointer<UniChar>(OpaquePointer(CFAllocatorAllocate(allocator, (len + 10) * MemoryLayout<UniChar>.size, 0)))
  35. // } else {
  36. // bufferPtr = UnsafeMutablePointer<UniChar>(mutating: buffer)
  37. // }
  38. //
  39. // CFStringGetCharacters(item as! CFString, CFRange(location: 0, length: len), bufferPtr)
  40. //
  41. // let mutableString = CFStringCreateMutableWithExternalCharactersNoCopy(allocator, bufferPtr, len, len + 10, (bufferPtr != buffer ? allocator : kCFAllocatorNull))
  42. // CFStringLowercase(mutableString, nil)
  43. // hash = (mutableString as AnyObject).hash
  44. // CFRelease(mutableString)
  45. //
  46. // if bufferPtr != buffer {
  47. // CFAllocatorDeallocate(allocator, bufferPtr)
  48. // }
  49. //
  50. // return hash
  51. //}
  52. protocol KMPDFSynchronizerDelegate: AnyObject {
  53. func synchronizer(_ synchronizer: KMPDFSynchronizer, foundLine line: Int, inFile file: String)
  54. func synchronizer(_ synchronizer: KMPDFSynchronizer, foundLocation point: NSPoint, atPageIndex pageIndex: UInt, options: Int)
  55. }
  56. class KMPDFSynchronizer: NSObject {
  57. weak var delegate: KMPDFSynchronizerDelegate?
  58. var fileName: String = ""
  59. var shouldKeepRunning: Bool {
  60. OSMemoryBarrier()
  61. return shouldKeepRunningFlag == 1
  62. }
  63. private var queue: DispatchQueue = DispatchQueue(label: "net.sourceforge.queue.KMPDFSynchronizer")
  64. private var lockQueue: DispatchQueue = DispatchQueue(label: "net.sourceforge.lockQueue.KMPDFSynchronizer")
  65. private var syncFileName: String = "" {
  66. didSet {
  67. if syncFileName.count == 0 {
  68. lastModDate = nil
  69. } else {
  70. do {
  71. let attributes = try FileManager.default.attributesOfItem(atPath: syncFileName)
  72. lastModDate = attributes[.modificationDate] as? Date
  73. } catch {
  74. }
  75. }
  76. }
  77. }
  78. private var lastModDate: Date?
  79. private var isPdfsync: Bool = true
  80. private var fileManager: FileManager = FileManager()
  81. private var pages: [Any] = []
  82. private var lines: NSMapTable<AnyObject, AnyObject>?
  83. private var filenames: NSMapTable<AnyObject, AnyObject>?
  84. private var scanner: synctex_scanner_p?
  85. private var shouldKeepRunningFlag: Int32 = 1
  86. func setFileName(_ newFileName: String) {
  87. // We compare filenames in canonical form throughout, so we need to make sure fileName also is in canonical form
  88. if let canonicalFileName = NSURL(fileURLWithPath: newFileName).resolvingSymlinksInPath?.standardizedFileURL.path {
  89. DispatchQueue.global().async { [weak self] in
  90. guard let self = self else { return }
  91. self.lockQueue.sync {
  92. if self.fileName != canonicalFileName {
  93. if self.fileName != newFileName {
  94. self.syncFileName = ""
  95. self.lastModDate = nil
  96. }
  97. self.fileName = canonicalFileName
  98. }
  99. }
  100. }
  101. }
  102. }
  103. func terminate() {
  104. delegate = nil
  105. let originalValue = OSAtomicCompareAndSwap32Barrier(1, 0, &shouldKeepRunningFlag)
  106. // originalValue 为原来的值,如果原来的值是 1,表示已经交换成功,如果是 0,表示没有成功交换
  107. // 如果需要对返回值进行处理,可以在这里添加逻辑
  108. }
  109. func findFileAndLine(forLocation point: NSPoint, inRect rect: NSRect, pageBounds bounds: NSRect, atPageIndex pageIndex: UInt) {
  110. // Implement this method
  111. }
  112. func findPageAndLocation(forLine line: Int, inFile file: String, options: Int) {
  113. // Implement this method
  114. }
  115. }
  116. //MARK: Support
  117. extension KMPDFSynchronizer {
  118. func sourceFile(forFileName file: String, isTeX: Bool, removeQuotes: Bool) -> String {
  119. var fileName = file
  120. if removeQuotes && fileName.count > 2 && fileName.first == "\"" && fileName.last == "\"" {
  121. fileName = String(fileName.dropFirst().dropLast())
  122. }
  123. if !(fileName as NSString).isAbsolutePath {
  124. fileName = (self.fileName as NSString).deletingLastPathComponent + "/" + fileName
  125. }
  126. if isTeX && !FileManager.default.fileExists(atPath: fileName) && !SKPDFSynchronizerTexExtensions.contains((fileName as NSString).pathExtension.lowercased()) {
  127. for ext in SKPDFSynchronizerTexExtensions {
  128. let tryFile = fileName + "." + ext
  129. if FileManager.default.fileExists(atPath: tryFile) {
  130. fileName = tryFile
  131. break
  132. }
  133. }
  134. }
  135. // Swift's `standardizedFileURL` property does both `resolvingSymlinksInPath` and `standardizingPath`
  136. return URL(fileURLWithPath: fileName).standardizedFileURL.path
  137. }
  138. func defaultSourceFile() -> String {
  139. let file = (self.fileName as NSString).deletingPathExtension
  140. for `extension` in SKPDFSynchronizerTexExtensions {
  141. let tryFile = file + "." + `extension`
  142. if FileManager.default.fileExists(atPath: tryFile) {
  143. return tryFile
  144. }
  145. }
  146. return file + "." + SKPDFSynchronizerTexExtensions.first!
  147. }
  148. }
  149. //MARK: PDFSync
  150. extension KMPDFSynchronizer{
  151. func recordForIndex(_ records: NSMapTable<AnyObject, AnyObject>, _ recordIndex: Int) -> KMPDFSyncRecord {
  152. if let record = records.object(forKey: recordIndex as AnyObject) as? KMPDFSyncRecord {
  153. return record
  154. } else {
  155. let record = KMPDFSyncRecord(recordIndex: recordIndex)
  156. records.setObject(record, forKey: recordIndex as AnyObject)
  157. return record
  158. }
  159. }
  160. func loadPdfsyncFile(_ theFileName: String) -> Bool {
  161. pages.removeAll()
  162. if lines != nil {
  163. lines?.removeAllObjects()
  164. } else {
  165. // let keyPointerFunctions = NSPointerFunctions(options: [.strongMemory, .objectPersonality])
  166. // keyPointerFunctions.isEqualFunction = { (a, b) in
  167. // caseInsensitiveStringEqual(a, b) { <#UnsafeRawPointer#> in
  168. // <#code#>
  169. // }
  170. // guard let strA = a as? String, let strB = b as? String else { return false }
  171. // return strA.caseInsensitiveCompare(strB) == .orderedSame
  172. // }
  173. // keyPointerFunctions.hashFunction = { (ptr) in
  174. // guard let str = ptr as? String else { return 0 }
  175. // return str.hash
  176. // }
  177. // let valuePointerFunctions = NSPointerFunctions(options: [.strongMemory, .objectPersonality])
  178. // lines = NSMapTable(keyPointerFunctions: keyPointerFunctions, valuePointerFunctions: valuePointerFunctions,capacity: 0)
  179. }
  180. syncFileName = theFileName
  181. isPdfsync = true
  182. guard let pdfsyncString = try? String(contentsOfFile: theFileName, encoding: .utf8) else { return false }
  183. var rv = false
  184. let records = NSMapTable<NSNumber, AnyObject>.strongToStrongObjects()
  185. let files = NSMutableArray()
  186. var recordIndex = 0, line = 0, pageIndex = 0
  187. var x = 0.0, y = 0.0
  188. var record: KMPDFSyncRecord?
  189. var array: NSMutableArray?
  190. var ch: unichar = 0
  191. let sc = Scanner(string: pdfsyncString)
  192. let newlines = CharacterSet.newlines
  193. sc.charactersToBeSkipped = CharacterSet.whitespaces
  194. var file: NSString?
  195. var tryFile: String
  196. let scanString = sc.scanUpToCharacters(from: newlines)
  197. let scanCharactersString = sc.scanCharacters(from: newlines)
  198. if scanString?.count != 0 && scanCharactersString?.count != 0 {
  199. file = sourceFile(forFileName: scanString! as String, isTeX: true, removeQuotes: true) as NSString
  200. files.add(file!)
  201. array = NSMutableArray()
  202. lines!.setObject(array!, forKey: file!)
  203. sc.scanString("version", into: nil)
  204. sc.scanInt(nil)
  205. sc.scanCharacters(from: newlines, into: nil)
  206. // while sc.shouldKeepRunning() && sc.scanCharacter(&ch) {
  207. // switch ch {
  208. // case "l":
  209. // if sc.scanInt(&recordIndex) && sc.scanInt(&line) {
  210. // // we ignore the column
  211. // sc.scanInt(nil)
  212. // record = recordForIndex(records, recordIndex)
  213. // record!.file = file! as String
  214. // record!.line = line
  215. // lines!.object(forKey: file! as NSString)!.add(record!)
  216. // }
  217. // case "p":
  218. // // we ignore * and + modifiers
  219. // if !sc.scanString("*", into: nil) {
  220. // sc.scanString("+", into: nil)
  221. // }
  222. // if sc.scanInt(&recordIndex) && sc.scanDouble(&x) && sc.scanDouble(&y) {
  223. // record = recordForIndex(records, recordIndex)
  224. // record!.pageIndex = pages.count - 1
  225. // record!.point = NSMakePoint(CGFloat(PDFSYNC_TO_PDF(x) + pdfOffset.x), CGFloat(PDFSYNC_TO_PDF(y) + pdfOffset.y))
  226. // (pages.lastObject as! NSMutableArray).add(record!)
  227. // }
  228. // case "s":
  229. // // start of a new page, the scanned integer should always equal [pages count]+1
  230. // var tempPageIndex = 0
  231. // if !sc.scanInt(&tempPageIndex) {
  232. // pageIndex = pages.count + 1
  233. // } else {
  234. // pageIndex = tempPageIndex
  235. // }
  236. // while pageIndex > pages.count {
  237. // array = NSMutableArray()
  238. // pages.add(array!)
  239. // }
  240. // case "(":
  241. // // start of a new source file
  242. // var tempFile: NSString?
  243. // if sc.scanUpToCharacters(from: newlines, into: &tempFile as? String) {
  244. // file = sourceFile(forFileName: tempFile! as String, isTeX: true, removeQuotes: true) as NSString
  245. // files.add(file!)
  246. // if lines!.object(forKey: file!) == nil {
  247. // array = NSMutableArray()
  248. // lines!.setObject(array!, forKey: file!)
  249. // }
  250. // }
  251. // case ")":
  252. // // closing of a source file
  253. // if files.count > 0 {
  254. // files.removeLastObject()
  255. // file = files.lastObject as? NSString
  256. // }
  257. // default:
  258. // // shouldn't reach
  259. // break
  260. // }
  261. //
  262. // sc.scanUpToCharacters(from: newlines, into: nil)
  263. // sc.scanCharacters(from: newlines, into: nil)
  264. // }
  265. let lineSortDescriptor = NSSortDescriptor(key: "line", ascending: true)
  266. let lineSortDescriptors = [lineSortDescriptor]
  267. // for array in lines!.objectEnumerator() {
  268. // (array as! NSMutableArray).sort(using: lineSortDescriptors)
  269. // }
  270. for array in pages {
  271. (array as! NSMutableArray).sort(using: [NSSortDescriptor(key: "y", ascending: false), NSSortDescriptor(key: "x", ascending: true)])
  272. }
  273. // rv = sc.shouldKeepRunning
  274. }
  275. return rv
  276. }
  277. func pdfsyncFindFileLine(_ linePtr: inout Int, file filePtr: inout String?, forLocation point: NSPoint, inRect rect: NSRect, pageBounds bounds: NSRect, atPageIndex pageIndex: Int) -> Bool {
  278. var rv = false
  279. if pageIndex < pages.count {
  280. var record: KMPDFSyncRecord?
  281. var beforeRecord: KMPDFSyncRecord?
  282. var afterRecord: KMPDFSyncRecord?
  283. var atRecords = [Double: KMPDFSyncRecord]()
  284. // for case let tempRecord as KMPDFSyncRecord in pages[pageIndex] {
  285. // if tempRecord.line == 0 {
  286. // continue
  287. // }
  288. // let p = tempRecord.point
  289. // if p.y > NSMaxY(rect) {
  290. // beforeRecord = tempRecord
  291. // } else if p.y < NSMinY(rect) {
  292. // afterRecord = tempRecord
  293. // break
  294. // } else if p.x < NSMinX(rect) {
  295. // beforeRecord = tempRecord
  296. // } else if p.x > NSMaxX(rect) {
  297. // afterRecord = tempRecord
  298. // break
  299. // } else {
  300. // atRecords[abs(p.x - point.x)] = tempRecord
  301. // }
  302. // }
  303. var nearestRecord: KMPDFSyncRecord?
  304. if !atRecords.isEmpty {
  305. let nearest = atRecords.keys.sorted()[0]
  306. nearestRecord = atRecords[nearest]
  307. } else if let beforeRecord = beforeRecord, let afterRecord = afterRecord {
  308. let beforePoint = beforeRecord.point
  309. let afterPoint = afterRecord.point
  310. if beforePoint.y - point.y < point.y - afterPoint.y {
  311. nearestRecord = beforeRecord
  312. } else if beforePoint.y - point.y > point.y - afterPoint.y {
  313. nearestRecord = afterRecord
  314. } else if beforePoint.x - point.x < point.x - afterPoint.x {
  315. nearestRecord = beforeRecord
  316. } else if beforePoint.x - point.x > point.x - afterPoint.x {
  317. nearestRecord = afterRecord
  318. } else {
  319. nearestRecord = beforeRecord
  320. }
  321. } else if let beforeRecord = beforeRecord {
  322. nearestRecord = beforeRecord
  323. } else if let afterRecord = afterRecord {
  324. nearestRecord = afterRecord
  325. }
  326. if let record = nearestRecord {
  327. linePtr = record.line
  328. filePtr = record.file
  329. rv = true
  330. }
  331. }
  332. if !rv {
  333. print("PDFSync was unable to find file and line.")
  334. }
  335. return rv
  336. }
  337. func pdfsyncFindPage(_ pageIndexPtr: inout Int, location pointPtr: inout NSPoint, forLine line: Int, inFile file: String) -> Bool {
  338. var rv = false
  339. if let theLines = lines!.object(forKey: file as NSString) as? [KMPDFSyncRecord] {
  340. var record: KMPDFSyncRecord?
  341. var beforeRecord: KMPDFSyncRecord?
  342. var afterRecord: KMPDFSyncRecord?
  343. var atRecord: KMPDFSyncRecord?
  344. for tempRecord in theLines {
  345. if tempRecord.pageIndex == NSNotFound {
  346. continue
  347. }
  348. let l = tempRecord.line
  349. if l < line {
  350. beforeRecord = tempRecord
  351. } else if l > line {
  352. afterRecord = tempRecord
  353. break
  354. } else {
  355. atRecord = tempRecord
  356. break
  357. }
  358. }
  359. if let atRecord = atRecord {
  360. record = atRecord
  361. } else if let beforeRecord = beforeRecord, let afterRecord = afterRecord {
  362. let beforeLine = beforeRecord.line
  363. let afterLine = afterRecord.line
  364. if beforeLine - line > line - afterLine {
  365. record = afterRecord
  366. } else {
  367. record = beforeRecord
  368. }
  369. } else if let beforeRecord = beforeRecord {
  370. record = beforeRecord
  371. } else if let afterRecord = afterRecord {
  372. record = afterRecord
  373. }
  374. if let record = record {
  375. pageIndexPtr = record.pageIndex
  376. pointPtr = record.point
  377. rv = true
  378. }
  379. }
  380. if !rv {
  381. print("PDFSync was unable to find location and page.")
  382. }
  383. return rv
  384. }
  385. }
  386. //MARK: SyncTeX
  387. extension KMPDFSynchronizer {
  388. func loadSynctexFile(forFile theFileName: String) -> Bool {
  389. var rv = false
  390. if let scanner = scanner {
  391. synctex_scanner_free(scanner)
  392. }
  393. scanner = synctex_scanner_new_with_output_file(theFileName.cString(using: .utf8), nil, 1)
  394. // if let scanner = scanner {
  395. // let fileRep = synctex_scanner_get_synctex(scanner)
  396. // syncFileName = sourceFile(forFileName: String(cString: fileRep!), isTeX: false, removeQuotes: false)
  397. // if let filenames = filenames {
  398. // NSResetMapTable(filenames)
  399. // } else {
  400. // let keyPointerFunctions = NSPointerFunctions(options: [.strongMemory, .objectPersonality])
  401. // keyPointerFunctions.isEqualFunction = { (a, b) in
  402. // guard let strA = a as? String, let strB = b as? String else { return false }
  403. // return strA.caseInsensitiveCompare(strB) == .orderedSame
  404. // }
  405. // keyPointerFunctions.hashFunction = { (ptr) in
  406. // guard let str = ptr as? String else { return 0 }
  407. // return str.hash
  408. // }
  409. // let valuePointerFunctions = NSPointerFunctions(options: [.mallocMemory, .cstringPersonality, .copyIn])
  410. // filenames = NSMapTable(keyOptions: keyPointerFunctions, valueOptions: valuePointerFunctions)
  411. // }
  412. // var node = synctex_scanner_input(scanner)
  413. // repeat {
  414. // if let fileRep = synctex_scanner_get_name(scanner, synctex_node_tag(node)) {
  415. // filenames!.setObject(String(cString: fileRep), forKey: sourceFile(forFileName: String(cString: fileRep), isTeX: true, removeQuotes: false))
  416. // }
  417. // } while ((node = synctex_node_next(node)) != nil)
  418. // isPdfsync = false
  419. // rv = shouldKeepRunning
  420. // }
  421. return rv
  422. }
  423. func synctexFindFileLine(_ linePtr: inout Int, file filePtr: inout String?, forLocation point: NSPoint, inRect rect: NSRect, pageBounds bounds: NSRect, atPageIndex pageIndex: Int) -> Bool {
  424. var rv = false
  425. if synctex_edit_query(scanner, Int32(pageIndex + 1), Float(Double(point.x)), Float(Double(NSMaxY(bounds) - point.y))) > 0 {
  426. var node: synctex_node_p?
  427. var file: UnsafePointer<Int8>?
  428. while rv == false && (node = synctex_scanner_next_result(scanner)) != nil {
  429. if let tempFile = synctex_scanner_get_name(scanner, synctex_node_tag(node)) {
  430. linePtr = Int(max(synctex_node_line(node), 1) - 1)
  431. filePtr = sourceFile(forFileName: String(cString: tempFile), isTeX: true, removeQuotes: false)
  432. rv = true
  433. }
  434. }
  435. }
  436. if rv == false {
  437. NSLog("SyncTeX was unable to find file and line.")
  438. }
  439. return rv
  440. }
  441. func synctexFindPage(pageIndexPtr: UnsafeMutablePointer<UInt>, pointPtr: UnsafeMutablePointer<CGPoint>, forLine line: Int, inFile file: String) -> Bool {
  442. guard let filenames = filenames else { return false }
  443. var rv = false
  444. // var filename: UnsafeMutableRawPointer = NSMapGet(filenames, file) ?? UnsafeMutableRawPointer(<#Builtin.RawPointer#>)
  445. //?? NSMapGet(filenames, (file as NSString).resolvingSymlinksInPath.standardizingPath)
  446. // if filename == nil {
  447. // for fn in filenames.allKeys {
  448. // if let fnString = fn as? String, fnString.lastPathComponent.caseInsensitiveCompare(file.lastPathComponent) == .orderedSame {
  449. // filename = NSMapGet(filenames, fn) as? UnsafePointer<Int8>
  450. // break
  451. // }
  452. // }
  453. //
  454. // if filename == nil {
  455. // filename = (file as NSString).lastPathComponent.utf8String
  456. // }
  457. // }
  458. // if synctex_display_query(scanner, filename, Int32(line) + 1, 0, -1) > 0 {
  459. // if let node = synctex_scanner_next_result(scanner) {
  460. // let page = UInt(synctex_node_page(node))
  461. // pageIndexPtr.pointee = max(page, 1) - 1
  462. // pointPtr.pointee = CGPoint(x: CGFloat(synctex_node_visible_h(node)), y: CGFloat(synctex_node_visible_v(node)))
  463. // rv = true
  464. // }
  465. // }
  466. if !rv {
  467. NSLog("SyncTeX was unable to find location and page.")
  468. }
  469. return rv
  470. }
  471. }
  472. //MARK: Generic
  473. extension KMPDFSynchronizer {
  474. func loadSyncFileIfNeeded() -> Bool {
  475. let theFileName = fileName
  476. var rv = false
  477. if theFileName.count != 0 {
  478. var theSyncFileName = self.syncFileName
  479. var modDate: NSDate?
  480. if theSyncFileName.count != 0 && fileManager.fileExists(atPath: theSyncFileName) {
  481. do {
  482. let attributes = try FileManager.default.attributesOfItem(atPath: theFileName)
  483. modDate = attributes[.modificationDate] as? NSDate
  484. } catch {
  485. }
  486. let currentModDate = self.lastModDate
  487. if (currentModDate != nil) && modDate?.compare(currentModDate!) != ComparisonResult.orderedDescending {
  488. rv = true
  489. } else if (isPdfsync) {
  490. rv = self.loadPdfsyncFile(theSyncFileName)
  491. } else {
  492. rv = self.loadSynctexFile(forFile: theFileName)
  493. }
  494. } else {
  495. rv = self.loadSynctexFile(forFile: theFileName)
  496. if rv == false {
  497. theSyncFileName = (theFileName as NSString).deletingPathExtension.stringByAppendingPathExtension("pdfsync")
  498. if fileManager.fileExists(atPath: theSyncFileName) {
  499. rv = self.loadPdfsyncFile(theSyncFileName)
  500. }
  501. }
  502. }
  503. }
  504. if rv == false {
  505. print("Unable to find or load synctex or pdfsync file.")
  506. }
  507. return rv
  508. }
  509. }
  510. //MARK: Finding API
  511. extension KMPDFSynchronizer {
  512. func findFileAndLine(forLocation point: NSPoint, inRect rect: NSRect, pageBounds bounds: NSRect, atPageIndex pageIndex: Int) {
  513. queue.async {
  514. guard self.shouldKeepRunning, self.loadSyncFileIfNeeded() else { return }
  515. var foundLine = 0
  516. var foundFile: String?
  517. var success = false
  518. if self.isPdfsync {
  519. success = self.pdfsyncFindFileLine(&foundLine, file: &foundFile, forLocation: point, inRect: rect, pageBounds: bounds, atPageIndex: pageIndex)
  520. } else {
  521. success = self.synctexFindFileLine(&foundLine, file: &foundFile, forLocation: point, inRect: rect, pageBounds: bounds, atPageIndex: pageIndex)
  522. }
  523. if success, self.shouldKeepRunning {
  524. DispatchQueue.main.async {
  525. self.delegate?.synchronizer(self, foundLine: foundLine, inFile: foundFile ?? "")
  526. }
  527. }
  528. }
  529. }
  530. func findPageAndLocation(forLine line: Int, inFile file: String?, options: Int) {
  531. let fixedFile = file ?? defaultSourceFile()
  532. // queue.async {
  533. // guard let fixedFile = fixedFile, self.shouldKeepRunning, self.loadSyncFileIfNeeded() else { return }
  534. //
  535. // var foundPageIndex = NSNotFound
  536. // var foundPoint = NSZeroPoint
  537. // var foundOptions = options
  538. //
  539. // if self.isPdfsync {
  540. // self.pdfsyncFindPage(&foundPageIndex, location: &foundPoint, forLine: line, inFile: fixedFile)
  541. // } else {
  542. // self.synctexFindPage(&foundPageIndex, location: &foundPoint, forLine: line, inFile: fixedFile)
  543. // }
  544. //
  545. // if self.shouldKeepRunning() {
  546. // if self.isPdfsync {
  547. // foundOptions &= ~KMPDFSynchronizerType.flippedMask
  548. // } else {
  549. // foundOptions |= KMPDFSynchronizerType.flippedMask
  550. // }
  551. // DispatchQueue.main.async {
  552. // self.delegate?.synchronizer(self, foundLocation: foundPoint, atPageIndex: foundPageIndex, options: foundOptions)
  553. // }
  554. // }
  555. // }
  556. }
  557. }