ScenicFramework / scenic

Core Scenic library
Apache License 2.0
1.97k stars 137 forks source link

How to use custom fonts #261

Closed bcardarella closed 2 years ago

bcardarella commented 2 years ago

I've downloaded FiraCode: https://github.com/tonsky/FiraCode and have copied FiraCode-Regular.ttf to assets/fonts

In my Scenic app I have an assets module:

defmodule WeatherStation.Assets do
  use Scenic.Assets.Static,
    otp_app: :weather_station
end

However when I try to make use of the font:

@graph Graph.build(font: "fonts/FiraCoda-Regular.ttf")

I get the following error:

== Compilation error in file lib/assets.ex ==
** (FunctionClauseError) no function clause matching in TruetypeMetrics.do_parse_unicode_cmap_glyphs/1    

    The following arguments were given to TruetypeMetrics.do_parse_unicode_cmap_glyphs/1:

        # 1
        <<0, 12, 0, 0, 0, 0, 52, 60, 0, 0, 0, 0, 0, 0, 4, 89, 0, 0, 0, 13, 0, 0, 0, 13, 0, 0, 4, 75, 0, 0, 0, 32, 0, 0, 0, 32, 0, 0, 4, 79, 0, 0, 0, 33, 0, 0, 0, 33, 0, 0, ...>>

    Attempted function clauses (showing 1 out of 1):

        defp do_parse_unicode_cmap_glyphs(<<4::integer()-big()-unsigned()-size(16), _size::integer()-big()-unsigned()-size(16),
      _language::integer()-big()-unsigned()-size(16),
      seg_count_x2::integer()-big()-unsigned()-size(16),
      _search_range::integer()-big()-unsigned()-size(16),
      _entry_selector::integer()-big()-unsigned()-size(16),
      _range_shift::integer()-big()-unsigned()-size(16), end_codes::binary()-size(seg_count_x2),
      0::integer()-big()-unsigned()-size(16), start_codes::binary()-size(seg_count_x2),
      id_deltas::binary()-size(seg_count_x2), id_range_offsets::binary()-size(seg_count_x2),
      sub_table::binary()>>)

    (truetype_metrics 0.5.2) lib/truetype_metrics.ex:359: TruetypeMetrics.do_parse_unicode_cmap_glyphs/1
    (truetype_metrics 0.5.2) lib/truetype_metrics.ex:77: TruetypeMetrics.parse/2
    (scenic 0.11.0-beta.0) lib/scenic/assets/static/font.ex:10: Scenic.Assets.Static.Font.parse_meta/1
    (scenic 0.11.0-beta.0) lib/scenic/assets/static.ex:564: anonymous fn/2 in Scenic.Assets.Static.parse_bin/1
    (elixir 1.13.1) lib/enum.ex:4076: Enum.find_value_list/3
    (scenic 0.11.0-beta.0) lib/scenic/assets/static.ex:563: Scenic.Assets.Static.parse_bin/1
    (scenic 0.11.0-beta.0) lib/scenic/assets/static.ex:529: Scenic.Assets.Static.ingest_file/5
    (elixir 1.13.1) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3

Am I doing something wrong or is there an issue with how this font file is trying to be read?

boydm commented 2 years ago

This was actually an issue in the truetype_metrics font parser.

The FiraCode font uses type 12 cmap tables, which the parser didn't have support for. This has been added and is now released to hex as truetype_metrics version 0.6.0

https://github.com/boydm/truetype_metrics/pull/8