oslllo-potrace 1.2.0 → 2.0.0

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/CHANGELOG.md CHANGED
@@ -5,6 +5,24 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.0.0](https://www.github.com/oslllo/potrace/compare/v1.2.0...v2.0.0) (2021-12-31)
9
+
10
+
11
+ ### Features
12
+
13
+ * **deps:** Simplify dependencies, remove canvas/jsdom ([c013c58](https://www.github.com/oslllo/potrace/commit/c013c58e0fc11c1757fbcf3089e6b42d647fb6af))
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * **deps:** use svg2png-wasm-node-10 for compatibility ([655fe37](https://www.github.com/oslllo/potrace/commit/655fe37a80bc61b62ea0a21c4346dba353154842))
19
+ * **example:** add white background ([f3b3980](https://www.github.com/oslllo/potrace/commit/f3b3980298ffed0fcc6a9e4186ac7e6a2cb0627b))
20
+
21
+
22
+ ### Miscellaneous Chores
23
+
24
+ * release 2.0.0 ([6575466](https://www.github.com/oslllo/potrace/commit/6575466f0f4aeeaedb8302684c0d14903d26f350))
25
+
8
26
  ## [1.2.0](https://www.github.com/oslllo/potrace/compare/v1.1.4...v1.2.0) (2021-12-30)
9
27
 
10
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oslllo-potrace",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "A \"1:1 output\" JavaScript port of Potrace JS for NodeJS.",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -39,17 +39,14 @@
39
39
  },
40
40
  "homepage": "https://github.com/oslllo/potrace#readme",
41
41
  "dependencies": {
42
- "canvas": "^2.8.0",
43
- "jimp": "^0.16.1",
44
- "jsdom": "^16.7.0",
45
- "oslllo-validator": "^2.0.1"
42
+ "jimp": "^0.16.1"
46
43
  },
47
44
  "devDependencies": {
48
45
  "chai": "^4.3.4",
49
46
  "chai-as-promised": "^7.1.1",
50
47
  "fast-glob": "^3.2.7",
51
48
  "fs-extra": "^10.0.0",
52
- "mocha": "^8.4.0",
53
- "oslllo-svg2": "^0.3.0"
49
+ "mocha": "^9.1.3",
50
+ "svg2png-wasm-node-10": "^1.3.1"
54
51
  }
55
52
  }
@@ -3,13 +3,11 @@
3
3
  const Svg = require("./svg");
4
4
  const Loader = require("./loader");
5
5
  const Option = require("./option");
6
- const { JSDOM } = require("jsdom");
7
- const is = require("oslllo-validator");
8
6
  const Processor = require("./processor");
9
7
  const constants = require("./constants");
10
8
 
11
9
  const Potrace = function (image, options) {
12
- if (!is.instance(this, Potrace)) {
10
+ if (!(this instanceof Potrace)) {
13
11
  return new Potrace(image, options);
14
12
  }
15
13
  this.image = image;
@@ -17,7 +15,6 @@ const Potrace = function (image, options) {
17
15
  this.bitmap = undefined;
18
16
  this.pathlist = new Array();
19
17
  this.options = new Option(options);
20
- this.dom = new JSDOM("<!DOCTYPE html><html></html>", { resources: "usable" });
21
18
 
22
19
  return this;
23
20
  };
@@ -25,10 +22,9 @@ const Potrace = function (image, options) {
25
22
  Potrace.prototype = {
26
23
  trace: async function () {
27
24
  var info = this.options.all();
28
- var load = new Loader(this.dom);
25
+ var load = new Loader();
29
26
  this.image = await load.image(this.image);
30
- this.canvas = load.canvas(this.image);
31
- this.bitmap = load.bitmap(this.canvas, info);
27
+ this.bitmap = load.bitmap(this.image, info);
32
28
  this.bitmap.pathlist(this.pathlist);
33
29
  var process = new Processor(info, this.pathlist);
34
30
  process.init();
package/src/loader.js CHANGED
@@ -2,24 +2,15 @@
2
2
 
3
3
  const jimp = require("jimp");
4
4
  const error = require("./error");
5
- const is = require("oslllo-validator");
6
5
  const Bitmap = require("./types/Bitmap");
7
6
 
8
- const Loader = function (dom) {
9
- this.document = dom.window.document;
7
+ const Loader = function () {
10
8
  };
11
9
 
12
- const base64ToImage = (document, base64) => new Promise((resolve) => {
13
- var element = document.createElement("img");
14
- element.onload = () => {
15
- resolve(element);
16
- };
17
- element.src = base64;
18
- });
19
-
20
10
  Loader.prototype = {
21
- image: async function (image) {
22
- if (!is.buffer(image) && !is.string(image)) {
11
+ image: function (image) {
12
+ const valid = image instanceof Buffer || typeof image === 'string'
13
+ if (!valid) {
23
14
  var err = error.invalidParameterError(
24
15
  "image",
25
16
  "buffer or path to image",
@@ -27,38 +18,27 @@ Loader.prototype = {
27
18
  );
28
19
  throw err;
29
20
  }
30
- image = await jimp.read(image);
31
- var base64 = await image.getBase64Async(jimp.AUTO);
32
- var element = await base64ToImage(this.document, base64);
33
- return {
34
- file: image,
35
- base64: base64,
36
- element: element,
37
- };;
38
- },
39
- canvas: function (image) {
40
- var { element } = image;
41
- var canvas = this.document.createElement("canvas");
42
- canvas.width = element.width;
43
- canvas.height = element.height;
44
- var ctx = canvas.getContext("2d");
45
- ctx.drawImage(element, 0, 0);
46
-
47
- return canvas;
21
+ return jimp.read(image);
48
22
  },
49
- bitmap: function (canvas, info) {
50
- var ctx = canvas.getContext("2d");
51
- var bitmap = new Bitmap(canvas.width, canvas.height, info);
52
- var imageDataObject = ctx.getImageData(0, 0, bitmap.w, bitmap.h);
53
- var l = imageDataObject.data.length,
23
+ bitmap: function (image, info) {
24
+ var {
25
+ bitmap: {
26
+ data,
27
+ width,
28
+ height,
29
+ }
30
+ } = image;
31
+ var u8 = new Uint8ClampedArray(data.buffer)
32
+ var bitmap = new Bitmap(width, height, info);
33
+ var l = data.length,
54
34
  i,
55
35
  j,
56
36
  color;
57
37
  for (i = 0, j = 0; i < l; i += 4, j++) {
58
38
  color =
59
- 0.2126 * imageDataObject.data[i] +
60
- 0.7153 * imageDataObject.data[i + 1] +
61
- 0.0721 * imageDataObject.data[i + 2];
39
+ 0.2126 * data[i] +
40
+ 0.7153 * data[i + 1] +
41
+ 0.0721 * data[i + 2];
62
42
  bitmap.data[j] = color < 128 ? 1 : 0;
63
43
  }
64
44
 
package/src/option.js CHANGED
@@ -1,10 +1,8 @@
1
1
  "use strict";
2
2
 
3
- const error = require("./error");
4
- const is = require("oslllo-validator");
5
3
  const constants = require("./constants");
6
4
 
7
- const Option = function (options = {}) {
5
+ const Option = function (options) {
8
6
  this.data = {
9
7
  turnpolicy: constants.TURNPOLICY_MINORITY,
10
8
  turdsize: 2,
@@ -13,13 +11,8 @@ const Option = function (options = {}) {
13
11
  opttolerance: 0.2,
14
12
  svgSize: 1,
15
13
  opt_type: undefined,
14
+ ...options,
16
15
  };
17
- if (!is.object(options)) {
18
- throw error.invalidParameterError("options", "object", options);
19
- }
20
- if (!is.empty(options)) {
21
- this.update(options);
22
- }
23
16
 
24
17
  return this;
25
18
  };
@@ -28,27 +21,6 @@ Option.prototype = {
28
21
  all: function () {
29
22
  return this.data;
30
23
  },
31
- get: function (option) {
32
- if (!is.defined(option) || !is.string(option)) {
33
- throw error.invalidParameterError("option", "string", option);
34
- }
35
- var options = Object.keys(this.data);
36
- if (!options.includes(option)) {
37
- throw error.invalidParameterError(
38
- "setting",
39
- `one of ${options.toString()}`,
40
- option
41
- );
42
- }
43
- return this.data[option];
44
- },
45
- update: function (options) {
46
- for (var key in options) {
47
- if (options.hasOwnProperty(key)) {
48
- this.data[key] = options[key];
49
- }
50
- }
51
- },
52
24
  };
53
25
 
54
26
  module.exports = Option;