Skip to content

Commit

Permalink
starting to add shapeFromText tests
Browse files Browse the repository at this point in the history
  • Loading branch information
catdad committed Oct 4, 2023
1 parent dc26bab commit 78897eb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"eslint": "^4.16.0",
"eslint-plugin-ava": "9.0.0",
"jimp": "^0.2.28",
"puppeteer": "^1.0.0",
"puppeteer": "^19.11.1",
"rootrequire": "^1.0.0",
"send": "^0.16.1",
"terser": "^3.14.1"
Expand Down
2 changes: 1 addition & 1 deletion src/confetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@
};
}

function shapeFromText(textData, text, opts) {
function shapeFromText(textData) {
var text,
scalar = 1,
color = '#000000',
Expand Down
71 changes: 69 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ ${funcName}(${opts ? JSON.stringify(opts) : ''});
`;
}

const base64ToBuffer = base64png => createBuffer(base64png.replace(/data:image\/png;base64,/, ''), 'base64');

async function confettiImage(page, opts = {}, funcName = 'confetti') {
const base64png = await page.evaluate(`
${funcName}(${JSON.stringify(opts)});
Expand All @@ -121,8 +123,7 @@ async function confettiImage(page, opts = {}, funcName = 'confetti') {
});
`);

const imageData = base64png.replace(/data:image\/png;base64,/, '');
return createBuffer(imageData, 'base64');
return base64ToBuffer(base64png);
}

function hex(n) {
Expand Down Expand Up @@ -646,6 +647,66 @@ test('[path] shoots confetti of a custom shape', async t => {
t.is(t.context.image.hash(), '9I0p03d03c0');
});

/*
* Shape from text
*/

const loadFont = async page => {
// Noto Color Emoji
const url = 'https://fonts.gstatic.com/s/notocoloremoji/v25/Yq6P-KqIXTD0t4D9z1ESnKM3-HpFabsE4tq3luCC7p-aXxcn.9.woff2';
const name = 'Web Font';

await page.evaluate(`
Promise.resolve().then(async () => {
const fontFile = new FontFace(
"${name}",
"url(${url})",
);
document.fonts.add(fontFile);
await fontFile.load();
});
`, );

return name;
};

test('[text] shapeFromText renders an emoji', async t => {
const page = t.context.page = await fixturePage();

const fontFace = await loadFont(page);

const { base64png, ...shape } = await page.evaluate(`
Promise.resolve().then(async () => {
const { bitmap, ...shape } = confetti.shapeFromText({ text: '😀', fontFamily: '"${fontFace}"', scalar: 10 });
const canvas = document.createElement('canvas');
canvas.width = bitmap.width;
canvas.height = bitmap.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height);
return {
...shape,
base64png: canvas.toDataURL('image/png')
};
});
`);

t.context.buffer = base64ToBuffer(base64png);
t.context.image = await readImage(t.context.buffer);

t.deepEqual({
hash: t.context.image.hash(),
...shape
}, {
type: 'bitmap',
matrix: [ 0.1, 0, 0, 0.1, -6.25, -5.8500000000000005 ],
hash: '8647FpWTCBH'
});
});

/*
* Custom canvas
*/
Expand Down Expand Up @@ -1071,3 +1132,9 @@ test('[esm] exposed confetti method has a `shapeFromPath` property', async t =>

t.is(await page.evaluate(`typeof confettiAlias.shapeFromPath`), 'function');
});

test('[esm] exposed confetti method has a `shapeFromText` property', async t => {
const page = t.context.page = await fixturePage('fixtures/page.module.html');

t.is(await page.evaluate(`typeof confettiAlias.shapeFromText`), 'function');
});

0 comments on commit 78897eb

Please sign in to comment.