upsertTemplate method

bool upsertTemplate(
  1. String font,
  2. String character,
  3. Matrix matrix
)

Updates or inserts a template matrix for a given character and font.

If a definition for the character doesn't exist, a new one is created. If a matrix for the given font already exists, it is updated; otherwise, it's added.

font The font name for the matrix. character The character this matrix represents. matrix The Matrix object containing the character's pixel data.

Implementation

bool upsertTemplate(
  final String font,
  final String character,
  final Matrix matrix,
) {
  matrix.font = font;
  final CharacterDefinition? found = getDefinition(character);
  if (found == null) {
    final CharacterDefinition newDefinition = CharacterDefinition(
      character: character,
      matrices: [matrix],
    );
    _definitions.add(newDefinition);
    return true;
  } else {
    final existingMatrixIndex =
        found.matrices.indexWhere((m) => m.font == font);

    if (existingMatrixIndex == -1) {
      found.matrices.add(matrix);
    } else {
      found.matrices[existingMatrixIndex] = matrix;
    }
    return false;
  }
}