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

Implement matchFamilyStyle for FontMgr. #2031

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions package/cpp/api/JsiSkTypefaceFontProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,17 @@ class JsiSkTypefaceFontProvider
JSI_HOST_FUNCTION(matchFamilyStyle) {
auto name = arguments[0].asString(runtime).utf8(runtime);
auto fontStyle = JsiSkFontStyle::fromValue(runtime, arguments[1]);

sk_sp<SkFontStyleSet> set(getObject()->onMatchFamily(name.c_str()));
if (set == nullptr) {
return jsi::Value::null();
}

sk_sp<SkTypeface> typeface(set->matchStyle(*fontStyle));
if (typeface == nullptr) {
return jsi::Value::null();
}

return jsi::Object::createFromHostObject(
runtime, std::make_shared<JsiSkTypeface>(getContext(), typeface));
}
Expand Down
2 changes: 1 addition & 1 deletion package/src/skia/types/Font/FontMgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import type { FontStyle } from "./Font";
export interface SkFontMgr extends SkJSIInstance<"FontMgr"> {
countFamilies(): number;
getFamilyName(index: number): string;
matchFamilyStyle(name?: string, style?: FontStyle): SkTypeface;
matchFamilyStyle(name?: string, style?: FontStyle): SkTypeface | null;
}
1 change: 1 addition & 0 deletions package/src/skia/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "./ImageFilter";
export * from "./Font";
export * from "./Typeface";
export * from "./Paint";
export * from "./Paragraph";
export * from "./Path";
export * from "./Color";
export * from "./Canvas";
Expand Down
17 changes: 14 additions & 3 deletions package/src/skia/web/JsiSkFontMgr.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { CanvasKit, FontMgr } from "canvaskit-wasm";

import { JsiSkTypeface } from "./JsiSkTypeface";
import type { FontStyle, SkFontMgr, SkTypeface } from "../types";

import { HostObject, NotImplementedOnRNWeb } from "./Host";
import { HostObject, optEnum } from "./Host";

export class JsiSkFontMgr
extends HostObject<FontMgr, "FontMgr">
Expand All @@ -20,7 +21,17 @@ export class JsiSkFontMgr
getFamilyName(index: number) {
return this.ref.getFamilyName(index);
}
matchFamilyStyle(_familyName: string, _fontStyle: FontStyle): SkTypeface {
throw new NotImplementedOnRNWeb();
matchFamilyStyle(name: string, style: FontStyle): SkTypeface | null {
const fontStyles = {
weight: optEnum(style.weight),
width: optEnum(style.width),
slant: optEnum(style.slant),
};

const tf = this.ref.matchFamilyStyle(name, fontStyles);
if (tf == null) {
return null;
}
return new JsiSkTypeface(this.CanvasKit, tf);
}
}
17 changes: 14 additions & 3 deletions package/src/skia/web/JsiSkTypefaceFontProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { CanvasKit, TypefaceFontProvider } from "canvaskit-wasm";

import type { SkTypefaceFontProvider } from "../types/Paragraph/TypefaceFontProvider";
import { JsiSkTypeface } from "./JsiSkTypeface";
import type { FontStyle, SkTypeface } from "../types";

import { HostObject, NotImplementedOnRNWeb } from "./Host";
import { HostObject, optEnum } from "./Host";

export class JsiSkTypefaceFontProvider
extends HostObject<TypefaceFontProvider, "FontMgr">
Expand All @@ -15,8 +16,18 @@ export class JsiSkTypefaceFontProvider
super(CanvasKit, ref, "FontMgr");
}

matchFamilyStyle(_name: string, _style: FontStyle): SkTypeface {
throw new NotImplementedOnRNWeb();
matchFamilyStyle(name: string, style: FontStyle): SkTypeface | null {
const fontStyles = {
weight: optEnum(style.weight),
width: optEnum(style.width),
slant: optEnum(style.slant),
}

const tf = this.ref.matchFamilyStyle(name, fontStyles);
if (tf == null) {
return null;
}
return new JsiSkTypeface(this.CanvasKit, tf);
}
countFamilies() {
return this.ref.countFamilies();
Expand Down
Loading