-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathworker.ts
254 lines (230 loc) · 9.31 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import * as fs from 'fs'
import * as iconv from 'iconv-lite'
import * as path from 'path'
import * as zlib from 'zlib'
import { lw } from '../../lw'
import type { SyncTeXRecordToPDF, SyncTeXRecordToTeX } from '../../types'
import { PdfSyncObject, parseSyncTex, Block } from './synctexjs'
import { iconvLiteSupportedEncodings } from '../../utils/convertfilename'
import { isSameRealPath } from '../../utils/pathnormalize'
const logger = lw.log('SyncTeX')
export {
syncTeXToPDF,
syncTeXToTeX
}
class Rectangle {
readonly top: number
readonly bottom: number
readonly left: number
readonly right: number
constructor( {top, bottom, left, right}: { top: number, bottom: number, left: number, right: number} ) {
this.top = top
this.bottom = bottom
this.left = left
this.right = right
}
include(rect: Rectangle): boolean {
return this.left <= rect.left && this.right >= rect.right && this.bottom >= rect.bottom && this.top <= rect.top
}
distanceY(y: number): number {
return Math.min( Math.abs(this.bottom - y), Math.abs(this.top - y) )
}
distanceXY(x: number, y: number): number {
return Math.sqrt(Math.pow(Math.min( Math.abs(this.bottom - y), Math.abs(this.top - y) ), 2) + Math.pow(Math.min( Math.abs(this.left - x), Math.abs(this.right - x) ), 2))
}
distanceFromCenter(x: number, y: number): number {
return Math.sqrt(Math.pow((this.left + this.right) / 2 - x, 2) + Math.pow((this.bottom + this.top) / 2 - y, 2))
}
}
function getBlocks(linePageBlocks: { [inputLineNum: number]: { [pageNum: number]: Block[] } }, lineNum: number ): Block[] {
const pageBlocks = linePageBlocks[lineNum]
const pageNums = Object.keys(pageBlocks)
if (pageNums.length === 0) {
logger.log('No page number found.')
return []
}
const page = pageNums[0]
return pageBlocks[Number(page)]
}
function toRect(blocks: Block): Rectangle
function toRect(blocks: Block[]): Rectangle
function toRect(blocks: any): Rectangle {
if (!Array.isArray(blocks)) {
const block = blocks as Block
const top = block.bottom - block.height
const bottom = block.bottom
const left = block.left
const right = block.width ? block.left + block.width : block.left
return new Rectangle({top, bottom, left, right})
} else {
let cTop = 2e16
let cBottom = 0
let cLeft = 2e16
let cRight = 0
for (const b of blocks as Block[]) {
// Skip a block if they have boxes inside, or their type is kern or rule.
// See also https://github.com/jlaurens/synctex/blob/2017/synctex_parser.c#L4655 for types.
if (b.elements !== undefined || b.type === 'k' || b.type === 'r') {
continue
}
cBottom = Math.max(b.bottom, cBottom)
const top = b.bottom - b.height
cTop = Math.min(top, cTop)
cLeft = Math.min(b.left, cLeft)
if (b.width !== undefined) {
const right = b.left + b.width
cRight = Math.max(right, cRight)
}
}
return new Rectangle({ top: cTop, bottom: cBottom, left: cLeft, right: cRight })
}
}
function parseSyncTexForPdf(pdfFile: string): PdfSyncObject | undefined {
const filename = path.basename(pdfFile, path.extname(pdfFile))
const dir = path.dirname(pdfFile)
const synctexFile = path.resolve(dir, filename + '.synctex')
const synctexFileGz = synctexFile + '.gz'
if (fs.existsSync(synctexFile)) {
try {
logger.log(`Parsing .synctex ${synctexFile} .`)
const s = fs.readFileSync(synctexFile, {encoding: 'binary'})
return parseSyncTex(s)
} catch (e: unknown) {
logger.logError(`Failed parsing .synctex ${synctexFile}:`, e)
}
} else if (fs.existsSync(synctexFileGz)) {
try {
logger.log(`Parsing .synctex.gz ${synctexFileGz} .`)
const data = fs.readFileSync(synctexFileGz)
const b = zlib.gunzipSync(data)
const s = b.toString('binary')
return parseSyncTex(s)
} catch (e: unknown) {
logger.logError(`Failed parsing .synctex.gz ${synctexFileGz}:`, e)
}
}
logger.log(`${synctexFile}, ${synctexFileGz} not found.`)
return undefined
}
function findInputFilePathForward(filePath: string, pdfSyncObject: PdfSyncObject): string | undefined {
for (const inputFilePath in pdfSyncObject.blockNumberLine) {
try {
if (isSameRealPath(inputFilePath, filePath)) {
return inputFilePath
}
} catch { }
}
for (const inputFilePath in pdfSyncObject.blockNumberLine) {
for (const enc of iconvLiteSupportedEncodings) {
let convertedInputFilePath = ''
try {
convertedInputFilePath = iconv.decode(Buffer.from(inputFilePath, 'binary'), enc)
if (isSameRealPath(convertedInputFilePath, filePath)) {
return inputFilePath
}
} catch { }
}
}
return
}
function syncTeXToPDF(line: number, filePath: string, pdfFile: string): SyncTeXRecordToPDF | undefined {
const pdfSyncObject = parseSyncTexForPdf(pdfFile)
if (!pdfSyncObject) {
return undefined
}
const inputFilePath = findInputFilePathForward(filePath, pdfSyncObject)
if (inputFilePath === undefined) {
logger.log('No relevant entries found.')
return undefined
}
const linePageBlocks = pdfSyncObject.blockNumberLine[inputFilePath]
const lineNums = Object.keys(linePageBlocks).map(x => Number(x)).sort( (a, b) => { return (a - b) } )
const i = lineNums.findIndex( x => x >= line )
if (i === 0 || lineNums[i] === line) {
const l = lineNums[i]
const blocks = getBlocks(linePageBlocks, l)
const c = toRect(blocks)
return { page: blocks[0].page, x: c.left + pdfSyncObject.offset.x, y: c.bottom + pdfSyncObject.offset.y, indicator: true }
}
const line0 = lineNums[i - 1]
const blocks0 = getBlocks(linePageBlocks, line0)
const c0 = toRect(blocks0)
const line1 = lineNums[i]
const blocks1 = getBlocks(linePageBlocks, line1)
const c1 = toRect(blocks1)
let bottom: number
if (c0.bottom < c1.bottom) {
bottom = c0.bottom * (line1 - line) / (line1 - line0) + c1.bottom * (line - line0) / (line1 - line0)
} else {
bottom = c1.bottom
}
return { page: blocks1[0].page, x: c1.left + pdfSyncObject.offset.x, y: bottom + pdfSyncObject.offset.y, indicator: true }
}
function syncTeXToTeX(page: number, x: number, y: number, pdfPath: string): SyncTeXRecordToTeX | undefined {
const pdfSyncObject = parseSyncTexForPdf(pdfPath)
if (!pdfSyncObject) {
return undefined
}
const y0 = y - pdfSyncObject.offset.y
const x0 = x - pdfSyncObject.offset.x
const fileNames = Object.keys(pdfSyncObject.blockNumberLine)
if (fileNames.length === 0) {
logger.log('No relevant entries found.')
return undefined
}
const record = {
input: '',
line: 0,
distanceXY: 2e16,
distanceFromCenter: 2e16,
rect: new Rectangle({top: 0, bottom: 2e16, left: 0, right: 2e16})
}
for (const fileName of fileNames) {
const linePageBlocks = pdfSyncObject.blockNumberLine[fileName]
for (const lineNum in linePageBlocks) {
const pageBlocks = linePageBlocks[Number(lineNum)]
for (const pageNum in pageBlocks) {
if (page !== Number(pageNum)) {
continue
}
const blocks = pageBlocks[Number(pageNum)]
for (const block of blocks) {
// Skip a block if they have boxes inside, or their type is kern or rule.
// See also https://github.com/jlaurens/synctex/blob/c11fe00dbdc6423a0e54d4e531563be645f78679/synctex_parser.c#L4706-L4727 for types.
if (block.elements !== undefined || block.type === 'k' || block.type === 'r') {
continue
}
const rect = toRect(block)
const distFromCenter = rect.distanceFromCenter(x0, y0)
if ( record.rect.include(rect) || (distFromCenter < record.distanceFromCenter && !rect.include(record.rect)) ) {
record.input = fileName
record.line = Number(lineNum)
record.distanceFromCenter = distFromCenter
record.rect = rect
}
}
}
}
}
if (record.input === '') {
logger.log('Cannot find any line to jump to.')
return undefined
}
const input = convInputFilePath(record.input)
return input ? { input, line: record.line, column: 0 } : undefined
}
function convInputFilePath(inputFilePath: string): string | undefined {
if (fs.existsSync(inputFilePath)) {
return inputFilePath
}
for (const enc of iconvLiteSupportedEncodings) {
try {
const s = iconv.decode(Buffer.from(inputFilePath, 'binary'), enc)
if (fs.existsSync(s)) {
return s
}
} catch {}
}
logger.log(`Non-existent file to jump to ${inputFilePath} .`)
return undefined
}