Module: Fontisan::Collection::SharedLogic

Included in:
BaseCollection, DfontCollection
Defined in:
lib/fontisan/collection/shared_logic.rb

Overview

Shared logic for font collection classes

This module provides common functionality for all collection types (TTC, OTC, dfont) to maintain DRY principles.

Instance Method Summary collapse

Instance Method Details

#calculate_table_sharing_for_fonts(fonts) ⇒ Models::TableSharingInfo

Calculate table sharing statistics

Analyzes which tables are shared between fonts and calculates space savings from deduplication.

Parameters:

Returns:



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
# File 'lib/fontisan/collection/shared_logic.rb', line 17

def calculate_table_sharing_for_fonts(fonts)
  require_relative "../models/table_sharing_info"

  # Build table hash map (checksum -> size)
  table_map = {}
  total_table_size = 0

  fonts.each do |font|
    font.tables.each do |entry|
      key = entry.checksum
      size = entry.table_length
      table_map[key] ||= size
      total_table_size += size
    end
  end

  # Count unique vs shared
  unique_tables = table_map.size
  total_tables = fonts.sum { |f| f.tables.length }
  shared_tables = total_tables - unique_tables

  # Calculate space saved
  unique_size = table_map.values.sum
  space_saved = total_table_size - unique_size

  # Calculate sharing percentage
  sharing_pct = total_tables.positive? ? (shared_tables.to_f / total_tables * 100).round(2) : 0.0

  Models::TableSharingInfo.new(
    shared_tables: shared_tables,
    unique_tables: unique_tables,
    sharing_percentage: sharing_pct,
    space_saved_bytes: space_saved,
  )
end