@napi-rs/canvas 0.1.26 → 0.1.27

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/README.md CHANGED
@@ -76,7 +76,7 @@ ctx.closePath()
76
76
  ctx.stroke()
77
77
 
78
78
  async function main() {
79
- const pngData = await canvas.encode('png') // JPEG and WebP is also supported
79
+ const pngData = await canvas.encode('png') // JPEG, AVIF and WebP are also supported
80
80
  // encoding in libuv thread pool, non-blocking
81
81
  await promises.writeFile(join(__dirname, 'simple.png'), pngData)
82
82
  }
package/index.d.ts CHANGED
@@ -360,7 +360,7 @@ export enum PathOp {
360
360
  Difference = 0, // subtract the op path from the first path
361
361
  Intersect = 1, // intersect the two paths
362
362
  Union = 2, // union (inclusive-or) the two paths
363
- XOR = 3, // exclusive-or the two paths
363
+ Xor = 3, // exclusive-or the two paths
364
364
  ReverseDifference = 4, // subtract the first path from the op path
365
365
  }
366
366
 
package/index.js CHANGED
@@ -11,95 +11,35 @@ const {
11
11
  Image,
12
12
  CanvasPattern,
13
13
  GlobalFonts,
14
- convertSVGTextToPath: _convertSVGTextToPath,
14
+ PathOp,
15
+ FillType,
16
+ StrokeJoin,
17
+ StrokeCap,
18
+ convertSVGTextToPath,
15
19
  } = require('./js-binding')
16
20
 
17
21
  const { DOMPoint, DOMMatrix, DOMRect } = require('./geometry')
18
22
 
19
23
  const loadImage = require('./load-image')
20
24
 
21
- const StrokeJoin = {
22
- Miter: 0,
23
- Round: 1,
24
- Bevel: 2,
25
- }
26
-
27
- const StrokeCap = {
28
- Butt: 0,
29
- Round: 1,
30
- Square: 2,
31
- }
32
-
33
- const PathOp = {
34
- Difference: 0, // subtract the op path from the first path
35
- Intersect: 1, // intersect the two paths
36
- Union: 2, // union (inclusive-or) the two paths
37
- XOR: 3, // exclusive-or the two paths
38
- ReverseDifference: 4, // subtract the first path from the op path
39
- }
40
-
41
- const FillType = {
42
- Winding: 0,
43
- EvenOdd: 1,
44
- InverseWinding: 2,
45
- InverseEvenOdd: 3,
46
- }
47
-
48
25
  const SvgExportFlag = {
49
26
  ConvertTextToPaths: 0x01,
50
27
  NoPrettyXML: 0x02,
51
28
  RelativePathEncoding: 0x04,
52
29
  }
53
30
 
54
- const GlobalFontsSingleton = new GlobalFonts()
55
- let FamilyNamesSet = JSON.parse(GlobalFontsSingleton._families)
56
-
57
31
  // eslint-disable-next-line sonarjs/no-unused-collection
58
32
  const Fonts = []
59
33
 
60
- Object.defineProperty(GlobalFontsSingleton, 'register', {
61
- value: function register(fontData, nameAlias = '') {
62
- const result = GlobalFontsSingleton._register(fontData, nameAlias)
63
- FamilyNamesSet = JSON.parse(GlobalFontsSingleton._families)
64
- Fonts.push(fontData)
65
- return result
66
- },
67
- configurable: false,
68
- enumerable: false,
69
- writable: false,
70
- })
71
-
72
- Object.defineProperty(GlobalFontsSingleton, 'registerFromPath', {
73
- value: function registerFromPath(path, nameAlias = '') {
74
- const result = GlobalFontsSingleton._registerFromPath(path, nameAlias)
75
- FamilyNamesSet = JSON.parse(GlobalFontsSingleton._families)
76
- return result
77
- },
78
- configurable: false,
79
- enumerable: false,
80
- writable: false,
81
- })
82
-
83
- Object.defineProperty(GlobalFontsSingleton, 'loadFontsFromDir', {
84
- value: function loadFontsFromDir(path) {
85
- const result = GlobalFontsSingleton._loadFontsFromDir(path)
86
- FamilyNamesSet = JSON.parse(GlobalFontsSingleton._families)
87
- return result
88
- },
89
- configurable: false,
90
- enumerable: false,
91
- writable: false,
92
- })
93
-
94
- Object.defineProperty(GlobalFontsSingleton, 'families', {
34
+ Object.defineProperty(GlobalFonts, 'families', {
95
35
  get: function () {
96
- return FamilyNamesSet
36
+ return JSON.parse(GlobalFonts.getFamilies())
97
37
  },
98
38
  })
99
39
 
100
- Object.defineProperty(GlobalFontsSingleton, 'has', {
40
+ Object.defineProperty(GlobalFonts, 'has', {
101
41
  value: function has(name) {
102
- return !!FamilyNamesSet.find(({ family }) => family === name)
42
+ return !!JSON.parse(GlobalFonts.getFamilies()).find(({ family }) => family === name)
103
43
  },
104
44
  configurable: false,
105
45
  enumerable: false,
@@ -129,27 +69,6 @@ CanvasRenderingContext2D.prototype.getImageData = function getImageData(x, y, w,
129
69
  return new ImageData(data, w, h)
130
70
  }
131
71
 
132
- Path2D.prototype.stroke = function stroke(strokeOptions = {}) {
133
- const width = typeof strokeOptions.width === 'undefined' ? 1 : strokeOptions.width
134
- const miterLimit = typeof strokeOptions.miterLimit === 'undefined' ? 4 : strokeOptions.miterLimit
135
- const join = typeof strokeOptions.join === 'undefined' ? StrokeJoin.Miter : strokeOptions.join
136
- const cap = typeof strokeOptions.cap === 'undefined' ? StrokeCap.Butt : strokeOptions.cap
137
-
138
- return this._stroke(width, miterLimit, join, cap)
139
- }
140
-
141
- Path2D.prototype.getFillTypeString = function getFillTypeString() {
142
- const fillType = this.getFillType()
143
-
144
- if (fillType === FillType.Winding) {
145
- return 'nonzero'
146
- } else if (fillType === FillType.EvenOdd) {
147
- return 'evenodd'
148
- } else {
149
- return 'nonzero' // default
150
- }
151
- }
152
-
153
72
  function createCanvas(width, height, flag) {
154
73
  const isSvgBackend = typeof flag !== 'undefined'
155
74
  const canvasElement = isSvgBackend ? new SVGCanvas(width, height) : new CanvasElement(width, height)
@@ -163,8 +82,8 @@ function createCanvas(width, height, flag) {
163
82
  ctx = ctx
164
83
  ? ctx
165
84
  : isSvgBackend
166
- ? new CanvasRenderingContext2D(this.width, this.height, GlobalFontsSingleton, attrs.colorSpace, flag)
167
- : new CanvasRenderingContext2D(this.width, this.height, GlobalFontsSingleton, attrs.colorSpace)
85
+ ? new CanvasRenderingContext2D(this.width, this.height, attrs.colorSpace, flag)
86
+ : new CanvasRenderingContext2D(this.width, this.height, attrs.colorSpace)
168
87
  createContext(ctx, this.width, this.height, attrs)
169
88
 
170
89
  // napi can not define writable: true but enumerable: false property
@@ -258,26 +177,21 @@ class Canvas {
258
177
  }
259
178
 
260
179
  if (!process.env.DISABLE_SYSTEM_FONTS_LOAD) {
261
- GlobalFontsSingleton.loadSystemFonts()
180
+ GlobalFonts.loadSystemFonts()
262
181
  const platformName = platform()
263
182
  const homedirPath = homedir()
264
183
  switch (platformName) {
265
184
  case 'win32':
266
- GlobalFontsSingleton.loadFontsFromDir(join(homedirPath, 'AppData', 'Local', 'Microsoft', 'Windows', 'Fonts'))
185
+ GlobalFonts.loadFontsFromDir(join(homedirPath, 'AppData', 'Local', 'Microsoft', 'Windows', 'Fonts'))
267
186
  break
268
187
  case 'darwin':
269
- GlobalFontsSingleton.loadFontsFromDir(join(homedirPath, 'Library', 'Fonts'))
188
+ GlobalFonts.loadFontsFromDir(join(homedirPath, 'Library', 'Fonts'))
270
189
  break
271
190
  case 'linux':
272
- GlobalFontsSingleton.loadFontsFromDir(join('usr', 'local', 'share', 'fonts'))
273
- GlobalFontsSingleton.loadFontsFromDir(join(homedirPath, '.fonts'))
191
+ GlobalFonts.loadFontsFromDir(join('usr', 'local', 'share', 'fonts'))
192
+ GlobalFonts.loadFontsFromDir(join(homedirPath, '.fonts'))
274
193
  break
275
194
  }
276
- FamilyNamesSet = JSON.parse(GlobalFontsSingleton._families)
277
- }
278
-
279
- function convertSVGTextToPath(input) {
280
- return _convertSVGTextToPath(Buffer.isBuffer(input) ? input : Buffer.from(input), GlobalFontsSingleton)
281
195
  }
282
196
 
283
197
  module.exports = {
@@ -291,7 +205,7 @@ module.exports = {
291
205
  StrokeCap,
292
206
  StrokeJoin,
293
207
  SvgExportFlag,
294
- GlobalFonts: GlobalFontsSingleton,
208
+ GlobalFonts: GlobalFonts,
295
209
  convertSVGTextToPath,
296
210
  DOMPoint,
297
211
  DOMMatrix,
package/js-binding.js CHANGED
@@ -200,21 +200,29 @@ const {
200
200
  CanvasElement,
201
201
  createContext,
202
202
  SVGCanvas,
203
- Path2D,
203
+ Path,
204
204
  ImageData,
205
205
  Image,
206
206
  CanvasPattern,
207
207
  GlobalFonts,
208
208
  convertSVGTextToPath,
209
+ PathOp,
210
+ FillType,
211
+ StrokeJoin,
212
+ StrokeCap,
209
213
  } = nativeBinding
210
214
 
211
215
  module.exports.CanvasRenderingContext2D = CanvasRenderingContext2D
212
216
  module.exports.CanvasElement = CanvasElement
213
217
  module.exports.createContext = createContext
214
218
  module.exports.SVGCanvas = SVGCanvas
215
- module.exports.Path2D = Path2D
219
+ module.exports.Path2D = Path
216
220
  module.exports.ImageData = ImageData
217
221
  module.exports.Image = Image
218
222
  module.exports.CanvasPattern = CanvasPattern
219
223
  module.exports.GlobalFonts = GlobalFonts
220
224
  module.exports.convertSVGTextToPath = convertSVGTextToPath
225
+ module.exports.PathOp = PathOp
226
+ module.exports.FillType = FillType
227
+ module.exports.StrokeJoin = StrokeJoin
228
+ module.exports.StrokeCap = StrokeCap
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@napi-rs/canvas",
3
- "version": "0.1.26",
3
+ "version": "0.1.27",
4
4
  "description": "Canvas for Node.js with skia backend",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/Brooooooklyn/canvas.git",
@@ -83,7 +83,7 @@
83
83
  "eslint-config-prettier": "^8.5.0",
84
84
  "eslint-plugin-import": "^2.26.0",
85
85
  "eslint-plugin-prettier": "^4.0.0",
86
- "eslint-plugin-sonarjs": "^0.14.0",
86
+ "eslint-plugin-sonarjs": "^0.15.0",
87
87
  "husky": "^8.0.1",
88
88
  "lint-staged": "^13.0.0",
89
89
  "lodash": "^4.17.21",
@@ -130,14 +130,14 @@
130
130
  },
131
131
  "packageManager": "yarn@3.2.2",
132
132
  "optionalDependencies": {
133
- "@napi-rs/canvas-win32-x64-msvc": "0.1.26",
134
- "@napi-rs/canvas-darwin-x64": "0.1.26",
135
- "@napi-rs/canvas-linux-x64-gnu": "0.1.26",
136
- "@napi-rs/canvas-linux-arm-gnueabihf": "0.1.26",
137
- "@napi-rs/canvas-linux-x64-musl": "0.1.26",
138
- "@napi-rs/canvas-linux-arm64-gnu": "0.1.26",
139
- "@napi-rs/canvas-linux-arm64-musl": "0.1.26",
140
- "@napi-rs/canvas-darwin-arm64": "0.1.26",
141
- "@napi-rs/canvas-android-arm64": "0.1.26"
133
+ "@napi-rs/canvas-win32-x64-msvc": "0.1.27",
134
+ "@napi-rs/canvas-darwin-x64": "0.1.27",
135
+ "@napi-rs/canvas-linux-x64-gnu": "0.1.27",
136
+ "@napi-rs/canvas-linux-arm-gnueabihf": "0.1.27",
137
+ "@napi-rs/canvas-linux-x64-musl": "0.1.27",
138
+ "@napi-rs/canvas-linux-arm64-gnu": "0.1.27",
139
+ "@napi-rs/canvas-linux-arm64-musl": "0.1.27",
140
+ "@napi-rs/canvas-darwin-arm64": "0.1.27",
141
+ "@napi-rs/canvas-android-arm64": "0.1.27"
142
142
  }
143
143
  }