diff --git a/packages/skia/cpp/api/JsiSkFontMgr.h b/packages/skia/cpp/api/JsiSkFontMgr.h index 33eb9ef649..368c92b14c 100644 --- a/packages/skia/cpp/api/JsiSkFontMgr.h +++ b/packages/skia/cpp/api/JsiSkFontMgr.h @@ -10,6 +10,7 @@ #include "JsiSkFontStyle.h" #include "JsiSkNativeObjects.h" #include "JsiSkTypeface.h" +#include "JsiSkTypefaceFontProvider.h" #include "utils/RNSkLog.h" #include @@ -71,6 +72,11 @@ class JsiSkFontMgr static sk_sp fromValue(jsi::Runtime &runtime, const jsi::Value &obj) { + // A TypefaceFontProvider (what useFonts returns) is an SkFontMgr too. + if (auto provider = + tryGetJsiObject(runtime, obj)) { + return provider->getObject(); + } return objectFromValue(runtime, obj); } diff --git a/packages/skia/src/renderer/__tests__/e2e/SVG.spec.tsx b/packages/skia/src/renderer/__tests__/e2e/SVG.spec.tsx index bd5a38d0d5..44e642b05d 100644 --- a/packages/skia/src/renderer/__tests__/e2e/SVG.spec.tsx +++ b/packages/skia/src/renderer/__tests__/e2e/SVG.spec.tsx @@ -3,7 +3,9 @@ import fs from "fs"; import React from "react"; import { checkImage, docPath, itRunsE2eOnly } from "../../../__tests__/setup"; -import { importSkia, surface } from "../setup"; +import { importSkia, resolveFile, surface } from "../setup"; +import { AlphaType, ColorType } from "../../../skia/types"; +import type { SkFontMgr } from "../../../skia/types"; import { Blur, ColorMatrix, @@ -40,6 +42,14 @@ const tiger = new SVGAsset( 800 ); +const RobotoRegular = Array.from( + resolveFile("skia/__tests__/assets/Roboto-Regular.ttf") +); + +const robotoText = ` +Hi +`; + const svgWithoutSize = new SVGAsset( ` @@ -207,4 +217,82 @@ describe("Displays SVGs", () => { ); checkImage(image, docPath("svg.png")); }); + + itRunsE2eOnly( + "should accept a TypefaceFontProvider as the font manager", + async () => { + const result = await surface.eval( + (Skia, ctx) => { + const roboto = Skia.Typeface.MakeFreeTypeFaceFromData( + Skia.Data.fromBytes(new Uint8Array(ctx.RobotoRegular)) + )!; + const provider = Skia.TypefaceFontProvider.Make(); + provider.registerFont(roboto, "Roboto"); + const fromString = Skia.SVG.MakeFromString(ctx.svg, provider)!; + const fromData = Skia.SVG.MakeFromData( + Skia.Data.fromBytes(new Uint8Array(ctx.svgBytes)), + provider + )!; + const fromSystem = Skia.SVG.MakeFromString( + ctx.svg, + Skia.FontMgr.System() + ); + const offscreen = Skia.Surface.MakeOffscreen(64, 32)!; + const canvas = offscreen.getCanvas(); + canvas.drawColor(Skia.Color("white")); + canvas.drawSvg(fromString); + const pixels = canvas.readPixels(0, 0, { + width: 64, + height: 32, + colorType: ctx.colorType, + alphaType: ctx.alphaType, + })!; + let inked = 0; + for (let i = 0; i < pixels.length; i += 4) { + if (pixels[i] < 128) { + inked++; + } + } + return { + fromString: [fromString.width(), fromString.height()], + fromData: [fromData.width(), fromData.height()], + fromSystem: fromSystem !== null, + inked, + }; + }, + { + RobotoRegular, + svg: robotoText, + svgBytes: Array.from(Buffer.from(robotoText)), + colorType: ColorType.RGBA_8888, + alphaType: AlphaType.Unpremul, + } + ); + expect(result.fromString).toEqual([64, 32]); + expect(result.fromData).toEqual([64, 32]); + expect(result.fromSystem).toBe(true); + expect(result.inked).toBeGreaterThan(0); + } + ); + + itRunsE2eOnly( + "should reject a font manager of a different type", + async () => { + const message = await surface.eval( + (Skia, ctx) => { + try { + Skia.SVG.MakeFromString( + ctx.svg, + Skia.Paint() as unknown as SkFontMgr + ); + return "did not throw"; + } catch (e) { + return (e as Error).message; + } + }, + { svg: robotoText } + ); + expect(message).toContain("Expected FontMgr but got different type"); + } + ); });