Skip to content

Commit

Permalink
feat: added /napi-rs engine (#480)
Browse files Browse the repository at this point in the history
feat: added `/napi-rs` engine
feat: added `createImageData()`
feat: added `createConicGradient()`
feat: added `printConicColorGradient()`
feat: added `printConicStrokeGradient()`
refactor: renamed `resolveImage` to `loadImage`
refactor: renamed `registerFont` to `loadFont`
feat(cairo): added `jpeg()`
feat(cairo): added `jpegAsync()`
feat(cairo): added `pdf()`
feat(cairo): added `pdfAsync()`
feat(cairo): added `png()`
feat(cairo): added `pngAsync()`
feat(skia): added `jpegAsync()`
feat(skia): added `pdfAsync()`
feat(skia): added `pngAsync()`
feat(skia): added `svgAsync()`
docs: add `@note`s defining which methods are engine-specific

BREAKING CHANGE: refactor: removed `cb` parameter in `getImageData()`
BREAKING CHANGE: refactor: removed `cb` parameter in `measureText()`
BREAKING CHANGE: refactor: removed `cb` parameter in `createPattern()`
BREAKING CHANGE: refactor: removed `cb` parameter in `wrapText()`
BREAKING CHANGE: refactor(cairo): renamed `createJPEGStream()` to `jpegStream()`
BREAKING CHANGE: refactor(cairo): renamed `createPDFStream()` to `pdfStream()`
BREAKING CHANGE: refactor(cairo): renamed `createPNGStream()` to `pngStream()`
BREAKING CHANGE: refactor(cairo): renamed `setAntiAliasing()` to `setAntialiasMode()`
BREAKING CHANGE: refactor(skia): `jpg()` now returns a synchronous response
BREAKING CHANGE: refactor(skia): `pdf()` now returns a synchronous response
BREAKING CHANGE: refactor(skia): `png()` now returns a synchronous response
BREAKING CHANGE: refactor(skia): `svg()` now returns a synchronous response
BREAKING CHANGE: refactor(skia): removed `cb` parameter in `getFontVariant()`
BREAKING CHANGE: refactor(skia): removed `cb` parameter in `getTextTracking()`
BREAKING CHANGE: refactor(skia): removed `cb` parameter in `getTextWrap()`
BREAKING CHANGE: refactor(skia): renamed `getPages()` to `get pages`
BREAKING CHANGE: refactor(skia): renamed `jpg()` to `jpeg()`
BREAKING CHANGE: refactor(skia): renamed `newPages()` to `addPage()`
  • Loading branch information
kyranet authored Jun 1, 2022
1 parent 544b222 commit a082135
Show file tree
Hide file tree
Showing 24 changed files with 3,377 additions and 3,005 deletions.
2 changes: 0 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
node_modules/
dist/
*.js
*.mjs
23 changes: 6 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,13 @@ docs/
# Ignore tsc dist folder
dist/

# Ignore JavaScript files
**/*.js
**/*.mjs
**/*.js.map
**/*.d.ts
!src/**/*.d.ts
# Ignore generated files
src/browser.ts
src/cairo.ts
src/napi-rs.ts
src/skia.ts
**/*.tsbuildinfo

!browser.d.ts
!browser.js
!browser.mjs
!cairo.d.ts
!cairo.js
!cairo.mjs
!skia.d.ts
!skia.js
!skia.mjs

# Ignore heapsnapshot and log files
*.heapsnapshot
*.log
Expand All @@ -42,4 +31,4 @@ wiki/

# Yarn files
.yarn/install-state.gz
.yarn/build-state.yml
.yarn/build-state.yml
1 change: 0 additions & 1 deletion browser.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions browser.js

This file was deleted.

1 change: 0 additions & 1 deletion browser.mjs

This file was deleted.

1 change: 0 additions & 1 deletion cairo.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions cairo.js

This file was deleted.

1 change: 0 additions & 1 deletion cairo.mjs

This file was deleted.

22 changes: 22 additions & 0 deletions generator.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"main": "./src/main.tst",
"output": "./src/[name].ts",
"entries": [
{
"name": "browser",
"defines": ["BROWSER"]
},
{
"name": "cairo",
"defines": ["CAIRO"]
},
{
"name": "napi-rs",
"defines": ["NAPI_RS"]
},
{
"name": "skia",
"defines": ["SKIA"]
}
]
}
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"require": "./dist/cairo.js",
"types": "./dist/cairo.d.ts"
},
"./napi-rs": {
"import": "./dist/napi-rs.mjs",
"require": "./dist/napi-rs.js",
"types": "./dist/napi-rs.d.ts"
},
"./skia": {
"import": "./dist/skia.mjs",
"require": "./dist/skia.js",
Expand All @@ -31,10 +36,12 @@
"sideEffects": false,
"scripts": {
"prepublishOnly": "yarn build",
"lint": "eslint src --ext ts --fix",
"docs": "typedoc",
"lint": "eslint scripts --ext mjs --fix",
"docs": "rn build:gen && typedoc",
"update": "yarn upgrade-interactive --latest",
"build": "rollup -c rollup.config.ts",
"build": "yarn build:gen && yarn build:ts",
"build:gen": "node ./scripts/generate.mjs",
"build:ts": "rollup -c rollup.config.ts",
"watch": "yarn build -w",
"sversion": "standard-version",
"prepare": "husky install .github/husky"
Expand All @@ -45,6 +52,7 @@
"devDependencies": {
"@commitlint/cli": "^17.0.1",
"@commitlint/config-conventional": "^17.0.0",
"@napi-rs/canvas": "0.1.21",
"@sapphire/eslint-config": "^4.3.5",
"@sapphire/prettier-config": "1.4.3",
"@sapphire/ts-config": "3.3.4",
Expand Down
6 changes: 5 additions & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig([
file: './dist/browser.umd.js',
format: 'umd',
name: 'CanvasConstructor',
strict: true,
sourcemap: true
}
],
Expand All @@ -30,6 +31,7 @@ export default defineConfig([
input: {
browser: 'src/browser.ts',
cairo: 'src/cairo.ts',
'napi-rs': 'src/napi-rs.ts',
skia: 'src/skia.ts'
},
output: [
Expand All @@ -39,6 +41,7 @@ export default defineConfig([
format: 'cjs',
exports: 'named',
sourcemap: true,
strict: true,
compact: false
},
{
Expand All @@ -47,10 +50,11 @@ export default defineConfig([
format: 'es',
exports: 'named',
sourcemap: true,
strict: true,
compact: false
}
],
plugins: [pluginsTypeScript, pluginsTerser],
external: ['canvas', 'skia-canvas']
external: ['node:util', 'canvas', 'skia-canvas', '@napi-rs/canvas']
}
]);
60 changes: 60 additions & 0 deletions scripts/generate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { readFile, writeFile } from 'node:fs/promises';
import configuration from '../generator.config.json' assert { type: 'json' };

const root = new URL('../', import.meta.url);
const input = new URL(configuration.main, root);
console.debug(`Input file: '${input.href}'`);

const text = await readFile(input, 'utf8');

for (const entry of configuration.entries) {
const output = new URL(configuration.output.replace('[name]', entry.name), root);

const content = replaceAll(entry.defines);
await writeFile(output, content, 'utf8');
console.info(`Written ${content.length.toLocaleString('en-US')} bytes into`, output.href, '...');
}

/**
* @param {string} variables The variable to check.
* @param {string[]} defines
*/
function isIncluded(variables, defines) {
for (const variable of variables.split(',')) {
if (variable.startsWith('!')) {
return !defines.includes(variable.slice(1));
} else if (defines.includes(variable)) {
return true;
}
}

return false;
}

/**
* @param {string} content
* @param {string} header
* @returns {string}
*/
function getContent(content, header) {
return content.includes('\n')
? content
.slice(8 + header.length)
.replaceAll('\\\n', '\n')
.split('\n')
.map((line) => line.trimEnd())
.join('\n')
: content.slice(8 + header.length).trim();
}

/**
* @param {string[]} defines
* @returns {string}
*/
function replaceAll(defines) {
return text
.replaceAll(/\/\/ IF\(([A-Z_,!]+)\):([^\n]|(?<=\\)\n)+/g, (match, variables) =>
isIncluded(variables, defines) ? getContent(match, variables) : ''
)
.replaceAll(/\n\n+/g, '\n\n');
}
1 change: 0 additions & 1 deletion skia.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions skia.js

This file was deleted.

1 change: 0 additions & 1 deletion skia.mjs

This file was deleted.

93 changes: 0 additions & 93 deletions src/browser.ts

This file was deleted.

Loading

0 comments on commit a082135

Please sign in to comment.