Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created November 30, 2009 17:12
Show Gist options
  • Select an option

  • Save practicingruby/245562 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/245562 to your computer and use it in GitHub Desktop.
-module(ttfunk).
-define(uint8, 8/unsigned-big-integer).
-define(int8, 8/signed-big-integer).
-define(uint16, 16/unsigned-big-integer).
-define(int16, 16/signed-big-integer).
-define(uint32, 32/unsigned-big-integer).
-define(int32, 32/signed-big-integer).
-export([parse/1]).
% this is a bit stubbed out, will be more complete later and return all relevant metrics as a tuple of 2-tuples.
parse(File) ->
Dir = directory(File),
{{ dir, { hmtx, directory_info("hmtx", Dir) }}}.
directory_info(TagName, Dir) ->
{ _, { tables, Tables }, _ } = Dir,
Filter = fun(E) ->
{tag, TableTag} = lists:keyfind(tag, 1, E),
TableTag == TagName end,
hd(lists:filter(Filter, Tables)).
directory(File) ->
{ok, Bin} = file:read_file(File),
<< ScalarType : ?uint32,
NumTables : ?uint16,
SearchRange : ?uint16,
EntrySelector : ?uint16,
RangeShift : ?uint16,
Rest/binary >> = Bin,
Tables = parse_tables(Rest, [], NumTables),
{ { scalar_type, ScalarType }, { tables, Tables }, { raw, Bin } }.
parse_tables(Bin, List, 0) ->
List;
parse_tables(Bin, List, N) ->
<< Tag : 32/bitstring,
CheckSum : ?uint32,
Offset : ?uint32,
Length : ?uint32,
Rest/binary >> = Bin,
parse_tables(Rest, [[ { tag, binary_to_list(Tag) },
{ checksum, CheckSum },
{ offset, Offset },
{ len, Length }] | List ], N-1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment