Skip to content

Commit

Permalink
temp 13
Browse files Browse the repository at this point in the history
  • Loading branch information
gfngfn committed Sep 30, 2023
1 parent 424f8ad commit a0012dc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
10 changes: 6 additions & 4 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ let print_cff_lex (source : D.source) (gid : V.glyph_id) =
| D.Cff(cff) ->
let empty = D.Cff.LexicalSubroutineIndex.empty in
D.Cff.fdindex cff gid |> inj >>= fun fdindex_opt ->
let global_bias = D.Cff.get_global_bias cff in
let local_bias = D.Cff.get_local_bias cff fdindex_opt in
D.Cff.lexical_charstring cff ~gsubrs:empty ~lsubrs:empty gid |> inj >>= function
| None ->
Format.printf " not defined@,";
Expand All @@ -341,17 +343,17 @@ let print_cff_lex (source : D.source) (gid : V.glyph_id) =
| Some((gsubrs, lsubrs, lcharstring)) ->
Format.printf " %a@," I.Cff.pp_lexical_charstring lcharstring;
Format.printf " dependent gsubrs:@,";
D.Cff.LexicalSubroutineIndex.fold (fun i lcs () ->
Format.printf " - non-biased number: %d:@," i;
D.Cff.LexicalSubroutineIndex.fold (fun i_biased lcs () ->
Format.printf " - biased: %d / non-biased: %d:@," i_biased (i_biased + global_bias);
Format.printf " %a@," I.Cff.pp_lexical_charstring lcs
) gsubrs ();
begin
match fdindex_opt with
| None -> Format.printf " dependent lsubrs (non-CID):@,"
| Some(fdindex) -> Format.printf " dependent lsubrs (CID, fdindex: %d):@," fdindex
end;
D.Cff.LexicalSubroutineIndex.fold (fun i lcs () ->
Format.printf " - non-biased number: %d:@," i;
D.Cff.LexicalSubroutineIndex.fold (fun i_biased lcs () ->
Format.printf " - biased: %d / non-biased: %d:@," i_biased (i_biased + local_bias);
Format.printf " %a@," I.Cff.pp_lexical_charstring lcs
) lsubrs ();
return ()
Expand Down
8 changes: 8 additions & 0 deletions src/decodeCff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,10 @@ and d_lexical_subroutine ~(depth : int) ~(local : bool) (cconst : charstring_con

let subrs = if local then cconst.lsubr_index else cconst.gsubr_index in

(* TODO: remove this *)
Format.printf "** |%s ACCESS %s (i_old_biased: %d)@,"
(String.make depth '@') (if local then "LOCAL" else "GLOBAL") i_biased;

transform_result @@ access_subroutine subrs i_biased >>= fun (offset, length, _biased_number) ->
let lcstate = { lcstate with lexical_lexing = { lcstate.lexical_lexing with remaining = length } } in
pick offset (d_lexical_charstring ~depth:(depth + 1) cconst lcstate) >>= fun (lcstate, acc) ->
Expand All @@ -1184,11 +1188,15 @@ and d_lexical_subroutine ~(depth : int) ~(local : bool) (cconst : charstring_con
(* Adds the tokenized CharString and resets the remaining byte length. *)
let lcstate =
if local then
(* TODO: remove this *)
let () = Format.printf "** |%s ADD LOCAL (i_old_biased: %d)@," (String.make depth '@') i_biased in
{ lcstate with
lexical_lsubrs = lcstate.lexical_lsubrs |> LexicalSubroutineIndex.add i_biased lcs;
lexical_lexing = { lcstate.lexical_lexing with remaining = remaining };
}
else
(* TODO: remove this *)
let () = Format.printf "** |%s ADD GLOBAL (i_old_biased: %d)@," (String.make depth '@') i_biased in
{ lcstate with
lexical_gsubrs = lcstate.lexical_gsubrs |> LexicalSubroutineIndex.add i_biased lcs;
lexical_lexing = { lcstate.lexical_lexing with remaining = remaining };
Expand Down
6 changes: 6 additions & 0 deletions src/otfed.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,12 @@ module Decode : sig

(** Converts a CharString into a cubic Bézier path. *)
val path_of_charstring : Intermediate.Cff.charstring -> (Value.cubic_path list) ok

(* TODO: remove this; temporary *)
val get_global_bias : cff_source -> int

(* TODO: remove this; temporary *)
val get_local_bias : cff_source -> Intermediate.Cff.fdindex option -> int
end
end

Expand Down
51 changes: 33 additions & 18 deletions src/subset.ml
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ module Old = struct

(* TODO: remove this; temporary *)
let pp_pair ppf (old, gid_new) =
Format.fprintf ppf "%a ---> %d@ " pp old gid_new
Format.fprintf ppf "%a ---> %d" pp old gid_new


let compare ((cat1, i1) : t) ((cat2, i2) : t) =
Expand All @@ -520,7 +520,7 @@ end
module RenumberMap = Map.Make(Old)


let renumber_subroutine ~msg ~(bias_new : int) ~(category_old : Old.category) (renumber_map : int RenumberMap.t) (tokens_old : Intermediate.Cff.lexical_charstring) =
let renumber_subroutine ~(bias_new : int) ~(category_old : Old.category) (renumber_map : int RenumberMap.t) (tokens_old : Intermediate.Cff.lexical_charstring) =
let open ResultMonad in
let open Intermediate.Cff in
let rec aux token_new_acc = function
Expand All @@ -530,8 +530,15 @@ let renumber_subroutine ~msg ~(bias_new : int) ~(category_old : Old.category) (r
| ArgumentInteger(i_old_biased) :: OpCallGSubr :: tokens ->
let i_new_biased =
match renumber_map |> RenumberMap.find_opt (Old.Global, i_old_biased) with
| None -> assert false
| Some(i_new) -> i_new - bias_new
| None ->
assert false

| Some(i_new) ->
let i_new_biased = i_new - bias_new in
(* TODO: remove this *)
Format.printf "** = RENUM GLOBAL (i_old_biased: %d, i_new: %d, i_new_biased: %d)@,"
i_old_biased i_new i_new_biased;
i_new_biased
in
aux (Alist.append token_new_acc [ArgumentInteger(i_new_biased); OpCallGSubr]) tokens

Expand All @@ -547,15 +554,15 @@ let renumber_subroutine ~msg ~(bias_new : int) ~(category_old : Old.category) (r
let i_new_biased =
match renumber_map |> RenumberMap.find_opt (Old.Local(fdindex_opt), i_old_biased) with
| None ->
(* TODO: remove this; temporary *)
failwith @@
Format.asprintf "msg: %s, i_old_biased: %d, knowns: %a"
msg
i_old_biased
(Format.pp_print_list Old.pp_pair) (renumber_map |> RenumberMap.bindings)
assert false

| Some(i_new) ->
i_new - bias_new
let i_new_biased = i_new - bias_new in
(* TODO: remove this *)
Format.printf "** = RENUM LOCAL %a (i_old_biased: %d, i_new: %d, i_new_biased: %d)@,"
(Format.pp_print_option Format.pp_print_int) fdindex_opt
i_old_biased i_new i_new_biased;
i_new_biased
in
aux (Alist.append token_new_acc [ArgumentInteger(i_new_biased); OpCallGSubr]) tokens

Expand All @@ -582,6 +589,8 @@ let get_glyph_name (cff : Decode.cff_source) (gid : glyph_id) : string ok =
let make_cff ~(num_glyphs : int) (cff : Decode.cff_source) (gids : glyph_id list) =
let open ResultMonad in

Format.printf "@[<v>"; (* TODO: remove this *)

(* Initializes `lsubrs_info` by judging whether the font has FDIndex: *)
inj_dec @@ Decode.Cff.top_dict cff >>= fun top_dict ->
begin
Expand All @@ -602,6 +611,9 @@ let make_cff ~(num_glyphs : int) (cff : Decode.cff_source) (gids : glyph_id list
foldM (fun (lcsacc, gsubrs, lsubrs_info) (gid : glyph_id) ->
get_glyph_name cff gid >>= fun name ->
inj_dec @@ Decode.Cff.fdindex cff gid >>= fun fdindex_opt ->
(* TODO: remove this *)
Format.printf "** TRAVERSE CS (gid: %d, fdindex_opt: %a)@,"
gid (Format.pp_print_option Format.pp_print_int) fdindex_opt;
match (lsubrs_info, fdindex_opt) with
| (SingleLsubrs(lsubrs), None) ->
begin
Expand Down Expand Up @@ -672,6 +684,10 @@ let make_cff ~(num_glyphs : int) (cff : Decode.cff_source) (gids : glyph_id list
(i_new, renumber_map, subr_old_acc |> Alist.to_list)
in

(* TODO: remove this *)
Format.printf "** RENUMBER MAP: %a@,"
(Format.pp_print_list Old.pp_pair) (renumber_map |> RenumberMap.bindings);

(* Calculates the bias for new Subrs indices: *)
let bias_new =
if num_subrs < 1240 then
Expand All @@ -691,21 +707,20 @@ let make_cff ~(num_glyphs : int) (cff : Decode.cff_source) (gids : glyph_id list
| Old.Global -> Decode.Cff.get_global_bias cff
| Old.Local(fdindex_opt) -> Decode.Cff.get_local_bias cff fdindex_opt
in
let msg =
Format.asprintf "A(i_new: %d, bias_old: %d, category_old: %a, i_old_biased: %d)"
i_new bias_old Old.pp_category category_old i_old_biased
in
renumber_subroutine ~msg ~category_old ~bias_new renumber_map subrs
Format.printf "** TRAVERSE SUBRS (i_new: %d, bias_old: %d, category_old: %a, i_old_biased: %d)@,"
i_new bias_old Old.pp_category category_old i_old_biased;
renumber_subroutine ~category_old ~bias_new renumber_map subrs
) >>= fun gsubrs ->

(* Modifies indices in CharStrings of the subset glyphs: *)
charstrings_old |> mapM (fun (lcs, fdindex_opt, name) ->
let category_old = Old.Local(fdindex_opt) in
(* TODO: remove `msg`; temporary *)
renumber_subroutine ~msg:"B" ~category_old ~bias_new renumber_map lcs >>= fun charstring ->
renumber_subroutine ~category_old ~bias_new renumber_map lcs >>= fun charstring ->
return (name, charstring)
) >>= fun names_and_charstrings ->

Format.printf "@]"; (* TODO: remove this *)

(* Produces the `CFF` table: *)
inj_enc @@ Encode.Cff.make { top_dict with number_of_glyphs = num_glyphs } ~gsubrs ~names_and_charstrings

Expand Down

0 comments on commit a0012dc

Please sign in to comment.