Anafanafo, Common Lisp implementation!

This library is implementation of JavaScript version of anafanafo.

It is useful, when you want to know how long in pixels will be the string when the browser will render it in a given font.

Library uses prebuilt data about character widths from the original repository. It supports these fonts:

This implementation differ from the JavaScript version. It returns text width for any font size, automatically applying needed math transformation. All you need is to specify font family and weight.

Default font is "Helvetica Bold 16px".

To use it, you need to load data using the load-data function:

&key (family \*default-font-family\*) (weight \*default-font-weight\*) (size \*default-font-size\*)

Loads data for specified font name.

Returns an object which can be used to retrieve text width:

CL-USER> (anafanafo:load-data :family "Verdana"
                              :weight "bold")
#<ANAFANAFO::DATA "Verdana" "bold" 16px :file "data/verdana-10px-bold.json">

Then you can calculate the width of the string:

Returns width of the text in pixels.

Result just a sum of all text characters:

CL-USER> (let ((data (anafanafo:load-data :family "Verdana"
                                          :weight "normal")))
           (anafanafo:string-width data
CL-USER> (let ((data (anafanafo:load-data :family "Verdana"
                                          :weight "normal")))
           (+ (anafanafo:char-width data
              (anafanafo:char-width data
              (anafanafo:char-width data
              (anafanafo:char-width data

Or width of a single character:

data char &key (guess t)

Returns a float width of given char. Width is measured in pixels.

CL-USER> (let ((data (anafanafo:load-data :family "Verdana"
                                          :weight "normal")))
           (values (cons #щ
                         (anafanafo:char-width data #щ))
                   (cons #i
                         (anafanafo:char-width data #i))))
(#i . 4.3927274)