10 ms·
Implementing Font-Size in Servo: An Unexpectedly Complex CSS Property
- mynewtb 9y agoWhat an interesting and educational post! Thanks! It's baffling just how complex CSS became over the years. I wonder, is the extra work involved for all the relative tracking something to consider for website performance?
- Manishearth 9y agoGlad you liked it! Re: perf tips for authors: I don't think so, the tracking is pretty central to fundamental features of fonts (i.e. the default font size) I guess using absolute font sizes as close to the top of the DOM tree as possible will help a tiny bit. But it also means that folks with custom default font size settings wont have the settings get respected. I think this has a pretty minor perf impact overall as well.
- icebraining 9y agoIt's baffling just how complex CSS became over the years Well, except for calc(), these cases seems to come all from CSS 1. Interestingly, the font-size section mentioned how a font may be sized in a VR environment. I remember VRML, but didn't know there were plans to apply CSS to VR back then! https://www.w3.org/TR/1999/REC-CSS1-19990111#font-size https://www.w3.org/TR/1999/REC-CSS1-19990111#font-size
- olavk 9y agoEven before CSS 1... Initially CSS tried to emulate what was available in "presentational HTML" available at the time. The absolute and relative font sizes were introduced by the FONT-element and BASEFONT-element, and the BIG and SMALL elements.
- irishsultan 9y agoWell, not the MathML cases I think (although that's admittedly not part of CSS)
- irrlichthn 9y agoI wrote a free responsive website editor (named "RocketCake"), and was surprised how complex CSS and these HTML rules are to implement, although I only needed not all of them. When I told my fellow programmers, they didn't believe me. I'll send them a link to this. Nice article!
- leeoniya 9y ago> A lot of the web platform has hidden complexities like this, and it's always fun to encounter more of them. "fun" is an odd choice of words here; (╯°□°)╯︵ ┻━┻ seems more apt. also, what's the future of MathML? what's the point of continuing to support a standard that Chrome has decided not to implement? it means 50% of web users won't see MathML, so web authors will never bother writing it. there was this a while ago: https://news.ycombinator.com/item?id=11444830 https://news.ycombinator.com/item?id=11444830
- Manishearth 9y agoWell, from my point of view it's less work to implement it than it is to argue removal from Gecko (which could happen, but it would take a while). Stylo must have Gecko parity, whether it's by adding more features to stylo or unshipping features from Gecko. We've done the latter in some cases for nonstandard stuff folks no longer use. I don't really know what the future of MathML is. Some folks seem to still use it, though less often on web pages.
- leeoniya 9y agoThanks. Also, does Servo incur any overhead from Stylo having to implement legacy or Gecko-parity features (eg -moz prefixes, etc)? Or does Servo not use Stylo directly? Same question for other Quantum components.
- Manishearth 9y agoStylo is the project name for taking Servo's CSS system and hooking it up to Gecko. We conditionally compile out unsupported stuff. There is some complexity in shared code introduced but very little of it is anything but a complication of the abstractions (not an actual behavior-level change which can have a perf impact). So for example the weird cascading behavior exists when you compile pure servo (because that's a core functionality of font-size, which servo does implement), but servo doesn't have to deal with the mathml stuff or text-zoom. Servo has some no-op code for text-zoom that lets shared code be written cleaner, but it compiles out.
- 9y ago
- Rodd45 9y agoInteresting to see how complex it has become.
- _pmf_ 9y agoWould one really expect anything at all in CSS to be simple?
- p0nce 9y agoThanks, I had always wondered how font-size was implemented.
- tannhaeuser 9y agoWhat an amazing article. Thanks for sharing. I've been wondering for some time now whether CSS should have a formal semantics to help implementations, and for posteriority (eg. not leaving behind a mess of specs making it infeasible to implement browsers from scratch/specs for generations to come). For an example of what I'm after, see [1], which is using attribute grammars/logic grammars for a small fragment of CSS box layout, and is one of the precious few attempts of a formal semantics for CSS. [1]: https://lmeyerov.github.io/projects/pbrowser/pubfiles/extended.pdf https://lmeyerov.github.io/projects/pbrowser/pubfiles/extend...
- thinkMOAR 9y agoThe above, plus i did not know about 'rem'. And now i do, thanks for that.
- Manishearth 9y agoThere are a bunch more interesting ones that folks often haven't heard of, like ex (relative to the height of an x) or ch (relative to the width of a 0) https://developer.mozilla.org/en-US/docs/Web/CSS/length https://developer.mozilla.org/en-US/docs/Web/CSS/length (not all of the ones in that page are implemented by browsers, e.g. I don't think anyone implements `cap` or `ic` since it's from a draft spec)
- christophilus 9y agoI'm always amazed by how performant complex CSS can be. Really complicated pages render in a few hundred ms, even though so much complicated computation is happening under the hood. Browser developers are true wizards.
- rhythmvs 9y ago> “monospace fonts tend to be wider, so the default font size (medium) is scaled so that they have similar widths” > “the base size depends on the font family and the language … Default system fonts are often really ugly for non-Latin- using scripts.” ‘often’, ‘tend to be’: I am worried. I think it’s a really bad idea to deflect default behavior based on such assumptions, certainly when the deviations are a blind process triggered by proxies (like language tags and some vaguely statistical rules-of-thumb for dealing with generic font family names). That is: without even looking at the actual design and metrics of the actual font involved.¹ What happens if the monospaced font in case has a normal x-height and/or an advance-width equal to that of its serif counterpart? What if the CJK and Devanagari fonts have characters drawn already ‘big on the body’?² Then such hard-coded default moonshot-fixes which try to cater for the lowest common denominator will make things needlessly hard to debug and force the designer still to ad-hoc size-adjust font per font, but now also trying to fix the browser’s ‘fixes’. (Too bad: any `normalize.css` wont help…³) And yet, all of the needed data is available in the font file. There’s even a dedicated CSS property for dealing with fonts’ varying metrics: `font-size-adjust`.⁴ Not that browser makers care to implement⁵, but since the OP’s post concerns Firefox (which does support `font-size-adjust`, but the article does not discuss it) I wonder: is it a matter of performance that retrieving the actual font metadata and metrics is left out of the equation? Surely, the fact that local font files, base64 data-URI embedded or externally hosted ones can be used, makes implementation all but trivial… At Textus.io⁶ we’re going great lengths to solve typographic issues such as these. Point in case: for each font we read out the `xHeight` value, then calculate the actual font-size relative to the font’s UPM (unitsPerEm), so we have consistent apparent font-sizes, c.q. aspect ratios. I think it all boils down to a separation of concerns: proportion and interrelated sizes (ascender, caps, descender, x heights, stem width, etc.) are up to the discretion of the font designer, overall aspect size is the business of the typesetter (css stylesheet author), and the browser ought always draw consistently, regardless of generic font family name, language and/or Unicode code range. ¹ https://opentype.js.org/font-inspector.html https://opentype.js.org/font-inspector.html ² https://medium.com/@xtianmiller/your-body-text-is-too-small-5e02d36dc902 https://medium.com/@xtianmiller/your-body-text-is-too-small-... ³ https://github.com/h5bp/html5-boilerplate/issues/724 https://github.com/h5bp/html5-boilerplate/issues/724 ⁴ https://developer.mozilla.org/en/docs/Web/CSS/font-size-adjust https://developer.mozilla.org/en/docs/Web/CSS/font-size-adju... ⁵ http://caniuse.com/#feat=font-size-adjust http://caniuse.com/#feat=font-size-adjust ⁶ http://www.textus.io http://www.textus.io
- Illniyar 9y agoThese kind of complexities is why, as much as I would love it to, the web as it is cannot compete with the performance of native apps. What is in native a simple number (maybe two) is dozens of pages of specifications - which result in insane amount of work to deduce the value of a simple variable. Layout and rendering is probably even worse in this regard.
- Asooka 9y agoIf you are writing a Web application, you can always just use the one number and skip the complexity. A lot of work is poured into browser engines to make them render the simple cases as quickly as possible, while allowing for rich styling grammar for use by people writing documents.
- Illniyar 9y agoFont size is the least of the issues really. Layout and animation are the big issues.
- augustus0 9y agoThat was my reaction to the article as well. The amount of work being done here for Servo (and which has been done in other browsers) is hugely impressive, but this problem feels a little artificial — we could have build the same web that we have today with half these options and not even have lost much in terms of ergonomics. I wonder if some kind of "Expedient CSS" might be possible as a way forward. Site developers could opt themselves in with a comment tag and promise to use only a subset of CSS features and only simple cascades (maybe none) in return for better rendering performance. A little like what's been done for JavaScript with asm.js.
- pcwalton 9y agoOn the contrary, when native apps support styling (e.g. through React Native), it's invariably very slow compared to all the optimizations that have gone into browsers. They generally can't compete with the decades of optimization that have gone into the Web platform. Sure, cascading of certain properties can be complex. But the compressed in-memory style representation, style sharing optimizations, parallel restyling, etc. more than make up for this. Each one of these is at least a 2x speedup on its own. Every single implementation of native app styling that I've seen hasn't even begun to implement any of these optimizations.
- LoSboccacc 9y agohe's missing out sizing in viewport dimensions, even if is probably reducible to the pixel dimensioning adjusted for zoom.
- Manishearth 9y agoYeah, since that's just another length unit and works for all length values I didn't go into it.
- deleted 9y ago[deleted]
- jancsika 9y ago> Text zoom is also disabled within <svg:text> elements, which leads to some trickiness here. Not sure I understand this. Is the author saying that if the user tries to zoom in, svg text won't change size?
- Manishearth 9y agoIf the user tries to text zoom. Yes, SVG text will not zoom if you text zoom. SVG is fundamentally a absolutely-position image, not a laid out document. The concept of text-only zoom makes sense for documents, but not for images, where the position of the text is far more important. (This won't affect `<img>` embedded SVG anyway, however it is possible to embed SVG directly within HTML via `<svg>`, which is where this matters.)
- jancsika 9y ago> If the user tries to text zoom. Ah, thanks for the clarification. I missed that part. That makes absolute sense. :) > The concept of text-only zoom makes sense for documents, but not for images, where the position of the text is far more important. > SVG is fundamentally a absolutely-position image, not a laid out document. svt text is a fundament of svg that is incompatible with absolute-positioning. The OS's font stack might render px-sized mono-spaced font text at width x, or it might render it at x+7. Edit: typo
- jdmichal 9y ago> What this effectively means is that you need to keep track of two separate computed font size values. There’s one value that is used to actually determine the font size used for the text, and one value that is used whenever the style system needs to know the font-size (e.g. to compute an em unit.) As someone who actually sets minimum font size, and to a healthy 14px at that... Does anyone know the actual reason behind this? It would probably go a long way to helping layouts not break all the time with my minimum font.
- Manishearth 9y agoI don't, and I'm pretty baffled that it does affect em units for text-zoom (despite the two having very similar purposes). Perhaps the difference is that with min font sizes everything gets clamped, which can make two objects of different sizes get the same size if they used em units (if em units affect things). Which is both good and bad, depending on the intent. (Whereas with text-zoom everything is nicely scaled so it's less of a problem).
- jdmichal 9y agoThanks for the response. Is this behavior something defined in the spec? Or is it just what Gecko does, and is being carried over to Servo?
- Manishearth 9y agotext-zoom isn't a web feature, it's only a feature Firefox/Gecko has. Servo doesn't implement it either, only Stylo (the confluence of Servo's style system and Gecko). In Servo mode all the code for text-zoom becomes a no-op.
- jdmichal 9y agoSorry, I meant the em sizing ignoring clamping, not the text zoom!
- 9y ago
- nnwright 9y agoFascinating read, thanks for sharing!