Skip to content

Commit

Permalink
Added basic field types
Browse files Browse the repository at this point in the history
  • Loading branch information
kemkriszt committed Jan 18, 2024
1 parent f5e680e commit c2b082e
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/commands/CommandGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Point, UnitSystem } from "./index"
import Command from "./Command";
import CommandGroup from "./CommandGroup";
import { LabelDirection } from "./tspl";
import { Alignment, BarcodeHumanReable, BarcodeType, GraphicMode, LabelDirection, Rotation } from "./tspl";
import { BitmapLike } from "@/helpers/ImageUtils";

/**
* Interface that should be implemented by a command generator object for each language
Expand All @@ -12,6 +13,9 @@ export default interface CommandGenerator<T extends Command> {
text: (content: string, x: number, y: number, font: string|"default", size: number) => T
upload: (name: string, data: ArrayBuffer|Uint8Array) => T
line: (start: Point, end: Point, thickness: number) => T
image: (image: BitmapLike, x: number, y: number, mode?: GraphicMode) => T
qrCode: (content: string, width: number, x: number, y: number) => T,
barCode: (content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment) => T
/**
* Should instruct the printer to display the image of the label on its screen instead of printing it
*/
Expand Down
18 changes: 16 additions & 2 deletions src/commands/tspl/TSPLCommandGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BitmapLike } from "@/helpers/ImageUtils";
import { Point, UnitSystem } from "..";
import CommandGenerator from "../CommandGenerator";
import TSPLCommand from "./TSPLCommand";
import { TSPLCLSCommand, TSPLCommandGroup, TSPLDiagonal, TSPLDirectionCommand, TSPLDisplay, TSPLDownload, TSPLGapCommand, TSPLPrintCommand, TSPLRawCommand, TSPLSizeCommand, TSPLTextCommand } from "./commands";
import { LabelDirection } from "./types";
import { TSPLBitmapCommand, TSPLCLSCommand, TSPLCommandGroup, TSPLDiagonal, TSPLDirectionCommand, TSPLDisplay, TSPLDownload, TSPLGapCommand, TSPLPrintCommand, TSPLQRCommand, TSPLRawCommand, TSPLSizeCommand, TSPLTextCommand } from "./commands";
import { Alignment, BarcodeHumanReable, BarcodeType, GraphicMode, LabelDirection, Rotation } from "./types";
import TSPLBarcodeCommand from "./commands/basic/TSPLBarcodeCommand";

/**
* Command generator for tspl commands
Expand Down Expand Up @@ -46,6 +48,18 @@ class TSPLCommandGenerator implements CommandGenerator<TSPLCommand> {
line(start: Point, end: Point, thickness: number): TSPLCommand {
return new TSPLDiagonal(start, end, thickness)
}

image(image: BitmapLike, x: number, y: number, mode?: GraphicMode | undefined): TSPLCommand {
return new TSPLBitmapCommand(image, x, y, mode)
}

qrCode(content: string, width: number, x: number, y: number): TSPLCommand {
return new TSPLQRCommand(content, x, y, width / 177)
}

barCode(content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment): TSPLCommand {
return new TSPLBarcodeCommand(x, y, type, height, 1, 1, content, rotation, humanReadable, alignment)
}
}

export default new TSPLCommandGenerator()
4 changes: 1 addition & 3 deletions src/examples/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Label } from "@/labels"
import { Line, Text } from "@/labels/fields"
import { Text } from "@/labels/fields"
import { PrinterService } from "@/printers"
import fs from "fs"
import { NodeType, parse, HTMLElement } from "node-html-parser"
import fontkit from "fontkit"

export default async () => {
const printers = await PrinterService.getPrinters()
Expand Down
10 changes: 6 additions & 4 deletions src/helpers/ImageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export type Pixels = {
bitsPerPixel: number
}

/**
* Helper type to transmit black and white bitmap data
*/
export type BWBitmap = {
export type BitmapLike = {
width: number,
height: number,
bytes: Uint8Array
}

/**
* Helper type to transmit black and white bitmap data
*/
export type BWBitmap = BitmapLike

const BLACK_PIXEL = 0
const WHITE_PIXEL = 1

Expand Down
31 changes: 31 additions & 0 deletions src/labels/fields/BarCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Command, PrinterLanguage } from "@/commands";
import { PrintConfig } from "../Printable";
import LabelField from "./LabelField"
import { Alignment, BarcodeHumanReable, BarcodeType, Rotation } from "@/commands/tspl";

export default class BarCode extends LabelField {
private readonly content: string
private readonly x: number
private readonly y: number
private readonly type: BarcodeType
private readonly height: number
private readonly rotation: Rotation
private readonly humanReadable: BarcodeHumanReable
private readonly alignment: Alignment

constructor(content: string, x: number, y: number, type: BarcodeType, height: number, rotatio: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment) {
super()
this.content = content
this.x = x
this.y = y
this.type = type
this.height = height
this.rotation = rotatio
this.humanReadable = humanReadable
this.alignment = alignment
}

async commandForLanguage(language: PrinterLanguage, config?: PrintConfig | undefined): Promise<Command> {
return await this.commandGeneratorFor(language).barCode(this.content, this.x, this.y, this.type, this.height, this.rotation, this.humanReadable, this.alignment)
}
}
43 changes: 43 additions & 0 deletions src/labels/fields/Image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Command, PrinterLanguage } from "@/commands";
import { PrintConfig } from "../Printable";
import LabelField from "./LabelField";
import ImageUtils, { BitmapLike } from "@/helpers/ImageUtils";

export default class Image extends LabelField {
/**
* X coordinate in dots
*/
private readonly x: number
/**
* Y coordinate in dots
*/
private readonly y: number

private readonly image: BitmapLike

constructor(x: number, y: number, image: BitmapLike) {
super()
this.x = x
this.y = y
this.image = image
}

async commandForLanguage(language: PrinterLanguage, _config?: PrintConfig | undefined): Promise<Command> {
return await this.commandGeneratorFor(language).image(this.image, this.x, this.y)
}

/**
* Create an image field for an image
* @param image
* @param x
* @param y
* @param width
* @param height
* @returns
*/
static async create(image: string, x: number, y: number, width: number, height: number): Promise<Image> {
const bitmap = await ImageUtils.getBWBitmap(image, width, height)

return new Image(x, y, bitmap)
}
}
22 changes: 22 additions & 0 deletions src/labels/fields/QRCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Command, PrinterLanguage } from "@/commands";
import { PrintConfig } from "../Printable";
import LabelField from "./LabelField";

export default class QRCode extends LabelField {
private readonly content: string
private readonly x: number
private readonly y: number
private readonly width: number

constructor(content: string, x: number, y: number, width: number) {
super()
this.content = content
this.x = x
this.y = y
this.width = width
}

async commandForLanguage(language: PrinterLanguage, config?: PrintConfig | undefined): Promise<Command> {
return await this.commandGeneratorFor(language).qrCode(this.content, this.width, this.x, this.y)
}
}
5 changes: 4 additions & 1 deletion src/labels/fields/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export * from "./LabelField"

export { default as Line } from "./Line"
export { default as Text } from "./Text"
export { default as Text } from "./Text"
export { default as BarCode } from "./BarCode"
export { default as Image } from "./Image"
export { default as qrCode } from "./QRCode"

0 comments on commit c2b082e

Please sign in to comment.