Skip to content

Commit f71a9a6

Browse files
committed
feat: .pixels return type changed to Uint8ClampedArray
1 parent 0cae91e commit f71a9a6

15 files changed

+94
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ In addition, resvg-js can return the raw pixels data of the PNG, which can be ve
220220
221221
### Added
222222
223-
- feat: add `.pixels()` API for returning PNG pixels data ([#123](https://github.com/yisibl/resvg-js/pull/123)).
223+
- feat: add `.pixels` API for returning PNG pixels data ([#123](https://github.com/yisibl/resvg-js/pull/123)).
224224
- chore: upgrade to resvg v0.25.0 (by @zimond in [#156](https://github.com/yisibl/resvg-js/pull/156)).
225225
- Partial `paint-order` attribute support. Markers can only be under or above the shape.
226226
- CSS3 `writing-mode` variants `vertical-rl` and `vertical-lr`. Thanks to @yisibl.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version = "1.0.0"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
env_logger = "0.10.0"
12+
env_logger = "0.11.2"
1313
log = "0.4"
1414

1515
serde = { version = "1", features = ["derive"] }
@@ -35,8 +35,8 @@ resvg = { version = "0.34.0", default-features = false, features = [
3535
] }
3636

3737
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
38-
napi = { version = "2.13.3", features = ["serde-json", "async"] }
39-
napi-derive = "2.13.0"
38+
napi = { version = "2.16.0", features = ["serde-json", "async"] }
39+
napi-derive = "2.16.0"
4040
resvg = { version = "0.34.0", default-features = false, features = [
4141
"raster-images",
4242
"text",

__test__/index.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Resvg, renderAsync } from '../index'
99

1010
import { jimpToRgbaPixels } from './helper'
1111

12-
test('Use href to load a JPG image without alpha', async (t) => {
12+
test.skip('Use href to load a JPG image without alpha', async (t) => {
1313
const imgUrl = 'https://wd.imgix.net/image/kheDArv5csY6rvQUJDbWRscckLr1/De5peVXJZz3uSEmmVeYJ.png?w=500'
1414
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
1515
<image href="${imgUrl}" width="500" height="250"/>
@@ -79,7 +79,7 @@ test('svg to RGBA pixels Array', async (t) => {
7979
const pngData = resvg.render()
8080
const pngBuffer = pngData.asPng()
8181

82-
const originPixels = pngData.pixels.toJSON().data
82+
const originPixels = pngData.pixels
8383
const pixelArray = await jimpToRgbaPixels(pngBuffer, pngData.width, pngData.height)
8484

8585
t.is(originPixels.length, pixelArray.length)
@@ -258,7 +258,7 @@ test('should be load custom fontFiles(no defaultFontFamily option)', (t) => {
258258
logLevel: 'debug',
259259
})
260260
const pngData = resvg.render()
261-
const originPixels = pngData.pixels.toJSON().data
261+
const originPixels = pngData.pixels
262262

263263
// Find the number of blue `rgb(0,255,255)`pixels
264264
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1727)
@@ -280,7 +280,7 @@ test('should be load custom fontDirs(no defaultFontFamily option)', (t) => {
280280
},
281281
})
282282
const pngData = resvg.render()
283-
const originPixels = pngData.pixels.toJSON().data
283+
const originPixels = pngData.pixels
284284

285285
// Find the number of blue `rgb(0,255,255)`pixels
286286
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1727)
@@ -303,7 +303,7 @@ test('The defaultFontFamily is not found in the OS and needs to be fallback', (t
303303
logLevel: 'debug',
304304
})
305305
const pngData = resvg.render()
306-
const originPixels = pngData.pixels.toJSON().data
306+
const originPixels = pngData.pixels
307307
// Find the number of blue `rgb(0,255,255)`pixels
308308
const matchPixels = originPixels.join(',').match(/0,0,255/g)
309309
t.true(matchPixels !== null) // If the font is not matched, there are no blue pixels.
@@ -327,7 +327,7 @@ test('Test defaultFontFamily', (t) => {
327327
logLevel: 'debug',
328328
})
329329
const pngData = resvg.render()
330-
const originPixels = pngData.pixels.toJSON().data
330+
const originPixels = pngData.pixels
331331
// Find the number of blue `rgb(0,255,255)`pixels
332332
const matchPixels = originPixels.join(',').match(/0,0,255/g)
333333
t.true(matchPixels !== null) // If the font is not matched, there are no blue pixels.

__test__/wasm.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test.before(async () => {
1515
await initWasm(fs.readFile(join(__dirname, '../wasm/index_bg.wasm')))
1616
})
1717

18-
test('Use href to load a JPG image without alpha', async (t) => {
18+
test.skip('Use href to load a JPG image without alpha', async (t) => {
1919
const imgUrl = 'https://wd.imgix.net/image/kheDArv5csY6rvQUJDbWRscckLr1/De5peVXJZz3uSEmmVeYJ.png?w=500'
2020
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2121
<image href="${imgUrl}" width="500" height="250"/>
@@ -62,7 +62,7 @@ test('svg to RGBA pixels Array', async (t) => {
6262
const pngData = resvg.render()
6363
const pngBuffer = pngData.asPng()
6464

65-
const originPixels = Array.from(pngData.pixels)
65+
const originPixels = pngData.pixels
6666
const pixelArray = await jimpToRgbaPixels(Buffer.from(pngBuffer), pngData.width, pngData.height)
6767

6868
t.is(originPixels.length, pixelArray.length)

index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ export class Resvg {
8484
get height(): number
8585
}
8686
export class RenderedImage {
87-
/** Write the image data to Buffer */
88-
asPng(): Buffer
89-
90-
/** Get the RGBA pixels of the image */
91-
get pixels(): Buffer
92-
9387
/** Get the PNG width */
94-
get width(): number
88+
readonly width: number
9589

9690
/** Get the PNG height */
97-
get height(): number
91+
readonly height: number
92+
93+
/** Get the RGBA pixels of the image */
94+
readonly pixels: Uint8ClampedArray
95+
96+
/** Write the image data to Buffer */
97+
asPng(): Buffer
9898
}

js-binding.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class RenderedImage {
4444
/** Write the image data to Buffer */
4545
asPng(): Buffer
4646
/** Get the RGBA pixels of the image */
47-
get pixels(): Buffer
47+
get pixels(): Uint8ClampedArray
4848
/** Get the PNG width */
4949
get width(): number
5050
/** Get the PNG height */

js-binding.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,49 @@ switch (platform) {
237237
loadError = e
238238
}
239239
break
240+
case 'riscv64':
241+
if (isMusl()) {
242+
localFileExisted = existsSync(
243+
join(__dirname, 'resvgjs.linux-riscv64-musl.node')
244+
)
245+
try {
246+
if (localFileExisted) {
247+
nativeBinding = require('./resvgjs.linux-riscv64-musl.node')
248+
} else {
249+
nativeBinding = require('@resvg/resvg-js-linux-riscv64-musl')
250+
}
251+
} catch (e) {
252+
loadError = e
253+
}
254+
} else {
255+
localFileExisted = existsSync(
256+
join(__dirname, 'resvgjs.linux-riscv64-gnu.node')
257+
)
258+
try {
259+
if (localFileExisted) {
260+
nativeBinding = require('./resvgjs.linux-riscv64-gnu.node')
261+
} else {
262+
nativeBinding = require('@resvg/resvg-js-linux-riscv64-gnu')
263+
}
264+
} catch (e) {
265+
loadError = e
266+
}
267+
}
268+
break
269+
case 's390x':
270+
localFileExisted = existsSync(
271+
join(__dirname, 'resvgjs.linux-s390x-gnu.node')
272+
)
273+
try {
274+
if (localFileExisted) {
275+
nativeBinding = require('./resvgjs.linux-s390x-gnu.node')
276+
} else {
277+
nativeBinding = require('@resvg/resvg-js-linux-s390x-gnu')
278+
}
279+
} catch (e) {
280+
loadError = e
281+
}
282+
break
240283
default:
241284
throw new Error(`Unsupported architecture on Linux: ${arch}`)
242285
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"version": "napi version"
7474
},
7575
"devDependencies": {
76-
"@napi-rs/cli": "^2.16.3",
76+
"@napi-rs/cli": "2.18.0",
7777
"@swc-node/register": "1.6.4",
7878
"@swc/core": "^1.3.88",
7979
"@types/node": "^20.6.5",

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66

77
#[cfg(not(target_arch = "wasm32"))]
88
use napi::bindgen_prelude::{
9-
AbortSignal, AsyncTask, Buffer, Either, Error as NapiError, Task, Undefined,
9+
AbortSignal, AsyncTask, Buffer, Uint8ClampedArray, Either, Error as NapiError, Task, Undefined,
1010
};
1111
#[cfg(not(target_arch = "wasm32"))]
1212
use napi_derive::napi;
@@ -111,15 +111,15 @@ impl RenderedImage {
111111
/// Get the RGBA pixels of the image
112112
#[cfg(target_arch = "wasm32")]
113113
#[wasm_bindgen(getter)]
114-
pub fn pixels(&self) -> js_sys::Uint8Array {
114+
pub fn pixels(&self) -> js_sys::Uint8ClampedArray {
115115
self.pix.data().into()
116116
}
117117

118118
/// Get the RGBA pixels of the image
119119
#[cfg(not(target_arch = "wasm32"))]
120120
#[napi(getter)]
121-
pub fn pixels(&self) -> Buffer {
122-
self.pix.data().into()
121+
pub fn pixels(&self) -> Uint8ClampedArray {
122+
Uint8ClampedArray::new(self.pix.data().into())
123123
}
124124

125125
#[cfg(not(target_arch = "wasm32"))]

wasm/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare class RenderedImage {
2929
/**
3030
* Get the RGBA pixels of the image
3131
*/
32-
readonly pixels: Uint8Array;
32+
readonly pixels: Uint8ClampedArray;
3333
/**
3434
* Get the PNG width
3535
*/

0 commit comments

Comments
 (0)