-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started implementing labels layer. Added label implementation
- Loading branch information
Showing
16 changed files
with
194 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { UnitSystem } from ".."; | ||
import Command from "./Command"; | ||
import CommandGroup from "./CommandGroup"; | ||
import { LabelDirection } from "./tspl"; | ||
|
||
/** | ||
* Interface that should be implemented by a command generator object for each language | ||
*/ | ||
export default interface CommandGenerator<T extends Command> { | ||
commandGroup: (commands: T[]) => CommandGroup<T> | ||
print: (sets: number, copiesPerSet: number) => T | ||
/** | ||
* This should generate the needed commands to set up a label before printing | ||
*/ | ||
setUp: (width: number, height: number, gap: number, offset: number, direction: LabelDirection, mirror: boolean, unitSystem: UnitSystem) => T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { UnitSystem } from ".."; | ||
import CommandGenerator from "../CommandGenerator"; | ||
import TSPLCommand from "./TSPLCommand"; | ||
import { TSPLCLSCommand, TSPLCommandGroup, TSPLDirectionCommand, TSPLGapCommand, TSPLPrintCommand, TSPLSizeCommand } from "./commands"; | ||
import { LabelDirection } from "./types"; | ||
|
||
/** | ||
* Command generator for tspl commands | ||
*/ | ||
class TSPLCommandGenerator implements CommandGenerator<TSPLCommand> { | ||
commandGroup(commands: TSPLCommand[]) { | ||
return new TSPLCommandGroup(commands) | ||
} | ||
|
||
print(sets: number, copiesPerSet: number): TSPLCommand { | ||
return new TSPLPrintCommand(sets, copiesPerSet) | ||
} | ||
|
||
setUp(width: number, height: number, gap: number, offset: number, direction: LabelDirection, mirror: boolean = false, unitSystem: UnitSystem): TSPLCommand { | ||
const commands = [ | ||
new TSPLSizeCommand(width, height, unitSystem), | ||
new TSPLGapCommand(gap, offset, unitSystem), | ||
new TSPLDirectionCommand(direction, mirror), | ||
new TSPLCLSCommand() | ||
] | ||
|
||
return new TSPLCommandGroup(commands) | ||
} | ||
} | ||
|
||
export default new TSPLCommandGenerator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as TSPLCommand } from "./TSPLCommand" | ||
export { default as commandGenerator } from "./TSPLCommandGenerator" | ||
|
||
export * from "./commands" | ||
export * from "./types" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { UnitSystem } from "@/commands" | ||
|
||
export function valueWithUnit(value: number, unitSystem: UnitSystem) { | ||
switch(unitSystem) { | ||
case "dot": return `${value} dot` | ||
case "imperial": return value | ||
case "metric": return `${value} mm` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * as commands from "./commands" | ||
export * as printers from "./printers" | ||
export * from "./commands" | ||
export * from "./printers" | ||
export * from "./labels" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Command, PrinterLanguage } from "@/commands"; | ||
import Printable from "./Printable"; | ||
import { UnitSystem } from "@/commands"; | ||
import { LabelDirection } from "@/commands/tspl"; | ||
import LabelField from "./fields/LabelField"; | ||
|
||
/** | ||
* Holds the content of a label and handles printing | ||
*/ | ||
export default class Label extends Printable { | ||
/** | ||
* Width of label in dots, mm or inch | ||
*/ | ||
private readonly width: number | ||
/** | ||
* Height of label in dots, mm or inch | ||
*/ | ||
private readonly height: number | ||
|
||
/** | ||
* Units for width, height, gap and offset | ||
*/ | ||
private readonly unitSystem: UnitSystem | ||
|
||
|
||
constructor(width: number, height: number, dimensionUnit: UnitSystem = "metric") { | ||
super() | ||
this.width = width | ||
this.height = height | ||
this.unitSystem = dimensionUnit | ||
} | ||
|
||
/** | ||
* List of fields on the label | ||
*/ | ||
private fields: LabelField[] = [] | ||
|
||
async commandForLanguage(language: PrinterLanguage): Promise<Command> { | ||
const commandList = await Promise.all(this.fields.map(field => field.commandForLanguage(language))) | ||
return this.commandGeneratorFor(language).commandGroup(commandList) | ||
} | ||
|
||
add(field: LabelField) { | ||
this.fields.push(field) | ||
} | ||
|
||
/** | ||
* Generate a command that is complete for printing | ||
* @param language Printing language to use | ||
* @param gap Distance between two labels. It is measured between the two points where the sensor | ||
* leaves the label and enters the next one | ||
* @param direction Direction relative to printing direction. See documentation for more details | ||
* @param sets Number of sets to print. If you have counters for example, it will not change in a set | ||
* @param copiesPerSet Number of labels per set | ||
* @param mirror Mirror the label along the vertical axis | ||
* @param gapOffset Used with non uniform shaped labels. Is the distance between the point where the sensor leaves the label and the | ||
* furthest point of the label in the direction of printing. Check documentation for more info | ||
* TODO: Is this too TSPL Specific? | ||
*/ | ||
async fullPrintCommand(language: PrinterLanguage, | ||
gap: number, | ||
direction: LabelDirection, | ||
sets: number, | ||
copiesPerSet: number = 1, | ||
mirror: boolean = false, | ||
gapOffset: number = 0 | ||
): Promise<Command> { | ||
|
||
const generator = this.commandGeneratorFor(language) | ||
const commands = [ | ||
...generator.setUp(this.width, this.height, gap, gapOffset, direction, mirror, this.unitSystem), | ||
this.commandForLanguage(language), | ||
generator.print(sets, copiesPerSet) | ||
] | ||
|
||
return generator.commandGroup(commands) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Command, PrinterLanguage, tspl } from "@/commands"; | ||
import CommandGenerator from "@/commands/CommandGenerator"; | ||
import { Printer } from "@/printers"; | ||
|
||
/** | ||
* A component that can be directly printed to label printer | ||
*/ | ||
export default abstract class Printable { | ||
/** | ||
* Generates a printable command | ||
* @param language The printing language that should be used | ||
* @returns A promise of a command that can be printed | ||
*/ | ||
abstract commandForLanguage(language: PrinterLanguage): Promise<Command> | ||
|
||
/** | ||
* Generates printable command for the given printer. Can be used to obtain a command for fields supported by the package then customizing it before printing | ||
* @param printer Printer to generate the command. Important because the command is printer language specific | ||
* @returns A promise for a command. Most commands are syncronouse but some may require to access async resources | ||
*/ | ||
async commandForPrinter(printer: Printer): Promise<Command> { | ||
return await this.commandForLanguage(printer.language) | ||
} | ||
|
||
/** | ||
* Obtain a command generator for the given language | ||
* @param language Language to get generator for | ||
*/ | ||
protected commandGeneratorFor(language: PrinterLanguage): CommandGenerator<any> { | ||
switch(language) { | ||
case "tspl": return tspl.commandGenerator | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Printable from "../Printable"; | ||
|
||
export default abstract class LabelField extends Printable { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./LabelField" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as Label } from "./Label" | ||
export * from "./fields" | ||
export * from "./types" |