Skip to content

Commit

Permalink
parallelise loading engines
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale committed Dec 20, 2023
1 parent 048ef08 commit 9389d53
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/lib/latex.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
import { PdfTeXEngine, XeTeXEngine, DvipdfmxEngine } from 'swiftlatex'
import { LaTeXOpts } from '../types'

let pdftex
let xetex
let dvipdfmx
let pdftex = new PdfTeXEngine()
let xetex = new XeTeXEngine()
let dvipdfmx = new DvipdfmxEngine()
let engineLoaded = false

export default async function latex(texDoc: string, opts: LaTeXOpts) {
if (!engineLoaded) {
pdftex = new PdfTeXEngine()
await pdftex.loadEngine()

xetex = new XeTeXEngine()
await xetex.loadEngine()

dvipdfmx = new DvipdfmxEngine()
await dvipdfmx.loadEngine()

await Promise.all([
pdftex.loadEngine(),
xetex.loadEngine(),
dvipdfmx.loadEngine()
])
engineLoaded = true

await pdftex.makeMemFSFolder('fonts/')
await xetex.makeMemFSFolder('fonts/')
await dvipdfmx.makeMemFSFolder('fonts/')
}

const fonts = await resolveAssets(opts.fonts || [])
const inputs = await resolveAssets(opts.inputs || [])

switch (opts.cmd) {
case 'pdflatex': {
await pdftex.makeMemFSFolder('fonts/')
for (const [name, content] of fonts) {
await pdftex.writeMemFSFile(`fonts/${name}`, content)
}

for (const [name, content] of inputs) {
await xetex.writeMemFSFile(name, content)
await pdftex.writeMemFSFile(name, content)
}

await pdftex.writeMemFSFile('main.tex', texDoc)
Expand All @@ -42,7 +41,6 @@ export default async function latex(texDoc: string, opts: LaTeXOpts) {
}
case 'xelatex': {
for (const engine of [xetex, dvipdfmx]) {
await engine.makeMemFSFolder('fonts/')
for (const [name, content] of fonts) {
await engine.writeMemFSFile(`fonts/${name}`, content)
}
Expand Down

0 comments on commit 9389d53

Please sign in to comment.