LWN.net Logo

LGM: Unusual typography

By Nathan Willis
May 16, 2012

Libre Graphics Meeting (LGM) always has a healthy serving of typography and font talks on the menu. This year in Vienna, there were several sessions on the program that showcased new and unusual approaches to getting text on screen. One was an OpenGL-based renderer that offers noticeably smoother text in video and animation, while another was a toolkit for web developers to seamlessly integrate advanced type design features into modern web pages — including multi-color fonts.

Fonts and the GPU

Google's Behdad Esfahbod is the author and maintainer of HarfBuzz, the OpenType layout engine in use by Gecko and many other free software projects. HarfBuzz takes a sequence of Unicode characters and returns a set of properly-arranged glyphs that corresponds to them in the current language and script. For simple alphabets, the process of selecting the right output is rarely more complex than looking for pre-composed versions of any accented characters, then deciding how to compose any missing ones out of base glyphs and accents. But for complex scripts like Arabic, the shaping, positioning, and cursive joining process is tricky enough to require a complete shaping engine.

[Behdad Esfahbod]

Esfahbod gave a talk about HarfBuzz, which is a well-established project, but he also presented his personal side-project GLyphy, which uses OpenGL to render smooth text in video and animation. He wrote GLyphy in an attempt to circumvent a common rendering problem visible with text rendered in animations: the stuttering of outlines whenever a piece of text moves on screen. The root of the problem, he explained, was that most such text was re-rendered for each frame using the traditional rasterization process designed for still images (on screen or in print). When the text moves in every frame, the anti-aliasing algorithm picks a different set of pixels to shade, and the sharp outlines of the glyphs make visible jumps.

Esfahbod's approach was to ditch the algorithm most frequently used to rasterize the glyphs: coverage-based anti-aliasing. In this approach, the program calculates the portion of each pixel covered by the outline, and uses that percentage to shade each pixel. The fact that the algorithm is pixel-oriented is what causes the jitter when translating or rotating text in an animation. GLyphy instead uses a signed-distance-field (SDF) algorithm to shade the glyphs, which is independent of scaling and other transformations. SDF calculates the distance from each pixel to the nearest point on the outline; together those values form an anti-aliased outline of the glyph — but one that looks good at high resolutions.

SDF is math-intensive, so GLyphy leverages OpenGL to speed up the process. It also decomposes the Bezier curves used in most outline fonts into arc segments, because arcs are easier to compute distances against. Unlike other OpenGL renderers, GLyphy sends the glyph outlines to the GPU in vector format rather than copying pre-rendered samples into textures. The result, as Esfahbod demonstrated, is very smooth text animations. Getting decent performance is not quite as simple, he said — GLyphy hits lots of driver bugs, and the specific font and text directly affect how fast and how much memory is required for rendering. Consequently, making good use of GLyphy requires some knowledge of the content and the graphics hardware, so it will probably never be a general-purpose text renderer. For now, it remains experimental — but other projects certainly may find things to learn from it.

Web typography gone wild

Ana Carvalho and Ricardo Lafuente of Manufactura Independente presented a session on web typography with open fonts and open source software. Manufactura Independente handled the recent redesign of the Open Font Library web site, which they demonstrated during their discussion of other online resources. But in addition to fonts themselves, their talk focused on a set of jQuery-based JavaScript libraries that implement new and (for now) unusual text rendering options for the web.

[Carvalho and Lafuente]

Covered in the talk were FitText.js, which auto-scales text to fit a given box element (regardless of the screen orientation or size) and Lettering.js, a library that automatically splits typographic elements into individual CSS classes so that they can be manipulated precisely. Both are open source products from design firm Paravel. Also discussed were Kerning.js, which offers CSS rules for altering the size, kerning, weight, slant, and color of text elements — by the word, individual character, or by matching letter patterns — and Ligature.js which allows you to replace specific letter sequences with ligatures, beyond the basic fl, fi, ae, and AE ligatures that the developers say can be reliably rendered in vanilla HTML.

It is certainly possible already to use CSS to manually tweak individual page elements, right down to wrapping individual characters in <span> tags, but web designers will appreciate the simplicity of using a library instead.

[Colorfont example]

The team also presented its own library, Colorfont.js, which implements fonts sporting multiple colors in each glyph, using CSS. The process is specific to the typeface used — you start with a base font and create an "overlay" version separately. The overlay font should match up to the glyphs of the original exactly, but only include the portions of each letter to be shown in the second color. Colorfont.js then allows you to mark up text with the colorfont style class, which it will render with the two versions of the font superimposed.

Here again, a designer could use other means to accomplish the same end result (such as generating a PNG image), but using Colorfont allows the two-tone text to remain actual text — visible to search engines, mouse-selectable, and everything else. Depending on the size of the image it replaces, it is likely to save load time as well, plus fall back gracefully on image-less browsers or those without JavaScript.

Carvalho and Lafuente also led a design workshop in which participants created an overlay font to match an existing base font. I was unable to attend, as it was scheduled at the same time as a workshop I was proctoring. However, I was able to talk to several of the attendees after the fact and see their results. The hands-on experience certainly makes the library more useful to the attendees.

Final word

Having worked with font design and development for the past two years, I am keenly aware how easy it is to take it for granted. Users and developers both count on fonts appearing on screen as desired with little intervention, so it can be quite a reality check to dig into the glyph shaping and rendering process and see how many steps are involved. Esfahbod's work with GLyphy may remain an experimental exercise for the foreseeable future, but as more and more processing gets offloaded from the CPU onto graphics hardware, it is also possible that someday OpenGL rendering will be commonplace — in which case we will be glad that open source software is ready.

Ironically, the jQuery font libraries showcased by Carvalho and Lafuente at the same event are really backward-looking, in the sense that they restore a level of control and precision to typography that was commonplace centuries ago, but lost in the transition to digital fonts and web publishing. After all, illuminated manuscripts are some of the oldest multi-color typography projects in history; it is quite entertaining to see JavaScript and CSS used to reproduce them today.

[Thanks to the Libre Graphics Meeting for assistance with travel to Vienna.]


(Log in to post comments)

LGM: Unusual typography

Posted May 17, 2012 0:57 UTC (Thu) by Sho (subscriber, #8956) [Link]

You might be interested to learn that Qt 5 uses the distance field rendering technique in its OpenGL canvas as well, for much the same reason motivating GLyphy: http://labs.qt.nokia.com/2011/07/15/text-rendering-in-the...

LGM: Unusual typography

Posted May 17, 2012 1:06 UTC (Thu) by Sho (subscriber, #8956) [Link]

(Though to expand on that, GLyphy is not a redundant implementation by any means, because Qt much like Valve's implementation samples into a texture, which means sacrificing some accuracy in the glyph outline. GLyphy doesn't suffer that as it retains the vector data.)

LGM: Unusual typography

Posted May 17, 2012 17:01 UTC (Thu) by alankila (subscriber, #47141) [Link]

The technique is ad-hoc though, as it is using smoothstep to generate the antialiased values, and the sample code has no gamma correction either. I think clamped line + gamma correction could result nearly exact results though!

This looks like something I'll have to play with later.

LGM: Unusual typography

Posted May 21, 2012 7:40 UTC (Mon) by kRogue (guest, #84728) [Link]

Qt uses a sampled distance field computed on CPU. There are a lot of issues with sampled distance fields:
1) corners rounded
2) "boundary acne" where on stems of letters sometimes there are bumps. You also see these bumps if two features are very close

Atleast Qt made their distance field creation better, essentially rasterziing the distance values on CPU..

Buy Glyphy and Qt's distance field are in completely different categories. Roughly speaking, sampled distance field is from a visual point of view, is severely inferior to Glyphy.

On the other hand, Glyph's fragment shader is very, very heavy. Lots of branching, potentially lots of texture look ups. It will murder current embedded devices. Also a touch worried about it's memory usage.. I have not figured out if the arc-entries are reused, or if each texel has it's own list embedded within the texture. If the latter, Glyphy has very heavy texture usage, but this can be avoided. For each contour of the font, just record the arc entries together with a location of the "next" contour, or one can also just make the list repeated twice, i.e. if the contour list is A0, A1, .., An then record it as A0, A1,..., An, A0, A1, ..., An thus is one starts at say An, it will just work as a blind texture walk.

Also consider, if GL3 is on the menu, using texture buffer objects to hold the arcs.

As a side note, with (some) ATI Catalyst drivers, Glyphs renders ickily.. changing the texture look up from texture2D to texture2DLod fixes the issue. This is likely an ATI bug (I have experienced it in my own funky font rendering jazz).

LGM: Unusual typography

Posted May 24, 2012 22:10 UTC (Thu) by behdad (subscriber, #18708) [Link]

Thanks kRogue for the detailed feedback!

Yes, GLyphy as it is is very heavy on the GPU. I'm sure embedded devices would struggle, but seems like on desktop it's not far from usable. I mean, it gives me a decent framerate on my 2-year old Thinkpad with intel graphics and Linux mesa driver...

That said, after discussions at LGM, I have at least three pages full of ideas for optimizing it. It's just a matter of time.

Also, we already do try to share arcs among cells. Current memory usage is about 2k per glyph for a sans serif font. I think we can get that down to about 1k.

At any rate, feel free to join the Google Group and continue discussion there.

LGM: Unusual typography

Posted May 17, 2012 19:55 UTC (Thu) by knobunc (subscriber, #4678) [Link]

I'd really like to be able to use some of the Arabic shaping stuff to get a good handwriting font made. Or at least something that had good support for alternates in a OpenType font.

LGM: Unusual typography

Posted May 23, 2012 14:18 UTC (Wed) by micka (subscriber, #38720) [Link]

I find the ligature idea problematic. In french, there is a case where the decision to make a ligature is not based on the pair of letters but on the whole word.

Cœur will be written with a ligature (actually, I cheated, I used only one character) while moelleux will be written without a ligature.

I don't see how this could be correctly computed except with a dictionary.

LGM: Unusual typography

Posted May 23, 2012 15:43 UTC (Wed) by n8willis (editor, #43041) [Link]

As I understand it from Behdad's talk, HarfBuzz does -- intentionally -- leave this decision up to the application -- the app decides which bundle of codes to send to HarfBuzz as a unit, and that decision is both script- and language-dependent. So, yes, the word processor (or whatever) would need to know when an o-e sequence needed to be rendered as one ligature and when it didn't. HarfBuzz only needs to know how to form the ligature *when* it is asked to do so.

Whether or not any particular application does a good job of making that call is an issue for the application project's team; it's out of scope for HarfBuzz.

Nate

Ligature database

Posted May 23, 2012 16:31 UTC (Wed) by gioele (subscriber, #61675) [Link]

Whether or not any particular application does a good job of making that call is an issue for the application project's team; it's out of scope for HarfBuzz.

Yet I suppose that applications would love to have some kind of application-independent ligature database a la ICU instead of having to create and distribute a db of that kind for each application, something that you can query with ligatures_for("coeur", "fra-Latn") = {["œ", 1..2]}.

Ligature database

Posted May 24, 2012 0:12 UTC (Thu) by nix (subscriber, #2304) [Link]

It should be doable with suitable per-language general rules plus a list of exceptions. You know, like TeX has been doing for hyphenation for decades now.

LGM: Unusual typography

Posted May 24, 2012 22:14 UTC (Thu) by behdad (subscriber, #18708) [Link]

In these cases, from a Unicode and OpenType (and hence HarfBuzz) point of view, those are not really ligatures. Ie. fonts will not tell HarfBuzz to automatically convert 'oe' into 'œ'. To Unicode, 'œ' is a standalone character. Now, fonts *can* tell HarfBuzz to substitute such a ligaure, they can even say do it under French only. But if it's not mechanically decidable, then it shouldn't be in the font.

That said, the substitution mechanism in OpenType allows for contextual matches. So, if, say, a ligature should be formed only if followed by a certain string of characters, that can be expressed in the font.

LGM: Unusual typography

Posted May 25, 2012 7:18 UTC (Fri) by micka (subscriber, #38720) [Link]

Well, a bit curious about it, I read on the subject, I found that there seems to be a distinction between different kinds of ligatures : typographic ligatures and linguistic ligatures.

If I understand correctly, Harfbuzz does the first kind, and I was talking about the second kind (thus my confusion).

I even read a bit about how TeX does ligatures, and I wish I didn't :) But maybe that's to be expected from a system usable in so many languages and writing systems.

LGM: Unusual typography

Posted May 27, 2012 1:58 UTC (Sun) by behdad (subscriber, #18708) [Link]

Thanks for the note. Yes, you are spot on. I've worked on the typographic side so much that I have had forgotten that the term 'ligature' is used in other contexts too.

Thank you for Nathan Willis's graphics coverage

Posted Jul 10, 2012 1:37 UTC (Tue) by douglasbagnall (subscriber, #62736) [Link]

Obviously I am months behind in my reading and almost nobody will read this, but I want to say how much I like all these Nathan Willis articles about graphics software.

Thanks!

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds