Skip to content

Commit

Permalink
Fixed ligatures in Sinhala
Browse files Browse the repository at this point in the history
Replace zero-width joiners with temporary strip markers to prevent ICU from stripping them.
  • Loading branch information
1ec5 committed Aug 22, 2024
1 parent 5fd311f commit 5209ec8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/symbol/shaping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,14 @@ function shapeText(
lines = [];
// ICU operates on code units.
lineBreaks = lineBreaks.map(index => logicalInput.toCodeUnitIndex(index));
// Replace zero-width joiners with temporary strip markers (from the Private Use Area) to prevent ICU from stripping them out.
const markedInput = logicalInput.toString().replace(/\u200D/g, '\uF8FF');
const untaggedLines =
processBidirectionalText(logicalInput.toString(), lineBreaks);
processBidirectionalText(markedInput, lineBreaks);
for (const line of untaggedLines) {
const taggedLine = new TaggedString();
taggedLine.text = line;
// Restore zero-width joiners from temporary strip markers.
taggedLine.text = line.replace(/\uF8FF/g, '\u200D');
taggedLine.sections = logicalInput.sections;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const char of splitByGraphemeCluster(line)) {
Expand All @@ -290,20 +293,23 @@ function shapeText(
lines = [];
// ICU operates on code units.
lineBreaks = lineBreaks.map(index => logicalInput.toCodeUnitIndex(index));
// Replace zero-width joiners with temporary strip markers (from the Private Use Area) to prevent ICU from stripping them out.
const markedInput = logicalInput.toString().replace(/\u200D/g, '\uF8FF');

// Convert grapheme cluster–based section index to be based on code units.
let i = 0;
const sectionIndex = [];
for (const grapheme of splitByGraphemeCluster(logicalInput.text)) {
for (const grapheme of splitByGraphemeCluster(markedInput)) {
sectionIndex.push(...Array(grapheme.length).fill(logicalInput.sectionIndex[i]));
i++;
}

const processedLines =
processStyledBidirectionalText(logicalInput.text, sectionIndex, lineBreaks);
processStyledBidirectionalText(markedInput, sectionIndex, lineBreaks);
for (const line of processedLines) {
const taggedLine = new TaggedString();
taggedLine.text = line[0];
// Restore zero-width joiners from temporary strip markers.
taggedLine.text = line[0].replace(/\uF8FF/g, '\u200D');
taggedLine.sectionIndex = line[1];
taggedLine.sections = logicalInput.sections;
lines.push(taggedLine);
Expand Down

0 comments on commit 5209ec8

Please sign in to comment.