From 5d1bcb52325fda9bd1f52780dbd812511c9fb38c Mon Sep 17 00:00:00 2001 From: omeid matten Date: Wed, 6 Dec 2023 11:45:06 +1100 Subject: [PATCH] Implement matchFamilyStyle for FontMgr. --- package/cpp/api/JsiSkTypefaceFontProvider.h | 9 +++++++++ package/src/skia/types/Font/FontMgr.ts | 2 +- package/src/skia/types/index.ts | 1 + package/src/skia/web/JsiSkFontMgr.ts | 17 ++++++++++++++--- .../src/skia/web/JsiSkTypefaceFontProvider.ts | 17 ++++++++++++++--- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/package/cpp/api/JsiSkTypefaceFontProvider.h b/package/cpp/api/JsiSkTypefaceFontProvider.h index 11be71c4d6..2acef53a86 100644 --- a/package/cpp/api/JsiSkTypefaceFontProvider.h +++ b/package/cpp/api/JsiSkTypefaceFontProvider.h @@ -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 set(getObject()->onMatchFamily(name.c_str())); + if (set == nullptr) { + return jsi::Value::null(); + } + sk_sp typeface(set->matchStyle(*fontStyle)); + if (typeface == nullptr) { + return jsi::Value::null(); + } + return jsi::Object::createFromHostObject( runtime, std::make_shared(getContext(), typeface)); } diff --git a/package/src/skia/types/Font/FontMgr.ts b/package/src/skia/types/Font/FontMgr.ts index e4bda405ef..b7410457eb 100644 --- a/package/src/skia/types/Font/FontMgr.ts +++ b/package/src/skia/types/Font/FontMgr.ts @@ -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; } diff --git a/package/src/skia/types/index.ts b/package/src/skia/types/index.ts index 004d556887..d193fa0f9a 100644 --- a/package/src/skia/types/index.ts +++ b/package/src/skia/types/index.ts @@ -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"; diff --git a/package/src/skia/web/JsiSkFontMgr.ts b/package/src/skia/web/JsiSkFontMgr.ts index a316ae7ad8..a2a67a1bd6 100644 --- a/package/src/skia/web/JsiSkFontMgr.ts +++ b/package/src/skia/web/JsiSkFontMgr.ts @@ -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 @@ -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); } } diff --git a/package/src/skia/web/JsiSkTypefaceFontProvider.ts b/package/src/skia/web/JsiSkTypefaceFontProvider.ts index 940a09833d..908f3f75da 100644 --- a/package/src/skia/web/JsiSkTypefaceFontProvider.ts +++ b/package/src/skia/web/JsiSkTypefaceFontProvider.ts @@ -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 @@ -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();