Class: Fontisan::Stitcher::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/stitcher/source.rb

Overview

Wraps a source font (UFO or loaded TTF/OTF) behind a single extraction API used by the selectors.

For UFO sources, glyphs are accessed by name directly. For TTF or OTF sources, the source is lazily converted to a UFO::Font via Ufo::Convert::FromBinData on first glyph access, then cached. This is O(n) in donor glyph count but amortized across all codepoint extractions from that donor.

CBDT/CBLC sources (e.g. NotoColorEmoji) are detected via #bitmap_mode. When a source is :cbdt, the Stitcher propagates the raw CBDT/CBLC tables into the output instead of extracting outlines. The glyph data lives in the bitmap tables, not in glyf.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ Source

Returns a new instance of Source.



21
22
23
24
# File 'lib/fontisan/stitcher/source.rb', line 21

def initialize(font)
  @font = font
  @ufo_cache = nil
end

Instance Attribute Details

#fontObject (readonly)

Returns the value of attribute font.



19
20
21
# File 'lib/fontisan/stitcher/source.rb', line 19

def font
  @font
end

Instance Method Details

#bitmap_modeSymbol

Detect how this source stores glyph data.

  • :glyf — TrueType outlines (glyf table present)
  • :cbdt — Color bitmaps (CBDT + CBLC tables, no glyf)
  • :mixed — Both glyf and CBDT
  • :none — UFO source or neither table present

Returns:

  • (Symbol)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fontisan/stitcher/source.rb', line 44

def bitmap_mode
  return :none if @font.is_a?(Fontisan::Ufo::Font)
  return :none unless @font.respond_to?(:has_table?)

  has_cbdt = @font.has_table?("CBDT") && @font.has_table?("CBLC")
  has_glyf = @font.has_table?("glyf") || @font.has_table?("CFF ")
  return :mixed if has_cbdt && has_glyf
  return :cbdt if has_cbdt
  return :glyf if has_glyf

  :none
end

#formatSymbol

Returns :ufo, :ttf, :otf.

Returns:

  • (Symbol)

    :ufo, :ttf, :otf



27
28
29
30
31
32
33
34
# File 'lib/fontisan/stitcher/source.rb', line 27

def format
  case @font
  when Fontisan::Ufo::Font then :ufo
  when Fontisan::TrueTypeFont then :ttf
  when Fontisan::OpenTypeFont then :otf
  else :unknown
  end
end

#gid_for_codepoint(codepoint) ⇒ Integer?

Find the gid for a Unicode codepoint in this source.

Parameters:

  • codepoint (Integer)

Returns:

  • (Integer, nil)


60
61
62
63
64
65
# File 'lib/fontisan/stitcher/source.rb', line 60

def gid_for_codepoint(codepoint)
  case @font
  when Fontisan::Ufo::Font then ufo_gid_for(codepoint)
  else bin_data_gid_for(codepoint)
  end
end

#glyph_for_gid(gid) ⇒ Fontisan::Ufo::Glyph?

Extract a glyph by gid.

For CBDT sources, returns a placeholder glyph (no contours) since the glyph data lives in the bitmap tables, not outlines.

Parameters:

  • gid (Integer)

Returns:



73
74
75
76
77
78
# File 'lib/fontisan/stitcher/source.rb', line 73

def glyph_for_gid(gid)
  case @font
  when Fontisan::Ufo::Font then ufo_glyph_at(gid)
  else converted_ufo_glyph_at(gid)
  end
end

#raw_table_bytes(tag) ⇒ String?

Raw table bytes from the loaded font (for passthrough).

Parameters:

  • tag (String)

    4-byte table tag (e.g. "CBDT", "CBLC")

Returns:

  • (String, nil)

    raw bytes or nil if table not present



83
84
85
86
87
88
89
90
# File 'lib/fontisan/stitcher/source.rb', line 83

def raw_table_bytes(tag)
  sfnt_table = @font.table(tag)
  return nil unless sfnt_table

  sfnt_table.raw_data
rescue StandardError
  nil
end