Skip to content

Commit

Permalink
Try several dirs when loading font
Browse files Browse the repository at this point in the history
  • Loading branch information
linev committed Sep 24, 2024
1 parent 937982f commit 9aeef93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
20 changes: 3 additions & 17 deletions modules/base/BasePainter.mjs
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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'),
Expand Down
32 changes: 31 additions & 1 deletion modules/base/FontHandler.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 };

0 comments on commit 9aeef93

Please sign in to comment.