/
font.ex
40 lines (33 loc) · 1.23 KB
/
font.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule Infer.Font do
@moduledoc """
Font type matchers based on the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming))
"""
@doc """
Takes the binary file contents as arguments. Returns `true` if it's a woff font.
"""
@spec woff?(binary()) :: boolean()
def woff?(<<0x77, 0x4F, 0x46, 0x46, 0x00, 0x01, 0x00, 0x00, _rest::binary>>), do: true
def woff?(_binary), do: false
@doc """
Takes the binary file contents as arguments. Returns `true` if it's a woff2 font.
"""
@spec woff2?(binary()) :: boolean()
def woff2?(<<0x77, 0x4F, 0x46, 0x32, 0x00, 0x01, 0x00, 0x00, _rest::binary>>), do: true
def woff2?(_binary), do: false
@doc """
Takes the binary file contents as arguments. Returns `true` if it's a ttf font.
## Examples
iex> binary = File.read!("test/fonts/sample.ttf")
iex> Infer.Font.ttf?(binary)
true
"""
@spec ttf?(binary()) :: boolean()
def ttf?(<<0x00, 0x01, 0x00, 0x00, 0x00, _rest::binary>>), do: true
def ttf?(_binary), do: false
@doc """
Takes the binary file contents as arguments. Returns `true` if it's a otf font.
"""
@spec otf?(binary()) :: boolean()
def otf?(<<0x4F, 0x54, 0x54, 0x4F, 0x00>>), do: true
def otf?(_binary), do: false
end