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) ⇒ TableAnalyzer

Initialize analyzer with fonts

Parameters:

Raises:

  • (ArgumentError)

    if fonts array is empty or contains invalid fonts



27
28
29
30
31
32
33
34
35
36
# File 'lib/fontisan/collection/table_analyzer.rb', line 27

def initialize(fonts)
  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
  @report = nil
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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fontisan/collection/table_analyzer.rb', line 51

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



76
77
78
79
# File 'lib/fontisan/collection/table_analyzer.rb', line 76

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)



92
93
94
95
# File 'lib/fontisan/collection/table_analyzer.rb', line 92

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



84
85
86
87
# File 'lib/fontisan/collection/table_analyzer.rb', line 84

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