luisgabrielroldan / chisel

A library to sculpt text on any device that you can handle pixels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chisel

GitHub Workflow Status github.com Hex version Hex docs

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

  1. Take a function to draw pixels...
  put_pixel = fn x, y ->
    YourDisplay.draw_pixel(x, y, ...)
  end
  1. 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")
  1. Use Chisel to sculpt the text using the provided function and font
  Chisel.Renderer.draw_text("Hello World!", x, y, font, put_pixel)
  1. Enjoy!

Demo

(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, ...).

Render on an image with :egd

  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, pixels ->
    [{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:

                                                                                                     
                                                                                                     
 %%  %%                                          %%   %%                                   %%        
 %%  %%           %%%     %%%                    %%   %%                  %%%        %%    %%        
 %%  %%   %%%%     %%      %%     %%%%           %%   %%  %%%%   %%%%%     %%        %%    %%        
 %%%%%%  %%  %%    %%      %%    %%  %%          %% % %% %%  %%  %%  %%    %%     %%%%%    %%        
 %%  %%  %%%%%%    %%      %%    %%  %%          %%%%%%% %%  %%  %%        %%    %%  %%              
 %%  %%  %%        %%      %%    %%  %%          %%% %%% %%  %%  %%        %%    %%  %%              
 %%  %%   %%%%    %%%%    %%%%    %%%%           %%   %%  %%%%   %%       %%%%    %%%%%    %%        
                                                                                                     
                                                                                                     

Samples using OLED

OLED Demo

Using 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.

About

A library to sculpt text on any device that you can handle pixels

License:Apache License 2.0


Languages

Language:Elixir 100.0%