[racket-dev] Caching rendered icons

From: Neil Toronto (neil.toronto at gmail.com)
Date: Thu Jan 12 16:01:23 EST 2012

On 01/12/2012 11:52 AM, Matthew Flatt wrote:
> At Thu, 12 Jan 2012 11:19:55 -0700, Neil Toronto wrote:
>> 2. Compile time: Provide macros that render icons during expansion and
>> try to store them in the "compiled" directory.
>
> Do you need icons to be in separate files? A macro could expand to a
> byte-string literal, since it's just as easy to generate a bitmap from
> a PNG byte string as from a PNG file.

This is friggin' awesome. The expression

     (compiled-left-arrow-icon '(255 95 78) 24)

expands to

     (make-object bitmap% (open-input-bytes bs) 'png)

where `bs' is a literal byte string produced at compile time by

     (define bm (left-arrow-icon color-val height-val))
     (define p (open-output-bytes))
     (send bm save-file p 'png)
     (define bs-stx (datum->syntax stx (get-output-bytes p)))

The hardest part is parsing the color and height syntax, but 
`syntax/parse' makes it easy enough. There is no distribution problem, 
no versioning, hashing, caching, or unused bitmap cleanup. I can't think 
of a better solution, and I couldn't have done it in any other language.*

Racket so rocks.

Neil T

* Okay, I could have done it in some Lisp, but not safely or easily.


Posted on the dev mailing list.