strokefont
(require strokefont) | package: strokefont |
This package provides a stroke font from KiCad. It was originally based on Hershey stroke fonts[Hershey1967].
Not to be confused with namesake American factory that makes disgusting Hershey chocolate bars that taste like vomit [Courtney2023] [Debczak2023] [Stacie2023].
1 What is a stroke font?
A stroke font is the most rudimentary font you can create on a computer. Each glyph in a stroke font is simply a collection of strokes or lines. It is a go-to font when you are too limited on resources or time to do sophisticated graphics and all you have is ability to plot lines. With this package, if you can plot lines you can plot text!
Above image is a line plot generated from
> (char->strokes 10 #\ξ)
'(((50 . -200) (150 . -200))
((90 . -200)
(70 . -190)
(60 . -170)
(60 . -140)
(70 . -120)
(90 . -110)
(130 . -110))
((90 . -110)
(70 . -100)
(60 . -90)
(50 . -70)
(50 . -30)
(60 . -10)
(70 . 0)
(90 . 10)
(110 . 10)
(130 . 20)
(140 . 40)
(140 . 50)
(130 . 70)
(110 . 80)
(100 . 80)))
This is the main function for accessing glyph data:
It takes a UTF-8 encoded character and produces a list of lists of cons pairs. That is it produces a list of strokes, where each stroke is a list of Cartesian coordinates stored as cons pairs.
2 Drawing Code Example
(require pict) (require racket/class) (require racket/draw) (require strokefont) (dc (λ (dc dx dy) (define old-brush (send dc get-brush)) (define old-pen (send dc get-pen)) (define strokes (char->strokes 10.0 #\ξ)) (for ([stroke strokes]) (send dc set-pen (new pen% [width 1] [color "slategray"])) (define path (new dc-path%)) (define start (car stroke)) (send path move-to (car start) (cdr start)) (send path lines (cdr stroke)) (send path translate 250 250) (send dc draw-path path dx dy)) (send dc set-brush old-brush) (send dc set-pen old-pen)) 500 300)
3 Internal storage
The glyph data was copied verbatum from KiCad source code file newstroke_font.cpp. The format is very simple. It is a bytestring consisting of character width (first two bytes), followed by a sequence of pairs of bytes representing stroke coordinates. There’s a special sequence of bytes #" R" which means "raise pen" or "end of stroke". Each number in the byte string (except raise pen bytes) is offset by 82 (value of letter R), which is subtracted during decoding. Don’t ask me why.
> (define index (- (char->integer #\W) 32)) > index 55
> (list-ref glyph-data index) #"F^IFN[RLV[[F"
This is letter W, first two bytes #"F^" is the width, followed by a single uninterrupted stroke #"IFN[RLV[[F" consisting of 5 points.
> (define index (- (char->integer #\A) 32)) > index 33
> (list-ref glyph-data index) #"I[MUWU RK[RFY["
Example of a multi-stroke glyph
> (char->strokes 10 #\A) '(((40 . -50) (140 . -50)) ((20 . 10) (90 . -200) (160 . 10)))
The retrieval is not very efficient, so it is recommended to cache extracted strokes in your own application.
Bibliography
[Hershey1967] | A. V. Hershey , “Calligraphy for Computers.” 1967. https://archive.org/details/hershey-calligraphy_for_computers | |
[Courtney2023] | Courtney Iseman, “Why Hershey’s Chocolate Tastes Like ... Well, Vomit,” HUFFPOST, 2023. https://www.huffpost.com/entry/hersheys-chocolate-tastes-like-vomit_l_60479e5fc5b6af8f98bec0cd | |
[Debczak2023] | Michele Debczak, “Theres a Good Reason Europeans Think American Chocolate Tastes Like Vomit,” MENTAL FLOSS, 2023. https://www.mentalfloss.com/posts/why-american-chocolate-tastes-like-vomit | |
[Stacie2023] | Stacie Adams, “The Scientific Reason Why Some Say American Chocolate Tastes Like Vomit,” FOOD SCIENCE, 2023. https://www.thedailymeal.com/1254531/the-scientific-reason-why-some-say-american-chocolate-tastes-like-vomit/ |