Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reason-skia): bind to FontMetrics char width functions #904

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/skia-font-manager-cli/SkiaFontManagerCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,48 @@ let draw = canvas => {
Paint.setTypeface(fill, typeface);
Canvas.drawText(canvas, "Times New Roman (System)", 10., 40., fill);
};

let maybeTypeface =
FontManager.matchFamilyStyle(fontManager, "Consolas", style);
switch (maybeTypeface) {
| None =>
print_endline(
"Normal Consolas not found. Ensure your system has it available.",
)
| Some(typeface) =>
Paint.setTypeface(fill, typeface);
let metrics = FontMetrics.make();
let _ret: float = Paint.getFontMetrics(fill, metrics, 1.0);
print_endline("__Consolas__");
print_endline(
"-- Average character width: "
++ string_of_float(FontMetrics.getAvgCharacterWidth(metrics)),
);
print_endline(
"-- Maximum character width: "
++ string_of_float(FontMetrics.getMaxCharacterWidth(metrics)),
);
};

let filePath = Sys.getcwd() ++ "/examples/skia-cli/FiraCode-Regular.ttf";
print_endline("Loading font: " ++ filePath);
let maybeTypeface = Typeface.makeFromFile(filePath, 0);
switch (maybeTypeface) {
| None => failwith("Unable to load font: " ++ filePath)
| Some(typeface) =>
Paint.setTypeface(fill, typeface);
let metrics = FontMetrics.make();
let _ret: float = Paint.getFontMetrics(fill, metrics, 1.0);
print_endline("__Fira Code__");
print_endline(
"-- Average character width: "
++ string_of_float(FontMetrics.getAvgCharacterWidth(metrics)),
);
print_endline(
"-- Maximum character width: "
++ string_of_float(FontMetrics.getMaxCharacterWidth(metrics)),
);
};
};

let surface = makeSurface(640l, 480l);
Expand Down
2 changes: 2 additions & 0 deletions src/reason-skia/Skia.re
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ module FontMetrics = {
let getBottom = SkiaWrapped.FontMetrics.getBottom;
let getUnderlineThickness = SkiaWrapped.FontMetrics.getUnderlineThickness;
let getUnderlinePosition = SkiaWrapped.FontMetrics.getUnderlinePosition;
let getAvgCharacterWidth = SkiaWrapped.FontMetrics.getAvgCharacterWidth;
let getMaxCharacterWidth = SkiaWrapped.FontMetrics.getMaxCharacterWidth;
};

module Hinting = {
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 @@ -71,6 +71,8 @@ module FontMetrics: {
let getBottom: t => float;
let getUnderlinePosition: t => float;
let getUnderlineThickness: t => float;
let getAvgCharacterWidth: t => float;
let getMaxCharacterWidth: t => float;
};

module ImageFilter: {
Expand Down
4 changes: 4 additions & 0 deletions src/reason-skia/wrapped/bindings/SkiaWrappedBindings.re
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ module M = (F: FOREIGN) => {
getf(!@metrics, SkiaTypes.FontMetrics.underlineThickness);
let getUnderlinePosition = metrics =>
getf(!@metrics, SkiaTypes.FontMetrics.underlinePosition);
let getMaxCharacterWidth = metrics =>
getf(!@metrics, SkiaTypes.FontMetrics.maxCharacterWidth);
let getAvgCharacterWidth = metrics =>
getf(!@metrics, SkiaTypes.FontMetrics.avgCharacterWidth);
};

module ImageFilter = {
Expand Down
2 changes: 2 additions & 0 deletions src/reason-skia/wrapped/types/SkiaWrappedTypes.re
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ module M = (T: TYPE) => {
let underlinePosition = field(t, "fUnderlinePosition", float);
let strikeoutThickness = field(t, "fStrikeoutThickness", float);
let strikeoutPosition = field(t, "fStrikeoutPosition", float);
let maxCharacterWidth = field(t, "fMaxCharWidth", float);
let avgCharacterWidth = field(t, "fAvgCharWidth", float);
seal(t);
let t = typedef(t, "sk_fontmetrics_t");
};
Expand Down