mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 02:11:07 +05:30
feat(monospace): add xelatex fontspec header builder
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
function buildPdfFontHeader(settings, ttfPath, fontFamily) {
|
||||
if (!ttfPath || !fs.existsSync(ttfPath)) {
|
||||
throw new Error(`PdfFontHeader: TTF not found at ${ttfPath}`);
|
||||
}
|
||||
const ligatures = !!(settings && settings.monospaceLigatures === true);
|
||||
const fontDir = path.dirname(ttfPath);
|
||||
const basename = path.basename(ttfPath);
|
||||
const boldName = basename.replace('Regular', 'Bold').replace('Medium', 'Bold');
|
||||
const lines = [
|
||||
`\\setmonofont[Path = ${fontDir}/,`,
|
||||
` Extension = .ttf,`,
|
||||
` UprightFont = ${basename},`,
|
||||
` BoldFont = ${boldName},`,
|
||||
ligatures ? ' Ligatures=TeX,' : '',
|
||||
` Scale = 0.9]`,
|
||||
`{${fontFamily}}`,
|
||||
].filter(Boolean);
|
||||
const headerPath = path.join(os.tmpdir(), `monospace-pdf-${Date.now()}-${process.pid}.tex`);
|
||||
fs.writeFileSync(headerPath, lines.join('\n'), 'utf-8');
|
||||
return { headerPath, familyName: fontFamily };
|
||||
}
|
||||
|
||||
module.exports = { buildPdfFontHeader };
|
||||
Reference in New Issue
Block a user