5 ms·
It's not actually that different or complicated if you're already doing proportional plaintext rendering -- you need to store style attributes for a range of te
by KerrAvon 3y ago
It's not actually that different or complicated if you're already doing proportional plaintext rendering -- you need to store style attributes for a range of text, and you need to support different heights per line.
The real complexity is rendering all of unicode properly, and supporting international fonts, bidi layout, vertical text, etc.
- starfreakclone 3y agoIndeed, you could even use a piece tree just like in the blog but you store additional information in each piece which tell the renderer how to layout the associated text. My understanding is that most rich text editors represent the text as a node-based tree anyway.
- WorldMaker 3y agoSome early rich text editors never used a tree representation for formatting represented the formatting as "control character sequences". This was part of why some people loved WordPerfect so much because you could toggle a view of all the control characters and just delete/copy/paste them like any other text in the same document. It's basically how the classic RTF format [1] works, and things like VT100/ANSI escape codes in terminals. It's kind of like the difference between imperative code and declarative code: "this character sequence means toggle the state of bold" versus "this node of characters is bold". [1] https://docs.fileformat.com/word-processing/rtf/ https://docs.fileformat.com/word-processing/rtf/
- badsectoracula 3y agoI think unicode, fonts, etc would be an issue even with a plain text editor anyway. The "styling a range of text" is something i thought but you still need to somehow associate the text with the range - and vice versa - and this doesn't handle things like inserting images and other types of objects since these aren't text. You could have a document be a series of "paragraphs", each being a series of "elements" with each "element" being something like "text" (with a style), "image", etc. But then once tables enter the picture, you need to expand paragraphs to be of "table" type and each table cell is itself a self-contained "series of paragraphs" - and then start thinking about nested tables or images in tables! Generalize that enough to avoid special cases inside special cases and you end up with more of a tree-like structure representing a DOM and less with a linear structure with range-based styling. (of course, then again, i don't remember Write for Windows 3.1 having tables in the first place :-P but i'm interested if there are alternative approaches anyway) EDIT: one thing i forgot to mention - and why i am curious about non-DOM-based approaches - is that one problem with the DOM approach is the selection: with a linear/range-based structure the selection is just one or two indices inside the range, but with the DOM the selection can start from a node with node-specific subrange (e.g. character in a text node) and end with another node and both being very unrelated to each other (i.e. only having some distant common ancestor and not necessarily at the same level).
- b33j0r 3y agoI might have a way to simplify this? A plaintext document is an array of chars, a richtext document is tree, which may or may not be well-formed. Think about someone trying to bold semi-half of_a sentence_, and how MS Frontpage was made by smart people, it’s just really hard. The most interesting thing lately is the HTML attribute `contenteditable`, and how it almost just kinda works! You still have to be full-stack to make something good, but that was an amazing improvement to the browser.
- badsectoracula 3y agoYeah, that trying to bold half of a sentence - or even better, the middle of a sentence - is why i was wondering about simpler alternatives to DOM. Some time ago i toyed around with an HTML editor[0] (that one had to use a DOM anyway, but my question is for rich text editing in general - BTW the rectangles in the shot show a selection that goes across nodes) and doing something like that involved traversing all the nodes (going both down and up the node tree, starting from the cursor's starting position), finding the closest common ancestors under the selection, creating "B" siblings to them and then reparenting them under these new "B" nodes. You can move a lot of that stuff to reusable methods but personally i find the whole "editing" aspect to be more involved than the "drawing" side - and also the one more likely to be different than a plain text editor - when dealing with DOM-like structures. Hence why i am interested to see what alternatives there are. [0] https://i.imgur.com/jLlyNSS.png https://i.imgur.com/jLlyNSS.png
- b33j0r 3y agoNice. I’ve searched far and wide, and most people still end up starting a whole company that only does a text editor. Quill works but is basically dead since 2017. Almost anything foss is in a similarly ambiguous boat. And then people like us, defeated, eventually buy something when we actually need it. There are just so many ways that users try to use it. It’s a tough problem!
- alpaca128 3y agoText is usually stored as tree either way in an editor, using a DOM-like approach might work well on top of the usual datastructures. > with the DOM the selection can start from a node with node-specific subrange (e.g. character in a text node) and end with another node and both being very unrelated to each other I'd just store the range as character indices, using those the right nodes in the tree can be accessed pretty quickly as needed.
- z3t4 3y agoYou don't need any fancy data structures. 95% of the performance goes into glyph rendering. And with Unicode the performance gain from monospace fonts goes out the window as some Unicode characters are very large, and not only that they also take up many bytes. So one Unicode character can be up to 5 bytes long and take up the same canvas space as 3 characters. You also need to read ahead as there are combination characters, for example a smiley combined with the color brow becomes a brown smiley. I've blogged about implement support for Unicode in an editor here: https://zäta.com//en/blog/editor10.htm https://xn--zta-qla.com//en/blog/editor10.htm
- rcoveson 3y ago> So one Unicode character can be up to 5 bytes long and take up the same canvas space as 3 characters. 5 bytes? In what encoding?
- dredmorbius 3y agoThere was an emoji example of length seven posted to HN recently: <https://news.ycombinator.com/item?id=36159443 https://news.ycombinator.com/item?id=36159443>
- jychang 3y agoEmojis with skin color, mostly
- rcoveson 3y agoNo, that's a ZWJ sequence. Those can be arbitrarily long. Doesn't explain where "5 bytes" comes from.
- throwaway2037 3y agoIs there a maximum number of "zero-width joiner" (ZWJ) sequences that can be combined to create a single-width emoji? (Yes, I know the term "single-width" is a loaded term.) I cannot find a precise answer.