Band.splitArtifactIntoBand constructor

Band.splitArtifactIntoBand({
  1. required Artifact regionMatrix,
  2. required IntOffset offset,
})

Creates a Band from an artifact region matrix.

This factory method analyzes a region matrix to find sub-artifacts, positions them correctly using the provided offset, and creates a new Band containing these artifacts.

Parameters: regionMatrix: The artifact matrix representing a region to analyze. offset: The position offset to apply to found artifacts.

Returns: A new Band containing the properly positioned and processed artifacts.

Implementation

factory Band.splitArtifactIntoBand({
  required final Artifact regionMatrix,
  required final IntOffset offset,
}) {
  //
  // Find the Matrices in the Region
  //
  List<Artifact> artifactsFound = regionMatrix.findSubArtifacts();

  //
  // IntOffset their locations found
  //
  Artifact.offsetArtifacts(
    artifactsFound,
    offset.x.toInt(),
    offset.y.toInt(),
  );

  //
  // Band
  //
  final Band newBand = Band();

  // sort horizontally
  artifactsFound.sort(
    (a, b) => a.locationFound.x.compareTo(b.locationFound.x),
  );

  newBand.mergeDiscardableArtifactsIntoNeighbors(artifactsFound);

  int totalHeight = 0;
  for (final Artifact artifact in artifactsFound) {
    totalHeight += artifact.rectFound.height;
  }
  final int avgHeight = artifactsFound.isEmpty
      ? 0
      : (totalHeight / artifactsFound.length).round();
  final int minLineHeight = (avgHeight * _minLineArtifactHeightRatio).round();

  for (final Artifact artifact in artifactsFound) {
    final bool discardable = artifact.discardableContent();
    if (discardable && !_shouldKeepLineArtifact(artifact, minLineHeight)) {
      continue;
    }
    newBand.addArtifact(artifact);
  }

  // All artifacts will have the same grid height.
  newBand.padVerticallyArtifactToMatchTheBand();

  // Merge overlapping inner strokes (e.g., disconnected diagonals in 'X').
  newBand.mergeArtifactsBasedOnVerticalAlignment();
  // Repair small intra-glyph gaps caused by binarization (e.g., serif diagonals).
  newBand.mergeArtifactsWithTightGaps();

  newBand.clearStats();

  return newBand;
}