countVerticalStems method

int countVerticalStems()

Estimates the number of strong vertical stems in a glyph.

This is useful for disambiguating letters with different stroke counts (e.g., 'u' vs 'm') without relying on word-specific corrections.

Implementation

int countVerticalStems() {
  final IntRect content = getContentRect();
  if (content.isEmpty) {
    return 0;
  }

  final int width = content.width;
  final int height = content.height;
  if (width <= 0 || height <= 0) {
    return 0;
  }

  final List<int> histogram = List<int>.filled(width, 0);
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      if (cellGet(content.left + x, content.top + y)) {
        histogram[x]++;
      }
    }
  }

  final int maxCount = histogram.reduce(max);
  if (maxCount == 0) {
    return 0;
  }

  final int threshold = max(1, (maxCount * _stemThresholdRatio).round());

  int stems = 0;
  bool inStem = false;
  for (final int value in histogram) {
    if (value >= threshold) {
      if (!inStem) {
        stems++;
        inStem = true;
      }
    } else {
      inStem = false;
    }
  }

  return stems;
}