Class: Fontisan::Stitcher

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/stitcher.rb,
lib/fontisan/stitcher/source.rb,
lib/fontisan/stitcher/selector.rb,
lib/fontisan/stitcher/glyph_limit.rb,
lib/fontisan/stitcher/deduplicator.rb,
lib/fontisan/stitcher/selector/gid.rb,
lib/fontisan/stitcher/selector/range.rb,
lib/fontisan/stitcher/glyph_signature.rb,
lib/fontisan/stitcher/selector/codepoints.rb

Overview

Multi-source font stitcher with explicit subfont declaration.

Every set of codepoints is explicitly assigned to a named subfont via the required into: keyword. The user controls the collection structure upfront — there are no defaults and no after-the-fact splitting.

Single-font output: write_to requires a subfont: name. Collection output: write_collection writes all declared subfonts.

Examples:

Single font

stitcher = Fontisan::Stitcher.new
stitcher.add_source(:latin, Fontisan::Ufo::Font.open("latin.ufo"))
stitcher.include_range(0x41..0x5A, from: :latin, into: :main)
stitcher.write_to("out.ttf", format: :ttf, subfont: :main)

Collection

stitcher = Fontisan::Stitcher.new
stitcher.add_source(:noto_sans, noto_sans)
stitcher.add_source(:noto_cjk, noto_cjk)
stitcher.include_range(0x41..0x5A, from: :noto_sans, into: :latin)
stitcher.include_range(0x4E00..0x9FFF, from: :noto_cjk, into: :cjk)
stitcher.write_collection("out.otc", format: :otf2)

Defined Under Namespace

Modules: GlyphLimit, GlyphSignature, Selector Classes: Deduplicator, Source

Constant Summary collapse

DEFAULT_DEDUPLICATE =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deduplicate: DEFAULT_DEDUPLICATE) ⇒ Stitcher

Returns a new instance of Stitcher.



38
39
40
41
42
43
# File 'lib/fontisan/stitcher.rb', line 38

def initialize(deduplicate: DEFAULT_DEDUPLICATE)
  @sources = {}
  @subfonts = Hash.new { |h, k| h[k] = [] }
  @info = nil
  @deduplicate = deduplicate
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



36
37
38
# File 'lib/fontisan/stitcher.rb', line 36

def info
  @info
end

#sourcesObject (readonly)

Returns the value of attribute sources.



36
37
38
# File 'lib/fontisan/stitcher.rb', line 36

def sources
  @sources
end

#subfontsObject (readonly)

Returns the value of attribute subfonts.



36
37
38
# File 'lib/fontisan/stitcher.rb', line 36

def subfonts
  @subfonts
end

Instance Method Details

#add_source(label, font) ⇒ Object



45
46
47
# File 'lib/fontisan/stitcher.rb', line 45

def add_source(label, font)
  @sources[label.to_sym] = Source.new(font)
end

#build_target_font(subfont:) ⇒ Object



73
74
75
# File 'lib/fontisan/stitcher.rb', line 73

def build_target_font(subfont:)
  build_target_for(subfont)
end

#include_codepoints(codepoints, from:, into:) ⇒ Object



53
54
55
# File 'lib/fontisan/stitcher.rb', line 53

def include_codepoints(codepoints, from:, into:)
  Selector::Codepoints.new(codepoints).apply(source(from), @subfonts[into])
end

#include_gid(donor_gid, from:, into:) ⇒ Object



57
58
59
# File 'lib/fontisan/stitcher.rb', line 57

def include_gid(donor_gid, from:, into:)
  Selector::Gid.new(donor_gid).apply(source(from), @subfonts[into])
end

#include_notdef(from:, into:) ⇒ Object



61
62
63
# File 'lib/fontisan/stitcher.rb', line 61

def include_notdef(from:, into:)
  include_gid(0, from: from, into: into)
end

#include_range(range, from:, into:) ⇒ Object



49
50
51
# File 'lib/fontisan/stitcher.rb', line 49

def include_range(range, from:, into:)
  Selector::Range.new(range).apply(source(from), @subfonts[into])
end

#set_info(info_hash) ⇒ Object



65
66
67
# File 'lib/fontisan/stitcher.rb', line 65

def set_info(info_hash)
  @info = Ufo::Info.new(info_hash)
end

#subfont_namesObject



69
70
71
# File 'lib/fontisan/stitcher.rb', line 69

def subfont_names
  @subfonts.keys
end

#write_collection(path, format:) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fontisan/stitcher.rb', line 88

def write_collection(path, format:)
  raise ArgumentError, "no subfonts declared" if @subfonts.empty?

  compiled = @subfonts.keys.map do |name|
    compile_subfont_to_loaded_font(name, format: format)
  end

  collection_format = collection_format_for(format)
  Collection::Builder.new(compiled, format: collection_format,
                                    optimize: true).build_to_file(path)
  path
end

#write_to(path, format:, subfont:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/fontisan/stitcher.rb', line 77

def write_to(path, format:, subfont:)
  target = build_target_for(subfont)
  GlyphLimit.check!(target.glyphs.size, format: format)

  compiler = compiler_for(format)
  compiler.new(target).compile(output_path: path)

  propagate_cbdt_tables(path) if cbdt_source
  path
end