Skip to content

Commit

Permalink
feat: add image_gap to Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Overcash - c0o02bc authored and Chris Overcash - c0o02bc committed Feb 7, 2024
1 parent 08b973e commit 26c3e1d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 40 deletions.
18 changes: 7 additions & 11 deletions pkg/fast_qr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<InitInput>} module_or_path
* @param {WebAssembly.Memory} maybe_memory
*
* @returns {Promise<InitOutput>}
*/
export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>, maybe_memory?: WebAssembly.Memory): Promise<InitOutput>;
export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
46 changes: 28 additions & 18 deletions pkg/fast_qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@ 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;
}

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) {

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
}

Expand All @@ -222,7 +231,7 @@ export class SvgOptions {
}
/**
* Updates the shape of the QRCode modules.
* @param {number} shape
* @param {Shape} shape
* @returns {SvgOptions}
*/
shape(shape) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -326,7 +335,8 @@ export class SvgOptions {
*/
constructor() {
const ret = wasm.svgoptions_new();
return SvgOptions.__wrap(ret);
this.__wbg_ptr = ret >>> 0;
return this;
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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') {
Expand All @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions pkg/fast_qr_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
9 changes: 7 additions & 2 deletions src/convert/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
23 changes: 19 additions & 4 deletions src/convert/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>,
image_gap: Option<f64>,
/// Position of the image, default is center
image_position: Option<(f64, f64)>,
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 26c3e1d

Please sign in to comment.