Skip to content

Commit

Permalink
feat(reason-skia): bind to FontManager matchFamilyCharacterStyle (rev…
Browse files Browse the repository at this point in the history
…ery-ui#939)

* Print string in HarfbuzzCLI

* FontManager: bind to matchFamilyCharacterStyle

* Change matchFamilyStyleCharacter to accept a Uchar.t

* SkiaFontManagerCLI: add emoji font discovery
  • Loading branch information
zbaylin authored Jul 6, 2020
1 parent 0c49b38 commit ad7ebef
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/harfbuzz-cli/HarfbuzzCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ let run = () => {

let renderString = str => {
let shapes = Harfbuzz.hb_shape(font, str);
print_endline("-- " ++ str ++ " --");
Array.iter(s => {print_endline("- SHAPE: " ++ show(s))}, shapes);
print_endline("----");
};

renderString("abc");
Expand Down
16 changes: 16 additions & 0 deletions examples/skia-font-manager-cli/SkiaFontManagerCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ let draw = canvas => {
++ string_of_float(FontMetrics.getMaxCharacterWidth(metrics)),
);
};

let emoji = "😃";
let char = Zed_utf8.unsafe_extract(emoji, 0);
let maybeTypeface =
FontManager.matchFamilyStyleCharacter(
fontManager,
"Arial",
style,
["en_US"],
char,
);
switch (maybeTypeface) {
| Some(tf) =>
print_endline("Found font for emoji: " ++ Typeface.getFamilyName(tf))
| None => print_endline("No emoji font found")
};
};

let surface = makeSurface(640l, 480l);
Expand Down
2 changes: 1 addition & 1 deletion examples/skia-font-manager-cli/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(package ReveryExamples)
(public_names SkiaFontManagerCli)
(modes native byte)
(libraries skia skia.wrapped.bindings skia.wrapped))
(libraries skia skia.wrapped.bindings skia.wrapped Revery.Zed))

(install
(section bin)
Expand Down
24 changes: 24 additions & 0 deletions src/reason-skia/Skia.re
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,30 @@ module FontManager = {
| None => None
};
};

let matchFamilyStyleCharacter = (mgr, family, style, locales, character) => {
open Ctypes;
let cLocales = CArray.of_list(string, locales);

let character32 = character |> Uchar.to_int |> Int32.of_int;

let maybeTypeface =
SkiaWrapped.FontManager.matchFamilyStyleCharacter(
mgr,
family,
style,
cLocales |> CArray.start,
CArray.length(cLocales),
character32,
);

switch (maybeTypeface) {
| Some(tf) as ret =>
Gc.finalise(SkiaWrapped.Typeface.delete, tf);
ret;
| None => None
};
};
};

module RRect = {
Expand Down
2 changes: 2 additions & 0 deletions src/reason-skia/Skia.rei
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ module FontManager: {

let makeDefault: unit => t;
let matchFamilyStyle: (t, string, FontStyle.t) => option(Typeface.t);
let matchFamilyStyleCharacter:
(t, string, FontStyle.t, list(string), Uchar.t) => option(Typeface.t);
};

module FontMetrics: {
Expand Down
12 changes: 12 additions & 0 deletions src/reason-skia/wrapped/bindings/SkiaWrappedBindings.re
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ module M = (F: FOREIGN) => {
@-> returning(ptr_opt(SkiaTypes.Typeface.t)),
);

let matchFamilyStyleCharacter =
foreign(
"sk_fontmgr_match_family_style_character",
t
@-> string
@-> FontStyle.t
@-> ptr(string)
@-> int
@-> int32_t
@-> returning(ptr_opt(SkiaTypes.Typeface.t)),
);

let delete = foreign("sk_fontmgr_unref", t @-> returning(void));
};

Expand Down

0 comments on commit ad7ebef

Please sign in to comment.