Class: Fontisan::Ufo::Layer

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/ufo/layer.rb

Overview

A single layer in a UFO source. A Layer holds a set of glyphs keyed by name. The default layer is public.default per UFO 3.

Naming contract

Glyph names are the layer's primary key. Two distinct glyphs with the same name cannot coexist — adding the second would silently destroy the first, a class of bug that has historically cost real data (e.g. CJK Ext G cmap loss when CBDT placeholders collided with outline glyphs sharing "gidN" names).

To make that contract unbreakable, #add RAISES GlyphExistsError on conflict. Callers who want one of the other two semantics pick the method that names it:

+#add+::    insert; raise if the name is taken (the safe default)
+#put+::    insert; overwrite any existing glyph with the same name

Callers that need auto-renaming (insertion without giving up on a collision) call Stitcher::UniqueGlyphName.in(target, base) first, then #add the glyph under the returned name.

The error class itself lives at Fontisan::Ufo::GlyphExistsError (sibling, not nested) so the namespace stays flat.

Constant Summary collapse

DEFAULT_NAME =
"public.default"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = DEFAULT_NAME) ⇒ Layer

Returns a new instance of Layer.



34
35
36
37
# File 'lib/fontisan/ufo/layer.rb', line 34

def initialize(name = DEFAULT_NAME)
  @name = name
  @glyphs = {}
end

Instance Attribute Details

#glyphsObject (readonly)

Returns the value of attribute glyphs.



32
33
34
# File 'lib/fontisan/ufo/layer.rb', line 32

def glyphs
  @glyphs
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/fontisan/ufo/layer.rb', line 32

def name
  @name
end

Instance Method Details

#[](glyph_name) ⇒ Object



39
40
41
# File 'lib/fontisan/ufo/layer.rb', line 39

def [](glyph_name)
  @glyphs[glyph_name.to_s]
end

#add(glyph) ⇒ Glyph

Insert glyph. Raises GlyphExistsError if a glyph with the same name is already present — this is the contract that keeps the layer's key namespace trustworthy.

Parameters:

Returns:

  • (Glyph)

    the same glyph

Raises:



50
51
52
53
54
55
56
# File 'lib/fontisan/ufo/layer.rb', line 50

def add(glyph)
  name = glyph.name.to_s
  raise GlyphExistsError, name if @glyphs.key?(name)

  @glyphs[name] = glyph
  glyph
end

#eachObject



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

def each(&)
  @glyphs.each_value(&)
end

#put(glyph) ⇒ Glyph

Insert glyph, replacing any existing glyph with the same name. Use this when the caller has positively decided that the previous glyph (if any) should be discarded.

Parameters:

Returns:

  • (Glyph)

    the same glyph



64
65
66
67
# File 'lib/fontisan/ufo/layer.rb', line 64

def put(glyph)
  @glyphs[glyph.name.to_s] = glyph
  glyph
end

#sizeObject



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

def size
  @glyphs.size
end