@dicebear/converter 5.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Florian Körner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,10 @@
1
+ <h1 align="center"><img src="https://dicebear.com/api/male/seed.svg?mood=happy" width="124" /> <br />DiceBear</h1>
2
+ <p align="center">
3
+ <strong>SVG Converter for DiceBear</strong>
4
+ </p>
5
+
6
+ <p align="center">
7
+ <a href="https://dicebear.com/">
8
+ Read Documentation
9
+ </a>
10
+ </p>
@@ -0,0 +1,94 @@
1
+ Copyright (c) 2016-2020 The Inter Project Authors.
2
+ "Inter" is trademark of Rasmus Andersson.
3
+ https://github.com/rsms/inter
4
+
5
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
6
+ This license is copied below, and is also available with a FAQ at:
7
+ http://scripts.sil.org/OFL
8
+
9
+ -----------------------------------------------------------
10
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
11
+ -----------------------------------------------------------
12
+
13
+ PREAMBLE
14
+ The goals of the Open Font License (OFL) are to stimulate worldwide
15
+ development of collaborative font projects, to support the font creation
16
+ efforts of academic and linguistic communities, and to provide a free and
17
+ open framework in which fonts may be shared and improved in partnership
18
+ with others.
19
+
20
+ The OFL allows the licensed fonts to be used, studied, modified and
21
+ redistributed freely as long as they are not sold by themselves. The
22
+ fonts, including any derivative works, can be bundled, embedded,
23
+ redistributed and/or sold with any software provided that any reserved
24
+ names are not used by derivative works. The fonts and derivatives,
25
+ however, cannot be released under any other type of license. The
26
+ requirement for fonts to remain under this license does not apply
27
+ to any document created using the fonts or their derivatives.
28
+
29
+ DEFINITIONS
30
+ "Font Software" refers to the set of files released by the Copyright
31
+ Holder(s) under this license and clearly marked as such. This may
32
+ include source files, build scripts and documentation.
33
+
34
+ "Reserved Font Name" refers to any names specified as such after the
35
+ copyright statement(s).
36
+
37
+ "Original Version" refers to the collection of Font Software components as
38
+ distributed by the Copyright Holder(s).
39
+
40
+ "Modified Version" refers to any derivative made by adding to, deleting,
41
+ or substituting -- in part or in whole -- any of the components of the
42
+ Original Version, by changing formats or by porting the Font Software to a
43
+ new environment.
44
+
45
+ "Author" refers to any designer, engineer, programmer, technical
46
+ writer or other person who contributed to the Font Software.
47
+
48
+ PERMISSION AND CONDITIONS
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
51
+ redistribute, and sell modified and unmodified copies of the Font
52
+ Software, subject to the following conditions:
53
+
54
+ 1) Neither the Font Software nor any of its individual components,
55
+ in Original or Modified Versions, may be sold by itself.
56
+
57
+ 2) Original or Modified Versions of the Font Software may be bundled,
58
+ redistributed and/or sold with any software, provided that each copy
59
+ contains the above copyright notice and this license. These can be
60
+ included either as stand-alone text files, human-readable headers or
61
+ in the appropriate machine-readable metadata fields within text or
62
+ binary files as long as those fields can be easily viewed by the user.
63
+
64
+ 3) No Modified Version of the Font Software may use the Reserved Font
65
+ Name(s) unless explicit written permission is granted by the corresponding
66
+ Copyright Holder. This restriction only applies to the primary font name as
67
+ presented to the users.
68
+
69
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
70
+ Software shall not be used to promote, endorse or advertise any
71
+ Modified Version, except to acknowledge the contribution(s) of the
72
+ Copyright Holder(s) and the Author(s) or with their explicit written
73
+ permission.
74
+
75
+ 5) The Font Software, modified or unmodified, in part or in whole,
76
+ must be distributed entirely under this license, and must not be
77
+ distributed under any other license. The requirement for fonts to
78
+ remain under this license does not apply to any document created
79
+ using the Font Software.
80
+
81
+ TERMINATION
82
+ This license becomes null and void if any of the above conditions are
83
+ not met.
84
+
85
+ DISCLAIMER
86
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
89
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
90
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
91
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
92
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
93
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
94
+ OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file
Binary file
package/lib/core.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { ToFormat } from './types';
2
+ export declare const toFormat: ToFormat;
package/lib/core.js ADDED
@@ -0,0 +1,65 @@
1
+ import { getMimeType } from './utils/mime-type.js';
2
+ import { ensureSize } from './utils/svg.js';
3
+ export const toFormat = function (svg, format, exif) {
4
+ const blob = toBlob(svg, format, exif);
5
+ return {
6
+ toDataUri: async () => toDataUri(await blob),
7
+ toFile: async (name) => toFile(await blob, name),
8
+ toArrayBuffer: async () => (await blob).arrayBuffer(),
9
+ };
10
+ };
11
+ async function toDataUri(blob) {
12
+ if (blob.type === getMimeType('svg')) {
13
+ return `data:image/svg+xml;utf8,${encodeURIComponent(await blob.text())}`;
14
+ }
15
+ else {
16
+ return new Promise((resolve, reject) => {
17
+ const reader = new FileReader();
18
+ reader.readAsDataURL(blob);
19
+ reader.onload = () => resolve(reader.result);
20
+ reader.onerror = (e) => reject(e);
21
+ });
22
+ }
23
+ }
24
+ async function toFile(blob, name) {
25
+ const link = document.createElement('a');
26
+ link.href = await toDataUri(blob);
27
+ link.download = name;
28
+ link.click();
29
+ link.remove();
30
+ }
31
+ async function toBlob(rawSvg, format, exif) {
32
+ if (exif) {
33
+ console.warn('The `exif` option is not supported in the browser version of `@dicebear/converter`.');
34
+ console.warn('Please use the node version of `@dicebear/converter` to generate images with exif data.');
35
+ }
36
+ if (format === 'svg') {
37
+ return new Blob([rawSvg], { type: getMimeType('svg') });
38
+ }
39
+ let { svg, size } = ensureSize(rawSvg);
40
+ const svgBlob = new Blob([svg], { type: getMimeType('svg') });
41
+ const canvas = document.createElement('canvas');
42
+ canvas.width = size;
43
+ canvas.height = size;
44
+ const context = canvas.getContext('2d');
45
+ if (null === context) {
46
+ throw new Error('Could not get canvas context');
47
+ }
48
+ if (format === 'jpeg') {
49
+ context.fillStyle = 'white';
50
+ context.fillRect(0, 0, size, size);
51
+ }
52
+ var img = document.createElement('img');
53
+ img.width = size;
54
+ img.height = size;
55
+ img.setAttribute('src', await toDataUri(svgBlob));
56
+ return new Promise((resolve, reject) => {
57
+ img.onload = () => {
58
+ context.drawImage(img, 0, 0, size, size);
59
+ canvas.toBlob((blob) => {
60
+ blob ? resolve(blob) : reject(new Error('Could not create blob'));
61
+ }, `image/${format}`);
62
+ };
63
+ img.onerror = (e) => reject(e);
64
+ });
65
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * DiceBear Converter (@dicebear/converter)
3
+ *
4
+ * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
+ * Copyright (c) 2022 Florian Körner
6
+ */
7
+ export * from './core.js';
8
+ export * from './types.js';
package/lib/index.js ADDED
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * DiceBear Converter (@dicebear/converter)
3
+ *
4
+ * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
+ * Copyright (c) 2022 Florian Körner
6
+ */
7
+ export * from './core.js';
8
+ export * from './types.js';
@@ -0,0 +1,2 @@
1
+ import type { ToFormat } from '../types';
2
+ export declare const toFormat: ToFormat;
@@ -0,0 +1,64 @@
1
+ import { promises as fs } from 'node:fs';
2
+ import { Blob } from 'node:buffer';
3
+ import { getMimeType } from '../utils/mime-type.js';
4
+ import { ensureSize } from '../utils/svg.js';
5
+ import * as tmp from 'tmp-promise';
6
+ import { ensurePackage } from '../utils/package-helper.js';
7
+ export const toFormat = function (svg, format, exif) {
8
+ const blob = toBlob(svg, format, exif);
9
+ return {
10
+ toDataUri: async () => toDataUri(await blob),
11
+ toFile: async (name) => toFile(await blob, name),
12
+ toArrayBuffer: async () => (await blob).arrayBuffer(),
13
+ };
14
+ };
15
+ async function toDataUri(blob) {
16
+ if (blob.type === getMimeType('svg')) {
17
+ return `data:${blob.type};utf8,${encodeURIComponent(await blob.text())}`;
18
+ }
19
+ else {
20
+ const buffer = Buffer.from(await blob.arrayBuffer());
21
+ return `data:image/${blob.type};base64,${buffer.toString('base64')}`;
22
+ }
23
+ }
24
+ async function toFile(blob, name) {
25
+ return fs.writeFile(name, Buffer.from(await blob.arrayBuffer()));
26
+ }
27
+ async function toBlob(rawSvg, format, exif) {
28
+ if (format === 'svg') {
29
+ if (exif) {
30
+ console.warn('Exif is ignored when converting to svg.');
31
+ }
32
+ return new Blob([rawSvg], { type: getMimeType('svg') });
33
+ }
34
+ await ensurePackage('@resvg/resvg-js', '1.4.0');
35
+ const { renderAsync } = await import('@resvg/resvg-js');
36
+ const interRegular = new URL('../../fonts/inter/inter-regular.otf', import.meta.url);
37
+ const interBold = new URL('../../fonts/inter/inter-bold.otf', import.meta.url);
38
+ let { svg } = ensureSize(rawSvg);
39
+ let buffer = await renderAsync(svg, {
40
+ font: {
41
+ loadSystemFonts: false,
42
+ defaultFontFamily: 'Inter',
43
+ fontFiles: [interRegular.pathname, interBold.pathname],
44
+ },
45
+ });
46
+ if (format === 'jpeg') {
47
+ await ensurePackage('sharp', '0.30.0');
48
+ const sharp = (await import('sharp')).default;
49
+ buffer = await sharp(buffer)
50
+ .flatten({ background: '#ffffff' })
51
+ .toFormat(format)
52
+ .toBuffer();
53
+ }
54
+ if (exif) {
55
+ await ensurePackage('exiftool-vendored', '16.3.0');
56
+ const exiftool = (await import('exiftool-vendored')).exiftool;
57
+ await tmp.withFile(async ({ path }) => {
58
+ await fs.writeFile(path, buffer);
59
+ await exiftool.write(path, exif);
60
+ buffer = await fs.readFile(path);
61
+ });
62
+ }
63
+ return new Blob([buffer], { type: getMimeType(format) });
64
+ }
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * DiceBear Convert (@dicebear/converter)
3
+ *
4
+ * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
+ * Copyright (c) 2022 Florian Körner
6
+ */
7
+ export * from './core.js';
8
+ export * from '../types.js';
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * DiceBear Convert (@dicebear/converter)
3
+ *
4
+ * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
+ * Copyright (c) 2022 Florian Körner
6
+ */
7
+ export * from './core.js';
8
+ export * from '../types.js';
package/lib/types.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export declare type ToFormat = (svg: string, format: Format, exif?: Exif) => BinaryResult;
2
+ export interface BinaryResult {
3
+ toDataUri(): Promise<string>;
4
+ toFile(name: string): Promise<void>;
5
+ toArrayBuffer(): Promise<ArrayBuffer>;
6
+ }
7
+ export interface Exif {
8
+ [key: string]: string;
9
+ }
10
+ export declare type Format = 'svg' | 'png' | 'jpeg';
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { Format } from '../types';
2
+ export declare function getMimeType(format: Format): string;
@@ -0,0 +1,9 @@
1
+ export function getMimeType(format) {
2
+ switch (format) {
3
+ case 'png':
4
+ case 'jpeg':
5
+ return `image/${format}`;
6
+ default:
7
+ return 'image/svg+xml';
8
+ }
9
+ }
@@ -0,0 +1 @@
1
+ export declare function ensurePackage(name: string, version: string): Promise<void>;
@@ -0,0 +1,18 @@
1
+ import { createRequire } from 'node:module';
2
+ const require = createRequire(import.meta.url);
3
+ export async function ensurePackage(name, version) {
4
+ let packageJson;
5
+ try {
6
+ packageJson = require(`${name}/package.json`);
7
+ }
8
+ catch (e) {
9
+ // prettier-ignore
10
+ throw new Error(`Please install \`${name}@^${version}\` to use this feature.'`);
11
+ }
12
+ const packageVersion = packageJson.version.split('.').map((v) => parseInt(v));
13
+ const requiredVersion = version.split('.').map((v) => parseInt(v));
14
+ if (packageVersion[0] !== requiredVersion[0] ||
15
+ packageVersion[1] < requiredVersion[1]) {
16
+ throw new Error(`Please install \`${name}@^${version}\` to use this feature. (Currently installed version: \`${packageJson.version})\`})'`);
17
+ }
18
+ }
@@ -0,0 +1,4 @@
1
+ export declare function ensureSize(svg: string, defaultSize?: number): {
2
+ svg: string;
3
+ size: number;
4
+ };
@@ -0,0 +1,13 @@
1
+ export function ensureSize(svg, defaultSize = 512) {
2
+ let size = defaultSize;
3
+ svg = svg.replace(/<svg([^>]*?(?:width="(\d+)"[^>]*)?)>/, (match, g1, g2) => {
4
+ if (g2) {
5
+ size = parseInt(g2);
6
+ }
7
+ else {
8
+ g1 += ` width="${size}" height="${size}"`;
9
+ }
10
+ return `<svg${g1}>`;
11
+ });
12
+ return { svg, size };
13
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@dicebear/converter",
3
+ "version": "5.0.0-alpha.20",
4
+ "description": "SVG Converter for DiceBear",
5
+ "keywords": [
6
+ "dicebear"
7
+ ],
8
+ "homepage": "https://dicebear.com",
9
+ "bugs": {
10
+ "url": "https://github.com/dicebear/dicebear/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/dicebear/dicebear.git"
15
+ },
16
+ "license": "MIT",
17
+ "author": "Florian Körner <contact@florian-koerner.com>",
18
+ "type": "module",
19
+ "exports": {
20
+ "node": "./lib/node/index.js",
21
+ "default": "./lib/index.js"
22
+ },
23
+ "types": "./lib/index.d.ts",
24
+ "files": [
25
+ "fonts",
26
+ "LICENSE",
27
+ "lib",
28
+ "README.md"
29
+ ],
30
+ "scripts": {
31
+ "prebuild": "del-cli lib",
32
+ "build": "tsc",
33
+ "prepublishOnly": "npm run build",
34
+ "test": "uvu tests"
35
+ },
36
+ "dependencies": {
37
+ "@types/json-schema": "^7.0.7",
38
+ "tmp-promise": "^3.0.3"
39
+ },
40
+ "devDependencies": {
41
+ "@resvg/resvg-js": "^1.4.0",
42
+ "@tsconfig/recommended": "^1.0.1",
43
+ "@types/sharp": "^0.30.2",
44
+ "del-cli": "^4.0.1",
45
+ "exiftool-vendored": "^16.3.0",
46
+ "sharp": "^0.30.4",
47
+ "typescript": "^4.6.3",
48
+ "uvu": "^0.5.3"
49
+ },
50
+ "engines": {
51
+ "node": "^14.13.1 || >=16.0.0"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ },
56
+ "gitHead": "6b1c2bd64294365be21d0a133a362ede5ed62b5a"
57
+ }