Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · Lobsters · · 1:45:35 runtime

How to make a text box: A crash course in Unicode and OpenType

Comments

Transcript 7643 words · auto-generated captions

Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.

All right. Hello. Hello. Welcome back everyone. Well, our next guest is a very special guest. I'm sorry, speaker, not just guest because I particularly remember uh spring of 2024 when I

started hearing rumors of someone in the Molly Rocket Discord trying to have an attempt at creating a half fuzz replacement. And when I heard that, I was like first like, "Yeah, yeah, okay. I'll see you like in 30 years, you know, like kind of thing." And then later at year he finally does it and my mind's

absolutely blown. So please everyone welcome the man who did the impossible Jimmy Lee.

>> Thank you. Thank you. Um it's it's a great honor to be presenting here. I mean, I've I've I remember last year when I watched all of the talks uh from the previous BSE and just feeling so much admiration for everyone who uh presented and um I thought it'll be oh I

clicked uh I thought it'll be kind of different this year because I was here in person and like it's true that we I get to chat with these amazing people but honestly the admiration doesn't really go away like it just stays. So I guess I'll just do my best. Um I want to start my talk with um a bit of context and to be specific I want to put us in a specific mindset um to to really uh

ingest the information I'm about to tell you. So just imagine you're a young uh

innocent uh programmer and but you're

ambitious, right? And you want to make good things and so um you're motivated and so uh cool. Uh you want to make a

graphical application. Um now because you're young uh and motivated, you don't really start off from any pre-existing code base, right? You're going from scratch and from first principles. And um so that's the approach we're going to take here. Uh what's the first step we do when making graphical applications? Well, very simply, we open a window. Um

and you know that takes more or less effort depending on your favorite platform. Um but then the next step is

to actually draw something in that window. And so to draw something in that window, uh you're going to go through like talking to the GPU and again very

platform dependent. But after a few days, you might be able to get a triangle on screen. And yeah. Yeah. Cool. Now you have a harness to actually continue working. And um once you have a triangle, it's actually not that much work to go to a rectangle,

right? And and then once you have one rectangle, you're like, that's that's amazing because it's it's a simple primitive and I can use it to make plenty of things like buttons and checkboxes and sliders and I can just start like integrating mouse interaction and just start playing around with code, right? And that's that's really what we're here to do. Um, now you'll notice there's something conspicuously missing on the slides. Um,

if if I were to just take you a screenshot of this kind of interface and just send it to you and be like, "Oh, I'm I like my interface is going great." You might be like, "Well, what what does it do exactly?" Um, there's no way to know because there's no text. Okay. Um,

so, okay, going from first principles again, this is English text as it happens. Um, and if we look at it for a few seconds, we might be like, well, you know, it's just a bunch of like letters and symbols and, uh, each one looks like

it kind of fits in a rectangle. And so I I know how to do rectangles now. Um, so let's just say that each each one of these symbols gets a bounding box. And so that's I'm just going to draw one rectangle per glyph. Um, now if we draw

the bounding boxes, we see that there's there's still a gap in between each of these symbols here. Um, when we start from the left and we draw each character one by one, we actually want to advance the pen position a bit further than the actual like visual width of the glyph, right? Uh, so that's what we call the advance in typography. Um, and so to be

able to draw an English string, we're going to need widths and advances for each of those symbols, right? But like once we have those, it's actually kind of simple to just go and write the basic code. Um it might look something like this. You go through your string for each character, you map it to one glyph, and then you draw it exactly where the cursor is and then you advance the cursor position uh by your glyph advance. All good, right? Very simple. Everybody so far should understand this. Now, if you're feeling really spicy, you might even want your text to look good. In which case, uh, yeah, I know. Um, in which case, you might want to implement kerning. It's a three line change in my pseudo codeish example. Um, what kerning

is is exactly what is happening here in the bottom right hand corner is um, well, if we look at the T here, the bounding box is actually quite wide because the top bar is wide, but we know it's just the top bar that's wide. And we know smaller characters can actually nestle under it um if we pay attention.

And and so we need some kind of way to know that when we have a T and then a small letter after it, we can um adjust the positioning so that they're closer together, right? Uh so that's what coding is is when I go through my string, I just look at each pair of adjacent uh glyphs and I do this adjustment before drawing. Um, and then I just keep going. And so that's that's kerning. And honestly, if you're drawing simple English text, that's basically all you need. You're done. Now, um, you might be like, well,

I'm done, but I'm no I'm no typographer or anything. I'm no font designer. Like, I I still need data to draw, right? Uh now thankfully for us there's actually a very widely used and widely supported format that does exactly what I just told you right now and that's true type. Uh true type was made by the by Apple in the late 80s later licensed to Microsoft

and it has exactly what I just told you. You can map characters to glyphs in the cap table. You have vector outlines which I won't be covering in this talk. Um, and you have uh kerning information

that does exactly what I just told you. Just takes a pair of glyphs and gives you an adjustment. Cool. Um, so now we go and we take our example code and we make it work with true type fonts and and we get text. Uh, we get

simple English text and suddenly our

interface starts to look like something kind of kind of real. Um, and then one day we want to add a text field. Um, and there's a few

problems that we haven't tackled in our program yet. Uh, the first one is that we're going to be dealing with fully dynamic strings because we're not going to be the one deciding what goes in the text field, right? Um, and the second one is that well remember I told you that we're kind of ambitious and we want our software to be good. So, we kind of just want the user to type in the text field and for it to work even if um they're not typing in English, right? They might want to type with different alphabets than we use. So, like Greek or Cerillic. Um they might want to type in Chinese, Japanese, or Korean. Uh they might even want to type in Arabic or Hebrew, which don't go in the same direction as English does. um they might

even want to type in uh any of the brahmy derived scripts which um cover uh

India and uh that general area plus most

of Southeast Southeast Asia right or even like any other language on earth ideally I mean come on um we want to make good software um and

this is the point of my talk is how are we going to make this work Um, okay. I'm going to go fairly quickly. Um, there will be time for Q&A, but like the point of my talk mainly is to get you used to what the different steps are in going from uh a multilingual font or

multilingual font system to like actually drawing stuff uh on screen and presenting it correctly to the user. Um, so I'll describe a bunch of unicode and open type uh processes and that will inform you as to how you might want to use these systems. However, I will not be covering how to implement your own unicode and open type libraries as my talk will be much much longer and will be mostly cons will would mostly consist of uh honestly minutia and um I won't be

covering how to render glyphs because honestly there's several people even at this very conference who are much much better than I am at that. Um, and I won't be covering how to handle IME either, which um, if you're not familiar with the concept, that's just, um, input methods for complex languages.

Okay. Uh, let's define the standards we'll be working with. First, uh, when I say uniode, I could mean a whole bunch of things. Um, Uniode technically is a standard. It's a very large standard currently version 17, but it still gets updated. and it gets updated by the uni code consortium which is the people who write the standard. Okay, so far so good. Um now that standard covers a bunch of different concepts that fall under the unic code umbrella such as um which integers get

mapped to which characters that's code points um or how do we represent these

integers in memory and that's the family of UTF uh text encodings or um the

standard also publishes um a bunch of

tables that hold properties for each of those characters in the uni code space. And so that's the uni-ode character database or affectionately called UCD.

Um and uni-ode also defines a whole bunch of algorithms that you might want to use to handle uniode strings,

right? Openype on the other hand is a lot more focused. Um it's basically the SQL or an extension to true type that I talked about earlier. Uh it's it was made in the late 90s by Microsoft. um and it it's a standard that defines a file format and a bunch of procedures that you might want to use uh to handle this file format properly and it's a strict superset of true type. So the file format itself is very well documented uh the actual behavior of the fonts not so much. Um, now just to be super clear about this, uh, there's two distinct worlds and it's useful to think about the different spaces and kinds of data we're going to be working with. So, Uniode mainly deals with characters and characters are best thought of as elements of user input. Now, I know there's um, instinctively we would like to think of characters as elements of linguistic meaning uh, or something. Um, the reason why I say that uni-ode characters are best thought of as user input is because the uni-ode character set contains a whole bunch of characters that are just here for formatting or for controlling specific algorithms. And those don't have any linguistic meaning whatsoever.

Um, and so for that reason, and also because this happens to generalize well for us programmers, it's nice to think of a unic code string as just a stream of user commands. Okay. Um, these characters are represented by code points which are 20 bit integers.

Um, now on the other hand, open type deals with glyphs and glyphs are really just visual information. Okay, so a glyph is going to be a bounding box and then inside of that bounding box, you're expected to draw a bunch of glyph outlines. Um, and those glyphs are referred to through glyph indices which are 16 bit unsigned integers that uh the font decides to decide however it wants.

Now just to make this very clear here um there is a way to map a uni code string to a sequence of open type glyphs. Once you've done that conversion though uh you can't go back like this is this is a unidirectional data flow quite clearly. uh you you lose a lot of information uh once you are in the open type space.

Um so if we go look at this unidirectional data flow uh what do we see? Well there's a clear um direction going from uni code world to open type world. Um and I will be describing each

of these steps uh one after another in transformation order. So tech decoding

is the first thing that we want to look at. Um now just to be clear when I type

UTF here uh it could be any one of the UTF algorithms and in fact it could be any text encoding that you want to work with. Uh a text encoding is just how do we take the 20 bit integers defined by uni-ode and represent them in memory in some way right uh really the transformation is pretty much arbitrary. Um, I've heard uh some people say, "Oh,

text would be so much easier if it was just UTF32." Like you just have a 32-bit integer and that holds one code point and then it's so much simpler. Well, um, when you do that, effectively, what you're removing as like total code volume is this. So, if you think that's worth multiplying your uh, memory usage by potentially four um, or more realistically two or three, uh, it's it's up to you. But really, you're not saving much complexity by using UTF-32. Um, that's that's it. Uh, done with

decoding. Cool. Um, now we actually have

code points and once we have those code points, uh, we want to transform them to the open type world to know what to draw. Um, now, uh, remember I said for true type, uh, this worked with the cap table, which was a onetoone mapping. It still is in fact uh a onetoone mapping which is great if you're using a character set that does not have any ambiguity. What I mean by that is that when I type something and see something there's only one way to encode it in the text encoding or character set. Right? Unfortunately for us this is not the case for uniode. Uh uniode has in fact many many ambiguities. Um and so here's a very simple one. If I want to write this character uh I have two choices. I can either use a single code point that is letter E with acute accent or I can

use two code points one after another uh starting with letter E and then adding the combining acute accent afterwards.

Um so how do we reconcile this property of

uniode with the cap table that wants a onetoone mapping? Um just give giving you a hint right now. The cap table still exists in open type and it's still the only way to do this mapping. Okay. Um well thankfully for us we can look through the uni code standard and see what they have like maybe they have tools for us. Yes they do. Um there's one unic code annex. UX just stands for unicode annex by the way. Um there's one unic code annex that is uni- code normalization forms and this annex describes a bunch of methods to normalize strings. Now normalizing is exactly uh what I just described. It removes the ambiguities. Uh the main two that are described are fully decomposed form and fully composed form. Um as you might imagine, one will prefer the E and then accent and the other will prefer the single call point E with accent. Um so going back to open type let's just try to imagine what it might look like right to do this mapping. Um and what it might look like is maybe something like this right you take your code points you normalize uh your string and then you

send it to cmap and then the cmap table expected to be normalized in some way and so the one to one mapping still holds and so everyone's happy and we get the glyph indices right. Uh now there is one c question obviously I just said there were two main normalization forms. So like which one does open type use? Is it um written as a standard or is it like just a convention or something? Should we try both? Um well the answer is uh is that none of them are used. Um

too bad. Uh none of them are used. And what actually happens in practice is that the open type library needs to try every possible combination of code points and see which ones fit in the cap table. Okay. Um yeah. Yeah. Uh don't laugh. This has been my life for the past two years.

Um now after trying these combinations,

there's several other steps that are also involved in unicorn normalization. So like those steps are actually taken one for one from the standard. So that's that's great. We can actually apply some unic codes algorithms such as reordering marks. Um and reordering marks is

meant to deal with another kind of ambiguity which is well we have multiple combining characters right and so we can just keep stacking them. And so what exactly do we mean? Right? If I just want to write this C with the acute accent and a cadillo below uh I could rep I can also there well there's a single code point that does this um but forgetting the composition and decomposition aspect um say I really want to use the combining marks right uh I could use C accent and sadilla or I could use C sadilla and then accent and um the problem for us like this is not necessarily an issue for um our glyph

indices but it's an issue in general because fonts will try to pattern match neighboring glyphs and so the order actually matters a great deal right um so uh the way it works is that in

unicorn normalization we can we can consult the UCD the uni code character database um and it defines a property

per character that is the combining class and what the combining class is is an integer that tells us uh how to sort marks. So when we see a whole sequence of marks, we just sort them by their combining classes. It needs to be a stable sort because you can have multiple above marks and multiple below marks. Anyway, um so in fact the accent before

and after is the normalized form and that's what we're going to go with uh for for this code point sequence. Okay. Now cool. Uh we've already learned a few things just going through the first two steps uh of how text gets handled, right? Um well, we've learned that if if you receive a uni-ode string from an unknown source, right? Just like the user, maybe they copy pasted something from some some dubious website or something, you're not sure whether the um uni-ode code points are well ordered or anything. um well, you don't really need to worry if all you want is to send them to the open type library because it's going to like decompose and recompose everything anyway. So, um you don't need to care really about normalization uh if you're just wanting to communicate with these libraries.

Now, however, now that I've told you about normalization, you still might want to use it uh for caching your strings or for um comparing them um

because supposedly um if you normalize them on your end, then you can be pretty sure that um they will look the same um

no matter what the original encoding was. Okay, cool. So, now we've made it to the

open type world. Um now this singular uh

arrow for shaping is somewhat of an abstraction. Um shaping is really like the the meat of the problem here. Uh this is where the large majority of the time is spent and this is where the large majority of the uh code is written. Um so it really

deserves its own diagram and this is what open type shaping looks like. Now, I say open type shaping because there's actually a few alternative shapers out there that use different formats, but uh they're only used for like very niche circumstances. Like there's this script on Earth that literally cannot be represented well with the open type rules. And so we needed to invent a whole another way to represent them. Um

and so I mean you can go look look up uh graphite shaping for example. And so that's but uh the really like you're going to be dealing with open type fonts and open type shaping um for any widely

supported font. Now the way it works at a top level is that we get the glyph indices that we just got from the cmap table and uh the ga pass which is which

we can call glyph substitution um is going to loop over these glyph indices and modify them uh based on some some rules. Um once we have the final glyph indices we use the metrics tables to add

visual information so the advances uh to our glyphs. And now the positioning pass which is described in the GP pass table is going to modify the advances and offsets of our glyphs. Um and then there's a a bunch of like fix up and bookkeeping that needs to be done before actually giving you the results that you're going to see on your end as users. Now um here's some examples of what these different passes do. Um the glyph substitution pass might do uh this kind of a replacement and these are legatures. Um, I meant to add a picture of uh Martin's talk from yesterday because uh he had a a big slide with this specific FI liature. I thought it was neat. Um, and the but it also does like a bunch of other things that aren't liatures like contextual substitutions and it might take one glyph and uh multiply it into a bunch of other glyphs. It might take many glyphs and condense them into a single glyph and so on.

Um the G-P pass is not going to modify

any glyph indices. It's just going to bump them to some other place. So as you can see uh I just reused the kerning and the uh mark attachment example from earlier. Uh but again uh the rules can

be more complex than this. Um now the fix of pass you don't really need to worry about as users. But there is one specific thing that shapers do um that

you care about and that is flipping RTL results. Now what I mean by this is that when you give your string to open type uh it's going to be in logical order i.e. reading order, right? So it's if it's in English it's going to go left to right. Um but if you're giving it an Arabic string that's going to go right to left because the first glyph is visually on the right, right? Um unfortunately um the all of the visual information for open type kind of assumes that the glyphs are left aligned and so the origin is on the left. Uh and the mark attachment works very similarly. So it

actually reduces complexity a great deal if you don't um punt this problem uh to

the user and uh let them handle it. Um

it's actually a complexity because I tried it both ways. This is the only reason I'm telling you uh it's actually a complexity gain a win overall uh if

you flip everything uh to look like uh straight left to right sequences of glyphs no matter the input language or the input direction. Okay, cool. Now just a little bit more information about shaping because I've been very vague so far. Um in the font

there's these tables G sub and GPA that contain lookups. And what a lookup is is really just like some kind of pattern matching rule that tries to look at the sequence of glyphs and find places where it can be applied. And um these lookups are toggled on and off with open type features. And the reason I talk about these features is because you have access to them as users. Um you can tell your library uh

that any of those features you really want them or you really don't want them. Um and it's just going to deal it's just going to disable or enable the corresponding lookups. Okay. Uh now open type features well there's hundreds of them. Um but just as a few examples um

kerning lures and mark attachments are like very basic and what you might expect if you want a correct looking text. But uh there's a lot of fun things like superscripts and subscripts and even small caps. And fractions is one of my favorite because it tries to look for uh digit slashdigit and replace it with a singular fractional glyph. Um

anyway, I just thought it was fun. Um and so if you don't tell anything to your shaper, it's just going to select a default feature list. uh and that default feature list is going to depend on the script of the script you're uh the script of the string you're trying to shape uh and its language and its direction. Now um as users you should really not

think about all of this pattern matching stuff and just think of open type shaping as arbitrary by code. The reason for this is that even though it tries to pretend it just it's just like a few simple rules for replacement and um

visual bumps and whatever uh in practice fonts can and do use it for much more complicated purposes. Um and I mean this

is a problem for us in several ways. So

uh rules can have arbitrary look back or look ahead in the string. Um, so there's no easy way to like segment uh the string you're trying to use. There's no easy way to isolate a single glyph uh if you're still applying rules, right? Um, rules can generate arbitrary glyphs um

in arbitrary numbers. Um and there's

also a dispatch mechanism for rules uh that depending on your shaper can also be used to call itself uh which leads to recursion and has been used for a bunch of fun things um like emulating games or like uh doing a calculator in uh open type or uh things like that.

Okay. Um so as users you should really be aware that shaping can be slow. uh it can be really slow and it can be really complex. Okay. Um and there's no easy way to just split your string uh in ways that make sense like oh this is just a large string but I'm going to split it into different chunks and like shape each part independently. Well no you don't really get to do that. Um

now to be more accurate uh you do get to

do that. You just don't decide where that happens. Um, when you send your

string to the shaper, it expects all of your string to be a single font in a single language, in a single script, in a single direction, and on a single line, which means for us that if any of these parameters change, um, we can actually split the string and shape each part independently. Um, so I will now try to address every one of these points. Um, just so you understand the the context and what you need to do to handle these cases. Right? Now, why might you possibly want multiple fonts? Uh, well, if you want text styles, uh, it's a good idea. So, like bold or italic or even bold italic.

Um, if you want to handle font fallback and if you want to handle font collections, uh, just a quick word about font collections. Uh they're basically just a very simple um package format uh

where they just shove singular font files and into a single uh font collection. Right? So if you open a font collection or if you want to support font collections, the user kind of expects you to open each of the individual fonts inside of that collection as singular fonts. So either way, you're dealing with multiple fonts on the programming side. So like APIs for font collections are going to be just like give me the pointer to the base of the file and then give me the font index inside of that collection.

Okay. Um so the old style of handling

text styles is that you get one font per style. So you might have AIAL regular which is the normal style and then um if you want bold then you need Ariel bold as well and if you want italic then you need Ariel itic and if you want bold italic you need Ariel bold italic and so on and so forth. Um this is actually kind of enforced by the spec. Uh the the old uh font metadata tables have like a singular style that you can put on your text. So um that's that's how the format is. Uh now there's a newer alternative which is variable fonts. And what variable fonts are in is that uh well the the glyphs are going to have a default look to them. But um instead of just saying I'm bold or I'm regular, uh they're going to have a bunch of parameters that you can set to modulate stroke width and to modulate um uh font width or like GIF

width uh meaning like condensed and uh expanded fonts um within a single font.

Now, uh the important insight here is that either way, even if you're using variable fonts, uh if you change the style at all, uh that's a hard shaping boundary. Uh now, why might that be? Well, um if we just think it through, uh say I have uh a very a condensed uh run

uh followed by a very expanded run. Um well, all of the font metrics are going to change and uh the shaper needs to know about them. And so the shaper actually has branches in it that depend on the current font style or current font parameters being used. Um so a single rule can't really span that boundary because it would just be all wrong, right? Even if the glyph indices are the same. Okay. Um now the other case for supporting multiple fonts is font fallback. And font fallback is um comes

up very naturally. Say you have a primary font that you really like the look of, but again, you're not sure what the user is trying to type. And so you're like, well, if I don't have that glyph in my font, uh I would still like uh like Ariel, for example, which has plenty of glyphs, uh to render it. Um so

this does not happen on a code point granularity. It happens on a graphim granularity. Luckily for us, you already know what a graphim is because I just showed you a couple of them. Uh the E with the acute accent and the C with a sadilla and the acute accent. Uh both of them are graphims. What a graphim really is is just a base character with a bunch of marks after it that attach to that character. Okay. Um and that's an important unit uh of granularity in uni code. Uh this is why by the way UTF-32 doesn't really solve anything because you want to go through or like go across multiple code points very often in uni code anyway.

Okay. So very simply uh how you might implement font fallback right is that you go through each of the graph themes in your in your uh string and then you just test if they're representable in each font. Uh now you know how this goes. U this is the open type normalization. uh procedure that I explained earlier where we just try every combination. Luckily, this isn't only on a graphine by graphine basis. So like it shouldn't be too expensive. Um if there's no separate call to just run the normalization um on from your library,

you can also just like disable every feature and try to shape it and see if you get zero glyphs or not. Um and then you'll know if it's representable or not. Um but really it's it's just it's just this um meaning um

that as soon as our primary font can represent the next graphim then it's going to take over again because I'm just iterating over each font in priority order right okay um now just a quick aside this doesn't have anything to do with shaping uh it's more about presentation but now that we have multiple fonts how do we present them or how do we arrange them on screen Um well this is what I meant

by metrics. Uh these are the vertical metrics of the font. Uh we have a baseline which is y equals 0. That's also where the origin of glyphs lay. Uh

and then glyphs extend above the baseline. Uh and that's the ascender and they extend below the baseline and that's the descender. Um now if we if we just use the ascender and descender height uh collectively called body height. Uh if we just use the body height as our line height, we have a problem because well the top of my B might brush against the bottom of my G on the next line and like that that doesn't look good. I don't want my glyphs to like mesh together or anything. Um so fonts actually specify a third height that is the line gap which is just a mandatory spacing that you use on top uh to make sure that your lines look properly separate. Right? Okay. Now this is the simple case of course uh what we want to deal with is this case um where while the ascenders might not be the same and the descenders might not be the same. Obviously we still want to align everything to the baseline. Um so

which metrics do we use here? Well, we kind of just want to take the union of the bounding boxes, right? So, um, we'd

like to take the highest ascender and the lowest descender and so, uh, discard the other ones. Um, cool. Now, how do we handle line height? Uh, that's a bit more complicated. Um, really we care about line height and not line gap. And what we want to do is just take the ascender minus descender because the descender is below zero. So, it's a negative. Um, so we want to take asendrew minus desend plus the line gap. And that gives us the line height for each of our fonts. And um, if our union

bounding box is larger than that, then we don't really need to have a line gap at all because we know we've extended all of our metrics so far that there's no way that our characters could touch line to line, right? Uh, so in that case, we don't want line gap at all. Now, however, if one of the line heights of one of our fonts is still larger than the um the ambigant uh ascender and descender together, then we still want to have that remainder of a line gap. So, that's just what the these uh very simple expressions are computing. Okay, cool. Now, next case, uh we need

to split our string when we have a change in script or a change in language. Uh now I have good news and bad news. Um the script that your string is in actually can be inferred automatically by uni-ode. Uh this is this is described in uni code annex 24. Um unfortunately language cannot and the reason why is just because there's so much overlap in character sets between different languages that often times it's just completely ambiguous. So when you're defining or when you're uh making a new document format, uh you really just want to have like metadata that's provided by the author of that document that tells you which language this document is in.

Um otherwise uh the font has like a safe

default that it will default to if you say I don't I don't know I don't know what the language is. Um now let's just ask and let's just like check this information with our intuition, right? uh why would we need to change um why would we need to to like have a shaping boundary? Why would we need to shape each side separately when we change script or language? And the reason why is simply because there's very different typographic rules to um each different language even using the same script. So

uh what you might use um in English as just the letter O followed by the letter E. um in French a font might completely

want to replace these characters with a liature. And um well, if you have uh if you have a

change from English to French, then you don't really want like one French letter to glob the other English letter across the boundary. Like that doesn't make any sense. So either way, um completely different rules. Uh so completely different pipeline and completely different text shaping. So that's a hard boundary. Now I'll explain very quickly how script inference works. Uh we go back to our trusty UCD which defines a few properties that we're interested in this time. Um there's script and then script extensions which uh together allow us to have a mapping from a uni-ode character to a set of scripts. Now I do mean a set of scripts. A unic code character does not have a single script. Uh it can have multiple. Now this is an example. All of the punctuation here is considered common just because it's used in way too many scripts. It can belong in any of them. Uh so I start scanning from left

to right. And uh when I get to the semicolon, I still have no idea what my script is. Now when I get to the Devonagari digits here, they're just 0, two, and three, by the way. Um, when I get to these digits, I know that my script is going to be one of these four, but I don't know which one. Uh, now if the script if if the string were to stop here, I might just decide to choose Devonagari because it's first in the list or something. But, um, really there's just no way for me to infer with perfect accuracy which script this is meant to be. And only when I get to the very last letter here do I know that this is a Devanagari string and that

everything actually needs to be uh collapsed to Devanagari. Okay.

Okay. Now the next case where you want to split your strings before sending them to the shaper uh is when you change direction. Now, I can already hear uh the lazy programmer instinct uh saying, "Well, maybe I can just get uh by with uh handling left to right, which I already do, and then just strictly write to left lines." And um I'm just going to ask the

user to like manually go to the next line if they want to change directions. And like surely that's not so bad, right? And it's like way simpler. Um, unfortunately for the lazy programmer in each of you, uh, the answer is no. And

the answer is a very emphatic no for many different reasons. Um, I mean, in practice, right to left languages use left to right all the time. Uh, they use them for loan words and proper nouns, which hey, uh, they are often in English. Um, and they also use them for digits because like surprise surprise, uh, they use the same digits as we do and digits are always left to right. So unless you want your users to go to a new line every time they type a digit, you need to support birectional. Okay.

Um, now thankfully uh, Uniode also tells us how to infer direction from plain text. Um, and this

is described in another annex. Um, and

let's ask our intuition again or let's have like an intuition check again like why would we need to split the string uh if we have a direction change because surely if everything else is the same the rules don't change like maybe we can just like shape everything together. Um

well this is an example layout for like a birectional string. It starts off as left to right and then starting at lily index six it goes right to left. Right now, there's there's no issue here uh from index zero to index five because they're all connected and nicely contiguous. Um but when I get when I go

from glyph index 5 to glyph index six, there's a visual jump here. And uh while it just so happens that every open type rule kind of expects to work on visually adjacent glyphs. So all the attachments and all the liatures and stuff um don't work anymore. And so we have to add a hard shaping boundary here.

Um, now let's look at how to infer uh direction for your strings. Um, we look at the UCD again. It's

pretty useful. Um, and there's something called a birectional class that is some kind of integer. In practice, it's an enum uh that tells us some information, some directional information about each of these glyphs. Uh now it's not as simple as just left to right or right to left obviously. Um so let's check an example. This is an English string. So far so good. Uh if we look at the birectional information, we see that uh the English letters are strictly left to right. However, we see that the punctuation is uh neutral. So it could go either way. Uh turns out that in this case, the inference we do looks like this. uh the punctuation becomes left to right uh though each of the punctuation marks here becomes left to right for different reasons. The comma becomes left to right because it is surrounded on both sides by left to right whereas the uh exclamation mark becomes left to right because the line itself is left to right because it can be surrounded on both sides because it's it's at the end of the string. Um now how do I know that the line is left to right? Well, uh, you could say in this example, well, it's simple. It's just because the line starts with hello, and I know hello is left to right. So, surely the rest of the line, I mean, the entire line just takes direction left to right, right? It gets a bit more complicated when we look at all the characters. Now, uh, they don't need to be very exotic for the rules to get complicated. Uh, we can just look at digits.

Um, now digits have a special class that is a weaker left to right class. Okay. Now what this means is that they don't affect their surrounding context. They don't give their directions to the line. And um and so it means that actually it's the word time setting that makes our entire paragraph left to right here. Um now you might wonder well what does this look like if I remove the type setting word? Like what's what's the direction then? Uh well it might look something like this. If I replace type seting with an Arabic word, uh, well,

what happens is that the digits are still left to right. And by the way, this means that my string starts on one.

The first character in my string is one. Visually, my string starts in the middle. Okay, cool. Uh, yeah, I just

want you to like get used to thinking about this. Um, so my string starts with

the number one, two, three, four, which is weak left to right. So layout is still left to right for these ones, but actually the entire line becomes right to left at this point. Meaning that all of the punctuation marks also become right to left. N

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 538 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks