Scenic v0.10.2 Scenic.Primitive.Style.Font View Source

Set the font used to draw text.

Example:

graph
|> text("Hello World", font: :roboto)

Data

You can choose one of the three named system fonts, or one you've loaded into the cache.

  • :roboto - The standard Roboto sans-serif font.
  • :roboto_mono - The standard Roboto Mono mono-spaced font.

To use a font from the cache, set it's hash into the font style.

Example:

@my_font_path :code.priv_dir(:my_app)
           |> Path.join("/static/fonts/my_font.ttf")
@my_font_hash Scenic.Cache.Hash.file!( @my_font_path, :sha )

@graph Graph.build()
|> text("Hello World", font: @my_font_hash)

def init(_, _opts) do
  # load the font into the cache
  Scenic.Cache.File.load(@my_font_path, @my_font_hash)

  {:ok, :some_state, push: @graph}
end

Note 1: The font renderer used by Scenic is the fantastic stb_truetype library. This renderer was developed for games, which is another way of saying it is not trying to be the all-renderer for all fonts. Don't be surprised if you find a TrueType font that doesn't work as expected.

Note 2: Other font renderers might become available in the future. The named system fonts should keep working, but custom TrueType fonts might need to be ported.

It is up to you to test your custom font choices to see if they work for your application.