row static method

List<int> row({
  1. required List<String> texts,
  2. required List<int> proportions,
  3. FontSize fontSize = FontSize.normal,
  4. AlignPos align = AlignPos.left,
})

Returns a string for a printing line depending on the amount of text. @texts = List of texts to place on the line. @porportions = List of proportions between (1 and 100) %. @fontSize = FontSize.normal

Implementation

static List<int> row({required List<String> texts, required List<int> proportions, FontSize fontSize = FontSize.normal, AlignPos align = AlignPos.left}) {
  String textadd = "";
  String reset = '\x1B@';
  String enter = "\n";

  //revisar si los textops y las proporciones estan en la misma cantidad
  if (proportions.length != texts.length) {
    String msj = "error: La cantidad de proporciones y texts debe ser mayor igual (proportions: ${proportions.length} texts: ${texts.length})";
    throw Exception(msj);
  }
  //revisar el total de proporciones
  int totalProporciones = 0;
  for (int p in proportions) {
    totalProporciones += p;
  }

  const cFontNormal = '\x1b\x4d\x00'; // Fuente estándar ASCII (2)
  const cFontCompressed = '\x1b\x4d\x01'; // Fuente comprimida
  const cDoubleHeightFont = '\x1d\x21\x11'; // Altura doblada
  const cDoubleWidthFont = '\x1d\x21\x22'; // Ancho doblado
  const cBigFont = '\x1d\x21\x33'; // Ancho y alto doblados
  const cTripleHeightFont = '\x1d\x21\x33'; // Altura doblada (5)

  //80 mm
  String fontSizeCode = "";
  int maxCaracteres = 48; //normal
  switch (fontSize) {
    case FontSize.compressed:
      maxCaracteres = 64;
      fontSizeCode = cFontCompressed;
      break;
    case FontSize.normal:
      maxCaracteres = 48;
      fontSizeCode = cFontNormal;
      break;
    case FontSize.doubleWidth:
      maxCaracteres = 32;
      fontSizeCode = cDoubleWidthFont;
      break;
    case FontSize.doubleHeight:
      maxCaracteres = 32;
      fontSizeCode = cDoubleHeightFont;
      break;
    case FontSize.big:
      maxCaracteres = 24;
      fontSizeCode = cBigFont;
      break;
    case FontSize.superBig:
      maxCaracteres = 16;
      fontSizeCode = cTripleHeightFont;
  }

  /*if (_paperSize == PaperSize.mm58) {
    return (font == null || font == PosFontType.fontA) ? 32 : 42;
  } else if (_paperSize == PaperSize.mm72) {
    return (font == null || font == PosFontType.fontA) ? 42 : 56;
  } else {
    return (font == null || font == PosFontType.fontA) ? 48 : 64;
  }*/

  if (totalProporciones != 100) {
    String msj = "error: el total de proporciones debe ser igual a 100% ($totalProporciones %)";
    throw Exception(msj);
  }

  // Aplicar los comandos según los parámetros
  const cAlignLeft = '\x1Ba0'; // Alinear a la izquierda
  const cAlignCenter = '\x1Ba1'; // Alinear al centro
  const cAlignRight = '\x1Ba2'; // Alinear a la derecha

  String alignmentCode = "";
  if (align == AlignPos.left) {
    alignmentCode = cAlignLeft;
  } else if (align == AlignPos.center) {
    alignmentCode = cAlignCenter;
  } else if (align == AlignPos.right) {
    alignmentCode = cAlignRight;
  }

  textadd = "";

  //sacar cuantos caracteres por proporcion
  List<int> caracteres = [];
  for (int proporcion in proportions) {
    int ctrs = (proporcion * maxCaracteres) ~/ 100;
    //print("ctrs: $ctrs proporcion: $proporcion % max caracteres: $maxCaracteres");
    caracteres.add(ctrs);
  }

  for (int i = 0; i < texts.length; i++) {
    String text = texts[i];
    int ctrs = caracteres[i];
    //print("ctrs: $ctrs text: $text textadd: $textadd");
    if (text.length >= ctrs) {
      text = "${text.substring(0, ctrs - 2)}  ";
    } else {
      int espacios = ctrs - text.length;
      for (int j = 0; j < espacios; j++) {
        text += " ";
      }
    }

    textadd += text;
  }
  String textfinal = "$reset$fontSizeCode$alignmentCode$textadd$enter";

  return textfinal.codeUnits;
}