@amitkhare/capacitor-cat-printer 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -193,13 +193,17 @@ public class CatPrinterPlugin extends Plugin {
193
193
  }
194
194
 
195
195
  int paperWidth = call.getInt("paperWidth", PrinterProtocol.WIDTH_58MM);
196
+ int timeout = call.getInt("timeout", 10000);
197
+ Boolean dryRunObj = call.getBoolean("dryRun", false);
198
+ boolean dryRun = dryRunObj != null ? dryRunObj : false;
196
199
 
197
- printerCore.connect(address, paperWidth, new CatPrinterCore.ConnectionCallback() {
200
+ printerCore.connect(address, paperWidth, timeout, dryRun, new CatPrinterCore.ConnectionCallback() {
198
201
  @Override
199
202
  public void onConnected() {
200
203
  JSObject state = new JSObject();
201
204
  state.put("connected", true);
202
205
  state.put("address", address);
206
+ state.put("dryRun", dryRun);
203
207
  notifyListeners("connectionState", state);
204
208
  call.resolve();
205
209
  }
@@ -259,8 +263,19 @@ public class CatPrinterPlugin extends Plugin {
259
263
  int threshold = call.getInt("threshold", 127);
260
264
  Boolean ditherObj = call.getBoolean("dither", true);
261
265
  boolean dither = ditherObj != null ? ditherObj : true;
266
+ Boolean flipHObj = call.getBoolean("flipHorizontal", false);
267
+ boolean flipH = flipHObj != null ? flipHObj : false;
268
+ Boolean flipVObj = call.getBoolean("flipVertical", false);
269
+ boolean flipV = flipVObj != null ? flipVObj : false;
270
+ Boolean compressObj = call.getBoolean("compress", false);
271
+ boolean compress = compressObj != null ? compressObj : false;
272
+ int brightness = call.getInt("brightness", 0);
273
+ int contrast = call.getInt("contrast", 0);
274
+ Boolean invertObj = call.getBoolean("invert", false);
275
+ boolean invert = invertObj != null ? invertObj : false;
262
276
 
263
- printerCore.printImage(imageBase64, energy, quality, feedAfter, threshold, dither,
277
+ printerCore.printImageAdjusted(imageBase64, energy, quality, feedAfter, threshold, dither,
278
+ flipH, flipV, compress, brightness, contrast, invert,
264
279
  new CatPrinterCore.PrintCallback() {
265
280
  @Override
266
281
  public void onProgress(int percent, String status, String message) {
@@ -295,15 +310,26 @@ public class CatPrinterPlugin extends Plugin {
295
310
  String align = call.getString("align", "left");
296
311
  Boolean boldObj = call.getBoolean("bold", false);
297
312
  boolean bold = boldObj != null ? boldObj : false;
313
+ Boolean italicObj = call.getBoolean("italic", false);
314
+ boolean italic = italicObj != null ? italicObj : false;
315
+ Boolean underlineObj = call.getBoolean("underline", false);
316
+ boolean underline = underlineObj != null ? underlineObj : false;
317
+ Boolean strikethroughObj = call.getBoolean("strikethrough", false);
318
+ boolean strikethrough = strikethroughObj != null ? strikethroughObj : false;
298
319
  Float lineSpacingObj = call.getFloat("lineSpacing", 1.2f);
299
320
  float lineSpacing = lineSpacingObj != null ? lineSpacingObj : 1.2f;
300
321
  Float energyObj = call.getFloat("energy", 0.6f);
301
322
  float energy = energyObj != null ? energyObj : 0.6f;
302
323
  int quality = call.getInt("quality", 3);
303
324
  int feedAfter = call.getInt("feedAfter", 100);
325
+ Boolean wordWrapObj = call.getBoolean("wordWrap", true);
326
+ boolean wordWrap = wordWrapObj != null ? wordWrapObj : true;
327
+ Boolean rtlObj = call.getBoolean("rtl", false);
328
+ boolean rtl = rtlObj != null ? rtlObj : false;
329
+ String fontPath = call.getString("fontPath", null);
304
330
 
305
- printerCore.printText(text, fontSize, align, bold, lineSpacing,
306
- energy, quality, feedAfter,
331
+ printerCore.printTextStyled(text, fontSize, align, bold, italic, underline, strikethrough,
332
+ lineSpacing, energy, quality, feedAfter, wordWrap, rtl, fontPath,
307
333
  new CatPrinterCore.PrintCallback() {
308
334
  @Override
309
335
  public void onProgress(int percent, String status, String message) {
@@ -326,6 +352,55 @@ public class CatPrinterPlugin extends Plugin {
326
352
  });
327
353
  }
328
354
 
355
+ @PluginMethod
356
+ public void printRichText(PluginCall call) {
357
+ JSArray segmentsArray = call.getArray("segments");
358
+ if (segmentsArray == null || segmentsArray.length() == 0) {
359
+ call.reject("segments is required");
360
+ return;
361
+ }
362
+
363
+ try {
364
+ int segmentCount = segmentsArray.length();
365
+ String[] texts = new String[segmentCount];
366
+ boolean[] bolds = new boolean[segmentCount];
367
+ boolean[] italics = new boolean[segmentCount];
368
+ boolean[] underlines = new boolean[segmentCount];
369
+ boolean[] strikethroughs = new boolean[segmentCount];
370
+ int[] fontSizes = new int[segmentCount];
371
+
372
+ int defaultFontSize = call.getInt("fontSize", 24);
373
+
374
+ for (int i = 0; i < segmentCount; i++) {
375
+ org.json.JSONObject segment = segmentsArray.getJSONObject(i);
376
+ texts[i] = segment.optString("text", "");
377
+ bolds[i] = segment.optBoolean("bold", false);
378
+ italics[i] = segment.optBoolean("italic", false);
379
+ underlines[i] = segment.optBoolean("underline", false);
380
+ strikethroughs[i] = segment.optBoolean("strikethrough", false);
381
+ fontSizes[i] = segment.optInt("fontSize", defaultFontSize);
382
+ }
383
+
384
+ String align = call.getString("align", "left");
385
+ Float lineSpacingObj = call.getFloat("lineSpacing", 1.2f);
386
+ float lineSpacing = lineSpacingObj != null ? lineSpacingObj : 1.2f;
387
+ Float energyObj = call.getFloat("energy", 0.6f);
388
+ float energy = energyObj != null ? energyObj : 0.6f;
389
+ int quality = call.getInt("quality", 3);
390
+ int feedAfter = call.getInt("feedAfter", 100);
391
+ Boolean rtlObj = call.getBoolean("rtl", false);
392
+ boolean rtl = rtlObj != null ? rtlObj : false;
393
+ String fontPath = call.getString("fontPath", null);
394
+
395
+ printerCore.printRichText(texts, bolds, italics, underlines, strikethroughs, fontSizes,
396
+ defaultFontSize, align, lineSpacing, energy, quality, feedAfter, rtl, fontPath,
397
+ createPrintCallback(call));
398
+
399
+ } catch (Exception e) {
400
+ call.reject("Failed to parse segments: " + e.getMessage());
401
+ }
402
+ }
403
+
329
404
  @PluginMethod
330
405
  public void feedPaper(PluginCall call) {
331
406
  int pixels = call.getInt("pixels", 100);
@@ -345,4 +420,275 @@ public class CatPrinterPlugin extends Plugin {
345
420
  }
346
421
  });
347
422
  }
423
+
424
+ @PluginMethod
425
+ public void cancelPrint(PluginCall call) {
426
+ printerCore.cancelPrint(new CatPrinterCore.PrintCallback() {
427
+ @Override
428
+ public void onProgress(int percent, String status, String message) {}
429
+
430
+ @Override
431
+ public void onComplete() {
432
+ call.resolve();
433
+ }
434
+
435
+ @Override
436
+ public void onError(String error) {
437
+ call.reject(error);
438
+ }
439
+ });
440
+ }
441
+
442
+ @PluginMethod
443
+ public void retractPaper(PluginCall call) {
444
+ int pixels = call.getInt("pixels", 100);
445
+
446
+ printerCore.retractPaper(pixels, new CatPrinterCore.PrintCallback() {
447
+ @Override
448
+ public void onProgress(int percent, String status, String message) {}
449
+
450
+ @Override
451
+ public void onComplete() {
452
+ call.resolve();
453
+ }
454
+
455
+ @Override
456
+ public void onError(String error) {
457
+ call.reject(error);
458
+ }
459
+ });
460
+ }
461
+
462
+ @PluginMethod
463
+ public void getDeviceInfo(PluginCall call) {
464
+ printerCore.getDeviceInfo(new CatPrinterCore.PrintCallback() {
465
+ @Override
466
+ public void onProgress(int percent, String status, String message) {}
467
+
468
+ @Override
469
+ public void onComplete() {
470
+ JSObject result = new JSObject();
471
+ result.put("success", true);
472
+ call.resolve(result);
473
+ }
474
+
475
+ @Override
476
+ public void onError(String error) {
477
+ JSObject result = new JSObject();
478
+ result.put("success", false);
479
+ result.put("error", error);
480
+ call.resolve(result);
481
+ }
482
+ });
483
+ }
484
+
485
+ @PluginMethod
486
+ public void getPrinterModel(PluginCall call) {
487
+ if (!printerCore.isConnected()) {
488
+ call.reject("Not connected to printer");
489
+ return;
490
+ }
491
+
492
+ CatPrinterCore.PrinterModelInfo modelInfo = printerCore.getPrinterModel();
493
+
494
+ JSObject result = new JSObject();
495
+ result.put("model", modelInfo.model);
496
+ result.put("isNewKind", modelInfo.isNewKind);
497
+ result.put("hasFeedingProblems", modelInfo.hasFeedingProblems);
498
+ result.put("paperWidth", modelInfo.paperWidth);
499
+ result.put("deviceName", modelInfo.deviceName);
500
+
501
+ call.resolve(result);
502
+ }
503
+
504
+ // ==================== NEW FEATURES ====================
505
+
506
+ @PluginMethod
507
+ public void printBarcode(PluginCall call) {
508
+ String data = call.getString("data");
509
+ if (data == null || data.isEmpty()) {
510
+ call.reject("data is required");
511
+ return;
512
+ }
513
+
514
+ String type = call.getString("type", "CODE128");
515
+ int height = call.getInt("height", 80);
516
+ Boolean showTextObj = call.getBoolean("showText", true);
517
+ boolean showText = showTextObj != null ? showTextObj : true;
518
+ String align = call.getString("align", "center");
519
+ Float energyObj = call.getFloat("energy", 0.6f);
520
+ float energy = energyObj != null ? energyObj : 0.6f;
521
+ int feedAfter = call.getInt("feedAfter", 100);
522
+
523
+ printerCore.printBarcode(data, type, height, showText, align, energy, feedAfter,
524
+ createPrintCallback(call));
525
+ }
526
+
527
+ @PluginMethod
528
+ public void printQRCode(PluginCall call) {
529
+ String data = call.getString("data");
530
+ if (data == null || data.isEmpty()) {
531
+ call.reject("data is required");
532
+ return;
533
+ }
534
+
535
+ int size = call.getInt("size", 200);
536
+ String errorCorrection = call.getString("errorCorrection", "M");
537
+ String align = call.getString("align", "center");
538
+ Float energyObj = call.getFloat("energy", 0.6f);
539
+ float energy = energyObj != null ? energyObj : 0.6f;
540
+ int feedAfter = call.getInt("feedAfter", 100);
541
+
542
+ printerCore.printQRCode(data, size, errorCorrection, align, energy, feedAfter,
543
+ createPrintCallback(call));
544
+ }
545
+
546
+ @PluginMethod
547
+ public void printTable(PluginCall call) {
548
+ JSArray rowsArray = call.getArray("rows");
549
+ if (rowsArray == null || rowsArray.length() == 0) {
550
+ call.reject("rows is required");
551
+ return;
552
+ }
553
+
554
+ try {
555
+ // Parse rows
556
+ int rowCount = rowsArray.length();
557
+ String[][] rows = new String[rowCount][];
558
+ boolean[] boldRows = new boolean[rowCount];
559
+ int maxCols = 0;
560
+
561
+ for (int i = 0; i < rowCount; i++) {
562
+ org.json.JSONObject rowJson = rowsArray.getJSONObject(i);
563
+ org.json.JSONArray columnsJson = rowJson.getJSONArray("columns");
564
+ boolean rowBold = rowJson.optBoolean("bold", false);
565
+ boldRows[i] = rowBold;
566
+
567
+ int colCount = columnsJson.length();
568
+ maxCols = Math.max(maxCols, colCount);
569
+ rows[i] = new String[colCount];
570
+
571
+ for (int j = 0; j < colCount; j++) {
572
+ org.json.JSONObject colJson = columnsJson.getJSONObject(j);
573
+ rows[i][j] = colJson.optString("text", "");
574
+ }
575
+ }
576
+
577
+ // Calculate column widths (equal distribution for now)
578
+ int paperWidth = printerCore.getPaperWidth();
579
+ int[] columnWidths = new int[maxCols];
580
+ int colWidth = paperWidth / maxCols;
581
+ for (int i = 0; i < maxCols; i++) {
582
+ columnWidths[i] = colWidth;
583
+ }
584
+
585
+ // Default alignments
586
+ String[] alignments = new String[maxCols];
587
+ for (int i = 0; i < maxCols; i++) {
588
+ alignments[i] = "left";
589
+ }
590
+
591
+ Boolean showLinesObj = call.getBoolean("showLines", false);
592
+ boolean showLines = showLinesObj != null ? showLinesObj : false;
593
+ String lineChar = call.getString("lineChar", "-");
594
+ int fontSize = call.getInt("fontSize", 24);
595
+ Float energyObj = call.getFloat("energy", 0.6f);
596
+ float energy = energyObj != null ? energyObj : 0.6f;
597
+ int feedAfter = call.getInt("feedAfter", 100);
598
+
599
+ printerCore.printTable(rows, columnWidths, alignments, boldRows, showLines,
600
+ lineChar, fontSize, energy, feedAfter, createPrintCallback(call));
601
+
602
+ } catch (Exception e) {
603
+ call.reject("Failed to parse table data: " + e.getMessage());
604
+ }
605
+ }
606
+
607
+ @PluginMethod
608
+ public void getPreview(PluginCall call) {
609
+ // Preview generation - returns processed image as base64
610
+ String type = call.getString("type");
611
+ if (type == null) {
612
+ call.reject("type is required");
613
+ return;
614
+ }
615
+
616
+ // For now, return a placeholder - full implementation would process the options
617
+ JSObject result = new JSObject();
618
+ result.put("imageBase64", "");
619
+ result.put("width", printerCore.getPaperWidth());
620
+ result.put("height", 0);
621
+ call.resolve(result);
622
+ }
623
+
624
+ @PluginMethod
625
+ public void getPrinterStatus(PluginCall call) {
626
+ CatPrinterCore.PrinterStatus status = printerCore.getPrinterStatus();
627
+
628
+ JSObject result = new JSObject();
629
+ result.put("connected", status.connected);
630
+ result.put("paperWidth", status.paperWidth);
631
+
632
+ // These may be null if not supported
633
+ if (status.batteryLevel != null) {
634
+ result.put("batteryLevel", status.batteryLevel);
635
+ } else {
636
+ result.put("batteryLevel", JSObject.NULL);
637
+ }
638
+
639
+ if (status.isCharging != null) {
640
+ result.put("isCharging", status.isCharging);
641
+ } else {
642
+ result.put("isCharging", JSObject.NULL);
643
+ }
644
+
645
+ if (status.hasPaper != null) {
646
+ result.put("hasPaper", status.hasPaper);
647
+ } else {
648
+ result.put("hasPaper", JSObject.NULL);
649
+ }
650
+
651
+ if (status.firmwareVersion != null) {
652
+ result.put("firmwareVersion", status.firmwareVersion);
653
+ } else {
654
+ result.put("firmwareVersion", JSObject.NULL);
655
+ }
656
+
657
+ call.resolve(result);
658
+ }
659
+
660
+ @PluginMethod
661
+ public void setAutoReconnect(PluginCall call) {
662
+ Boolean enabledObj = call.getBoolean("enabled", false);
663
+ boolean enabled = enabledObj != null ? enabledObj : false;
664
+ int maxAttempts = call.getInt("maxAttempts", 3);
665
+ int delayMs = call.getInt("delayMs", 2000);
666
+
667
+ printerCore.setAutoReconnect(enabled, maxAttempts, delayMs);
668
+ call.resolve();
669
+ }
670
+
671
+ // Helper method to create print callback
672
+ private CatPrinterCore.PrintCallback createPrintCallback(PluginCall call) {
673
+ return new CatPrinterCore.PrintCallback() {
674
+ @Override
675
+ public void onProgress(int percent, String status, String message) {
676
+ JSObject progress = new JSObject();
677
+ progress.put("percent", percent);
678
+ progress.put("status", status);
679
+ progress.put("message", message);
680
+ notifyListeners("printProgress", progress);
681
+ }
682
+
683
+ @Override
684
+ public void onComplete() {
685
+ call.resolve();
686
+ }
687
+
688
+ @Override
689
+ public void onError(String error) {
690
+ call.reject(error);
691
+ }
692
+ };
693
+ }
348
694
  }