Class: Fontisan::Collection::DfontBuilder
- Inherits:
-
Object
- Object
- Fontisan::Collection::DfontBuilder
- Defined in:
- lib/fontisan/collection/dfont_builder.rb
Overview
DfontBuilder creates Apple dfont (Data Fork Font) resource fork structures
Main responsibility: Build complete dfont binary from multiple fonts by creating a resource fork structure containing 'sfnt' resources.
dfont is an Apple-specific format that stores Mac font suitcase resources in the data fork instead of the resource fork. It can contain multiple fonts.
Constant Summary collapse
- NO_NAME_OFFSET =
Constants for resource reference packing
[-1].freeze
- ZERO_ATTRIBUTES =
[0].freeze
- ZERO_RESERVED =
[0].freeze
Instance Attribute Summary collapse
-
#fonts ⇒ Array<TrueTypeFont, OpenTypeFont>
readonly
Source fonts.
-
#result ⇒ Hash?
readonly
Build result (populated after build).
Instance Method Summary collapse
-
#build ⇒ Hash
Build dfont and return binary.
-
#build_to_file(path) ⇒ Hash
Build dfont and write to file.
-
#initialize(fonts, options = {}) ⇒ DfontBuilder
constructor
Initialize builder with fonts.
Constructor Details
#initialize(fonts, options = {}) ⇒ DfontBuilder
Initialize builder with fonts
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fontisan/collection/dfont_builder.rb', line 39 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) unless fonts.all? { |f| f.respond_to?(:table_data) } raise ArgumentError, "all fonts must respond to table_data" end @fonts = fonts @options = @result = nil validate_fonts! end |
Instance Attribute Details
#fonts ⇒ Array<TrueTypeFont, OpenTypeFont> (readonly)
Source fonts
23 24 25 |
# File 'lib/fontisan/collection/dfont_builder.rb', line 23 def fonts @fonts end |
#result ⇒ Hash? (readonly)
Build result (populated after build)
27 28 29 |
# File 'lib/fontisan/collection/dfont_builder.rb', line 27 def result @result end |
Instance Method Details
#build ⇒ Hash
Build dfont and return binary
Executes the complete dfont creation process:
- Serialize each font to SFNT binary
- Build resource data section
- Calculate resource map size
- Build resource map
- Build resource fork header
- Assemble complete dfont binary
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fontisan/collection/dfont_builder.rb', line 70 def build # Step 1: Serialize all fonts to SFNT binaries sfnt_binaries = serialize_fonts # Step 2: Build resource data section resource_data = build_resource_data(sfnt_binaries) # Step 3: Calculate expected resource map size # Map header: 28 bytes # Type list header: 2 bytes # Type entry: 8 bytes # References: 12 bytes each map_size = 28 + 2 + 8 + (sfnt_binaries.size * 12) # Step 4: Build resource map resource_map = build_resource_map(sfnt_binaries, resource_data.bytesize, map_size) # Step 5: Build header header = build_header(resource_data.bytesize, resource_map.bytesize) # Step 6: Assemble complete dfont binary binary = header + resource_data + resource_map # Store result @result = { binary: binary, num_fonts: @fonts.size, total_size: binary.bytesize, format: :dfont, } @result end |
#build_to_file(path) ⇒ Hash
Build dfont and write to file
109 110 111 112 113 114 |
# File 'lib/fontisan/collection/dfont_builder.rb', line 109 def build_to_file(path) result = build File.binwrite(path, result[:binary]) result[:output_path] = path result end |