shape static method
Implementation
static String shape(String input) {
final runes = input.runes.toList();
final shaped = <int>[];
for (int i = 0; i < runes.length; i++) {
final current = runes[i];
final int? prev = i > 0 ? runes[i - 1] : null;
final int? next = i < runes.length - 1 ? runes[i + 1] : null;
final bool connectBefore =
prev != null && _canConnectBefore(prev) && _canConnectAfter(current);
final bool connectAfter = next != null && _canConnectAfter(next);
final forms = _shapingTable[current];
if (forms != null) {
int shapedChar;
if (forms.length == 2) {
shapedChar = connectBefore ? forms[1] : forms[0];
} else {
if (!connectBefore && !connectAfter) {
shapedChar = forms[0]; // Isolated
} else if (!connectBefore && connectAfter) {
shapedChar = forms[1]; // Initial
} else if (connectBefore && connectAfter) {
shapedChar = forms[2]; // Medial
} else {
shapedChar = forms[3]; // Final
}
}
shaped.add(shapedChar);
} else {
shaped.add(current); // Leave unchanged
}
}
return String.fromCharCodes(shaped.reversed);
}