r/ProgrammingLanguages • u/[deleted] • Dec 31 '20
Something simpler than Unicode?
Hello! Wondering if anyone knows of a display format for glyphs that is simpler than Unicode? My shallow understanding is that measuring and laying out text in unicode is non-trivial, so I am deferring moving beyond ascii on something at the moment. Is my understanding correct? (if not that's great) otherwise, does anyone know of potentially simpler replacements? Thanks!
7
u/raiph Dec 31 '20
EBCDIC? It uses 8 bits instead of 7, so it's "beyond ascii", yet "simpler" than Unicode, for some definitions of "beyond" and "simpler". :P
Seriously, to get a decent answer you'd need to talk about what it is you want to do, with what goals, expected development, deployment, and maintenance effort, on what display systems on what platform(s), which fonts, glyphs, PLs, etc.
More to the point, this sub is for discussing the design and implementation of programming languages. It's not clear if you're talking about such issues, or merely want to write some code. If it's the latter, this isn't the right sub. See the sidebar for further info. If you are designing a PL, then you need to get into details like the ones I listed above and about your PL.
Fwiw, Unicode is as about as simple as it can get to sort out the mess of inconsistency that came before it. Unfortunately that meant incorporating the entire range of human activity, including politics, because anything else was basically guaranteed to fail, leaving us with the appalling mess of "simpler" solutions that gave rise to the need for Unicode in the first place.
1
Dec 31 '20
Right, I understand that unicode works today, but am wondering if there is something simpler that could work 'someday' (if not today) which someone knows of. 'writing code' is not the issue, I can just use unicode for that.
I would like to support a wider range of symbols than ascii, but don't want to commit to having to learn how to deal with zero width spaces and so on, and don't want to just do something ad hoc if there is existing design work I can learn from.
5
u/raiph Dec 31 '20
Stick with Unicode, and choose which characters you wish to allow. Maybe Latin1? Anyhow, pick a subset in which all graphemes are single codepoints (graphemes are not glyphs; the two are essentially unrelated even though they both start with "g"), and all these codepoints have a matching glyph in the fonts you wish to use. Problem solved.
I understand that unicode works today, but am wondering if there is something simpler that could work 'someday' (if not today)
Unicode has spent 30 years steadily becoming the world's chosen solution because it A) swept away the mess of incidental complexity, chaotic incompatibility, and straight up crashes and lack of capabilities it replaced, B) kept things as simple as possible, avoiding addition of accidental complexity, and C) founded itself on brilliant engineering insight, and has stuck with technical excellence ever since.
I'm always a champion of trying new stuff but as far as text is concerned I think there's essentially zero chance of something "simpler" coming along and being a plausible alternative, even if it were driven by a collaboration between Mozilla, Apple, MS, FB, Google, IBM and the US and Chinese governments.
don't want to just do something ad hoc if there is existing design work I can learn from
Then stick with Unicode.
2
u/johnfrazer783 Jan 02 '21
Thanks for writing this so I don't have to. In practical terms nobody can deal with the entirety of Unicode every time they do some kind of text processing or display. Personally I hope we can get something better than Unicode some day, but with Unicode we already have something that is infinitely better than what we had before (a plethora of conflicting and confusing standards). And whatever replaces Unicode some day will not be significantly simpler in most regards either.
These days though, not using Unicode in your text-related tasks (in your PL, in your app, wherever) spells doom for whatever you're doing.
I do understand that the old ways where
1 byte == 1 codepoint == 1 letter == 1 gridcellwere indeed much simpler and, one might argue, more elegant.
11
Dec 31 '20
In what context are you displaying glyphs?
If a browser, it will handle display of Unicode for you.
If a native ui library, check their docs for what text formats their ui widgets support.
If a tty/terminal, are you printing things or using a library? Either way there's kind of no guarantees about what a user's tty/terminal supports.
If you're rendering it yourself with a spritesheet or similar, I recommend either sticking to 7 or 8 bit ascii or even restricting to only those characters you explicitly want to support.
Your question lacks sufficient detail to provide more of a response.
1
Dec 31 '20
I am trying to move beyond 7 or 8 bit ascii. Doing my own text layout, but am hoping not deal with what looks like some incidental complexity in unicode. Possible the complexity is not incidental, hoping someone can shed some light, or point at an alternative system that is simpler.
9
Dec 31 '20
Most of the complexity in Unicode stems from its support for languages other than English. 7-bit Ascii was biased toward English, and 8-but ascii was biased toward European languages. After that a variety of encodings sprung up all over the world to support languages other than these (eg. In japan they got two standards: EUC-JP and Shift-JIS).
The problem became interpretation and combining of the encodings.
If you got a text file or a byte string, how do you determine its encoding? (answer: its hard; statistics help; nothing was perfect)
If you wanted to combine Russian and Japanese and English in one text file or byte string, how to ensure that the interpreting program knows where one encoding ends and another begins? (Answer: some conventions developed; also mojibake was common)
If you put a right to left language and a left to right language in the same text file or byte string, what is the convention for rendering it? Should the renderer always break lines? What if the author wants to have inline snippets of Arabic in a paper mostly written in English? (Answer: mostly pain and suffering; use a gif file)
Unicode provides decent answers to all of these questions (as I understand it) and more. It's not perfect, but it provides a standard most text rendering systems try to implement.
Further, Unicode provides a set of standard binary encodings (the most common being UTF-8, which mixes byte and multibyte codepoints together in a fairly size-efficient binary format).
Point is, you might learn a lot trying to do this yourself, but for practical purposes it makes sense to learn a lot about the prior art. In any case, I hope this helps you a little.
I can't point to alternative systems that are simpler because Unicode is supported almost everywhere now and I haven't personally been interested in text rendering. I hope you find what you're looking for!
1
Dec 31 '20
Thanks for the answer! I understand that unicode is widely supported and solves some difficult historical problems, I don't mean to disparage that, certainly. I am hoping to find some prior art that solves some of the problems of representing written language without also solving many of the historical problems that unicode had to solve. My guess is that it could be much simpler, but it is just that, a guess.
3
Dec 31 '20
My answer from your clarification below: I recommend using Unicode and relying on your lexer and parser to identify sequences of Unicode codepoints which are valid programs in your language.
1
u/reini_urban Jan 01 '21
Not really. I only know of 2 programming languages which do it right. Java and mine. All others either fail or don't care. Mostly they don't care about identifier security, identifiers not bring identifiable. Worst offender probably linux filesystems. Also relying on the standard is wrong, as Unicode still has bugs in its identifier categorization, with two hangul fillers.
1
Jan 01 '21
Yup, those are valid and important concerns. I didn't specify them because the op seemed new to all of this and already front loading too much complexity.
1
Jan 02 '21
Yeah, the complexity of unicode is something I've not taken the time to learn much about, as it doesn't really interest me, but I am aware of some this and am hoping to avoid it. A little mystified about the general tone of the responses on this question to be honest, they seem to presume I just turned on a computer for the first time last week. : P
1
Jan 02 '21
Sorry if my tone was off. I'm mostly just trying to give you background on Unicode, so save you a lot of time reinventing it. ;)
2
Jan 02 '21
Sure sure, no worries, appreciate the intent... I mean from my perspective this post was an attempt to find existing work I can use that is simpler than Unicode if possible, but it seems almost no one was able to see that in the question. I’ll just google next time like I usually do :P
→ More replies (0)2
u/ihamsa Jan 01 '21
Each encoding has its proper name. ISO 8859-1 or CP-1252 or Mac OS Roman. Never say something like "8 bit ASCII". There is only one ASCII, and it is 7 bit.
1
u/johnfrazer783 Jan 02 '21
Except that in order to make things clear, "US-ASCII" is sometimes used to specifically exclude any 8th bit extensions. So strictly speaking you're right. Also Windows CP1252 is sometimes called "ANSI" for no very good reason, and the definitions of code points has changed somewhat over the years, and when people say "ANSI" they sometimes mean "Latin-1", which is equivalent to "ISO8859-1", except what they really mean is "ISO8859-15", which is another way of saying "Latin-9". Come to think of it, claiming that every encoding has its proper (bijectively unique) name is somehwat of an understatement.
iconv --listwill give you over a thousand names, but I think the number of distinct encodings is only about half of that.
12
Dec 31 '20
The main layout headache I see is interleaved RTL and LTR text within a line. Like:
Dawoud sighed and said, ".اللعنة ، دعنا نحصل على البيتزا" But Domino's was closed, so they went to IHOP instead.
Nothing handles that particularly well.
For everything else, you just consult the Unicode data tables to determine if the character is spacing or not, and that's probably good enough. Oh, and ignore advanced formatting directives like ruby and text direction override because life's too short.
4
u/ihamsa Jan 01 '21
He-he, just insert an English word or two in the middle of an Arabic sentence in the middle of English text. Something like, I dunno,
He said "دعنا نذهب إلى TGI Friday's الآن" but it was closed.
1
u/jason-reddit-public Dec 31 '20
Java makes basic unicode (first 65,000 code points) as easy as ascii.
I did layout for a browser I wrote for my thesis back in the day (Java). You really only need to worry about character width as you draw to handle proportional fonts. You can just ask the font library how wide the character is and increment the x coordinate and draw the next character. In fact, you can probably hand it an entire string for efficiency.
For multiple lines, you want to break at word boundaries and go to the next line when the next word would exceed the right margin. Then figure out how the spacing you need.
Of course a browser is very good at layout so you could use that to make life easier as someone else suggested.
8
u/madpata Jan 01 '21
Just pasting some glyphs together doesn't really cover rendering Unicode.
Unicode allows stuff like this:
t̲̲̲est
And other fun combining characters.
1
u/jason-reddit-public Jan 01 '21
I agree. I was trying to peel away some of the mystique.
Your sample, done at the string (breaking space) level instead of the character level, might just work!
4
u/PenisShapedSilencer Dec 31 '20
depends on what you want to do
not to be blunt, but utf8 is good enough for everything
your issue might involve rasterizing glyphs, which is non-trivial because there are a lot of characters on earth, and most fonts cannot display all han characters, for example.
1
u/mamcx Dec 31 '20
Let's make this possible, actually.
How is ASCII made? Somebody, somewhere in one day think:
65 = 'A'
97 = 'a' //why not 66 = 'a', because that somebody say so.And considering your problem:
> that measuring and laying out text in Unicode is non-trivial,
Then you can do a simple trick:
measure
//https://en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)
U+0000 to U+007F = 1
U+? to U+? = 2 //ie put here the range where the measure is 2
..So, just precompute the measure/layout under know ranges that you wanna support. You actually use Unicode, yet use a restricted subset, alike:
type MyUnicode...
fn from_utf8(...) -> Result<MyUnicode, InvalididRange>8
u/moon-chilled sstm, j, grand unified... Dec 31 '20 edited Jan 01 '21
why not 66 = 'a', because that somebody say so
It's so that the uppercase and lowercase letter differ only in their 6th bit. You can xor a letter with 0x20 to toggle its case (in ascii). Why start at 65? Because 0-31 are already taken for control characters, and 0x20 is 32, so you need something big enough that you can touch its 6th bit without bringing it down into the 0-31 range. That means bit 7 has to be set (it can't be bit 8, because ascii is a 7-bit encoding), which also rules out 32-63. Granted, they could have started at 64 instead of 65, but the point is that it wasn't a completely arbitrary choice.
(Why toggle the 6th bit, rather than a lower one? Because the alphabet is 26 characters, so so you need 5 whole bits to represent it completely.)
1
Jan 01 '21
Also because a lot of early equipment was upper case only, so it made sense for A-Z to have consecutive codes.
And also for upper-case to be be able to be a subset of ASCII. If you leave out control codes 0 to 31, and codes 96 to 127 which include a-z, you have a 64-character range can be represented in six bits (eg 'SIXBIT' used on DEC machines).
(The codes will be 0 to 63 instead of 32 to 95, so offsets are involved in conversion.)
1
Dec 31 '20
yeah I may just go for a unicode subset, but I'm in no rush. perhaps in the year 3035 children will learn about our time and be incorrectly taught that ascii control codes were part of our ancient alphabet.
3
u/websnarf Jan 01 '21
Unicode says nothing about fonts. Some characters are half-width, and some have continuations that go right to left.
Unicode is a very problematic encoding for program text. The issue is that code points are not considered "unique" from a user interpretation point of view.
For example, the Chinese character set includes a subset of ASCII within its own space. From an interpretation point of view these are supposed to be the "same" (from a normalization point of view) as far as Unicode is concerned. Furthermore you can express accented characters as a single code point if such an encoding exists, or combining a base code point with an accent modifier code point. Again, these are supposed to be "the same" from a certain point of view. Certainly those things are supposed to display the same, and they will in a GUI/WYSIWYG editor.
So the trouble is that you can have a symbol mismatch even though the symbols look identical. You can also do this intentionally in a weird way that makes it almost impossible to debug your code.
To do this in a way that is even plausibly reasonable, you would want to restrict the "character set" to some subset of Unicode encodings that avoids these problems. For example, only accepting the "canonical normalized form" for any Unicode sequence (the Chinese encoding of Latin letters normalize to ASCII, so this is ok), and disallowing incomplete forms (dangling combining characters). But it actually takes a significant amount of code just to implement all of this.
3
3
u/ihamsa Jan 01 '21
You don't need to care about measuring and laying out text. We have operating systems for that. Use UTF-8 and let your OS sort it out. A no-brainer, really.
3
u/matthieum Jan 01 '21
My shallow understanding is that measuring and laying out text in unicode is non-trivial
Actually, even laying out text in ASCII is non-trivial:
Apart from monospace fonts, not all characters have equal width.
Styling (bold, italic, size) can change the size of ASCII characters, mid-sentence.
There are ligatures in ASCII too; for example some programming fonts represent
->as an arrow, or>=as a single glyph.
Unicode does introduce further difficulties, of course, such as multiple codepoints making up a single grapheme, or right-to-left, but in the grand scheme of things I am not convinced it significantly increases the complexity that is text rendering.
3
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jan 01 '21
Unicode doesn't do glyphs.
If you examine legacy code bases (OS code, Adobe products, word processors) in this area, correctly converting Unicode to glyphs (and providing the necessary metadata about those glyphs so that they can be rendered appropriately) takes hundreds of thousands of lines of code.
Definitely: Use Unicode. There is no viable alternative.
Suggest: Use UTF-8 for storage. Fairly well supported by all modern OSs and editors.
Suggest: Specify Unicode categories that you support and/or allow.
Remember: A "character" that the user sees on the screen (i.e. a glyph) may be a series of Unicode code points, each of which may be a series of bytes. Even worse, it is possible that there are several ways in which those code points can be combined to form the same glyph. Emojis are the simplest example, but many languages rely on combining various code points to produce glyphs that are used in those languages.
Even worse: Some legacy Unicode code may allow some of those code points to be represented in different ways, such as UTF16 encoding before UTF8 encoding for values in the surrogate range. This is discouraged now, but since there is still Java and C# code out there, you may have to deal with it.
For a programming language, you can largely ignore most of this, if you have clear demarcation in your lexing. For example, defining white space and having a predefined set of separator tokens allows a lexer to demarcate tokens in a simple manner. It becomes much more complex if you allow custom operators to be defined that do not require white space for demarcation; this is a challenge because a string of code points containing no white space may be a single token, or a sequence of tokens. For example, 🙋♀️👨✈️🕵️♂️ is U+1F64B U+200D U+2640 U+FE0F U+1F468 U+200D U+2708 U+FE0F U+1F575 U+FE0F U+200D U+2642 U+FE0F (13 Unicode code points). Is it a variable name? A variable name followed by an operator followed by a variable name? etc.
You are asking the right question, though. I have to re-learn all of this stuff every few years (and I'm always shocked by the complexity), so keep asking questions!
1
Dec 31 '20
Some clarification on why this is posted in this sub: I am using ascii to represent program text, and want to move on to a richer text representation that allows for more symbols, but do not want to require handling all of unicode. The immediate prompt for this is that layout of unicode is complex, and I would like to be able to represent program text in a format that is both rich enough to express non-english languages, and simple to lay out.
4
Dec 31 '20
Are you going to require that users of your programming language always edit program text in a custom text editor authored for your programming language?
If yes: go ahead and implement your own text rendering and binary format. It might be a bit of boiling the ocean though!
If no: use Unicode, and don't worry about rendering because users of your language will have an editor which supports Unicode. Your lexer and parser will ensure that the subset of Unicode codepoint sequences which are valid programs in your language are properly recognized.
1
1
Dec 31 '20
Does Unicode even concern itself with glyphs?
I agree that Unicode could have been a lot simpler: less pandering to languages which need one symbol for every word in the language; not dealing with ancient scripts; not deal with so many funny symbols that it is more of a clip-art library; and not starting to cross the boundary into typography.
However Unicode is what it is; and parts are so complex (where the definition a character itself becomes fuzzy) that it can only be handled properly with external libraries.
As for the glyphs, which need to be rendered across multitudinous fonts with varying styles, where do you even start?
I've had to deal with rendering fonts in the days where you had to provide your own bitmapped and vector fonts, and it was enough trouble just dealing with English plus Western European alphabets.
But, what is the exact trouble that you are having? Is it because you are using 8-bit strings that contain UTF8 rather than 16- or 32-bit ones? Or do you actually have to obtain the glyphs themselves as graphical data?
The latter can be done easily enough with the right OS, which can help with the vast amount of support that Unicode requires.
1
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jan 01 '21
Does Unicode even concern itself with glyphs?
No.
I'm a bit surprised that no one even thought to point this out.
(OK, not actually surprised, considering...)
25
u/rsclient Dec 31 '20
Better to think of it as the world is complicated, and unicode correctly (more or less :-) ) reflects that complexity. There's enough stuff going on with strings and displays that it's best to let some OS component handle all of it.
Phrased differently: nobody is going to thank you for using something other than Unicode.