Chisel
Chisel is a library that uses bitmap fonts to sculpt text on any device that can handle pixels.
Setup
Add Chisel to your mix.exs deps:
{:chisel, "~> 0.2.0"},
Run mix deps.get
to download the new dependency.
Usage
- Take a function to draw pixels...
put_pixel = fn x, y ->
YourDisplay.draw_pixel(x, y, ...)
end
- Pick a BDF font (Look for one on the Internet or take one from the fixtures folder on this project)
{:ok, font} = Chisel.Font.load("foo/bar/font.bdf")
- Use Chisel to sculpt the text using the provided function and font
Chisel.Renderer.draw_text("Hello World!", x, y, font, put_pixel)
- Enjoy!
(Thanks to lawik for the picture)
General purpose
Chisel is a general purpose library that can be used to render text on any target based on pixels (LCD, Led matrixs, image files, ...).
:egd
Render on an image with img = :egd.create(200, 50)
color = :egd.color({0, 0, 0})
put_pixel = fn x, y ->
:egd.line(img, {x, y}, {x, y}, color)
end
{:ok, font} = Chisel.Font.load("font.bdf")
Chisel.Renderer.draw_text("Hello World!", 0, 0, font, put_pixel)
:egd.save(:egd.render(img, :png), "test.png")
Render ASCII art
put_pixel = fn x, y ->
[{x, y} | pixels]
end
{:ok, font} = Chisel.Font.load("c64.bdf")
{pixels, _, _} = Chisel.Renderer.reduce_draw_text("Hello World!", 0, 0, font, [], put_pixel)
for y <- 0..10 do
for x <- 0..100 do
if Enum.member?(pixels, {x, y}) do
"%"
else
" "
end
end
|> IO.puts()
end
Result:
%% %% %% %% %%
%% %% %%% %%% %% %% %%% %% %%
%% %% %%%% %% %% %%%% %% %% %%%% %%%%% %% %% %%
%%%%%% %% %% %% %% %% %% %% % %% %% %% %% %% %% %%%%% %%
%% %% %%%%%% %% %% %% %% %%%%%%% %% %% %% %% %% %%
%% %% %% %% %% %% %% %%% %%% %% %% %% %% %% %%
%% %% %%%% %%%% %%%% %%%% %% %% %%%% %% %%%% %%%%% %%
OLED
Samples usingUsing the right font it is even possible to render unicode strings.
Many good BDF fonts are available here on the U8g2 library repo.
Check fonts licenses here.