KMPDFSynchronizer.swift 25 KB

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