@forgesworn/qr
Version:
Dependency-free QR code encoder and decoder for TypeScript, browsers, workers and Node, with optional WebAssembly and tracked camera scanning.
90 lines (89 loc) • 4.51 kB
TypeScript
export { QrCapacityError } from './errors.js';
/** Error correction level: about 7, 15, 25 or 30 per cent of codewords recoverable. */
export type ErrorCorrection = 'L' | 'M' | 'Q' | 'H';
/** Data mode of a segment. */
export type Mode = 'numeric' | 'alphanumeric' | 'byte';
/** One data segment of a symbol, in stream order. */
export interface Segment {
readonly mode: Mode;
/** The character count field: characters in numeric and alphanumeric mode, bytes in byte mode. */
readonly count: number;
}
export interface EncodeOptions {
/** Error correction level. Default `'M'`. */
ecc?: ErrorCorrection;
/** Exact version, 1-40, not below `minVersion`. Default: the smallest version, from `minVersion`, that fits. */
version?: number;
/** Smallest version to consider when choosing one. Default 1. */
minVersion?: number;
/** Mask pattern, 0-7. Default: the pattern with the lowest penalty score. */
mask?: number;
/**
* Encode the whole text as one segment of this mode. Default for text: the
* mix of numeric, alphanumeric and byte segments with the fewest bits.
* Bytes (`Uint8Array`) are always one byte segment.
*/
mode?: Mode;
/**
* When the symbol has a byte segment, start the stream with an ECI designator for
* UTF-8 (assignment 26) so a reader need not guess the encoding. Default
* false: most readers assume UTF-8 for byte mode, and the designator costs 12 bits.
*/
eci?: boolean;
}
export interface QrCode {
readonly version: number;
readonly ecc: ErrorCorrection;
readonly mask: number;
/**
* The widest mode among the segments: byte if any segment is byte, else
* alphanumeric if any is, else numeric. For one segment, its mode.
*/
readonly mode: Mode;
/** The data segments in order; one unless the encoder chose a mix. */
readonly segments: readonly Segment[];
/** Modules per side, 17 + 4 * version. Excludes the quiet zone. */
readonly size: number;
/** Row-major modules, `size * size` entries, 1 for dark. */
readonly modules: Uint8Array;
}
/** True if the module at column `x`, row `y` is dark; false outside the symbol (the quiet zone is light). */
export declare function isDark(qr: QrCode, x: number, y: number): boolean;
export declare const ALPHANUMERIC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
export declare const EC_ORDER: readonly ErrorCorrection[];
export declare const COUNT_BITS: Record<Mode, readonly [number, number, number]>;
/** Encodes text (as UTF-8 where byte mode is needed) or bytes as one QR code. */
export declare function encode(data: string | Uint8Array, options?: EncodeOptions | null): QrCode;
export declare function countBand(version: number): 0 | 1 | 2;
/** UTF-8 bytes of `text`, with lone surrogates as U+FFFD (as TextEncoder does). */
export declare function utf8(text: string): Uint8Array;
export declare function blocksFor(version: number, ecc: ErrorCorrection): readonly [number, number, number, number];
export declare const EXP: Uint8Array<ArrayBuffer>;
export declare const LOG: Uint8Array<ArrayBuffer>;
export declare function gfMul(a: number, b: number): number;
/** Format information coordinates (x, y) for bits 0 to 14, beside the top-left finder. */
export declare const FORMAT_COORDS: readonly (readonly [number, number])[];
export declare class Grid {
readonly size: number;
private readonly base;
private readonly reserved;
private readonly version;
constructor(version: number);
/** True for finder, separator, timing, alignment, format and version modules. */
isReserved(x: number, y: number): boolean;
private fn;
private finder;
/** Zigzag placement of the final codeword stream; remainder bits are light. */
placeData(stream: Uint8Array): void;
/** A copy with the data masked and the format and version information drawn. */
render(ecc: ErrorCorrection, mask: number): Uint8Array;
}
export declare function maskBit(mask: number, x: number, y: number): boolean;
export declare function formatBits(ecc: ErrorCorrection, mask: number): number;
export declare function versionBits(version: number): number;
/**
* Mask penalty per ISO/IEC 18004 section 7.8.3, scoring the finished symbol.
* At `limit` the candidate cannot win: remaining penalties are non-negative.
* Without a limit this returns the exact score.
*/
export declare function penalty(modules: Uint8Array, size: number, limit?: number): number;