Skip to content

Commit

Permalink
Add more settings, advanced demo page (#39)
Browse files Browse the repository at this point in the history
* Add setting for label origin offset

* Add commands for continuous media

* Expose commands in builders

* Pull correct text element for ZPL

* Fix ZPL speed detect

* Add advanced demo with more controls

* Fix emoji font override order

* Fix clamping method call
  • Loading branch information
Cellivar authored Jan 10, 2023
1 parent 19e7181 commit 507e98a
Show file tree
Hide file tree
Showing 15 changed files with 1,083 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ See [the demo](https://cellivar.github.io/WebZLP/demo) that runs in your browser

The demo dynamically compiles the typescript in the browser so can take a minute to spin up.

Want to see more bells and whistles? Or maybe you just want to play around with a printer you have? Try the [advanced demo](https://cellivar.github.io/WebZLP/demo/advanced) for more options and controls. The code is a little harder to follow because of the additional things going on.

## Docs

This repo contains some docs and findings related to Zebra label printers and their various quirks. I'm interested in collecting as much of this information as I can as I just think they're neat. If you have something to add please feel free to open an issue!
Expand Down
724 changes: 724 additions & 0 deletions demo/advanced.html

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions demo/index.html

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions src/Documents/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export enum CommandType {
SetDarknessCommand = 'SetDarknessCommand',
SetLabelDimensionsCommand = 'SetLabelDimensionsCommand',
SetLabelHomeCommand = 'SetLabelHomeCommand',
SetLabelPrintOriginOffsetCommand = 'SetLabelPrintOriginOffsetCommand',
SetLabelToContinuousMediaCommand = 'SetLabelToContinuousMediaCommand',
SetLabelToWebGapMediaCommand = 'SetLabelToWebGapMediaCommand',
SetLabelToMarkMediaCommand = 'SetLabelToMarkMediaCommand',
SetPrintDirectionCommand = 'SetPrintDirectionCommand',
SetPrintSpeedCommand = 'SetPrintSpeedCommand',
SuppressFeedBackupCommand = 'SuppressFeedBackupCommand',
Expand Down Expand Up @@ -357,6 +361,74 @@ export class SetLabelHomeCommand implements IPrinterCommand {
constructor(public xOffset: number, public yOffset: number) {}
}

/** Command class to set the print offset from the top-left of the label. */
export class SetLabelPrintOriginOffsetCommand implements IPrinterCommand {
get name(): string {
return 'Sets the print offset from the top left corner.';
}
get type(): CommandType {
return CommandType.SetLabelPrintOriginOffsetCommand;
}
toDisplay(): string {
return `Sets the print offset to ${this.xOffset} in and ${this.yOffset} down from the top-left.`;
}
printerEffectFlags = PrinterCommandEffectFlags.altersPrinterConfig;

constructor(public xOffset: number, public yOffset: number) {}
}

/** A command class to set the media handling mode to continuous media. */
export class SetLabelToContinuousMediaCommand implements IPrinterCommand {
get name(): string {
return 'Sets the media handling mode to continuous media.';
}
get type(): CommandType {
return CommandType.SetLabelToContinuousMediaCommand;
}
toDisplay(): string {
return this.name;
}
printerEffectFlags = PrinterCommandEffectFlags.altersPrinterConfig;

constructor(public labelLengthInDots: number, public labelGapInDots = 0) {}
}

/** A command class to set the media handling mode to web gap detection. */
export class SetLabelToWebGapMediaCommand implements IPrinterCommand {
get name(): string {
return 'Sets the media handling mode to web gap detection.';
}
get type(): CommandType {
return CommandType.SetLabelToWebGapMediaCommand;
}
toDisplay(): string {
return this.name;
}
printerEffectFlags = PrinterCommandEffectFlags.altersPrinterConfig;

constructor(public labelLengthInDots: number, public labelGapInDots: number) {}
}

/** A command class to set the media handling mode to black mark detection. */
export class SetLabelToMarkMediaCommand implements IPrinterCommand {
get name(): string {
return 'Sets the media handling mode to black mark detection.';
}
get type(): CommandType {
return CommandType.SetLabelToMarkMediaCommand;
}
toDisplay(): string {
return this.name;
}
printerEffectFlags = PrinterCommandEffectFlags.altersPrinterConfig;

constructor(
public labelLengthInDots: number,
public blackLineThicknessInDots: number,
public blackLineOffset: number
) {}
}

/** Command class to cause the printer to auto-sense the media length. */
export class AutosenseLabelDimensionsCommand implements IPrinterCommand {
get name(): string {
Expand Down
81 changes: 77 additions & 4 deletions src/Documents/ConfigDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ConfigDocumentBuilder
return this.andThen(new Commands.SetPrintDirectionCommand(upsideDown));
}

setPrintSpeed(speed: Options.PrintSpeed, mediaSlewSpeed = Options.PrintSpeed.auto) {
setPrintSpeed(speed: Options.PrintSpeed, mediaSlewSpeed = Options.PrintSpeed.ipsAuto) {
if (!this._config.model.isSpeedValid(speed)) {
throw new UnsupportedPrinterConfigError(
'setPrintSpeed',
Expand All @@ -79,7 +79,7 @@ export class ConfigDocumentBuilder
}

// If the media slew speed is auto just copy the print speed.
if (mediaSlewSpeed === Options.PrintSpeed.auto) {
if (mediaSlewSpeed === Options.PrintSpeed.ipsAuto) {
mediaSlewSpeed = speed;
}
return this.andThen(
Expand Down Expand Up @@ -118,6 +118,50 @@ export class ConfigDocumentBuilder
new Commands.SetLabelHomeCommand(horizontalOffsetInDots, verticalOffsetInDots)
);
}

setLabelPrintOriginOffsetCommand(horizontalOffsetInDots: number, verticalOffsetInDots: number) {
return this.andThen(
new Commands.SetLabelPrintOriginOffsetCommand(
horizontalOffsetInDots,
verticalOffsetInDots
)
);
}

setLabelMediaToContinuous(labelHeightInInches: number): IConfigDocumentBuilder {
const dpi = this._config.model.dpi;
return this.andThen(
new Commands.SetLabelToContinuousMediaCommand(dpi * labelHeightInInches)
);
}

setLabelMediaToWebGapSense(
labelHeightInInches: number,
labelGapInInches: number
): IConfigDocumentBuilder {
const dpi = this._config.model.dpi;
return this.andThen(
new Commands.SetLabelToWebGapMediaCommand(
labelHeightInInches * dpi,
labelGapInInches * dpi
)
);
}

setLabelMediaToMarkSense(
labelLengthInInches: number,
blackLineThicknessInInches: number,
blackLineOffsetInInches: number
): IConfigDocumentBuilder {
const dpi = this._config.model.dpi;
return this.andThen(
new Commands.SetLabelToMarkMediaCommand(
labelLengthInInches * dpi,
blackLineThicknessInInches * dpi,
blackLineOffsetInInches * dpi
)
);
}
}

export interface IPrinterBasicCommandBuilder {
Expand Down Expand Up @@ -179,8 +223,8 @@ export interface IPrinterLabelConfigBuilder {
): IConfigDocumentBuilder;

/**
* Sets the origin offset from the top-left of the label that all other offsets
* are calculated from.
* Sets the temporary origin offset from the top-left of the label that all
* other offsets are calculated from. Only applies to current label.
*
* Use this to fine-tune the alignment of your printer to your label stock.
*
Expand All @@ -191,8 +235,37 @@ export interface IPrinterLabelConfigBuilder {
verticalOffsetInDots: number
): IConfigDocumentBuilder;

/** Sets the retained origin offset from the top-left of the label that all
* other offets are calculated from. Applies to all labels until a printer reset
* or power cycle.
*
* May or may not be stored depending on printer firmware.
*
* Avoid printing off the edges of a label, which can cause excessive head wear.
*/
setLabelPrintOriginOffsetCommand(
horizontalOffsetInDots: number,
verticalOffsetInDots: number
): IConfigDocumentBuilder;

/** Run the autosense operation to get label length. Must be last command. */
autosenseLabelLength(): IDocument;

/** Sets the media type to continuous (gapless) media. */
setLabelMediaToContinuous(labelLengthInDots: number): IConfigDocumentBuilder;

/** Sets the media type to web gap sensing media. It's recommended to run autosense after this. */
setLabelMediaToWebGapSense(
labelLengthInDots: number,
labelGapInDots: number
): IConfigDocumentBuilder;

/** Sets the media type to mark sensing media. */
setLabelMediaToMarkSense(
labelLengthInDots: number,
blackLineThicknessInDots: number,
blackLineOffset: number
): IConfigDocumentBuilder;
}

/** Error indicating setting a config value failed. */
Expand Down
27 changes: 25 additions & 2 deletions src/Documents/LabelDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export class LabelDocumentBuilder
return this.andThen(new Commands.OffsetCommand(horizontal, vertical, true));
}

setLabelHomeOffsetDots(horizontalOffsetInDots: number, verticalOffsetInDots: number) {
return this.andThen(
new Commands.SetLabelHomeCommand(horizontalOffsetInDots, verticalOffsetInDots)
);
}

addOffset(horizontal: number, vertical?: number): ILabelDocumentBuilder {
return this.andThen(new Commands.OffsetCommand(horizontal, vertical));
}
Expand Down Expand Up @@ -148,10 +154,27 @@ export interface ILabelActionCommandBuilder {
}

export interface ILabelPositionCommandBuilder {
/** Set the aboslute offset from the top left position of the label. */
/** Set the aboslute offset from the top left position of the label.
*
* Avoid printing off the edges of a label, which can cause excessive head wear.
*/
setOffset(horizontal: number, vertical?: number): ILabelDocumentBuilder;

/** Add a relative offset to the current offset from the top left position of the label. */
/**
* Sets the temporary origin offset from the top-left of the label that all
* other offsets are calculated from. Only applies to current label.
*
* Avoid printing off the edges of a label, which can cause excessive head wear.
*/
setLabelHomeOffsetDots(
horizontalOffsetInDots: number,
verticalOffsetInDots: number
): ILabelDocumentBuilder;

/** Add a relative offset to the current offset from the top left position of the label.
*
* Avoid printing off the edges of a label, which can cause excessive head wear.
*/
addOffset(horizontal: number, vertical?: number): ILabelDocumentBuilder;

/** Resets the offset back to origin (top left of label) */
Expand Down
2 changes: 1 addition & 1 deletion src/Documents/ReadyToPrintDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ReadyToPrintDocuments {
return printer
.getConfigDocument()
.setPrintDirection()
.setPrintSpeed(PrintSpeed.auto)
.setPrintSpeed(PrintSpeed.ipsAuto)
.setDarknessConfig(darknessPercent)
.setLabelDimensions(labelWidthInches)
.autosenseLabelLength();
Expand Down
6 changes: 1 addition & 5 deletions src/PrinterUsbManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,9 @@ export class PrinterUsbManager extends EventTarget {
e.name === 'NotFoundError' &&
e.message === 'No device selected.'
) {
console.log('User did not select a printer');
return;
}

console.log('Failed to connect to printer!');
console.log(e);
return;
throw e;
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/Printers/Configuration/MediaOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface IPrinterLabelMediaOptions {
get labelGapInches(): number;
/** Label gap in dots */
labelGapDots: number;
/** The offset in inches from the normal location of the label gap or black line. Can be negative. */
get labelLineOffsetInches(): number;
/** The offset in dots from the normal location of the label gap or black line. Can be negative. */
labelLineOffsetDots: number;
/** The height of the label media, in inches. */
get labelHeightInches(): number;
/** The height of the label media, in dots. */
Expand Down Expand Up @@ -73,7 +77,7 @@ export class PrintSpeedSettings {
public static getSpeedFromWholeNumber(speed: number): PrintSpeed {
switch (speed) {
case 0:
return PrintSpeed.auto;
return PrintSpeed.ipsAuto;
case 1:
return PrintSpeed.ips1;
case 2:
Expand Down Expand Up @@ -112,7 +116,7 @@ export class PrintSpeedSettings {
export enum PrintSpeed {
unknown = -1,
/** Mobile printers can't be configured otherwise. */
auto = 0,
ipsAuto = 0,
/** The lowest speed a given printer supports. */
ipsPrinterMin,
ips1,
Expand Down
5 changes: 5 additions & 0 deletions src/Printers/Configuration/PrinterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export class PrinterOptions implements IPrinterFactoryInformation, Media.IPrinte
return this.dotToInch(this.labelGapDots);
}

labelLineOffsetDots: number;
get labelLineOffsetInches() {
return this.dotToInch(this.labelLineOffsetDots);
}

labelWidthDots: number;
get labelWidthInches() {
return this.dotToInch(this.labelWidthDots);
Expand Down
Loading

0 comments on commit 507e98a

Please sign in to comment.