Class: Fontisan::Commands::UnicodeCommand
- Inherits:
-
BaseCommand
- Object
- BaseCommand
- Fontisan::Commands::UnicodeCommand
- 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.
Instance Method Summary collapse
-
#run ⇒ Models::UnicodeMappings
Execute the command to retrieve Unicode mappings.
Methods inherited from BaseCommand
Constructor Details
This class inherits a constructor from Fontisan::Commands::BaseCommand
Instance Method Details
#run ⇒ Models::UnicodeMappings
Execute the command to retrieve Unicode mappings
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 |