Skip to content

Commit

Permalink
Merge pull request #57 from gfngfn/gfngfn/temp-debug-subset
Browse files Browse the repository at this point in the history
Fix how to subset fonts and how to encode fractions
  • Loading branch information
gfngfn authored Sep 30, 2023
2 parents f3df372 + 860ca27 commit e1d49bd
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 103 deletions.
14 changes: 10 additions & 4 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ 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 =
match D.Cff.get_local_bias cff fdindex_opt with
| Some(local_bias) -> local_bias
| None -> assert false
in
D.Cff.lexical_charstring cff ~gsubrs:empty ~lsubrs:empty gid |> inj >>= function
| None ->
Format.printf " not defined@,";
Expand All @@ -402,17 +408,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
44 changes: 31 additions & 13 deletions src/decodeCff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,9 @@ let d_charstring_token (lstate : charstring_lexing_state) : (charstring_lexing_s
return_argument (2, ArgumentInteger(- (b0 - 251) * 256 - b1 - 108))

| 255 ->
d_twoscompl2 >>= fun ret1 ->
d_twoscompl2 >>= fun ret2 ->
let ret = float_of_int ret1 +. (float_of_int ret2) /. (float_of_int (1 lsl 16)) in
return_argument (5, ArgumentReal(ret))
d_twoscompl4 >>= fun n ->
let r = float_of_int n /. (float_of_int (1 lsl 16)) in
return_argument (5, ArgumentReal(r))

| n ->
err @@ Error.UnknownCharstringToken(n)
Expand Down Expand Up @@ -1133,8 +1132,8 @@ let rec d_lexical_charstring ~(depth : int) (cconst : charstring_constant) (lcst
| None ->
err Error.NoSubroutineIndexArgument

| Some(i) ->
d_lexical_subroutine ~depth ~local:true cconst lcstate i
| Some(i_biased) ->
d_lexical_subroutine ~depth ~local:true cconst lcstate i_biased
end

| OpCallGSubr ->
Expand All @@ -1143,8 +1142,8 @@ let rec d_lexical_charstring ~(depth : int) (cconst : charstring_constant) (lcst
| None ->
err Error.NoSubroutineIndexArgument

| Some(i) ->
d_lexical_subroutine ~depth ~local:false cconst lcstate i
| Some(i_biased) ->
d_lexical_subroutine ~depth ~local:false cconst lcstate i_biased
end

| ArgumentInteger(n) ->
Expand All @@ -1165,7 +1164,7 @@ let rec d_lexical_charstring ~(depth : int) (cconst : charstring_constant) (lcst
aux lcstate Alist.empty


and d_lexical_subroutine ~(depth : int) ~(local : bool) (cconst : charstring_constant) (lcstate : lexical_charstring_state) (i : int) =
and d_lexical_subroutine ~(depth : int) ~(local : bool) (cconst : charstring_constant) (lcstate : lexical_charstring_state) (i_biased : int) =
let open DecodeOperation in

if depth > max_depth_limit then
Expand All @@ -1175,8 +1174,7 @@ and d_lexical_subroutine ~(depth : int) ~(local : bool) (cconst : charstring_con
let remaining = lcstate.lexical_lexing.remaining in

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

transform_result @@ access_subroutine subrs i >>= fun (offset, length, _biased_number) ->
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) ->
let lcs = Alist.to_list acc in
Expand All @@ -1185,12 +1183,12 @@ and d_lexical_subroutine ~(depth : int) ~(local : bool) (cconst : charstring_con
let lcstate =
if local then
{ lcstate with
lexical_lsubrs = lcstate.lexical_lsubrs |> LexicalSubroutineIndex.add i lcs;
lexical_lsubrs = lcstate.lexical_lsubrs |> LexicalSubroutineIndex.add i_biased lcs;
lexical_lexing = { lcstate.lexical_lexing with remaining = remaining };
}
else
{ lcstate with
lexical_gsubrs = lcstate.lexical_gsubrs |> LexicalSubroutineIndex.add i lcs;
lexical_gsubrs = lcstate.lexical_gsubrs |> LexicalSubroutineIndex.add i_biased lcs;
lexical_lexing = { lcstate.lexical_lexing with remaining = remaining };
}
in
Expand Down Expand Up @@ -1509,3 +1507,23 @@ let path_of_charstring (ops : Intermediate.Cff.charstring) : (cubic_path list) o
| (_, Middle(middle)) ->
let path = (middle.start, Alist.to_list middle.elems) in
return @@ Alist.to_list (Alist.extend middle.paths path)


(* For experimental use. *)
let get_global_bias (cff : cff_source) : int =
convert_subroutine_number cff.cff_specific.charstring_info.gsubr_index 0


(* For experimental use. *)
let get_local_bias (cff : cff_source) (fdindex_opt : fdindex option) : int option =
let private_info = cff.cff_specific.charstring_info.private_info in
match (private_info, fdindex_opt) with
| (SinglePrivate{ local_subr_index; _ }, None) ->
Some(convert_subroutine_number local_subr_index 0)

| (FontDicts(fdarray, _fdselect), Some(fdindex)) ->
let single_private = fdarray.(fdindex) in
Some(convert_subroutine_number single_private.local_subr_index 0)

| (SinglePrivate(_), Some(_)) | (FontDicts(_, _), None) ->
None
6 changes: 6 additions & 0 deletions src/decodeCff.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ val fdindex : cff_source -> Value.glyph_id -> (Intermediate.Cff.fdindex option)
val lexical_charstring : cff_source -> gsubrs:LexicalSubroutineIndex.t -> lsubrs:LexicalSubroutineIndex.t -> Value.glyph_id -> ((LexicalSubroutineIndex.t * LexicalSubroutineIndex.t * Intermediate.Cff.lexical_charstring) option) ok

val path_of_charstring : Intermediate.Cff.charstring -> (Value.cubic_path list) ok

(** Gets the bias for Global Subrs (for experimental use). *)
val get_global_bias : cff_source -> int

(** Gets the bias for Local Subrs (for experimental use). *)
val get_local_bias : cff_source -> Intermediate.Cff.fdindex option -> int option
15 changes: 13 additions & 2 deletions src/encodeCff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,22 @@ let e_dict (dict : dict) =
e_list e_dict_entry entries


let e_charstring_integer =
e_integer_value


let e_charstring_real (r : float) : unit encoder =
let open EncodeOperation in
let n = int_of_float (r *. (float_of_int (1 lsl 16))) in
e_uint8 255 >>= fun () ->
e_twoscompl4 n


let e_charstring_token (ctoken : charstring_token) : unit encoder =
let open EncodeOperation in
match ctoken with
| ArgumentInteger(n) -> e_integer_value n
| ArgumentReal(r) -> e_real_value r
| ArgumentInteger(n) -> e_charstring_integer n
| ArgumentReal(r) -> e_charstring_real r

| OpHStem -> e_short_key 1
| OpVStem -> e_short_key 3
Expand Down
7 changes: 7 additions & 0 deletions src/otfed.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,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

(** Gets the bias for Global Subrs (for experimental use). *)
val get_global_bias : cff_source -> int

(** Gets the bias for Local Subrs (for experimental use). *)
val get_local_bias : cff_source -> Intermediate.Cff.fdindex option -> int option
end
end

Expand Down Expand Up @@ -1586,6 +1592,7 @@ module Subset : sig
| DecodeError of Decode.Error.t
| EncodeError of Encode.Error.t
| NonexplicitSubroutineNumber
| CallSubrInGlobalSubr of { old_biased : int }
[@@deriving show]
end

Expand Down
Loading

0 comments on commit e1d49bd

Please sign in to comment.