Skip to content

Instantly share code, notes, and snippets.

@hlotvonen
Last active September 12, 2024 12:04
Show Gist options
  • Save hlotvonen/310ab9cd41ba99bf9bb23abd2a04c033 to your computer and use it in GitHub Desktop.
Save hlotvonen/310ab9cd41ba99bf9bb23abd2a04c033 to your computer and use it in GitHub Desktop.
Ways to substitute specific words with OpenType
# Goal: substitute each letter in the word "while" with the corresponding alternate.
# Option 0 — does not work, error "many-to-many substitution not available"
lookup whileAttrCalt-0 useExtension {
ignore sub @AllLetters w' h i l e;
ignore sub w' h i l e @AllLetters;
sub w' h' i' l' e' by w.alt2 h.alt2 i.alt2 l.alt2 e.alt2;
} whileAttrCalt-0;
# Option 1 — one-to-one
lookup whileAttrCalt-1 useExtension {
ignore sub @AllLetters w' h i l e;
ignore sub w' h i l e @AllLetters;
sub w' h i l e by w.alt2;
sub w.alt2 h' i l e by h.alt2;
sub w.alt2 h.alt2 i' l e by i.alt2;
sub w.alt2 h.alt2 i.alt2 l' e by l.alt2;
sub w.alt2 h.alt2 i.alt2 l.alt2 e' by e.alt2;
} whileAttrCalt-1;
# Option 2 — many-to-many is not supported so we do this weird thing to achieve many-to-many
lookup WHILE_SUBS useExtension {
sub w by w.alt2;
sub h by h.alt2;
sub i by i.alt2;
sub l by l.alt2;
sub e by e.alt2;
} WHILE_SUBS;
lookup whileAttrCalt-2 useExtension {
ignore sub @AllLetters w' h i l e, w' h i l e @AllLetters;
substitute w' lookup WHILE_SUBS h' lookup WHILE_SUBS i' lookup WHILE_SUBS l' lookup WHILE_SUBS e' lookup WHILE_SUBS;
} whileAttrCalt-2;
# Option 3 — one-to-many, then many-to-one, works quite nicely!
lookup whileAttrCalt-3 useExtension {
ignore sub @AllLetters w' h i l e, w' h i l e @AllLetters;
sub w' h i l e by w.alt3 h.alt3 i.alt3 l.alt3;
sub w.alt3 h.alt3 i.alt3 l.alt3 h' i' l' e' by e.alt3;
} whileAttrCalt-3;
# Option 4 — does not work as desired. Will sub single letters everywhere.
lookup whileAttrCalt-4 useExtension {
ignore sub @AllLetters w' h i l e, w' h i l e @AllLetters;
sub [w h i l e]' by [w.alt2 h.alt2 i.alt2 l.alt2 e.alt2];
} whileAttrCalt-4;
# Suggestions? Is there another way?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment