Skip to content
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
6 changes: 6 additions & 0 deletions packages/skia/cpp/api/JsiSkFontMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "JsiSkFontStyle.h"
#include "JsiSkNativeObjects.h"
#include "JsiSkTypeface.h"
#include "JsiSkTypefaceFontProvider.h"
#include "utils/RNSkLog.h"
#include <jsi/jsi.h>

Expand Down Expand Up @@ -71,6 +72,11 @@ class JsiSkFontMgr

static sk_sp<SkFontMgr> fromValue(jsi::Runtime &runtime,
const jsi::Value &obj) {
// A TypefaceFontProvider (what useFonts returns) is an SkFontMgr too.
if (auto provider =
tryGetJsiObject<JsiSkTypefaceFontProvider>(runtime, obj)) {
return provider->getObject();
}
return objectFromValue(runtime, obj);
}

Expand Down
93 changes: 92 additions & 1 deletion packages/skia/src/renderer/__tests__/e2e/SVG.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -40,6 +42,14 @@ const tiger = new SVGAsset(
800
);

const RobotoRegular = Array.from(
resolveFile("skia/__tests__/assets/Roboto-Regular.ttf")
);

const robotoText = `<svg viewBox='0 0 64 32' width="64" height="32" xmlns='http://www.w3.org/2000/svg'>
<text x="2" y="26" font-family="Roboto" font-size="24" fill="black">Hi</text>
</svg>`;

const svgWithoutSize = new SVGAsset(
`<svg viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'>
<circle cx='10' cy='10' r='10' fill='#00FFFF'/>
Expand Down Expand Up @@ -209,4 +219,85 @@ describe("Displays SVGs", () => {
);
await 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.MakeFromData(
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);
return canvas
.readPixels(0, 0, {
width: 64,
height: 32,
colorType: ctx.colorType,
alphaType: ctx.alphaType,
})
.then((pixels) => {
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");
}
);
});
Loading