Class: Fontisan::Collection::TableAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/collection/table_analyzer.rb

Overview

TableAnalyzer analyzes tables across multiple fonts to identify sharing opportunities

Single responsibility: Analyze tables across fonts to identify identical tables that can be shared in a font collection. Uses SHA256 checksums for reliable content comparison.

Examples:

Analyze tables across fonts

analyzer = TableAnalyzer.new([font1, font2, font3])
report = analyzer.analyze
puts "Potential savings: #{report[:space_savings]} bytes"
puts "Shared tables: #{report[:shared_tables].keys.join(', ')}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fonts, parallel: false, thread_count: 4) ⇒ TableAnalyzer

Initialize analyzer with fonts

Parameters:

  • fonts (Array<TrueTypeFont, OpenTypeFont>)

    Fonts to analyze

  • parallel (Boolean) (defaults to: false)

    Use parallel processing for large collections (default: false)

  • thread_count (Integer) (defaults to: 4)

    Number of threads for parallel processing (default: 4)

Raises:

  • (ArgumentError)

    if fonts array is empty or contains invalid fonts



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fontisan/collection/table_analyzer.rb', line 29

def initialize(fonts, parallel: false, thread_count: 4)
  if fonts.nil? || fonts.empty?
    raise ArgumentError,
          "fonts cannot be nil or empty"
  end
  raise ArgumentError, "fonts must be an array" unless fonts.is_a?(Array)

  @fonts = fonts
  @parallel = parallel
  @thread_count = thread_count
  @report = nil
  @checksum_cache = {}.compare_by_identity
end

Instance Attribute Details

#reportHash (readonly)

Analysis report structure

Returns:

  • (Hash)

    Analysis results



21
22
23
# File 'lib/fontisan/collection/table_analyzer.rb', line 21

def report
  @report
end

Instance Method Details

#analyzeHash

Analyze tables across all fonts

Identifies tables that are identical across fonts based on content checksum. Returns a comprehensive analysis report with sharing opportunities and potential space savings.

Returns:

  • (Hash)

    Analysis report with:

    • :total_fonts [Integer] Number of fonts analyzed

    • :table_checksums [Hash<String, Hash>] Map of tag to checksum to font indices

    • :shared_tables [Hash<String, Array>] Map of tag to array of font indices sharing that table

    • :unique_tables [Hash<String, Array>] Map of tag to array of font indices with unique versions

    • :space_savings [Integer] Potential bytes saved by sharing

    • :sharing_percentage [Float] Percentage of tables that can be shared



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fontisan/collection/table_analyzer.rb', line 56

def analyze
  @report = {
    total_fonts: @fonts.size,
    table_checksums: {},
    shared_tables: {},
    unique_tables: {},
    space_savings: 0,
    sharing_percentage: 0.0,
  }

  # Collect checksums for all tables across all fonts
  collect_table_checksums

  # Identify which tables are shared
  identify_shared_tables

  # Calculate space savings
  calculate_space_savings

  @report
end

#shared_tablesHash<String, Array<Integer>>

Get tables that can be shared

Returns:

  • (Hash<String, Array<Integer>>)

    Map of table tag to font indices



81
82
83
84
# File 'lib/fontisan/collection/table_analyzer.rb', line 81

def shared_tables
  analyze unless @report
  @report[:shared_tables]
end

#sharing_percentageFloat

Get sharing percentage

Returns:

  • (Float)

    Percentage of tables that can be shared (0.0-100.0)



97
98
99
100
# File 'lib/fontisan/collection/table_analyzer.rb', line 97

def sharing_percentage
  analyze unless @report
  @report[:sharing_percentage]
end

#space_savingsInteger

Get potential space savings in bytes

Returns:

  • (Integer)

    Bytes that can be saved by sharing



89
90
91
92
# File 'lib/fontisan/collection/table_analyzer.rb', line 89

def space_savings
  analyze unless @report
  @report[:space_savings]
end