toBuffer method

Buffer toBuffer()

Rebuilds a detached UV buffer from this frame.

Drawable payloads are not included in native metadata, so frames carrying one are rejected rather than silently becoming lossy text captures.

Implementation

Buffer toBuffer() {
  if (width < 1 || height < 1 || lines.length != height) {
    throw const FormatException('native frame does not contain every row');
  }
  final buffer = Buffer.create(width, height);
  try {
    for (var y = 0; y < height; y++) {
      final line = lines[y];
      if (line.index != y || line.cells.length != width) {
        throw const FormatException('native frame is not rectangular');
      }
      final target = buffer.line(y)!;
      for (var x = 0; x < width; x++) {
        // Line.replace consumes the freshly rebuilt cell without cloning it.
        // This also avoids applying wide-cell overwrite semantics to the
        // explicit zero-width placeholders in a native frame.
        final rebuilt = line.cells[x].toCell();
        try {
          target.replace(x, rebuilt);
        } catch (_) {
          rebuilt.dispose();
          rethrow;
        }
      }
    }
    return buffer;
  } catch (_) {
    // A drawable or malformed row can fail after earlier cells have been
    // installed. The buffer owns those cells and must release them here.
    buffer.dispose();
    rethrow;
  }
}