barcode static method

List<int> barcode(
  1. String data,
  2. int type,
  3. int height,
  4. int width,
  5. int position,
  6. int font,
)

Implementation

static List<int> barcode(
    String data, int type, int height, int width, int position, int font) {
  List<int> commands = [];

  // GS h - Altura del código de barras
  commands.addAll([0x1D, 0x68, height.clamp(1, 255)]);

  // GS w - Ancho del código de barras
  commands.addAll([0x1D, 0x77, width.clamp(2, 6)]);

  // GS H - Posición del texto HRI
  commands.addAll([0x1D, 0x48, position.clamp(0, 3)]);

  // GS f - Fuente del texto HRI
  commands.addAll([0x1D, 0x66, font.clamp(0, 1)]);

  // GS k - Imprimir código de barras
  commands.addAll([0x1D, 0x6B, type.clamp(0, 73)]);

  // Longitud y datos (depende del tipo de código de barras)
  if (type >= 65 && type <= 73) {
    // Tipos 65-73 usan un formato diferente
    List<int> dataBytes = utf8.encode(data);
    commands.add(dataBytes.length);
    commands.addAll(dataBytes);
  } else {
    // Tipos 0-6 terminan con NUL
    commands.addAll(utf8.encode(data));
    commands.add(0x00);
  }

  return commands;
}