Skip to content

Commit

Permalink
Some progress with text field
Browse files Browse the repository at this point in the history
  • Loading branch information
kemkriszt committed Jan 8, 2024
1 parent 3d11e90 commit 92a051e
Show file tree
Hide file tree
Showing 27 changed files with 424 additions and 69 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ This layer contains code to interact with printers

## Documentation of supported languages

- [TSPL](documentations/TSPL.pdf)
- [TSPL](documentations/TSPL.pdf)

### Usefull units:

- 1 pt = 1/72 inch
- 1 dot = 1 / dpi
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"@types/w3c-web-usb": "^1.0.10",
"clean-html": "^2.0.1",
"image-pixels": "^2.2.2",
"usb": "^2.11.0"
}
Expand Down
53 changes: 52 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/commands/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default abstract class Command {
*/
abstract get commandString(): string

print(fn: (command: string) => void) {
fn(this.commandString)
}

/**
* Write the command data to a USB device
* @param device Device to write to
Expand Down Expand Up @@ -40,7 +44,7 @@ export default abstract class Command {
* @param data Byte array to send
* @param device Device to write to
*/
protected async writeBytes(data: Uint8Array, device: UsbDevice): Promise<void> {
protected async writeBytes(data: Uint8Array|ArrayBuffer, device: UsbDevice): Promise<void> {
await device.writeData(data)
}

Expand Down
11 changes: 8 additions & 3 deletions src/commands/CommandGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnitSystem } from "..";
import { Point, UnitSystem } from "./index"
import Command from "./Command";
import CommandGroup from "./CommandGroup";
import { LabelDirection } from "./tspl";
Expand All @@ -9,8 +9,13 @@ import { LabelDirection } from "./tspl";
export default interface CommandGenerator<T extends Command> {
commandGroup: (commands: T[]) => CommandGroup<T>
print: (sets: number, copiesPerSet: number) => T
text: (content: string, x: number, y: number) => T

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
/**
* Should instruct the printer to display the image of the label on its screen instead of printing it
*/
display: () => T
/**
* This should generate the needed commands to set up a label before printing
*/
Expand Down
6 changes: 6 additions & 0 deletions src/commands/CommandGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export default abstract class CommandGroup<T extends Command> extends Command {
this.commands = commands
}

print(fn: (command: string) => void) {
for (let commandIndex in this.commands) {
this.commands[commandIndex].print(fn)
}
}

async write(device: UsbDevice): Promise<void> {
for (let commandIndex in this.commands) {
await this.commands[commandIndex].write(device)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { default as CommandGroup } from "./CommandGroup"
export type PrinterLanguage = "tspl"
export type UnitSystem = "imperial"|"metric"|"dot"

export * as tspl from "./tspl"
export * as tspl from "./tspl"
export * from "./types"
24 changes: 20 additions & 4 deletions src/commands/tspl/TSPLCommandGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UnitSystem } from "..";
import { Point, UnitSystem } from "..";
import CommandGenerator from "../CommandGenerator";
import TSPLCommand from "./TSPLCommand";
import { TSPLCLSCommand, TSPLCommandGroup, TSPLDirectionCommand, TSPLGapCommand, TSPLPrintCommand, TSPLSizeCommand, TSPLTextCommand } from "./commands";
import { TSPLCLSCommand, TSPLCommandGroup, TSPLDiagonal, TSPLDirectionCommand, TSPLDisplay, TSPLDownload, TSPLGapCommand, TSPLPrintCommand, TSPLRawCommand, TSPLSizeCommand, TSPLTextCommand } from "./commands";
import { LabelDirection } from "./types";

/**
Expand All @@ -16,8 +16,13 @@ class TSPLCommandGenerator implements CommandGenerator<TSPLCommand> {
return new TSPLPrintCommand(sets, copiesPerSet)
}

text(content: string, x: number, y: number): TSPLCommand {
return new TSPLTextCommand(content, x, y, "1")
text(content: string, x: number, y: number, font: string|"default", size: number): TSPLCommand {
const fontName = font == "default" ? "0" : font
return new TSPLTextCommand(content, x, y, fontName, 0, size, size, "left")
}

upload(name: string, data: ArrayBuffer | Uint8Array): TSPLCommand {
return new TSPLDownload(name, data)
}

setUp(width: number, height: number, gap: number, offset: number, direction: LabelDirection, mirror: boolean = false, unitSystem: UnitSystem): TSPLCommand {
Expand All @@ -30,6 +35,17 @@ class TSPLCommandGenerator implements CommandGenerator<TSPLCommand> {

return new TSPLCommandGroup(commands)
}

display() {
return new TSPLCommandGroup([
new TSPLDisplay("CLS"),
new TSPLDisplay("IMAGE")
])
}

line(start: Point, end: Point, thickness: number): TSPLCommand {
return new TSPLDiagonal(start, end, thickness)
}
}

export default new TSPLCommandGenerator()
19 changes: 19 additions & 0 deletions src/commands/tspl/commands/basic/TSPLDiagonal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import TSPLCommand from "../../TSPLCommand";
import { Point } from "@/commands";

export default class TSPLDiagonal extends TSPLCommand {
private readonly start: Point
private readonly end: Point
private readonly thickness: number

constructor(start: Point, end: Point, thickness: number = 3) {
super()
this.start = start
this.end = end
this.thickness = thickness
}

get commandString(): string {
return `DIAGONAL ${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y}, ${this.thickness}`
}
}
20 changes: 20 additions & 0 deletions src/commands/tspl/commands/basic/TSPLDisplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import TSPLCommand from "../../TSPLCommand";

export type DisplayType = "CLS"|"IMAGE"

/**
* Displays the image buffer on the screen
* {@link /documentsions/TSPL.pdf}
*/
export default class TSPLDisplay extends TSPLCommand {
private readonly type: DisplayType

constructor(type: DisplayType) {
super()
this.type = type
}

get commandString(): string {
return `DISPLAY ${this.type}`
}
}
34 changes: 34 additions & 0 deletions src/commands/tspl/commands/basic/TSPLDownload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { UsbDevice } from "@/helpers/USBUtils"
import TSPLCommand from "../../TSPLCommand"

type Data = ArrayBuffer|Uint8Array
/**
* A raw TSPL command. Can be used to use a command that is not yet supported
*/
export default class TSPLDownload extends TSPLCommand {
/**
* Name of the file on the printer
*/
private readonly fileName: string
private readonly data: Data

/**
* Initialize a command with a raw body
* @param body
*/
constructor(fileName: string, data: Data) {
super()
this.fileName = fileName
this.data = data
}

get commandString(): string {
return `DOWNLOAD "${this.fileName}", ${this.data.byteLength},`
}

async write(device: UsbDevice): Promise<void> {
await this.writeString(this.commandString, device)
await this.writeBytes(this.data, device)
await this.terminateCommand(device)
}
}
5 changes: 4 additions & 1 deletion src/commands/tspl/commands/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export { default as TSPLDirectionCommand } from "./TSPLDirectionCommand"
export { default as TSPLPrintCommand } from "./TSPLPrintCommand"
export { default as TSPLBarCommand } from "./TSPLBarCommand"
export { default as TSPLQRCommand } from "./TSPLQRCommand"
export { default as TSPLBlockCommand } from "./TSPLBlockCommand"
export { default as TSPLBlockCommand } from "./TSPLBlockCommand"
export { default as TSPLDownload } from "./TSPLDownload"
export { default as TSPLDisplay } from "./TSPLDisplay"
export { default as TSPLDiagonal } from "./TSPLDiagonal"
1 change: 1 addition & 0 deletions src/commands/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Point = {x: number, y: number}
Binary file added src/examples/.DS_Store
Binary file not shown.
25 changes: 19 additions & 6 deletions src/examples/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Label } from "@/labels"
import TextField from "@/labels/fields/TextField"
import { Line, Text } from "@/labels/fields"
import { PrinterService } from "@/printers"
import fs from "fs"

export default async () => {
const printers = await PrinterService.getPrinters()
Expand All @@ -9,11 +10,23 @@ export default async () => {
if(printers.length > 0) {
const printer = printers[0]

const label = new Label(70, 30)
const text = new TextField("Hello", 5, 10)

label.add(text)
await printer.print(label, 1, 3)
const fontName = "super.TTF"
const testText = "Hello 4"
const fontSize = 40
const textX = 10
const textY = 10

const font = fs.readFileSync(__dirname+"/"+fontName).buffer

const label = new Label(50, 25)
const text = new Text(testText, textX, textY)
const line = new Line({x: textX, y: textY + fontSize}, {x: textX + 100, y: textY + fontSize})

text.setFont(fontName, fontSize)
label.registerFont(font, fontName)
label.add(text, line)

await printer.display(label)
await printer.close()
}
}
Binary file added src/examples/node/super.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/helpers/USBUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class UsbDevice {
* Write data to an USB device
* @param data Data to write
*/
async writeData(data: Uint8Array): Promise<void> {
async writeData(data: Uint8Array|ArrayBuffer): Promise<void> {
const endpointNumber = this.outEndpoint
await this.device.transferOut(endpointNumber!, data)
}
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/UnitUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { UnitSystem } from "@/commands"

const pointsPerInch = 72

export function valueWithUnit(value: number, unitSystem: UnitSystem) {
switch(unitSystem) {
case "dot": return `${value} dot`
case "imperial": return value
case "metric": return `${value} mm`
}
}

/**
* Convert a value from dots in points in a given dpi
*/
export function dotToPoint(dots: number, dpi: number): number {
const inch = dots / dpi
return Math.round(inch * pointsPerInch)
}
Loading

0 comments on commit 92a051e

Please sign in to comment.