Skip to content

Commit 77abc6d

Browse files
authored
Merge pull request #176 from stwf/custom-font-improvements
fix docs and add errors concerning custom fonts Thank you! This is much appreciated.
2 parents 2d02f11 + 4c47c94 commit 77abc6d

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

guides/custom_fonts.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ In your scene, you need to make load both the `\*.metrics` file into the `Scenic
6464

6565

6666
```elixir
67-
@font_folder :code.priv_dir(:my_app) |> Path.join("/static/fonts")
6867
@custom_font_hash "0IXAWqFTtjn6MKSgQOzxUgxNKGrmyhqz1e2d90PVHck"
6968
@custom_metrics_path :code.priv_dir(:scenic_example)
7069
|> Path.join("/static/fonts/Roboto_Slab/RobotoSlab-Regular.ttf.metrics")
7170
@custom_metrics_hash Scenic.Cache.Support.Hash.file!(@custom_metrics_path, :sha)
7271

7372
def init(_, _opts) do
7473
# load the custom font
75-
Cache.Static.Font.load(@font_folder, @custom_font_hash)
76-
Cache.Static.FontMetrics.load(@custom_metrics_path, @custom_metrics_hash)
74+
font_folder = :code.priv_dir(:my_app) |> Path.join("/static/fonts")
75+
custom_metrics_path = :code.priv_dir(:scenic_example)
76+
|> Path.join("/static/fonts/Roboto_Slab/RobotoSlab-Regular.ttf.metrics")
77+
78+
Cache.Static.Font.load(font_folder, @custom_font_hash)
79+
Cache.Static.FontMetrics.load(custom_metrics_path, @custom_metrics_hash)
7780

7881
# no need to put the graph into state as we won't be using it again
7982
{:ok, nil, push: @graph}

lib/scenic/cache/static/font.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
defmodule Scenic.Cache.Static.Font do
77
use Scenic.Cache.Base, name: "font", static: true
88
alias Scenic.Cache.Support
9+
require Logger
910

1011
# import IEx
1112

@@ -176,6 +177,7 @@ defmodule Scenic.Cache.Static.Font do
176177
{:ok, hash}
177178
else
178179
err ->
180+
Logger.error("Could not load font at #{font_folder}: #{inspect(err)}")
179181
err
180182
end
181183
end

lib/scenic/cache/static/font_metrics.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
defmodule Scenic.Cache.Static.FontMetrics do
77
use Scenic.Cache.Base, name: "font_metrics", static: true
88
alias Scenic.Cache.Support
9+
require Logger
910

1011
@moduledoc """
1112
In memory cache for static font_metrics assets.
@@ -225,7 +226,9 @@ defmodule Scenic.Cache.Static.FontMetrics do
225226
{:ok, metrics} <- FontMetrics.from_binary(data) do
226227
put_new(hash, metrics, opts[:scope])
227228
else
228-
err -> err
229+
err ->
230+
Logger.error("Could not load font metrics at #{path}: #{inspect(err)}")
231+
err
229232
end
230233
end
231234
end

test/scenic/cache/static/font_metrics_test.exs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Scenic.Cache.Static.FontMetricsTest do
99
alias Scenic.Cache.Base
1010
alias Scenic.Cache.Static
1111
alias Scenic.Cache.Support
12+
import ExUnit.CaptureLog
1213

1314
@base_path :code.priv_dir(:scenic)
1415
|> Path.join("static/font_metrics")
@@ -112,11 +113,15 @@ defmodule Scenic.Cache.Static.FontMetricsTest do
112113
end
113114

114115
test "load passes through errors" do
115-
assert Static.FontMetrics.load("wrong/path", @roboto_hash) ==
116-
{:error, :enoent}
117-
118-
assert Static.FontMetrics.load(@roboto_path, "bad_hash") ==
119-
{:error, :hash_failure}
116+
assert capture_log(fn ->
117+
assert Static.FontMetrics.load("wrong/path", @roboto_hash) ==
118+
{:error, :enoent}
119+
end) =~ "Could not load font metrics at"
120+
121+
assert capture_log(fn ->
122+
assert Static.FontMetrics.load(@roboto_path, "bad_hash") ==
123+
{:error, :hash_failure}
124+
end) =~ "Could not load font metrics at"
120125
end
121126

122127
# ============================================================================

test/scenic/cache/static/font_test.exs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Scenic.Cache.Static.FontTest do
99
alias Scenic.Cache.Base
1010
alias Scenic.Cache.Static.Font
1111
alias Scenic.Cache.Support
12+
import ExUnit.CaptureLog
1213

1314
@folder File.cwd!()
1415
|> Path.join("test/artifacts")
@@ -115,11 +116,15 @@ defmodule Scenic.Cache.Static.FontTest do
115116
end
116117

117118
test "load passes through errors" do
118-
assert Font.load("wrong/path", @hash) ==
119-
{:error, :not_found}
120-
121-
assert Font.load(@folder, "bad_hash") ==
122-
{:error, :hash_failure}
119+
assert capture_log(fn ->
120+
assert Font.load("wrong/path", @hash) ==
121+
{:error, :not_found}
122+
end) =~ "Could not load font at"
123+
124+
assert capture_log(fn ->
125+
assert Font.load(@folder, "bad_hash") ==
126+
{:error, :hash_failure}
127+
end) =~ "Could not load font at"
123128
end
124129

125130
# ============================================================================

0 commit comments

Comments
 (0)