You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RNTscCommand.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // RNTscCommand.m
  3. // RNBluetoothEscposPrinter
  4. //
  5. // Created by januslo on 2018/10/9.
  6. // Copyright © 2018年 Facebook. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <UIKit/UIKit.h>
  10. #import "RNTscCommand.h"
  11. #import "ImageUtils.h"
  12. @implementation RNTscCommand
  13. -(id)init
  14. {
  15. _command = [[NSMutableData alloc] init];
  16. return self;
  17. }
  18. -(id)initWithSize:(NSInteger)width height:(NSInteger) height andGap:(NSInteger)gap
  19. {
  20. _command = [[NSMutableData alloc] init];
  21. [self addSize:width height:height];
  22. [self addGap:gap];
  23. return self;
  24. }
  25. -(void)addStrToCommand:(NSString *)str
  26. {
  27. if(str){
  28. //encoding GBK? UTF-8? GB2312?
  29. [_command appendData:[str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)]];
  30. }
  31. }
  32. -(void)addGap:(NSInteger)gap
  33. {
  34. [self addStrToCommand:[NSString stringWithFormat:@"GAP %lu mm,%d mm\r\n",(int)gap,0]];
  35. }
  36. -(void)addSize:(NSInteger)width height:(NSInteger) height
  37. {
  38. [self addStrToCommand:[NSString stringWithFormat:@"SIZE %ld mm,%ld mm\r\n",width,height]];
  39. }
  40. -(void)addCashdrwer:(NSInteger) m/*0,1*/ t1:(NSInteger) t1 t2:(NSInteger) t2
  41. {
  42. [self addStrToCommand:[NSString stringWithFormat:@"CASHDRAWER %ld,%ld,%ld\r\n",m,t1,t2]];
  43. }
  44. -(void)addOffset:(NSInteger) offset
  45. {
  46. [self addStrToCommand:[NSString stringWithFormat:@"OFFSET %ld mm\r\n",offset]];
  47. }
  48. /* SPEED1DIV5(1.5F), SPEED2(2.0F), SPEED3(3.0F), SPEED4(4.0F){ } */
  49. -(void)addSpeed:(float) speed
  50. {
  51. [self addStrToCommand:[NSString stringWithFormat:@"SPEED %f\r\n",speed]];
  52. }
  53. /* DNESITY0(0), DNESITY1(1), DNESITY2(2), DNESITY3(3), DNESITY4(4), DNESITY5(5), DNESITY6(6), DNESITY7(
  54. 7), DNESITY8(8), DNESITY9(9), DNESITY10(10), DNESITY11(11), DNESITY12(12), DNESITY13(13), DNESITY14(
  55. 14), DNESITY15(15) */
  56. -(void)addDensity:(NSInteger)density
  57. {
  58. [self addStrToCommand:[NSString stringWithFormat:@"DENSITY %ld\r\n",density]];
  59. }
  60. /* FORWARD(0), BACKWARD(1){ }*/
  61. -(void)addDirection:(NSInteger) direction
  62. {
  63. [self addStrToCommand:[NSString stringWithFormat:@"DIRECTION %ld\r\n",direction]];
  64. }
  65. -(void)addReference:(NSInteger) x y:(NSInteger) y
  66. {
  67. [self addStrToCommand:[NSString stringWithFormat:@"REFERENCE %ld,%ld\r\n",x,y]];
  68. }
  69. -(void)addShif:(NSInteger) shif
  70. {
  71. [self addStrToCommand:[NSString stringWithFormat:@"SHIFT %ld\r\n",shif]];
  72. }
  73. -(void)addCls
  74. {
  75. [self addStrToCommand:@"CLS\r\n"];
  76. }
  77. -(void)addFeed:(NSInteger) dot
  78. {
  79. [self addStrToCommand:[NSString stringWithFormat:@"FEED %ld\r\n",dot]];
  80. }
  81. -(void)addBackFeed:(NSInteger) backDot
  82. {
  83. [self addStrToCommand:[NSString stringWithFormat:@"BACKFEED %ld\r\n",backDot]];
  84. }
  85. -(void)addFormFeed
  86. {
  87. [self addStrToCommand:@"FORMFEED\r\n"];
  88. }
  89. -(void)addHome
  90. {
  91. [self addStrToCommand:@"HOME\r\n"];
  92. }
  93. -(void)addPrint:(NSInteger) m n:(NSInteger) n
  94. {
  95. [self addStrToCommand:[NSString stringWithFormat:@"PRINT %ld,%ld\r\n",m,n]];
  96. }
  97. /* PC437(437), PC850(850), PC852(852), PC860(860), PC863(863), PC865(865), WPC1250(1250), WPC1252(1252), WPC1253(
  98. 1253), WPC1254(1254){ } */
  99. -(void)addCodePage:(NSInteger) page
  100. {
  101. [self addStrToCommand:[NSString stringWithFormat:@"CODEPAGE %ld\r\n",page]];
  102. }
  103. -(void)addSound:(NSInteger) level interval:(NSInteger) interval
  104. {
  105. [self addStrToCommand:[NSString stringWithFormat:@"SOUND %ld,%ld\r\n",level,interval]];
  106. }
  107. -(void)addLimitFeed:(NSInteger) n
  108. {
  109. [self addStrToCommand:[NSString stringWithFormat:@"LIMITFEED %ld\r\n",n]];
  110. }
  111. -(void)addSelfTest
  112. {
  113. [self addStrToCommand:@"SELFTEST\r\n"];
  114. }
  115. -(void)addBar:(NSInteger) x y:(NSInteger) y
  116. width:(NSInteger) width heigth:(NSInteger) height
  117. {
  118. [self addStrToCommand:[NSString stringWithFormat:@"BAR %ld,%ld,%ld,%ld\r\n",
  119. x,y,width,height]];
  120. }
  121. -(void)addText:(NSInteger) x y:(NSInteger) y
  122. fontType:(NSString *) font rotation:(NSInteger) rotation
  123. xscal:(NSInteger) Xscal yscal:(NSInteger) Yscal
  124. text:(NSString *) text
  125. {
  126. [self addStrToCommand:[NSString stringWithFormat:@"TEXT %ld,%ld,\"%@\",%ld,%ld,%ld,\"%@\"\r\n",
  127. x,y,font,rotation,Xscal,Yscal,text]];
  128. }
  129. -(void)add1DBarcode:(NSInteger) x y:(NSInteger) y
  130. barcodeType:(NSString *) type height:(NSInteger) height
  131. wide:(NSInteger) wide narrow:(NSInteger) narrow
  132. readable:(NSInteger) readable rotation:(NSInteger) rotation
  133. content:(NSString *) content
  134. {
  135. //str = "BARCODE " + x + "," + y + "," + "\"" + type.getValue() + "\"" + "," + height + "," + readable.getValue()
  136. // + "," + rotation.getValue() + "," + narrow + "," + wide + "," + "\"" + content + "\"" + "\r\n";
  137. NSString *c =[NSString stringWithFormat:@"BARCODE %ld,%ld,\"%@\",%ld,%ld,%ld,%d,%d,\"%@\"\r\n",
  138. x,y,type,height,readable,rotation,narrow,wide,content];
  139. NSLog(@"BARCODE COMMAND:%@",c);
  140. [self addStrToCommand:c];
  141. }
  142. -(void)addQRCode:(NSInteger) x y:(NSInteger) y errorCorrectionLevel:(NSString *) level
  143. width:(NSInteger) qrWidth rotation:(NSInteger) rotation
  144. code:(NSString *) code
  145. {
  146. [self addStrToCommand:[NSString stringWithFormat:@"QRCODE %ld,%ld,%@,%ld,A,%ld,M2,S1,\"%@\"\r\n",
  147. x,y,level,qrWidth,rotation,code]];
  148. }
  149. -(void)addBitmap:(NSInteger) x y:(NSInteger) y
  150. bitmapMode:(NSInteger) mode width:(NSInteger) nWidth
  151. bitmap:(UIImage *) b{
  152. //todo: NEED TO IMPLEMENT>>>>>>
  153. if (b) {
  154. CGFloat imgWidth = b.size.width;
  155. CGFloat imgHeigth = b.size.height;
  156. NSInteger width = (nWidth + 7) / 8 * 8;
  157. NSInteger height = imgHeigth * width / imgWidth;
  158. UIImage *resized = [ImageUtils imageWithImage:b scaledToFillSize:CGSizeMake(width, height)];
  159. uint8_t * graybits = [ImageUtils imageToGreyImage:resized];
  160. NSInteger srcLen = (int)resized.size.width*resized.size.height;
  161. NSData *codecontent = [ImageUtils pixToTscCmd:graybits width:srcLen];
  162. height = srcLen / width;
  163. width /= 8;
  164. NSString *str =[NSString stringWithFormat:@ "BITMAP %ld,%ld,%ld,%ld,%ld,",
  165. x,y,width,height,mode];
  166. [self addStrToCommand:str];
  167. [_command appendData:codecontent];
  168. [self addStrToCommand:@"\r\n"];
  169. }
  170. }
  171. -(void)addBox:(NSInteger) x y:(NSInteger) y xend:(NSInteger) xend yend:(NSInteger) yend
  172. {
  173. [self addStrToCommand:[NSString stringWithFormat:@"BAR %ld,%ld,%ld,%ld\r\n",
  174. x,y,xend,yend]];
  175. }
  176. -(void)addErase:(NSInteger) x y:(NSInteger) y xwidth:(NSInteger) xwidth yheigth:(NSInteger) yheight
  177. {
  178. [self addStrToCommand:[NSString stringWithFormat:@"ERASE %ld,%ld,%ld,%ld\r\n",
  179. x,y,xwidth,yheight]];
  180. }
  181. -(void)addReverse:(NSInteger) x y:(NSInteger) y xwidth:(NSInteger) xwidth yheigth:(NSInteger) yheight
  182. {
  183. [self addStrToCommand:[NSString stringWithFormat:@"REVERSE %ld,%ld,%ld,%ld\r\n",
  184. x,y,xwidth,yheight]];
  185. }
  186. -(void)addPeel:(NSString *) enable
  187. {
  188. [self addStrToCommand:[NSString stringWithFormat:@"SET PEEL %@\r\n",enable]];
  189. }
  190. -(void)addTear:(NSString *) enable
  191. {
  192. [self addStrToCommand:[NSString stringWithFormat:@"SET TEAR %@\r\n",enable]];
  193. }
  194. -(void)addCutter:(NSString *) enable
  195. {
  196. [self addStrToCommand:[NSString stringWithFormat:@"SET CUTTER %@\r\n",enable]];
  197. }
  198. -(void)addPartialCutter :(NSString *) enable
  199. {
  200. [self addStrToCommand:[NSString stringWithFormat:@"SET PARTIAL_CUTTER %@\r\n",enable]];
  201. }
  202. -(float)findSpeedValue:(NSInteger) sp{
  203. // SPEED1DIV5(1.5F), SPEED2(2.0F), SPEED3(3.0F), SPEED4(4.0F);
  204. switch (sp) {
  205. case 2:
  206. return 2.0f;
  207. break;
  208. case 3:
  209. return 3.0f;
  210. break;
  211. case 4:
  212. return 4.0f;
  213. break;
  214. case 1:
  215. default:
  216. return 1.5f;
  217. break;
  218. }
  219. }
  220. @end