webzlp
Version:
A small library using WebUSB to print labels on label printers.
114 lines • 5.56 kB
TypeScript
import { Percent } from './NumericRange.js';
import { WebZlpError } from './WebZlpError.js';
/** Padding information for a trimmed image. */
export interface ImageBoundingBox {
/** The total original width of the image, including padding. */
width: number;
/** The total original height of the image, including padding. */
height: number;
/** The number of pixels between the top of the box and the actual image. */
paddingTop: number;
/** The number of pixels between the right side of the box and the actual image. */
paddingRight: number;
/** The number of pixels between the bottom of the box and the actual image. */
paddingBottom: number;
/** The number of pixels between the left side of the box and the actual image. */
paddingLeft: number;
}
/** Settings for converting an image to a GRF. */
export interface ImageConversionOptions {
/** The threshold brightness below which to consider a pixel black. Defaults to 70. */
grayThreshold?: Percent;
/** Whether to trim whitespace around the image to reduce file size. Trimmed pixels will become padding in the bounding box. */
trimWhitespace?: boolean;
/** The dithering method to use when converting image to monochrome. */
ditheringMethod?: DitheringMethod;
}
/** List of available dithering methods for converting images to black/white. */
export declare enum DitheringMethod {
/** No dithering, cutoff with used. */
none = 0
}
/** Represents a GRF bitmap file. */
export declare class BitmapGRF {
private _bitmap;
private _width;
/** Gets the actual width of the image file, not including any padding. */
get width(): number;
private _height;
/** Gets the actual height of the image file, not inlcuding any padding. */
get height(): number;
private _bytesPerRow;
/** Gets the number of bytes per row (width) of the image file. */
get bytesPerRow(): number;
/** Gets the total number of uncompressed bytes of the image file. Usually used in printer commands. */
get bytesUncompressed(): number;
private _boundingBox;
/** Gets the bounding box information for this image, for proper alignment of trimmed images. */
get boundingBox(): ImageBoundingBox;
constructor(bitmapGRF: Uint8Array, imageWidth: number, imageHeight: number, bytesPerRow: number, boundingBox: ImageBoundingBox);
/** Get a raw binary representation of this GRF. This is raw binary with no compression, compatible with EPL and ZPL.
*
* The image may need to be offset according to the bounding box padding. Use the bytesPerRow for data length calculations.
*
* Example use:
* ```
* const grf = new BitmapGRF();
* const zplCmd = `^GFC,${grf.bytesUncompressed},${grf.bytesUncompressed},${grf.bytesPerRow},${grf.toBinaryGRF()}`;
*
* const eplCmd = `GW${grf.boundingBox.paddingLeft},${grf.boundingBox.paddingTop},${grf.bytesPerRow},${grf.height},${grf.toBitmapGRF}`;
* ```
*/
toBinaryGRF(): Uint8Array;
/** Gets an ImageData representation of this GRF. Can be used to draw into Canvas elements. */
toImageData(): ImageData;
/** Gets a bitmap GRF that has its colors inverted.
*
* EPL uses 1 as white. ZPL uses 1 as black. Use this to convert between them.
*/
toInvertedGRF(): BitmapGRF;
/** Get a compressed representation of this GRF. This is not compatible with EPL.
* This is also referred to as the "Alternate Compression Scheme" or "Zebra Compression".
*
* The image may need to be offset according to the bounding box.
*
* Example use:
* ```
* const grf = new BitmapGRF();
* const cmd = `^GFA,${grf.bytesUncompressed},${grf.bytesUncompressed},${grf.bytesPerRow},${grf.toZebraCompressedGrf()}`;
* ```
*/
toZebraCompressedGRF(): string;
/**
* Create a GRF bitmap from a raw RGBA array-like object.
*/
static fromRGBA(data: Uint8Array | Uint8ClampedArray | Array<number>, width: number, imageOptions?: ImageConversionOptions): BitmapGRF;
/**
* Create a GRF bitmap from a canvas ImageData object.
* @param imageData The canvas ImageData object to convert.
* @param grayThreshold The cutoff percentage below which values are considered black. Defaults to 75% of white.
* @param trimWhitespace Trim image to save space, storing trim amounts in the bounding box.
* @returns The bitmap GRF file.
*/
static fromCanvasImageData(imageData: ImageData, imageOptions?: ImageConversionOptions): BitmapGRF;
/** Use an SVG string as a bitmap image, rendered at the width and height provided. */
static fromSVG(svg: string, widthInDots: number, heightInDots: number, imageConversionOptions?: ImageConversionOptions): Promise<BitmapGRF>;
/** Converts monochrome data to GRF format. */
private static monochromeToGRF;
/**
* Converts an RGBA array to monochrome, 1-bit-per-byte.
* This supports trimming the image to save space, and will remove whitespace
* to an internal bounding box. The results of this padding are stored as the
* bounding box information for the bitmap.
*/
private static toMonochrome;
private static roundUpToByte;
private static colorToGrayscale;
/** Lookup table for binary to hex values. */
private static hexmap;
}
/** Error indicating an issue with the provided image. */
export declare class BitmapFormatError extends WebZlpError {
constructor(message: string);
}
//# sourceMappingURL=BitmapGRF.d.ts.map