Streamline AFMFont parsing. Do not keep contents in memory

This commit is contained in:
Luiz Américo Pereira Câmara 2025-05-03 00:12:01 -03:00
parent 715c2e39aa
commit 4b2030eedd

View File

@ -103,14 +103,16 @@ udieresis yacute thorn ydieresis\
`.split(/\s+/);
class AFMFont {
/**
* @param {string} contents
*/
constructor(contents) {
this.contents = contents;
this.attributes = {};
this.glyphWidths = {};
this.boundingBoxes = {};
this.kernPairs = {};
this.parse();
this.parse(contents);
this.bbox = this.attributes['FontBBox'].split(/\s+/).map((e) => +e);
this.ascender = +(this.attributes['Ascender'] || 0);
@ -121,9 +123,12 @@ class AFMFont {
this.bbox[3] - this.bbox[1] - (this.ascender - this.descender);
}
parse() {
/**
* @param {string} contents
*/
parse(contents) {
let section = '';
for (let line of this.contents.split('\n')) {
for (let line of contents.split('\n')) {
var match;
var a;
if ((match = line.match(/^Start(\w+)/))) {
@ -168,6 +173,10 @@ class AFMFont {
}
}
/**
* @param {string} text
* @returns
*/
encodeText(text) {
const res = [];
for (let i = 0, len = text.length; i < len; i++) {