Class: Fontisan::Subset::TableStrategy::Loca

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/subset/table_strategy/loca.rb

Overview

Loca strategy: emit one entry per subset glyph + a final offset, using short or long format based on the source head.index_to_loc_format. Glyph offsets come from [SharedState], populated earlier by the Glyf strategy. If Glyf hasn't run yet (e.g. direct invocation from a spec), this strategy triggers the build lazily.

Class Method Summary collapse

Class Method Details

.call(context:, tag:, table:) ⇒ String

Returns binary loca bytes for the subset.

Parameters:

  • context (SubsetContext)
  • tag (String)

    "loca"

  • table (Loca)

    parsed loca table (unused)

Returns:

  • (String)

    binary loca bytes for the subset



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fontisan/subset/table_strategy/loca.rb', line 16

def self.call(context:, tag:, table:)
  offsets = context.state.loca_offsets
  if offsets.nil?
    builder = GlyfLocaBuilder.new(font: context.font,
                                  mapping: context.mapping).build
    context.state.glyf_data = builder.glyf_data
    context.state.loca_offsets = builder.loca_offsets
    context.state.subset_bbox = builder.bbox
    offsets = builder.loca_offsets
  end

  head = context.font.table("head")
  data = String.new(encoding: Encoding::BINARY)

  if head.index_to_loc_format.zero?
    offsets.each { |o| data << [o / 2].pack("n") }
  else
    offsets.each { |o| data << [o].pack("N") }
  end

  data
end