diff --git a/pkg/fast_qr.d.ts b/pkg/fast_qr.d.ts index 772f70e..30f02d2 100644 --- a/pkg/fast_qr.d.ts +++ b/pkg/fast_qr.d.ts @@ -66,10 +66,10 @@ export class SvgOptions { free(): void; /** * Updates the shape of the QRCode modules. -* @param {number} shape +* @param {Shape} shape * @returns {SvgOptions} */ - shape(shape: number): SvgOptions; + shape(shape: Shape): SvgOptions; /** * Updates the module color of the QRCode. Tales a string in the format `#RRGGBB[AA]`. * @param {string} module_color @@ -102,10 +102,10 @@ export class SvgOptions { image_background_color(image_background_color: string): SvgOptions; /** * Updates the shape of the image background. Takes an convert::ImageBackgroundShape. -* @param {number} image_background_shape +* @param {ImageBackgroundShape} image_background_shape * @returns {SvgOptions} */ - image_background_shape(image_background_shape: number): SvgOptions; + image_background_shape(image_background_shape: ImageBackgroundShape): SvgOptions; /** * Updates the size of the image. Takes a size and a gap (unit being module size). * @param {number} size @@ -128,6 +128,7 @@ export class SvgOptions { export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; export interface InitOutput { + readonly memory: WebAssembly.Memory; readonly qr: (a: number, b: number, c: number) => void; readonly __wbg_svgoptions_free: (a: number) => void; readonly svgoptions_shape: (a: number, b: number) => number; @@ -141,13 +142,10 @@ export interface InitOutput { readonly svgoptions_image_position: (a: number, b: number, c: number) => number; readonly svgoptions_new: () => number; readonly qr_svg: (a: number, b: number, c: number, d: number) => void; - readonly memory: WebAssembly.Memory; readonly __wbindgen_add_to_stack_pointer: (a: number) => number; readonly __wbindgen_malloc: (a: number, b: number) => number; readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; readonly __wbindgen_free: (a: number, b: number, c: number) => void; - readonly __wbindgen_thread_destroy: (a: number, b: number) => void; - readonly __wbindgen_start: () => void; } export type SyncInitInput = BufferSource | WebAssembly.Module; @@ -156,19 +154,17 @@ export type SyncInitInput = BufferSource | WebAssembly.Module; * a precompiled `WebAssembly.Module`. * * @param {SyncInitInput} module -* @param {WebAssembly.Memory} maybe_memory * * @returns {InitOutput} */ -export function initSync(module: SyncInitInput, maybe_memory?: WebAssembly.Memory): InitOutput; +export function initSync(module: SyncInitInput): InitOutput; /** * If `module_or_path` is {RequestInfo} or {URL}, makes a request and * for everything else, calls `WebAssembly.instantiate` directly. * * @param {InitInput | Promise} module_or_path -* @param {WebAssembly.Memory} maybe_memory * * @returns {Promise} */ -export default function __wbg_init (module_or_path?: InitInput | Promise, maybe_memory?: WebAssembly.Memory): Promise; +export default function __wbg_init (module_or_path?: InitInput | Promise): Promise; diff --git a/pkg/fast_qr.js b/pkg/fast_qr.js index 4acf35c..055fa75 100644 --- a/pkg/fast_qr.js +++ b/pkg/fast_qr.js @@ -7,7 +7,7 @@ if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; let cachedUint8Memory0 = null; function getUint8Memory0() { - if (cachedUint8Memory0 === null || cachedUint8Memory0.buffer !== wasm.memory.buffer) { + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8Memory0; @@ -15,21 +15,25 @@ function getUint8Memory0() { function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8Memory0().slice(ptr, ptr + len)); + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); } let WASM_VECTOR_LEN = 0; const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); -const encodeString = function (arg, view) { +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; -}; +}); function passStringToWasm0(arg, malloc, realloc) { @@ -63,6 +67,7 @@ function passStringToWasm0(arg, malloc, realloc) { const ret = encodeString(arg, view); offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; } WASM_VECTOR_LEN = offset; @@ -72,7 +77,7 @@ function passStringToWasm0(arg, malloc, realloc) { let cachedInt32Memory0 = null; function getInt32Memory0() { - if (cachedInt32Memory0 === null || cachedInt32Memory0.buffer !== wasm.memory.buffer) { + if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); } return cachedInt32Memory0; @@ -96,7 +101,7 @@ export function qr(content) { var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var v2 = getArrayU8FromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 1); + wasm.__wbindgen_free(r0, r1 * 1, 1); return v2; } finally { wasm.__wbindgen_add_to_stack_pointer(16); @@ -106,7 +111,7 @@ export function qr(content) { let cachedFloat64Memory0 = null; function getFloat64Memory0() { - if (cachedFloat64Memory0 === null || cachedFloat64Memory0.buffer !== wasm.memory.buffer) { + if (cachedFloat64Memory0 === null || cachedFloat64Memory0.byteLength === 0) { cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer); } return cachedFloat64Memory0; @@ -196,6 +201,10 @@ Circle:1,"1":"Circle", * Rounded square shape */ RoundedSquare:2,"2":"RoundedSquare", }); + +const SvgOptionsFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_svgoptions_free(ptr >>> 0)); /** * Configuration for the SVG output. */ @@ -205,14 +214,14 @@ export class SvgOptions { ptr = ptr >>> 0; const obj = Object.create(SvgOptions.prototype); obj.__wbg_ptr = ptr; - + SvgOptionsFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; - + SvgOptionsFinalization.unregister(this); return ptr; } @@ -222,7 +231,7 @@ export class SvgOptions { } /** * Updates the shape of the QRCode modules. - * @param {number} shape + * @param {Shape} shape * @returns {SvgOptions} */ shape(shape) { @@ -290,7 +299,7 @@ export class SvgOptions { } /** * Updates the shape of the image background. Takes an convert::ImageBackgroundShape. - * @param {number} image_background_shape + * @param {ImageBackgroundShape} image_background_shape * @returns {SvgOptions} */ image_background_shape(image_background_shape) { @@ -326,7 +335,8 @@ export class SvgOptions { */ constructor() { const ret = wasm.svgoptions_new(); - return SvgOptions.__wrap(ret); + this.__wbg_ptr = ret >>> 0; + return this; } } @@ -372,7 +382,7 @@ function __wbg_get_imports() { } function __wbg_init_memory(imports, maybe_memory) { - imports.wbg.memory = maybe_memory || new WebAssembly.Memory({initial:18,maximum:65536,shared:true}); + } function __wbg_finalize_init(instance, module) { @@ -382,16 +392,16 @@ function __wbg_finalize_init(instance, module) { cachedInt32Memory0 = null; cachedUint8Memory0 = null; - wasm.__wbindgen_start(); + return wasm; } -function initSync(module, maybe_memory) { +function initSync(module) { if (wasm !== undefined) return wasm; const imports = __wbg_get_imports(); - __wbg_init_memory(imports, maybe_memory); + __wbg_init_memory(imports); if (!(module instanceof WebAssembly.Module)) { module = new WebAssembly.Module(module); @@ -402,7 +412,7 @@ function initSync(module, maybe_memory) { return __wbg_finalize_init(instance, module); } -async function __wbg_init(input, maybe_memory) { +async function __wbg_init(input) { if (wasm !== undefined) return wasm; if (typeof input === 'undefined') { @@ -414,7 +424,7 @@ async function __wbg_init(input, maybe_memory) { input = fetch(input); } - __wbg_init_memory(imports, maybe_memory); + __wbg_init_memory(imports); const { instance, module } = await __wbg_load(await input, imports); diff --git a/pkg/fast_qr_bg.wasm.d.ts b/pkg/fast_qr_bg.wasm.d.ts index c6e9a11..235d895 100644 --- a/pkg/fast_qr_bg.wasm.d.ts +++ b/pkg/fast_qr_bg.wasm.d.ts @@ -1,5 +1,6 @@ /* tslint:disable */ /* eslint-disable */ +export const memory: WebAssembly.Memory; export function qr(a: number, b: number, c: number): void; export function __wbg_svgoptions_free(a: number): void; export function svgoptions_shape(a: number, b: number): number; @@ -13,10 +14,7 @@ export function svgoptions_image_size(a: number, b: number, c: number): number; export function svgoptions_image_position(a: number, b: number, c: number): number; export function svgoptions_new(): number; export function qr_svg(a: number, b: number, c: number, d: number): void; -export const memory: WebAssembly.Memory; export function __wbindgen_add_to_stack_pointer(a: number): number; export function __wbindgen_malloc(a: number, b: number): number; export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number; export function __wbindgen_free(a: number, b: number, c: number): void; -export function __wbindgen_thread_destroy(a: number, b: number): void; -export function __wbindgen_start(): void; diff --git a/src/convert/image.rs b/src/convert/image.rs index 629d0f7..d7281ac 100644 --- a/src/convert/image.rs +++ b/src/convert/image.rs @@ -117,8 +117,13 @@ impl Builder for ImageBuilder { self } - fn image_size(&mut self, image_size: f64, gap: f64) -> &mut Self { - self.svg_builder.image_size(image_size, gap); + fn image_size(&mut self, image_size: f64) -> &mut Self { + self.svg_builder.image_size(image_size); + self + } + + fn image_gap(&mut self, gap: f64) -> &mut Self { + self.svg_builder.image_gap(gap); self } diff --git a/src/convert/mod.rs b/src/convert/mod.rs index 8517faf..3247503 100644 --- a/src/convert/mod.rs +++ b/src/convert/mod.rs @@ -337,7 +337,9 @@ pub trait Builder { -> &mut Self; /// Updates the image size and the gap between the image and the [`crate::QRCode`] /// Default is around 30% of the [`crate::QRCode`] size - fn image_size(&mut self, image_size: f64, gap: f64) -> &mut Self; + fn image_size(&mut self, image_size: f64) -> &mut Self; + /// Updates the gap between the image and the [`crate::QRCode`] + fn image_gap(&mut self, gap: f64) -> &mut Self; /// Updates the image position, anchor is the center of the image. Default is the center of the [`crate::QRCode`] fn image_position(&mut self, x: f64, y: f64) -> &mut Self; } diff --git a/src/convert/svg.rs b/src/convert/svg.rs index 9994588..a141a83 100644 --- a/src/convert/svg.rs +++ b/src/convert/svg.rs @@ -49,7 +49,8 @@ pub struct SvgBuilder { /// Background shape for the image, default is square image_background_shape: ImageBackgroundShape, /// Size of the image, default is ~1/3 of the svg - image_size: Option<(f64, f64)>, + image_size: Option, + image_gap: Option, /// Position of the image, default is center image_position: Option<(f64, f64)>, } @@ -79,6 +80,7 @@ impl Default for SvgBuilder { image_background_color: [255; 4].into(), image_background_shape: ImageBackgroundShape::Square, image_size: None, + image_gap: None, image_position: None, } } @@ -130,8 +132,13 @@ impl Builder for SvgBuilder { self } - fn image_size(&mut self, image_size: f64, gap: f64) -> &mut Self { - self.image_size = Some((image_size, gap)); + fn image_size(&mut self, image_size: f64) -> &mut Self { + self.image_size = Some(image_size); + self + } + + fn image_gap(&mut self, gap: f64) -> &mut Self { + self.image_gap = Some(gap); self } @@ -196,7 +203,15 @@ impl SvgBuilder { let (mut border_size, mut placed_coord, mut image_size) = Self::image_placement(self.image_background_shape, self.margin, n); - if let Some((override_size, gap)) = self.image_size { + if let Some(gap) = self.image_gap { + border_size = gap * 2f64; + let mut placed_coord_x = (self.margin * 2 + n) as f64 - border_size; + placed_coord_x /= 2f64; + placed_coord = (placed_coord_x, placed_coord_x); + } + + if let Some(override_size) = self.image_size { + let gap = self.image_gap.unwrap_or(0f64); border_size = override_size + gap * 2f64; let mut placed_coord_x = (self.margin * 2 + n) as f64 - border_size; placed_coord_x /= 2f64; diff --git a/src/wasm.rs b/src/wasm.rs index 695bc79..403aacc 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -187,7 +187,8 @@ pub fn qr_svg(content: &str, options: SvgOptions) -> String { if options.image_size.len() == 2 { let size = options.image_size[0]; let gap = options.image_size[1]; - builder.image_size(size, gap); + builder.image_size(size); + builder.image_gap(gap); } if options.image_size.len() == 2 {