Skip to content

Commit

Permalink
Handle ligatures when guessing placeholder string
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledhosny committed Feb 8, 2025
1 parent 26eac62 commit 5b939de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/fontra/client/core/glyph-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,32 @@ export function guessGlyphPlaceholderString(codePoints, glyphName) {

if (!glyphString && glyphName) {
const [baseGlyphName, extension] = splitGlyphNameExtension(glyphName);
if (extension) {
const codePoint = getCodePointFromGlyphName(baseGlyphName);
if (codePoint) {
const baseChar = getCharFromCodePoint(codePoint);

let baseGlyphNames = [baseGlyphName];
if (baseGlyphName.indexOf("_") != -1) {
const [base, suffix] = splitGlyphNameExtension(baseGlyphName, "-");
baseGlyphNames = base.split("_").map((name) => name + suffix);
}

const codePoints = baseGlyphNames.map((name) => getCodePointFromGlyphName(name));
if (codePoints.length == baseGlyphNames.length) {
glyphString = codePoints
.map((codePoint) => getCharFromCodePoint(codePoint))
.join("");

if (extension) {
const ZWJ = "\u200D";
switch (extension) {
case ".isol":
glyphString = baseChar;
break;
case ".init":
glyphString = baseChar + ZWJ;
glyphString = glyphString + ZWJ;
break;
case ".medi":
glyphString = ZWJ + baseChar + ZWJ;
glyphString = ZWJ + glyphString + ZWJ;
break;
case ".fina":
glyphString = ZWJ + baseChar;
glyphString = ZWJ + glyphString;
break;
default:
break;
Expand Down
4 changes: 2 additions & 2 deletions src/fontra/client/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ export function* iter(iterable) {
}
}

export function splitGlyphNameExtension(glyphName) {
const periodIndex = glyphName.indexOf(".");
export function splitGlyphNameExtension(glyphName, separator = ".") {
const periodIndex = glyphName.indexOf(separator);
const baseGlyphName = periodIndex >= 1 ? glyphName.slice(0, periodIndex) : glyphName;
const extension = periodIndex >= 1 ? glyphName.slice(periodIndex) : "";
return [baseGlyphName, extension];
Expand Down
8 changes: 8 additions & 0 deletions test-js/test-glyph-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ describe("glyph-data Tests", () => {
{ glyphName: "beh-ar.init", codePoints: [], glyphString: "\u0628\u200D" },
{ glyphName: "beh-ar.medi", codePoints: [], glyphString: "\u200D\u0628\u200D" },
{ glyphName: "uni0628.medi", codePoints: [], glyphString: "\u200D\u0628\u200D" },
{ glyphName: "f_i", codePoints: [], glyphString: "fi" },
{ glyphName: "lam_alef-ar", codePoints: [], glyphString: "\u0644\u0627" },
{
glyphName: "lam_alef-ar.fina",
codePoints: [],
glyphString: "\u200D\u0644\u0627",
},
{ glyphName: "lam_lam_alef-ar", codePoints: [], glyphString: "\u0644\u0644\u0627" },
];

parametrize(
Expand Down

0 comments on commit 5b939de

Please sign in to comment.