Class: Fontisan::Ufo::Layer
- Inherits:
-
Object
- Object
- Fontisan::Ufo::Layer
- 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
-
#glyphs ⇒ Object
readonly
Returns the value of attribute glyphs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #[](glyph_name) ⇒ Object
-
#add(glyph) ⇒ Glyph
Insert
glyph. - #each ⇒ Object
-
#initialize(name = DEFAULT_NAME) ⇒ Layer
constructor
A new instance of Layer.
-
#put(glyph) ⇒ Glyph
Insert
glyph, replacing any existing glyph with the same name. - #size ⇒ Object
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
#glyphs ⇒ Object (readonly)
Returns the value of attribute glyphs.
32 33 34 |
# File 'lib/fontisan/ufo/layer.rb', line 32 def glyphs @glyphs end |
#name ⇒ Object (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.
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 |
#each ⇒ Object
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.
64 65 66 67 |
# File 'lib/fontisan/ufo/layer.rb', line 64 def put(glyph) @glyphs[glyph.name.to_s] = glyph glyph end |
#size ⇒ Object
73 74 75 |
# File 'lib/fontisan/ufo/layer.rb', line 73 def size @glyphs.size end |