Skip to content

Commit 13ddd6d

Browse files
authored
Merge pull request #39 from dschweinbenz/feature/leftMargin
Add functionality to set left margin using GS command
2 parents dbdaffc + 8d76c83 commit 13ddd6d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

.changeset/strange-lies-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@node-escpos/core": minor
3+
---
4+
5+
Added function to set left margin using GS command

packages/core/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ export class Printer<AdapterCloseArgs extends []> extends EventEmitter {
162162
return this;
163163
}
164164

165+
/**
166+
* Set left margin using GS command
167+
* @param {[String]} size
168+
* @return {[Printer]} printer [the escpos printer instance]
169+
*/
170+
setMarginLeft(size: number): Printer<AdapterCloseArgs> {
171+
if (size > 65535) {
172+
throw new Error("Max margin range exceeded");
173+
}
174+
// 1D 4C nL nH
175+
this.buffer.write(_.GS);
176+
this.buffer.write("\x4C");
177+
const nL_nH = utils.intLowHighHex(size, 2);
178+
this.buffer.write(Buffer.from(nL_nH, 'hex'));
179+
return this;
180+
}
181+
165182
/**
166183
* Fix right margin
167184
* @param {[String]} size

packages/core/src/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,27 @@ export type AnyCase<T extends string> = Uppercase<T> | Lowercase<T>;
4040
export function isKey<T extends {} | []>(key: string | number | symbol, of: T): key is keyof T {
4141
return key in of;
4242
}
43+
44+
/**
45+
* Function to encode a number as hex in format low to high values
46+
* @param {[Number]} input
47+
* @param {[Number]} length of hex couples (0x01 0x00 ...)
48+
* @returns {[string]} hex value low-high order
49+
*/
50+
export function intLowHighHex(input: number, length: number = 1): string {
51+
if (input < 0 || input % 1 !== 0) {
52+
throw new Error('Input must be greater or equal than 0');
53+
} else if (length < 0) {
54+
throw new Error('Length muste be greater than 0');
55+
}
56+
let ret = ''
57+
for (let i = 0; i < length; i++) {
58+
let value = (input % 256).toString(16).toUpperCase();
59+
if (value === '0' || value.length % 2 !== 0) {
60+
value = `0${value}`;
61+
}
62+
ret += value;
63+
input = Math.floor(input / 256);
64+
}
65+
return ret;
66+
}

0 commit comments

Comments
 (0)