Class: Fontisan::Ufo::ImageSet
- Inherits:
-
Object
- Object
- Fontisan::Ufo::ImageSet
- Includes:
- Enumerable
- Defined in:
- lib/fontisan/ufo/image_set.rb
Overview
UFO 3 background image set. Lives at UFO/images/ in a UFO
source directory. Each image is a PNG file referenced by glyph
GLIF <image fileName="..."/> elements.
Per the UFO 3 spec, image files are arbitrary binary blobs (in practice always PNG). The image set deduplicates by content hash (sha256) so the same image referenced from multiple glyphs takes one slot.
Instance Attribute Summary collapse
-
#images ⇒ Hash{String=>Image}
readonly
Filename → Image.
Class Method Summary collapse
-
.load_from_dir(dir) ⇒ ImageSet
Load all PNG files from a UFO
images/directory.
Instance Method Summary collapse
-
#count ⇒ Object
Number of registered images.
-
#each ⇒ Object
Iterate over images in filename order.
- #empty? ⇒ Boolean
-
#find(file_name) ⇒ Image?
(also: #[])
Look up an image by its filename.
-
#initialize ⇒ ImageSet
constructor
A new instance of ImageSet.
-
#register_file(file_name, bytes) ⇒ Image
Register an image from its filename and raw bytes.
-
#write_to_dir(dir) ⇒ Object
Write all images to a target
images/directory.
Constructor Details
#initialize ⇒ ImageSet
Returns a new instance of ImageSet.
25 26 27 |
# File 'lib/fontisan/ufo/image_set.rb', line 25 def initialize @images = {} end |
Instance Attribute Details
#images ⇒ Hash{String=>Image} (readonly)
Returns filename → Image.
23 24 25 |
# File 'lib/fontisan/ufo/image_set.rb', line 23 def images @images end |
Class Method Details
.load_from_dir(dir) ⇒ ImageSet
Load all PNG files from a UFO images/ directory.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fontisan/ufo/image_set.rb', line 33 def self.load_from_dir(dir) is = new return is unless File.directory?(dir) Dir.each_child(dir).sort.each do |name| path = File.join(dir, name) next unless File.file?(path) is.register_file(name, File.binread(path)) end is end |
Instance Method Details
#count ⇒ Object
Number of registered images.
79 80 81 |
# File 'lib/fontisan/ufo/image_set.rb', line 79 def count @images.size end |
#each ⇒ Object
Iterate over images in filename order.
72 73 74 75 76 |
# File 'lib/fontisan/ufo/image_set.rb', line 72 def each(&) return enum_for(:each) unless block_given? @images.each_value(&) end |
#empty? ⇒ Boolean
83 84 85 |
# File 'lib/fontisan/ufo/image_set.rb', line 83 def empty? @images.empty? end |
#find(file_name) ⇒ Image? Also known as: []
Look up an image by its filename.
66 67 68 |
# File 'lib/fontisan/ufo/image_set.rb', line 66 def find(file_name) @images[file_name.to_s] end |
#register_file(file_name, bytes) ⇒ Image
Register an image from its filename and raw bytes. Computes the sha256 content hash automatically.
52 53 54 55 56 57 58 59 60 |
# File 'lib/fontisan/ufo/image_set.rb', line 52 def register_file(file_name, bytes) image = Image.new( file_name: file_name, sha: Digest::SHA256.hexdigest(bytes), bytes: bytes, ) @images[file_name.to_s] = image image end |
#write_to_dir(dir) ⇒ Object
Write all images to a target images/ directory. Useful for
UFO round-trip serialization.
91 92 93 94 95 96 |
# File 'lib/fontisan/ufo/image_set.rb', line 91 def write_to_dir(dir) FileUtils.mkpath(dir) unless File.directory?(dir) @images.each do |name, img| File.binwrite(File.join(dir, name), img.bytes) end end |