GlyphLookup.fromShape constructor

GlyphLookup.fromShape(
  1. TextShapeResult shape,
  2. int codeUnitCount
)

Implementation

factory GlyphLookup.fromShape(TextShapeResult shape, int codeUnitCount) {
  var glyphIndices = List<int>.filled(codeUnitCount + 1, 0);
  // Build a mapping of codePoints to glyphs indices.
  int glyphIndex = 0;
  int lastTextIndex = 0;
  for (final paragraph in shape.paragraphs) {
    for (final run in paragraph.runs) {
      for (int i = 0; i < run.glyphCount; i++) {
        var textIndex = run.textIndexAt(i);
        for (int j = lastTextIndex; j < textIndex; j++) {
          glyphIndices[j] = glyphIndex - 1;
        }
        lastTextIndex = textIndex;
        glyphIndex++;
      }
    }
  }
  for (int i = lastTextIndex; i < codeUnitCount; i++) {
    glyphIndices[i] = glyphIndex - 1;
  }
  // Store a fake unreachable glyph at the end to allow selecting the last
  // one.
  glyphIndices[codeUnitCount] =
      codeUnitCount == 0 ? 0 : glyphIndices[codeUnitCount - 1] + 1;
  return GlyphLookup(glyphIndices);
}