Random Contextual Alternates Code

Hi everyone!

I made a font with 60 alternate glyphs for each character of the Latin alphabet and am now trying to set it up/code in Glyphs.
The goal is that each alternate appears randomly when I type:
If I type the word CAT it would be typed (for ex.) with the characters C.ss22+A.ss08+T.ss41 and If I type it again : C.ss59+A.ss35+T.ss18 etc. so super randomly

I have looked through the forum+the tutorials and I started to code the alternates using the calt function, but the only way for me to get let’s say C.ss22 is to have 21 Cs typed in before. Also the space bar resets the code, so I would probably never get anywhere further than C.ss02…

Is there a way to fix that? using the rand function? (which I understood isn’t supported by major apps?)

thank you!

Did you read this: Features, Part 3: Advanced Contextual Alternates | Glyphs

1 Like

Yes, I used the contextual cycling code from that tutorial!
But as I have so many alternates I don’t think using the multiple classes method would work… or would it?

Is there a way to make the classes more random? so that they are not always displayed/chosen in their original order (so not ALT1->ALT2->ALT3 but ALT53->ALT7->ALT46 for ex.) ?

  1. Use cvXX, not ssXX. Should be easy to replace: select all glyphs and press Cmd-Shift-F, find ‘.ssx’ and replace it with ‘.cv’.

  2. There is no true randomization possible with fonts. Yes there is a rand feature but it is hardly supported anywhere. The code would be something like:

sub A from [A.cv01 A.cv02 A.cv03];

And so on. There is a script in the mekkablue scripts for building the rand feature.

However you will need to double up with pseudorandomisation in calt and your best shot is the method described in the tutorial. Writing the exact same line twice will give you the same alternates, but just a letter difference will create something very different already.

2 Likes

I am working on something similar to this, and would love to learn some more. firstly, what is the cv you mention? is the cv connected to the rand feature that you mention? (the one that is hardly supported anywhere). The font will be used to print a book via InDesign, and time is limited, so I’d rather not waste any on a feature most will ignore

“cv” features are Character Variants: cv01 through cv99. You can learn more about them in the OpenType specification.

As far as I know, cv## features are not offered in the InDesign user interface, but they are accessible using scripting. The following project might be helpful in this regard, but I have never used it myself:

thank you Florian, I shall investigate

Hi there — I’m trying to create the same effect (randomly selecting from the default and alternate glyphs) with primary use in Illustrator, Photoshop, and InDesign. But it doesn’t look like this thread came to a solution that doesn’t involve layering multiple cycles of contextual substitutions over one another in an attempt to create what appears to be randomness, or a solution is well supported (as mekkablue mentioned its lack of support).

Does anyone have new recommendations/insight on how to achieve this “random selection from glyph alternates” effect?

Here’s a quick script that writes a feature with a bunch of random subs, a few levels deep, if you will. I wonder if it actually looks random outside my little lab tests. Try giving it various numbers.
Watch out, it replaces the feature with the given name!

1 Like

Thanks @alexs — this sort of works but still leaves discernable patterns of repetition in my experimentation so far. One example is when typing the same character over and over (eg. aaaaaaaaaaaa) I would hope that each time the subsequent ‘a’ is selected randomly from the collection of glyph alternates, but even (as I understand it) the collection of glyph alternates may be split into different classes and substitution cycles by your script, it ultimately only leaves one possible outcome for any given string.

My knowledge of Python/scripting is basic at this point but if I was trying to script this myself directly in the feature section I would assign the glyph and glyph alternates to a list (maybe a class could serve this purpose) and then use the random.choice function to make a random selection from that list every time the character is typed, so something like the following

import random
a_Glyphs = [a a.ss01 a.ss02 a.ss03 etc.]
sub A from random.choice(a_Glyphs)

My syntax is probably incorrect, and I know you can’t just write python directly as feature code, but hopefully, it’s helpful in explaining what I’m trying to do!

Either way, thank you for sharing the script, I’m going to keep tinkering with it and see what I might be able to build using it as a starting point and guide.

Hm, repeating characters might be a special scenario. Indeed “aaa” will always look the same, but you can take into account anything around it: “aaa.” “gaaa” “aaah” or even “aaaa” etc.
I suspect the script does that if you’re lucky, but it may be a good idea to enforce it, let me check.

The OpenType feature language has nothing to do with Python :wink:

It has no support for loops or “true” randomness. You can only simulate it very manually to a certain extent.

May I ask which font user will type “aaaaaaaaaaaa” and compare it to a previous occurence of the same string? Compare it to actual text and see whether you still recognise a lot of patterns. You can check out LiebeHeide · LiebeFonts for instance, it uses the same principles as produced by the script above. Sure, if you type exactly the same word in the same context again, it will look the same, but this is quite simply the limitation of OpenType. It’s up to you to see how much time and effort you want to invest into defining new combinations within which to swap letters.

2 Likes

Shows my level of experience haha :sweat_smile:

To answer you’re question, probably no one. It was just in “testing” the script alexs shared that I wanted to see what kind of randomization was possible, even when typing a string of the same character. Admittedly, not the most realistic scenario to be testing.

I’ve actually seen LiebeHeide and Ulrike’s talk about the OpenType features behind it — However, it’s been quite a while and it had slipped my mind, thanks for the reminder!

Thanks for the extra advice though, if simulated randomness is the best that can be done with OpenType at the moment, than at least I know where the limits are and how best to approach the challenge.