Class: Fontisan::OpenTypeFont

Inherits:
SfntFont
  • Object
show all
Defined in:
lib/fontisan/open_type_font.rb,
lib/fontisan/open_type_font_extensions.rb

Overview

Extensions to OpenTypeFont for table-based construction

Constant Summary collapse

PAGE_SIZE =

Page size for lazy loading alignment (typical filesystem page size)

4096

Constants inherited from SfntFont

SfntFont::PADDING_BYTES, SfntFont::SFNT_TABLE_CLASS_MAP, SfntFont::TABLE_CLASS_MAP

Instance Attribute Summary collapse

Attributes inherited from SfntFont

#io_source, #lazy_load_enabled, #loading_mode, #parsed_tables, #sfnt_tables, #table_data, #table_entry_cache

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SfntFont

#all_sfnt_tables, #close, #family_name, finalize, #find_table_entry, from_collection, from_file, #full_name, #has_table?, #head_table, #post_script_name, #preferred_family_name, #preferred_subfamily_name, #read_metadata_tables_batched, #read_table_data, #setup_finalizer, #sfnt_table, #subfamily_name, #table, #table_available?, #table_names, #to_file, #units_per_em, #update_checksum_adjustment_in_file, #update_checksum_adjustment_in_io

Instance Attribute Details

#page_cacheObject

Page cache for lazy loading (maps page_start_offset => page_data)



30
31
32
# File 'lib/fontisan/open_type_font.rb', line 30

def page_cache
  @page_cache
end

Class Method Details

.from_tables(tables) ⇒ OpenTypeFont

Create font from hash of tables

This is used during font conversion when we have tables but not a file.

Parameters:

  • tables (Hash<String, String>)

    Map of table tag to binary data

Returns:



12
13
14
15
16
17
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
45
46
47
48
49
50
51
52
# File 'lib/fontisan/open_type_font_extensions.rb', line 12

def self.from_tables(tables)
  # Create minimal header structure
  font = new
  font.initialize_storage
  font.loading_mode = LoadingModes::FULL

  # Store table data
  font.table_data = tables

  # Build header from tables
  num_tables = tables.size
  max_power = 0
  n = num_tables
  while n > 1
    n >>= 1
    max_power += 1
  end

  search_range = (1 << max_power) * 16
  entry_selector = max_power
  range_shift = (num_tables * 16) - search_range

  font.header.sfnt_version = 0x4F54544F # 'OTTO' for OpenType/CFF
  font.header.num_tables = num_tables
  font.header.search_range = search_range
  font.header.entry_selector = entry_selector
  font.header.range_shift = range_shift

  # Build table directory
  font.tables.clear
  tables.each_key do |tag|
    entry = TableDirectory.new
    entry.tag = tag
    entry.checksum = 0 # Will be calculated on write
    entry.offset = 0 # Will be calculated on write
    entry.table_length = tables[tag].bytesize
    font.tables << entry
  end

  font
end

Instance Method Details

#cff?Boolean

Check if font is CFF flavored

Returns:

  • (Boolean)

    true for OpenType fonts



67
68
69
# File 'lib/fontisan/open_type_font.rb', line 67

def cff?
  true
end

#initialize_storagevoid

This method returns an undefined value.

Initialize storage hashes

Extends base class to add page_cache for lazy loading.



40
41
42
43
# File 'lib/fontisan/open_type_font.rb', line 40

def initialize_storage
  super
  @page_cache = {}
end

#truetype?Boolean

Check if font is TrueType flavored

Returns:

  • (Boolean)

    false for OpenType fonts



60
61
62
# File 'lib/fontisan/open_type_font.rb', line 60

def truetype?
  false
end

#valid?Boolean

Validate format correctness

Extends base class to check for CFF table (OpenType-specific).

Returns:

  • (Boolean)

    true if the OTF format is valid, false otherwise



50
51
52
53
54
55
# File 'lib/fontisan/open_type_font.rb', line 50

def valid?
  return false unless super
  return false unless has_table?(Constants::CFF_TAG)

  true
end