Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(wasm): change the fontsBuffers option to fontBuffers #255

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ test('Set the background without alpha by hsla()', async (t) => {
t.is(result.hasAlpha(), false)
})

test('Load custom font(use fontsBuffers option)', async (t) => {
test('Load custom font(use fontBuffers option)', async (t) => {
const filePath = '../example/text.svg'
const svg = await fs.readFile(join(__dirname, filePath))
const fontBuffer = await fs.readFile(join(__dirname, '../example/SourceHanSerifCN-Light-subset.ttf'))
const resvg = new Resvg(svg.toString('utf-8'), {
font: {
fontsBuffers: [fontBuffer], // Load custom fonts.
fontBuffers: [fontBuffer], // Load custom fonts.
},
})
const pngBuffer = resvg.render().asPng()
Expand All @@ -233,7 +233,7 @@ test('should be load custom font(no defaultFontFamily option)', async (t) => {
const fontBuffer = await fs.readFile(join(__dirname, '../example/SourceHanSerifCN-Light-subset.ttf'))
const resvg = new Resvg(svg, {
font: {
fontsBuffers: [fontBuffer],
fontBuffers: [fontBuffer],
// defaultFontFamily: 'Source Han Serif CN Light',
},
})
Expand All @@ -244,7 +244,7 @@ test('should be load custom font(no defaultFontFamily option)', async (t) => {
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1726)
})

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

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

const options = {
font: {
fontsBuffers: [pacificoBuffer],
fontBuffers: [pacificoBuffer],
defaultFontFamily: 'non-existent-font-family',
},
}
Expand Down
2 changes: 1 addition & 1 deletion js-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim();
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
Expand Down
14 changes: 7 additions & 7 deletions src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
#[cfg(target_arch = "wasm32")]
pub fn load_wasm_fonts(
font_options: &JsFontOptions,
fonts_buffers: Option<js_sys::Array>,
font_buffers: Option<js_sys::Array>,
fontdb: &mut Database,
) -> Result<(), js_sys::Error> {
if let Some(ref fonts_buffers) = fonts_buffers {
for font in fonts_buffers.values().into_iter() {
if let Some(ref font_buffers) = font_buffers {
for font in font_buffers.values().into_iter() {
let raw_font = font?;
let font_data = raw_font.dyn_into::<js_sys::Uint8Array>()?.to_vec();
fontdb.load_font_data(font_data);
}
}

set_wasm_font_families(font_options, fontdb, fonts_buffers);
set_wasm_font_families(font_options, fontdb, font_buffers);

Ok(())
}
Expand Down Expand Up @@ -120,7 +120,7 @@ fn set_font_families(font_options: &JsFontOptions, fontdb: &mut Database) {
fn set_wasm_font_families(
font_options: &JsFontOptions,
fontdb: &mut Database,
fonts_buffers: Option<js_sys::Array>,
font_buffers: Option<js_sys::Array>,
) {
let mut default_font_family = font_options.default_font_family.clone().trim().to_string();

Expand All @@ -138,8 +138,8 @@ fn set_wasm_font_families(
// 当 default_font_family 为空或系统无该字体时,尝试把 fontdb
// 中字体列表的第一个字体设置为默认的字体。
if default_font_family.is_empty() || fontdb_found_default_font_family.is_empty() {
// fonts_buffers 选项不为空时, 从已加载的字体列表中获取第一个字体的 font family。
if let Some(_fonts_buffers) = fonts_buffers {
// font_buffers 选项不为空时, 从已加载的字体列表中获取第一个字体的 font family。
if let Some(_font_buffers) = font_buffers {
default_font_family = get_first_font_family_or_fallback(fontdb);
}
}
Expand Down
6 changes: 3 additions & 3 deletions wasm-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ export const Resvg = class extends _Resvg {
...options,
font: {
...font,
fontsBuffers: undefined,
fontBuffers: undefined,
},
}

super(svg, JSON.stringify(serializableOptions), font.fontsBuffers)
super(svg, JSON.stringify(serializableOptions), font.fontBuffers)
} else {
super(svg, JSON.stringify(options))
}
}
}

function isCustomFontsOptions(value: SystemFontsOptions | CustomFontsOptions): value is CustomFontsOptions {
return Object.prototype.hasOwnProperty.call(value, 'fontsBuffers')
return Object.prototype.hasOwnProperty.call(value, 'fontBuffers')
}
2 changes: 1 addition & 1 deletion wasm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export type FontOptions = {
monospaceFamily?: string;
};
export type CustomFontsOptions = {
fontsBuffers: Uint8Array[]; // A list of raw font files to load.
fontBuffers: Uint8Array[]; // A list of raw font buffers to load.
} & FontOptions;
export type SystemFontsOptions = {
loadSystemFonts?: boolean; // Default: true. if set to false, it will be faster.
Expand Down
24 changes: 12 additions & 12 deletions wasm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
var BBox = class {
var BBox = class _BBox {
static __wrap(ptr) {
const obj = Object.create(BBox.prototype);
const obj = Object.create(_BBox.prototype);
obj.ptr = ptr;
return obj;
}
Expand Down Expand Up @@ -197,9 +197,9 @@ var BBox = class {
wasm.__wbg_set_bbox_height(this.ptr, arg0);
}
};
var RenderedImage = class {
var RenderedImage = class _RenderedImage {
static __wrap(ptr) {
const obj = Object.create(RenderedImage.prototype);
const obj = Object.create(_RenderedImage.prototype);
obj.ptr = ptr;
return obj;
}
Expand Down Expand Up @@ -256,9 +256,9 @@ var RenderedImage = class {
return takeObject(ret);
}
};
var Resvg = class {
var Resvg = class _Resvg {
static __wrap(ptr) {
const obj = Object.create(Resvg.prototype);
const obj = Object.create(_Resvg.prototype);
obj.ptr = ptr;
return obj;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ var Resvg = class {
if (r2) {
throw takeObject(r1);
}
return Resvg.__wrap(r0);
return _Resvg.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
Expand Down Expand Up @@ -483,7 +483,7 @@ function getImports() {
let result;
try {
result = getObject(arg0) instanceof Uint8Array;
} catch (e) {
} catch {
result = false;
}
const ret = result;
Expand Down Expand Up @@ -561,21 +561,21 @@ var Resvg2 = class extends Resvg {
constructor(svg, options) {
if (!initialized)
throw new Error("Wasm has not been initialized. Call `initWasm()` function.");
const font = options == null ? void 0 : options.font;
const font = options?.font;
if (!!font && isCustomFontsOptions(font)) {
const serializableOptions = {
...options,
font: {
...font,
fontsBuffers: void 0
fontBuffers: void 0
}
};
super(svg, JSON.stringify(serializableOptions), font.fontsBuffers);
super(svg, JSON.stringify(serializableOptions), font.fontBuffers);
} else {
super(svg, JSON.stringify(options));
}
}
};
function isCustomFontsOptions(value) {
return Object.prototype.hasOwnProperty.call(value, "fontsBuffers");
return Object.prototype.hasOwnProperty.call(value, "fontBuffers");
}
Loading
Loading