print method

Future<int> print(
  1. String prt,
  2. PrintPayload payload
)

Implementation

Future<int> print(String prt, PrintPayload payload) async {
  int resCode = await _plugin.open(prt);
  if (resCode != NRet.N_SUCCESS && resCode != NRet.N_WRN_PRTALREADYOPEN) {
    return resCode;
  }

  resCode = await _plugin.startDoc(prt);
  if (resCode != NRet.N_SUCCESS) {
    return resCode;
  }

  StringBuffer buffer = StringBuffer();
  buffer.write('1B40');
  buffer.write('1C43011C26');
  for (var i = 0; i < payload.nodes.length; i++) {
    PrintNode node = payload.nodes[i];
    if (node.type == PrintType.text) {
      String content = node.text!;
      String contentWithStyle;

      PrintTextStyle style = node.style ?? PrintTextStyle.defaultValue();
      if (style.isTitle == true) {
        contentWithStyle = '1B61011B21201C2104"$content"1C21001B21000A';
      } else {
        String align;
        switch (style.align) {
          case PrintTextAlign.left:
            align = '1B61001B241000';
            break;
          case PrintTextAlign.center:
            align = '1B6101';
            break;
          case PrintTextAlign.right:
            align = '1B42221B6102';
            break;
        }
        contentWithStyle = '$align"$content"0A';
      }
      String hexStr;
      if (i == 0) {
        buffer.write(contentWithStyle);
        hexStr = buffer.toString();
      } else {
        hexStr = contentWithStyle;
      }
      resCode = await _plugin.print(prt, hexStr);
    } else {
      if (node.path == null) {
        break;
      }
      resCode = await _plugin.printImage(prt, node.path!);
    }
    if (resCode != NRet.N_SUCCESS) {
      return resCode;
    }
  }

  String endStr = '1B4A801B6${payload.cutOff ? '9' : 'D'}1B40';
  resCode = await _plugin.print(prt, endStr);
  if (resCode != NRet.N_SUCCESS) {
    return resCode;
  }

  resCode = await _plugin.endDoc(prt);
  if (resCode != NRet.N_SUCCESS) {
    return resCode;
  }

  return resCode;
}