mergeArtifactsWithTightGaps method
void
mergeArtifactsWithTightGaps()
Merges adjacent artifacts that are separated by very small gaps but overlap strongly in the vertical axis. This helps repair split glyphs (e.g., serif diagonals) without merging full adjacent letters.
Implementation
void mergeArtifactsWithTightGaps() {
if (artifacts.length < _minArtifactsForStats) {
return;
}
sortArtifactsLeftToRight();
updateStatistics();
if (averageWidth <= 0) {
return;
}
final int baseKerning = averageKerning > 0 ? averageKerning : kerningWidth;
final int minGap = max(1, _minSpaceWidth - 1);
final int tightGapThreshold = max(
minGap,
min(baseKerning, kerningWidth) ~/ _pairArtifactsCount,
);
int i = 0;
while (i < artifacts.length - 1) {
final Artifact left = artifacts[i];
final Artifact right = artifacts[i + 1];
final int gap = right.rectFound.left - left.rectFound.right;
if (gap >= 0 && gap <= tightGapThreshold) {
final IntRect overlap = left.rectFound.intersect(right.rectFound);
if (!overlap.isEmpty) {
final int minHeight = min(
left.rectFound.height,
right.rectFound.height,
);
final double overlapRatio = minHeight == 0
? 0
: overlap.height / minHeight;
final double narrowThreshold = averageWidth * _similarWidthLowerRatio;
final bool leftNarrow = left.rectFound.width <= narrowThreshold;
final bool rightNarrow = right.rectFound.width <= narrowThreshold;
if (overlapRatio >= _mergeOverlapThreshold &&
leftNarrow &&
rightNarrow) {
left.mergeArtifact(right);
artifacts.removeAt(i + 1);
clearStats();
continue;
}
}
}
i++;
}
}