esc_pos_utils icon indicating copy to clipboard operation
esc_pos_utils copied to clipboard

abstraction layer for pos printer ticket

Open ayushin opened this issue 5 years ago • 0 comments

Hi,

I am working on a package for scanning and persistent federated printing (bluetooth, network, star printers, brother label printers etc) of pos receipts using BLoC patterns (pos_printer_bloc)[https://github.com/apexlabs-ai/pos_printer_bloc] and I'm trying to base it on esc_pos_utils

One limitation I've come across is that I would like to have an abstraction layer between the ticket lines and the actual ESC representation.

I've now come up with this:

https://github.com/apexlabs-ai/pos_printer_bloc/blob/master/lib/src/pos_ticket_line.dart

and it would be good to have a constructor inside of esc_pos_utils Ticket.fromLines() to avoid the code duplication to go something like:

  Ticket _ticketFromLines(PaperSize paperSize, CapabilityProfile profile, {List <PosTicketLine> lines}) {
    final ticket = Ticket(paperSize, profile);
    for(var line in lines) {
      if(line is PosTicketText) {
        ticket.text(line.text, styles: line.styles, linesAfter: line.linesAfter);
      } else if(line is PosTicketRow) {
        ticket.row(line.cols);
      } else if(line is PosTicketBeep) {
        ticket.beep(n: line.n, duration: line.duration);
      } else if(line is PostTicketReset) {
        ticket.reset();
      } else if(line is PosTicketCut) {
        ticket.cut(mode: line.mode);
      } else if(line is PosTicketHr) {
        ticket.hr();
      } else if(line is PosTicketDrawer) {
        ticket.drawer(pin: line.pin);
      } else if(line is PosTicketFeed) {
        ticket.feed(line.lines);
      } else if(line is PosTicketImage) {
        ticket.image(line.image, align: line.align);
      } else if(line is PosTicketBarcode) {
        ticket.barcode(line.barcode,
            width: line.width,
            height: line.height,
            font: line.font,
            textPos: line.textPos,
            align: line.align
        );
      } else if(line is PosTicketQrcode) {
        ticket.qrcode(line.text, align: line.align,
            size: line.size,
            cor: line.correction);
      } else {
        print("_ticketFromLines: unsupported line type ${line.runtimeType}");
      }
    }
    return ticket;
  }

This would allow us to have similar methods for other printers, like:

  PrintCommands _commandsFromLines({List <PosTicketLine> lines}) {
    PrintCommands commands = PrintCommands();

    //    commands.appendEncoding(StarEncoding.UTF8);
    for(var line in lines) {
      if(line is PosTicketText) {
        commands.appendBitmapText(
            text: line.text,
            alignment:  _starAlignment[line.styles.align],
//            fontSize: line.styles.height.value,
//            width: line.styles.width.value
        );
      } else if(line is PosTicketCut) {
        commands.appendCutPaper(_starCutAction[line.mode]);
      } else if(line is PosTicketHr) {
        commands.appendBitmapText(text: '--------------------------------------');
      } else if(line is PosTicketFeed) {
        for (int i = 1; i < line.lines; i++) {
          commands.appendBitmapText(text: "\n\n");
        }
      } else {
        print("_commandsFromLines: unsupported line type ${line.runtimeType}");
      }
    }

ayushin avatar Jun 18 '20 09:06 ayushin