Skip to content

Commit 093b839

Browse files
committed
refactor(wasm): change the fontsBuffers option to fontBuffers
1 parent b490b7d commit 093b839

File tree

8 files changed

+45
-45
lines changed

8 files changed

+45
-45
lines changed

__test__/wasm.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ test('Set the background without alpha by hsla()', async (t) => {
206206
t.is(result.hasAlpha(), false)
207207
})
208208

209-
test('Load custom font(use fontsBuffers option)', async (t) => {
209+
test('Load custom font(use fontBuffers option)', async (t) => {
210210
const filePath = '../example/text.svg'
211211
const svg = await fs.readFile(join(__dirname, filePath))
212212
const fontBuffer = await fs.readFile(join(__dirname, '../example/SourceHanSerifCN-Light-subset.ttf'))
213213
const resvg = new Resvg(svg.toString('utf-8'), {
214214
font: {
215-
fontsBuffers: [fontBuffer], // Load custom fonts.
215+
fontBuffers: [fontBuffer], // Load custom fonts.
216216
},
217217
})
218218
const pngBuffer = resvg.render().asPng()
@@ -233,7 +233,7 @@ test('should be load custom font(no defaultFontFamily option)', async (t) => {
233233
const fontBuffer = await fs.readFile(join(__dirname, '../example/SourceHanSerifCN-Light-subset.ttf'))
234234
const resvg = new Resvg(svg, {
235235
font: {
236-
fontsBuffers: [fontBuffer],
236+
fontBuffers: [fontBuffer],
237237
// defaultFontFamily: 'Source Han Serif CN Light',
238238
},
239239
})
@@ -244,7 +244,7 @@ test('should be load custom font(no defaultFontFamily option)', async (t) => {
244244
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1726)
245245
})
246246

247-
test('should be load custom fontsBuffers(no defaultFontFamily option)', async (t) => {
247+
test('should be load custom fontBuffers(no defaultFontFamily option)', async (t) => {
248248
const svg = `
249249
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
250250
<text fill="blue" font-family="serif" font-size="120">
@@ -255,7 +255,7 @@ test('should be load custom fontsBuffers(no defaultFontFamily option)', async (t
255255
const fontBuffer = await fs.readFile(join(__dirname, '../example/SourceHanSerifCN-Light-subset.ttf'))
256256
const resvg = new Resvg(svg, {
257257
font: {
258-
fontsBuffers: [fontBuffer],
258+
fontBuffers: [fontBuffer],
259259
},
260260
})
261261
const pngData = resvg.render()
@@ -265,7 +265,7 @@ test('should be load custom fontsBuffers(no defaultFontFamily option)', async (t
265265
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1726)
266266
})
267267

268-
test('should be load custom multiple fontsBuffers', async (t) => {
268+
test('should be load custom multiple fontBuffers', async (t) => {
269269
const svg = `
270270
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="200" viewBox="0 0 500 200">
271271
<text fill="blue" font-size="60">
@@ -278,7 +278,7 @@ test('should be load custom multiple fontsBuffers', async (t) => {
278278
const pacificoBuffer = await fs.readFile(join(__dirname, './Pacifico-Regular.ttf'))
279279
const resvg = new Resvg(svg, {
280280
font: {
281-
fontsBuffers: [pacificoBuffer, fontBuffer],
281+
fontBuffers: [pacificoBuffer, fontBuffer],
282282
defaultFontFamily: ' Pacifico ', // Multiple spaces
283283
},
284284
})
@@ -421,7 +421,7 @@ test('should render using font buffer provided by options', async (t) => {
421421

422422
const options = {
423423
font: {
424-
fontsBuffers: [pacificoBuffer],
424+
fontBuffers: [pacificoBuffer],
425425
defaultFontFamily: 'non-existent-font-family',
426426
},
427427
}

js-binding.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function isMusl() {
1717
// For Node 10
1818
if (!process.report || typeof process.report.getReport !== 'function') {
1919
try {
20-
const lddPath = require('child_process').execSync('which ldd').toString().trim();
20+
const lddPath = require('child_process').execSync('which ldd').toString().trim()
2121
return readFileSync(lddPath, 'utf8').includes('musl')
2222
} catch (e) {
2323
return true

src/fonts.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
5555
#[cfg(target_arch = "wasm32")]
5656
pub fn load_wasm_fonts(
5757
font_options: &JsFontOptions,
58-
fonts_buffers: Option<js_sys::Array>,
58+
font_buffers: Option<js_sys::Array>,
5959
fontdb: &mut Database,
6060
) -> Result<(), js_sys::Error> {
61-
if let Some(ref fonts_buffers) = fonts_buffers {
62-
for font in fonts_buffers.values().into_iter() {
61+
if let Some(ref font_buffers) = font_buffers {
62+
for font in font_buffers.values().into_iter() {
6363
let raw_font = font?;
6464
let font_data = raw_font.dyn_into::<js_sys::Uint8Array>()?.to_vec();
6565
fontdb.load_font_data(font_data);
6666
}
6767
}
6868

69-
set_wasm_font_families(font_options, fontdb, fonts_buffers);
69+
set_wasm_font_families(font_options, fontdb, font_buffers);
7070

7171
Ok(())
7272
}
@@ -120,7 +120,7 @@ fn set_font_families(font_options: &JsFontOptions, fontdb: &mut Database) {
120120
fn set_wasm_font_families(
121121
font_options: &JsFontOptions,
122122
fontdb: &mut Database,
123-
fonts_buffers: Option<js_sys::Array>,
123+
font_buffers: Option<js_sys::Array>,
124124
) {
125125
let mut default_font_family = font_options.default_font_family.clone().trim().to_string();
126126

@@ -138,8 +138,8 @@ fn set_wasm_font_families(
138138
// 当 default_font_family 为空或系统无该字体时,尝试把 fontdb
139139
// 中字体列表的第一个字体设置为默认的字体。
140140
if default_font_family.is_empty() || fontdb_found_default_font_family.is_empty() {
141-
// fonts_buffers 选项不为空时, 从已加载的字体列表中获取第一个字体的 font family。
142-
if let Some(_fonts_buffers) = fonts_buffers {
141+
// font_buffers 选项不为空时, 从已加载的字体列表中获取第一个字体的 font family。
142+
if let Some(_font_buffers) = font_buffers {
143143
default_font_family = get_first_font_family_or_fallback(fontdb);
144144
}
145145
}

wasm-binding.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ export const Resvg = class extends _Resvg {
3131
...options,
3232
font: {
3333
...font,
34-
fontsBuffers: undefined,
34+
fontBuffers: undefined,
3535
},
3636
}
3737

38-
super(svg, JSON.stringify(serializableOptions), font.fontsBuffers)
38+
super(svg, JSON.stringify(serializableOptions), font.fontBuffers)
3939
} else {
4040
super(svg, JSON.stringify(options))
4141
}
4242
}
4343
}
4444

4545
function isCustomFontsOptions(value: SystemFontsOptions | CustomFontsOptions): value is CustomFontsOptions {
46-
return Object.prototype.hasOwnProperty.call(value, 'fontsBuffers')
46+
return Object.prototype.hasOwnProperty.call(value, 'fontBuffers')
4747
}

wasm/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export type FontOptions = {
7878
monospaceFamily?: string;
7979
};
8080
export type CustomFontsOptions = {
81-
fontsBuffers: Uint8Array[]; // A list of raw font files to load.
81+
fontBuffers: Uint8Array[]; // A list of raw font buffers to load.
8282
} & FontOptions;
8383
export type SystemFontsOptions = {
8484
loadSystemFonts?: boolean; // Default: true. if set to false, it will be faster.

wasm/index.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ function handleError(f, args) {
129129
wasm.__wbindgen_exn_store(addHeapObject(e));
130130
}
131131
}
132-
var BBox = class {
132+
var BBox = class _BBox {
133133
static __wrap(ptr) {
134-
const obj = Object.create(BBox.prototype);
134+
const obj = Object.create(_BBox.prototype);
135135
obj.ptr = ptr;
136136
return obj;
137137
}
@@ -197,9 +197,9 @@ var BBox = class {
197197
wasm.__wbg_set_bbox_height(this.ptr, arg0);
198198
}
199199
};
200-
var RenderedImage = class {
200+
var RenderedImage = class _RenderedImage {
201201
static __wrap(ptr) {
202-
const obj = Object.create(RenderedImage.prototype);
202+
const obj = Object.create(_RenderedImage.prototype);
203203
obj.ptr = ptr;
204204
return obj;
205205
}
@@ -256,9 +256,9 @@ var RenderedImage = class {
256256
return takeObject(ret);
257257
}
258258
};
259-
var Resvg = class {
259+
var Resvg = class _Resvg {
260260
static __wrap(ptr) {
261-
const obj = Object.create(Resvg.prototype);
261+
const obj = Object.create(_Resvg.prototype);
262262
obj.ptr = ptr;
263263
return obj;
264264
}
@@ -288,7 +288,7 @@ var Resvg = class {
288288
if (r2) {
289289
throw takeObject(r1);
290290
}
291-
return Resvg.__wrap(r0);
291+
return _Resvg.__wrap(r0);
292292
} finally {
293293
wasm.__wbindgen_add_to_stack_pointer(16);
294294
}
@@ -483,7 +483,7 @@ function getImports() {
483483
let result;
484484
try {
485485
result = getObject(arg0) instanceof Uint8Array;
486-
} catch (e) {
486+
} catch {
487487
result = false;
488488
}
489489
const ret = result;
@@ -561,21 +561,21 @@ var Resvg2 = class extends Resvg {
561561
constructor(svg, options) {
562562
if (!initialized)
563563
throw new Error("Wasm has not been initialized. Call `initWasm()` function.");
564-
const font = options == null ? void 0 : options.font;
564+
const font = options?.font;
565565
if (!!font && isCustomFontsOptions(font)) {
566566
const serializableOptions = {
567567
...options,
568568
font: {
569569
...font,
570-
fontsBuffers: void 0
570+
fontBuffers: void 0
571571
}
572572
};
573-
super(svg, JSON.stringify(serializableOptions), font.fontsBuffers);
573+
super(svg, JSON.stringify(serializableOptions), font.fontBuffers);
574574
} else {
575575
super(svg, JSON.stringify(options));
576576
}
577577
}
578578
};
579579
function isCustomFontsOptions(value) {
580-
return Object.prototype.hasOwnProperty.call(value, "fontsBuffers");
580+
return Object.prototype.hasOwnProperty.call(value, "fontBuffers");
581581
}

0 commit comments

Comments
 (0)