From 9aeef93245a5e44f2bc5dce4e150f33d4ba4d229 Mon Sep 17 00:00:00 2001 From: Sergey Linev Date: Tue, 24 Sep 2024 15:50:13 +0200 Subject: [PATCH] Try several dirs when loading font --- modules/base/BasePainter.mjs | 20 +++----------------- modules/base/FontHandler.mjs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/modules/base/BasePainter.mjs b/modules/base/BasePainter.mjs index 210ee9c78..88c375161 100644 --- a/modules/base/BasePainter.mjs +++ b/modules/base/BasePainter.mjs @@ -1,6 +1,6 @@ import { select as d3_select } from '../d3.mjs'; -import { settings, internals, isNodeJs, isFunc, isStr, isObject, btoa_func, getDocument, source_dir, httpRequest } from '../core.mjs'; -import { detectFont, addCustomFont, getCustomFont, FontHandler } from './FontHandler.mjs'; +import { settings, internals, isNodeJs, isFunc, isStr, isObject, btoa_func, getDocument } from '../core.mjs'; +import { detectFont, addCustomFont, getCustomFont, loadFontFile, FontHandler } from './FontHandler.mjs'; import { approximateLabelWidth, replaceSymbolsInTextNode } from './latex.mjs'; import { getColor } from './colors.mjs'; @@ -754,20 +754,6 @@ function addHighlightStyle(elem, drag) { } } -/** @summary Read font file from some pre-configured locations - * @return {Promise} with base64 code of the font - * @private */ -async function readFontFile(fname) { - return isNodeJs() ? import('fs').then(fs => { - let path = 'fonts/' + fname; - if (source_dir.indexOf('file://') === 0) - path = source_dir.slice(7) + path; - else - path = '../../' + path; - return fs.readFileSync(path).toString('base64'); - }) : httpRequest(source_dir + 'fonts/' + fname, 'bin').then(buf => btoa_func(buf)); -} - /** @summary Create pdf for existing SVG element * @return {Promise} with produced PDF file as url string * @private */ @@ -856,7 +842,7 @@ async function svgToPDF(args, as_buffer) { if (need_symbols && !custom_fonts.symbol) { if (!getCustomFont('symbol')) - pr2 = readFontFile('symbol.ttf').then(base64 => addCustomFont(25, 'symbol', 'ttf', base64)); + pr2 = loadFontFile('symbol.ttf').then(base64 => addCustomFont(25, 'symbol', 'ttf', base64)); pr2 = pr2.then(() => { const fh = getCustomFont('symbol'), diff --git a/modules/base/FontHandler.mjs b/modules/base/FontHandler.mjs index 8a649e6ad..c480a61d2 100644 --- a/modules/base/FontHandler.mjs +++ b/modules/base/FontHandler.mjs @@ -1,3 +1,6 @@ +import { isNodeJs, httpRequest, btoa_func, source_dir } from '../core.mjs'; + + const kArial = 'Arial', kTimes = 'Times New Roman', kCourier = 'Courier New', kVerdana = 'Verdana', kSymbol = 'Symbol', kWingdings = 'Wingdings', // average width taken from symbols.html, counted only for letters and digits root_fonts = [null, // index 0 not exists @@ -215,4 +218,31 @@ function detectFont(node) { return handler; } -export { FontHandler, addCustomFont, getCustomFont, detectFont }; +/** @summary Read font file from some pre-configured locations + * @return {Promise} with base64 code of the font + * @private */ +async function loadFontFile(fname) { + const locations = [source_dir + 'fonts/']; + if (isNodeJs()) + locations.push('../../fonts/'); + + console.log('Trying to load ', fname, 'from', locations); + + async function tryNext() { + if (locations.length === 0) + throw new Error(`Fail to load ${fname} font`); + let path = locations.shift() + fname; + const pr = isNodeJs() ? import('fs').then(fs => { + if (path.indexOf('file://') === 0) + path = path.slice(7); + return fs.readFileSync(path).toString('base64'); + }) : httpRequest(path, 'bin').then(buf => btoa_func(buf)); + + return pr.catch(() => tryNext()); + } + + return tryNext(); +} + + +export { FontHandler, addCustomFont, getCustomFont, detectFont, loadFontFile };