Skip to content

Commit

Permalink
Implement matchFamilyStyle for FontMgr.
Browse files Browse the repository at this point in the history
  • Loading branch information
omeid committed Dec 6, 2023
1 parent cc67924 commit 5d1bcb5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
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";

Check failure on line 3 in package/src/skia/web/JsiSkFontMgr.ts

View workflow job for this annotation

GitHub Actions / build

There should be at least one empty line between import groups
import type { FontStyle, SkFontMgr, SkTypeface } from "../types";

Check failure on line 4 in package/src/skia/web/JsiSkFontMgr.ts

View workflow job for this annotation

GitHub Actions / build

`../types` type import should occur before import of `./JsiSkTypeface`

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";

Check failure on line 3 in package/src/skia/web/JsiSkTypefaceFontProvider.ts

View workflow job for this annotation

GitHub Actions / build

There should be at least one empty line between import groups
import { JsiSkTypeface } from "./JsiSkTypeface";

Check failure on line 4 in package/src/skia/web/JsiSkTypefaceFontProvider.ts

View workflow job for this annotation

GitHub Actions / build

There should be at least one empty line between import groups
import type { FontStyle, SkTypeface } from "../types";

Check failure on line 5 in package/src/skia/web/JsiSkTypefaceFontProvider.ts

View workflow job for this annotation

GitHub Actions / build

`../types` type import should occur before import of `./JsiSkTypeface`

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),
}

Check failure on line 24 in package/src/skia/web/JsiSkTypefaceFontProvider.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

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

0 comments on commit 5d1bcb5

Please sign in to comment.