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:

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
                                   "борщ"))
43.70909
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
                                    #щ)))
43.70909

Or width of a single character:

function
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))))
(#CYRILLIC_SMALL_LETTER_SHCHA . 14.196364)
(#i . 4.3927274)