Skip to content

Commit

Permalink
Merge pull request #24 from hitz-group/GaneshTILLX/change-to-utf-enco…
Browse files Browse the repository at this point in the history
…ding

Ganesh tillx/change to utf encoding
  • Loading branch information
snehithv authored Nov 30, 2022
2 parents 48447ab + e5fc0b9 commit b351476
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"xml",
"printer"
],
"version": "0.2.4",
"version": "0.2.7",
"files": [
"lib",
"LICENSE",
Expand Down
47 changes: 37 additions & 10 deletions src/buffer-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { MutableBuffer } from "mutable-buffer";
import Image from "./image";
export class BufferBuilder {
private buffer: MutableBuffer;
private hasGSCommand: boolean;
private doEmphasise: boolean;

constructor(private defaultSettings: boolean = true) {
this.buffer = new MutableBuffer();
this.hasGSCommand = true;
this.doEmphasise = false;

if (this.defaultSettings) {
this.resetCharacterSize();
this.resetCharacterCodeTable();
}
}

public end(): BufferBuilder {
Expand All @@ -26,13 +26,40 @@ export class BufferBuilder {
width: number = 0,
height: number = 0
): BufferBuilder {
let size = (width << 4) + height;
this.buffer.write(Command.GS_exclamation(size));
if(this.hasGSCommand)
{
let size = (width << 4) + height;
this.buffer.write(Command.GS_exclamation(size));
}
else{
let mode = (width>0?(1<<5):0)+(height>0?(1<<4):0)+(this.doEmphasise?(1<<3):0)+1;
this.buffer.write(Command.ESC_exclamation(mode));
}

return this;
}

public setPrintMode(setting:boolean): BufferBuilder {
this.hasGSCommand = setting;
if(setting) this.resetCharacterEncoding();
this.resetCharacterSize();
return this;
}

public resetCharacterSize(): BufferBuilder {
this.buffer.write(Command.GS_exclamation(0));
if(this.hasGSCommand)
{
this.buffer.write(Command.GS_exclamation(0));
}
else
{
this.buffer.write(Command.ESC_exclamation(1));
}
return this;
}

public resetCharacterEncoding(): BufferBuilder {
this.buffer.write(Command.FS_ob_C_fe_utf);
return this;
}

Expand All @@ -47,12 +74,12 @@ export class BufferBuilder {
}

public startBold(): BufferBuilder {
this.buffer.write(Command.ESC_E(1));
this.hasGSCommand? this.buffer.write(Command.ESC_E(1)):this.doEmphasise=true;
return this;
}

public endBold(): BufferBuilder {
this.buffer.write(Command.ESC_E(0));
this.hasGSCommand? this.buffer.write(Command.ESC_E(0)):this.doEmphasise=false;
return this;
}

Expand Down Expand Up @@ -142,7 +169,7 @@ export class BufferBuilder {
}

public printText(text: string): BufferBuilder {
this.buffer.write(text, "ascii");
this.buffer.write(text, "utf8");
return this;
}

Expand Down
1 change: 1 addition & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Command {
public static ESC_Z = (m: number, n: number, k: number): number[] => [Command.ESC, 0x5A, m, n, k]; // ESCZmnk

public static FS_and: number[] = [Command.FS, 0x40]; //ESC@
public static FS_ob_C_fe_utf = [Command.FS, 0x28, 0x43, 0x02, 0x00, 0x30, 0x02]; //UTF-8 encoding

public static GS_exclamation = (n: number): number[] => [Command.GS, 0x21, n]; // ESC!n
public static GS_B = (n: number): number[] => [Command.GS, 0x42, n]; // GSBn
Expand Down
2 changes: 2 additions & 0 deletions src/node-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import WhiteModeNode from './nodes/white-mode-node';
import PaperCutNode from './nodes/paper-cut-node';
import ImageNode from './nodes/image-node';
import OpenCashDrawerNode from './nodes/open-cash-drawer-node';
import PrintModeNode from './nodes/print-mode';

export class NodeFactory {

Expand All @@ -33,6 +34,7 @@ export class NodeFactory {
case 'paper-cut': return new PaperCutNode(node);
case 'open-cash-drawer': return new OpenCashDrawerNode(node);
case 'image': return new ImageNode(node);
case 'print-mode': return new PrintModeNode(node);

default: return null;
}
Expand Down
27 changes: 27 additions & 0 deletions src/nodes/print-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { XMLNode } from '../xml-node';
import { BufferBuilder } from '../buffer-builder';

export default class PrintModeNode extends XMLNode {

constructor(node: any) {
super(node);
}

public open(bufferBuilder: BufferBuilder): BufferBuilder {
if(this.attributes.mode === 'U220')
{
bufferBuilder.setPrintMode(false);
}
else
{
bufferBuilder.setPrintMode(true);
}
return bufferBuilder;
}

public close(bufferBuilder: BufferBuilder): BufferBuilder {
bufferBuilder.resetAlign();
return bufferBuilder;
}

}

0 comments on commit b351476

Please sign in to comment.