identifySpacesInBand method

void identifySpacesInBand()

Identifies and inserts space artifacts between existing artifacts in the band.

This method analyzes the Kerning between artifacts and inserts space artifacts where the Kerning exceeds a certain threshold.

The process involves:

  1. Calculating a threshold Kerning size based on the average width.
  2. Iterating through artifacts to identify Kerning exceeding the threshold.
  3. Creating a list of artifacts that need spaces inserted before them.
  4. Inserting space artifacts at the appropriate positions.

The threshold is derived from the median gap between artifacts.

Implementation

void identifySpacesInBand() {
  updateStatistics();

  if (artifacts.length < _minArtifactsForSpaceDetection) {
    return;
  }

  final List<int> gaps = [];
  for (int i = 1; i < artifacts.length; i++) {
    final int leftEdge = artifacts[i - 1].rectFound.right;
    final int rightEdge = artifacts[i].rectFound.left;
    final int gap = rightEdge - leftEdge;
    if (gap > 0) {
      gaps.add(gap);
    }
  }

  // Calculate a threshold based on gap distribution (robust to large spaces)
  final int spaceThreshold = calculateSpaceThreshold(gaps);

  for (int i = 1; i < artifacts.length; i++) {
    final Artifact leftArtifact = artifacts[i - 1];
    final Artifact rightArtifact = artifacts[i];

    final int leftEdge = leftArtifact.rectFound.right;
    final int rightEdge = rightArtifact.rectFound.left;
    final int gap = rightEdge - leftEdge;

    if (gap >= spaceThreshold) {
      final int spaceWidth = (gap - (_spaceBorderWidth * 2)).toInt();
      if (spaceWidth >= _minSpaceWidth) {
        // this space is big enough
        insertArtifactForSpace(
          artifacts: artifacts,
          insertAtIndex: i,
          cols: spaceWidth,
          rows: rectangleOriginal.height.toInt(),
          locationFoundAt: IntOffset(
            leftArtifact.rectFound.right + _spaceBorderWidth,
            leftArtifact.rectFound.top,
          ),
        );
        i++;
      }
    }
  }
}