Skip to content

Commit

Permalink
Merge pull request #54 from gfngfn/add-kern-test
Browse files Browse the repository at this point in the history
Add a unit test for `kern` tables
  • Loading branch information
gfngfn authored Sep 29, 2023
2 parents 4dcdf76 + 3ccec84 commit 183754a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ $ dune exec otfedcli input/Junicode.ttf subset 0,113,302 output/Junicode-subset.
<tr><td>SVG</td> <td colspan="2">SVG␣</td> <td>-</td><td>-</td><td>-</td><td>-</td></tr>

<tr><td rowspan="6">Optional</td> <td colspan="2">DSIG</td> <td>-</td><td>-</td><td>-</td><td>-</td></tr>
<tr> <td rowspan="2">kern</td><td>Format 0</td> <td>-</td><td>-</td><td>V</td><td>o</td></tr>
<tr> <td rowspan="2">kern</td><td>Format 0</td> <td>-</td><td>-</td><td>V</td><td>V</td></tr>
<tr> <td>other</td> <td>-</td><td>-</td><td>-</td><td>-</td></tr>
<tr> <td rowspan="2">vhea</td><td>ver. 1.0</td> <td>V</td><td>V</td><td>V</td><td>V</td></tr>
<tr> <td>ver. 1.1</td> <td>V</td><td>x</td><td>V</td><td>x</td></tr>
Expand Down Expand Up @@ -228,3 +228,5 @@ Some unit tests use data extracted from the following fonts:
- See the license [here](https://www.latex-project.org/lppl/)
* [Junicode](https://junicode.sourceforge.io/)
- See the license [here](https://github.com/psb1558/Junicode-font/blob/master/OFL.txt)
* [DejaVu Sans](https://dejavu-fonts.github.io/): `DejaVuSans-ExtraLight.ttf`
- See the license [here](https://dejavu-fonts.github.io/License.html)
29 changes: 15 additions & 14 deletions src/decodeKern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,19 @@ let rec d_kerning_tables i t p acc =
err @@ UnknownTableVersion(!% version)


let fold t p acc ikern =
let dec =
let open DecodeOperation in
(* Only the Windows version of `kern` Table is supported;
the Apple version, which has a 32-bit version number, is not. *)
d_uint16 >>= fun version ->
match version with
| 0 ->
d_uint16 >>= fun nTables ->
d_kerning_tables nTables t p acc
let d_kern t p acc =
let open DecodeOperation in
(* Only the Windows version of `kern` Table is supported;
the Apple version, which has a 32-bit version number, is not. *)
d_uint16 >>= fun version ->
match version with
| 0 ->
d_uint16 >>= fun nTables ->
d_kerning_tables nTables t p acc

| _ ->
err @@ UnknownTableVersion(!% version)
in
dec |> DecodeOperation.run ikern.core ikern.offset
| _ ->
err @@ UnknownTableVersion(!% version)


let fold t p acc ikern =
d_kern t p acc |> DecodeOperation.run ikern.core ikern.offset
28 changes: 28 additions & 0 deletions test/otfedTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module DecodeTtf = Otfed__DecodeTtf
module EncodeTtf = Otfed__EncodeTtf
module DecodeCff = Otfed__DecodeCff
module DecodeMath = Otfed__DecodeMath
module DecodeKern = Otfed__DecodeKern
module DecodeError = Otfed__DecodeError
module EncodeError = Otfed__EncodeError
module Value = Otfed__Value
Expand Down Expand Up @@ -464,6 +465,30 @@ let math_decoder_tests () =
end


(** Tests for `DecodeKern` *)
let d_kern_tests () =
let dec =
DecodeKern.d_kern
(fun subtable_acc kern_info -> (true, (kern_info, Alist.empty) :: subtable_acc))
(fun subtable_acc gid1 gid2 value ->
match subtable_acc with
| [] -> assert false
| (kern_info, acc) :: tail -> (kern_info, Alist.extend acc (gid1, gid2, value)) :: tail
)
[]
in
let got =
dec |> run_decoder TestCaseKern1.marshaled |> Result.map (fun subtable_acc ->
subtable_acc |> List.rev_map (fun (kern_info, acc) ->
(kern_info, Alist.to_list acc)
)
)
in
let expected = Ok(TestCaseKern1.unmarshaled) in
Alcotest.(check (decoding (list (pair (of_pp DecodeKern.pp_kern_info) (list (triple int int int))))))
"d_kern" expected got


let () =
let open Alcotest in
run "Otfed" [
Expand Down Expand Up @@ -551,4 +576,7 @@ let () =
("DecodeMath", [
test_case "math_decoder" `Quick math_decoder_tests;
]);
("DecodeKern", [
test_case "d_kern" `Quick d_kern_tests;
]);
]
84 changes: 84 additions & 0 deletions test/testCaseKern1.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

open! Otfed__Basic
module DecodeKern = Otfed__DecodeKern


let marshaled =
TestUtil.make_string_even [
(* `kern` header: *)
0x0000; 0x0003;
(* First subtable's header: *)
0x0000; 0x0086; 0x0001; 0x0014; 0x0040; 0x0004;
0x0018;
(* First subtable's contents;
`DejaVuSans-ExtraLight.ttf` (ottset: 93214, length: 120): *)
0x0010; 0x0024; 0xffd3; 0x0010; 0x0025; 0xffb7; 0x0010;
0x002a; 0x004b; 0x0010; 0x002d; 0x0072; 0x0010; 0x0032; 0x0039;
0x0010; 0x0034; 0x004b; 0x0010; 0x0037; 0xff44; 0x0010; 0x0039;
0xff88; 0x0010; 0x003a; 0xffad; 0x0010; 0x003b; 0xff9a; 0x0010;
0x003c; 0xff0d; 0x0010; 0x0052; 0x0026; 0x0010; 0x0059; 0xffc9;
0x0010; 0x005c; 0xffdc; 0x0010; 0x0082; 0xffd3; 0x0010; 0x0083;
0xffd3; 0x0010; 0x0084; 0xffd3; 0x0010; 0x0085; 0xffd3; 0x0010;
0x0086; 0xffd3; 0x0010; 0x0087; 0xffd3;
(* Second subtable's header: *)
0x0000; 0x0086; 0x0001;
0x0014; 0x0040; 0x0004; 0x0018;
(* Second subtable's contents;
`DejaVuSans-ExtraLight.ttf` (offset: 157164, length: 120): *)
0x0141; 0x0011; 0xff6b; 0x0141;
0x001d; 0xffb7; 0x0141; 0x0057; 0xffdc; 0x0141; 0x005a; 0xffdc;
0x0141; 0x005c; 0xffdc; 0x0141; 0x006d; 0xffb7; 0x0141; 0x007d;
0xffdc; 0x0141; 0x00c1; 0xffdc; 0x0141; 0x0125; 0xffdc; 0x0141;
0x0127; 0xffdc; 0x0141; 0x0129; 0xffdc; 0x0141; 0x0137; 0xffdc;
0x0141; 0x0139; 0xffdc; 0x0141; 0x0165; 0xffdc; 0x0141; 0x0167;
0xffdc; 0x0141; 0x016e; 0xffdc; 0x0141; 0x01c6; 0xffdc; 0x0141;
0x01d6; 0xffdc; 0x0141; 0x03e5; 0xffdc; 0x0141; 0x03e7; 0xffdc;
(* Third subtable's header: *)
0x0000; 0x0086; 0x0001; 0x0014; 0x0040; 0x0004; 0x0018;
(* Third subtable's contents;
`DejaVuSans-ExtraLight.ttf` (offset: 220736, length: 120): *)
0x03d9;
0x0010; 0xff7d; 0x03d9; 0x0011; 0xff44; 0x03d9; 0x001d; 0xffdc;
0x03d9; 0x0046; 0xffd3; 0x03d9; 0x0047; 0xffdc; 0x03d9; 0x0048;
0xffd3; 0x03d9; 0x004a; 0xffdc; 0x03d9; 0x004b; 0xffdc; 0x03d9;
0x0050; 0xffdc; 0x03d9; 0x0051; 0xffdc; 0x03d9; 0x0052; 0xffd3;
0x03d9; 0x0054; 0xffdc; 0x03d9; 0x005b; 0xffc9; 0x03d9; 0x006d;
0xffb7; 0x03d9; 0x00a9; 0xffd3; 0x03d9; 0x00aa; 0xffd3; 0x03d9;
0x00ab; 0xffd3; 0x03d9; 0x00ac; 0xffd3; 0x03d9; 0x00ad; 0xffd3;
0x03d9; 0x00b3; 0xffdc;
]


let unmarshaled =
[
(
DecodeKern.{ horizontal = true; minimum = false; cross_stream = false },
[
(16, 36, -45); (16, 37, -73); (16, 42, 75); (16, 45, 114);
(16, 50, 57); (16, 52, 75); (16, 55, -188); (16, 57, -120);
(16, 58, -83); (16, 59, -102); (16, 60, -243); (16, 82, 38);
(16, 89, -55); (16, 92, -36); (16, 130, -45); (16, 131, -45);
(16, 132, -45); (16, 133, -45); (16, 134, -45); (16, 135, -45);
]
);
(
DecodeKern.{ horizontal = true; minimum = false; cross_stream = false },
[
(321, 17, -149); (321, 29, -73); (321, 87, -36); (321, 90, -36);
(321, 92, -36); (321, 109, -73); (321, 125, -36); (321, 193, -36);
(321, 293, -36); (321, 295, -36); (321, 297, -36); (321, 311, -36);
(321, 313, -36); (321, 357, -36); (321, 359, -36); (321, 366, -36);
(321, 454, -36); (321, 470, -36); (321, 997, -36); (321, 999, -36);
]
);
(
DecodeKern.{ horizontal = true; minimum = false; cross_stream = false },
[
(985, 16, -131); (985, 17, -188); (985, 29, -36); (985, 70, -45);
(985, 71, -36); (985, 72, -45); (985, 74, -36); (985, 75, -36);
(985, 80, -36); (985, 81, -36); (985, 82, -45); (985, 84, -36);
(985, 91, -55); (985, 109, -73); (985, 169, -45); (985, 170, -45);
(985, 171, -45); (985, 172, -45); (985, 173, -45); (985, 179, -36);
]
);
]

0 comments on commit 183754a

Please sign in to comment.