From 14406083cd37cafd72e58a3df41dc9d19368c638 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Tue, 19 May 2026 21:15:46 +0300 Subject: [PATCH 1/3] Fix demo Fixes https://github.com/harfbuzz/harfbuzzjs/issues/211 --- demo/index.html | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/demo/index.html b/demo/index.html index 210e498..4fa5377 100644 --- a/demo/index.html +++ b/demo/index.html @@ -19,10 +19,16 @@ var buffer = new hb.Buffer(); buffer.addText(text.value); + // buffer.setDirection(hb.Direction.LTR); // optional as guessSegmentProperties also handles it buffer.guessSegmentProperties(); - // buffer.setDirection('ltr'); // optional as can be by guessSegmentProperties also hb.shape(font, buffer); - var result = buffer.json(); + var result = JSON.parse( + buffer.serialize({ + font: font, + format: hb.BufferSerializeFormat.JSON, + flags: hb.BufferSerializeFlag.NO_GLYPH_NAMES, + }), + ); // returns glyphs paths, totally optional var glyphs = {}; From 7e4df954a9ccc68ec7541bd2f378e7ad84eceab4 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Tue, 19 May 2026 21:16:20 +0300 Subject: [PATCH 2/3] Simplify by using Font.glyphToPath() --- demo/index.html | 112 +++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 78 deletions(-) diff --git a/demo/index.html b/demo/index.html index 4fa5377..1a8cb6f 100644 --- a/demo/index.html +++ b/demo/index.html @@ -34,68 +34,46 @@ var glyphs = {}; result.forEach(function (x) { if (glyphs[x.g]) return; - glyphs[x.g] = font.glyphToJson(x.g); + glyphs[x.g] = font.glyphToPath(x.g); }); - var xmin = 10000; - var xmax = -10000; - var ymin = 10000; - var ymax = -10000; var ax = 0; var ay = 0; - var path = pathToRelative( - result - .map(function (x) { - var result = glyphs[x.g] - .filter(function (command) { - return command.type !== "Z"; - }) - .map(function (command) { - var result = command.values - .map(function (p, i) { - // apply ax/ay/dx/dy to coords - return i % 2 ? -(p + ay + x.dy) : p + ax + x.dx; - }) - .map(function (x, i) { - // bbox calc - if (i % 2) { - if (x < ymin) ymin = x; - if (x > ymax) ymax = x; - } else { - if (x < xmin) xmin = x; - if (x > xmax) xmax = x; - } - return x; - }); - return [command.type].concat(result); - }); - ax += x.ax; - ay += x.ay; - return result; - }) - .reduce((acc, val) => acc.concat(val), []), - ) - .map((x) => x[0] + x.slice(1).join(" ")) - .join("") - .replace(/ -/g, "-"); - var width = xmax - xmin; - var height = ymax - ymin; - // pad it a bit - var pad = Math.round(Math.min(width / 10, height / 10)); - xmin -= pad; - ymin -= pad; - width += pad * 2; - height += pad * 2; - - var bbox = xmin + " " + ymin + " " + width + " " + height; + var paths = result + .map(function (x) { + var p = + ''; + ax += x.ax; + ay += x.ay; + return p; + }) + .join(""); svgResult.innerHTML = - '' + - ''; + '' + + paths + + ""; + // Tighten viewBox to the rendered path bounds. + var svg = svgResult.querySelector("svg"); + var b = svg.getBBox(); + var pad = Math.round(Math.min(b.width / 10, b.height / 10)); + svg.setAttribute( + "viewBox", + b.x - + pad + + " " + + (b.y - pad) + + " " + + (b.width + 2 * pad) + + " " + + (b.height + 2 * pad), + ); svgResult.download = text.value + ".svg"; // revoke previous URL if (svgResult.href) URL.revokeObjectURL(svgResult.href); @@ -152,26 +130,4 @@ }); reader.readAsArrayBuffer(file); } - - // Totally optional, https://github.com/adobe-webplatform/Snap.svg/blob/7abe4d1/src/path.js#L532 - function pathToRelative(pathArray) { - if (!pathArray.length) return []; - var x = pathArray[0][1], - y = pathArray[0][2]; - var prevCmd = ""; - return [["M", x, y]].concat( - pathArray.slice(1).map(function (pa) { - var r = [prevCmd === pa[0] ? " " : pa[0].toLowerCase()].concat( - pa.slice(1).map(function (z, i) { - return z - (i % 2 ? y : x); - }), - ); - var lastPoint = r.slice(-2); - x += lastPoint[0]; - y += lastPoint[1]; - prevCmd = pa[0]; - return r; - }), - ); - } From 8a23ed68e69cee9ab1f03ba138b6ca57985581a0 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Tue, 19 May 2026 21:22:21 +0300 Subject: [PATCH 3/3] Minor --- demo/index.html | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/demo/index.html b/demo/index.html index 1a8cb6f..ce86afe 100644 --- a/demo/index.html +++ b/demo/index.html @@ -41,38 +41,21 @@ var ay = 0; var paths = result .map(function (x) { - var p = - ''; + var p = ``; ax += x.ax; ay += x.ay; return p; }) .join(""); - svgResult.innerHTML = - '' + - paths + - ""; + svgResult.innerHTML = `${paths}`; // Tighten viewBox to the rendered path bounds. var svg = svgResult.querySelector("svg"); var b = svg.getBBox(); var pad = Math.round(Math.min(b.width / 10, b.height / 10)); svg.setAttribute( "viewBox", - b.x - - pad + - " " + - (b.y - pad) + - " " + - (b.width + 2 * pad) + - " " + - (b.height + 2 * pad), + `${b.x - pad} ${b.y - pad} ${b.width + 2 * pad} ${b.height + 2 * pad}`, ); svgResult.download = text.value + ".svg"; // revoke previous URL