Class: Fontisan::Commands::UnicodeCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/fontisan/commands/unicode_command.rb

Overview

Command to list Unicode to glyph index mappings from a font file

Retrieves character code to glyph index mappings from the cmap table. Optionally includes glyph names from the post table if available.

Examples:

List Unicode mappings from a font

command = UnicodeCommand.new("font.ttf")
result = command.run
puts result.count

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from Fontisan::Commands::BaseCommand

Instance Method Details

#runModels::UnicodeMappings

Execute the command to retrieve Unicode mappings

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fontisan/commands/unicode_command.rb', line 18

def run
  result = Models::UnicodeMappings.new
  result.mappings = []
  result.count = 0

  return result unless font.has_table?(Constants::CMAP_TAG)

  cmap_table = font.table(Constants::CMAP_TAG)
  mappings_hash = cmap_table.unicode_mappings

  return result if mappings_hash.empty?

  # Optionally get glyph names if post table exists
  glyph_names = fetch_glyph_names if font.has_table?(Constants::POST_TAG)

  # Convert hash to array of mapping objects, sorted by codepoint
  result.mappings = mappings_hash.map do |codepoint, glyph_index|
    Models::UnicodeMapping.new(
      codepoint: format_codepoint(codepoint),
      glyph_index: glyph_index,
      glyph_name: glyph_names&.[](glyph_index),
    )
  end.sort_by(&:codepoint)

  result.count = result.mappings.length
  result
end