fasttext

fasttext is a FastCGI server, written in C, that takes parameters on the HTTP query string and renders text into a transparent png that it serves back through the webserver. It uses Cairo2 with Freetype, supports TrueType font files, and is about as fast as reasonably possible, the bottleneck in rendering being the png compression. It is intended to be used behind a caching web server or proxy, but can be used directly for low-traffic sites. Here's an example:


This little project was built as a learning exercise and also to explore the feasibility of building back-end web services in a lower level language such as C or C++. It turned out to be relatively straightforward, with only the usual contortions that one might expect working in C. Of course, with proper caching at the webserver level this is much simpler to achieve in a language such as PHP or Python and the performance difference would probably be minimal. But it was a nice project for expanding my horizons, and now I have a server-side text renderer!

fasttext was somewhat inspired by the Google Charts API, which has a very clean and straightforward approach to embedding dynamic chart or graph data in a webpage. There's an architectural elegance in breaking out simple, single purpose web services such as these that have no need of state management or database access. It makes it easier to scale an app if you manage these kinds of modules independently of your main app.

Other advantages include:
  • vs. Google webfonts: no reliance on javascript, greater control over the fonts that are available
  • vs. sIFR/FLIR/Cuf√≥n rendering: no reliance on flash/javascript/canvas
  • vs. CSS3 @font-face (Web Open Font Format): no exposure to copyright issues for making proprietary fonts publicly downloadable, works with any browser that can render PNGs
  • vs. everything else: In most cases just plain simpler for your web developers/designers and more forgiving for browsers.


To use it a developer inserts an img tag with a src URL that includes parameters on the query string for rendering the text. The README has more comprehensive documentation, but here are some useful parameters:

  • text or t : The text that you want to render
  • font or f : The font face, configured to refer to a TrueType font on the server
  • size or s : The size in pixels
  • fg or r/g/b/a : The color to render, hex-encoded 3/4 or 6/8 RGB(A) (css-style), or individual decimal (0.0-1.0) values
  • bg or bgr/bgg/bgb/bga : Same as fg, but for the background field

Try it:


rendered with at pixels, in

URL: http://fasttext.bartgrantham.com/?t=Type%20Something&f=vera&s=16&fg=4169E1


(The server is currently a 500Mhz VIA Eden CPU and I've throttled the subdomain serving these images down to 1MB/sec. It would likely be significantly faster on a dedicated production machine)


To build and install fasttext you'll need Cairo2 (with Freetype2) and FastCGI libraries and headers. You'll also need a FastCGI-capable webserver, which should be just about any webserver today. The README has more details.

You can browse the code here, or download a tarball here. Or if you're using git and would like to clone it, you'll also need to make sure to init and update the submodule projects (qs_parse and simplehash) before building:


# git clone http://www.bartgrantham.com/_projects/fasttext/.git
# cd fasttext
# git submodule init
# git submodule update
# make


The license for fasttext is MIT, so feel free to use it however you'd like. Enjoy!