row method

void row({
  1. required List<PrintColumn> columns,
  2. int spacing = 10,
})

Implementation

void row({required List<PrintColumn> columns, int spacing = 10}) {
  int xPosition = margin.left;
  int totalWidth =
      paperSize.width - margin.width - (spacing * (columns.length - 1));
  int totalRatio = columns.fold(0, (sum, col) => sum + col.flex);

  int maxLines = 0;
  for (PrintColumn column in columns) {
    int columnWidth = (totalWidth * (column.flex / totalRatio)).round();
    int textXPosition = xPosition;
    final rowYPosition = runningHeight;

    // Split text into lines that fit within the column width
    List<String> lines = [];
    String currentLine = '';
    for (String word in column.text.split(' ')) {
      String testLine = currentLine.isEmpty ? word : '$currentLine $word';
      if (font.getMetrics(testLine).width <= columnWidth) {
        currentLine = testLine;
      } else {
        lines.add(currentLine);
        currentLine = word;
      }
    }
    if (currentLine.isNotEmpty) {
      lines.add(currentLine);
    }

    int tempRunningHeight = rowYPosition;

    if (maxLines < lines.length) {
      maxLines = lines.length;
    }

    _ensureHeight(runningHeight + (maxLines * font.lineHeight) + 10);
    img.BitmapFont textFont = font;
    PrintAlign align = PrintAlign.left;
    // Draw each line within the column
    for (String line in lines) {
      final arabic = isArabic(line);
      if (arabic) {
        textFont = arabic24;
        // Step 1: Shape the Arabic characters into their presentation forms.
        // The output of ShapeArabic.shape is in logical order (LTR sequence of glyphs).
        line = ShapeArabic.shape(line);
      }

      textFont = _getFont(
        column.style,
        isArabic: arabic,
      ); // Make sure _getFont returns a font that handles RTL if you use it
      align = column.style.align;

      if (align == PrintAlign.center) {
        textXPosition =
            xPosition +
            ((columnWidth - font.getMetrics(line).width) / 2).round();
      } else if (align == PrintAlign.right) {
        textXPosition =
            xPosition + (columnWidth - font.getMetrics(line).width).round();
      }

      drawString(
        utilImage,
        line,
        font: textFont,
        x: textXPosition,
        y: tempRunningHeight,
        color: textColor,
      );
      tempRunningHeight += font.lineHeight;
    }

    xPosition += columnWidth + spacing;
  }

  runningHeight += (maxLines * font.lineHeight) + 10;
}