File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @node-escpos/core " : minor
3
+ ---
4
+
5
+ Added function to set left margin using GS command
Original file line number Diff line number Diff line change @@ -162,6 +162,23 @@ export class Printer<AdapterCloseArgs extends []> extends EventEmitter {
162
162
return this ;
163
163
}
164
164
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
+
165
182
/**
166
183
* Fix right margin
167
184
* @param {[String] } size
Original file line number Diff line number Diff line change @@ -40,3 +40,27 @@ export type AnyCase<T extends string> = Uppercase<T> | Lowercase<T>;
40
40
export function isKey < T extends { } | [ ] > ( key : string | number | symbol , of : T ) : key is keyof T {
41
41
return key in of ;
42
42
}
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
+ }
You can’t perform that action at this time.
0 commit comments