upsertTemplate method

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

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

This method manages character definitions by either creating a new definition or updating an existing one based on the provided character and font.

Parameters:

  • font: The font name for the matrix. Used to identify which font variation of the character is being updated or added.
  • character: The character this matrix represents (e.g., 'A', '5', '&').
  • matrix: The Artifact object containing the character's pixel data.

Behavior:

  • If no definition exists for the character, creates a new CharacterDefinition with the provided matrix and adds it to the collection.
  • If a definition exists but no matrix for the specified font, adds the new matrix.
  • If a definition and matrix for the font already exist, updates the existing matrix.

Returns:

  • bool true if a new character definition was created
  • bool false if an existing definition was updated

Implementation

bool upsertTemplate(
  final String font,
  final String character,
  final Artifact 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 int existingMatrixIndex = found.matrices.indexWhere(
      (final Artifact m) => m.font == font,
    );

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