Class: Fontisan::Ucd::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fontisan/ucd/index.rb

Overview

Sorted, run-length-encoded lookup table over Unicode codepoints.

One Index answers "what does codepoint N belong to?" for one property (block, or script). Lookup is O(log N) via bsearch.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries) ⇒ Index

Returns a new instance of Index.

Parameters:

  • entries (Array<RangeEntry>)

    sorted, disjoint



16
17
18
# File 'lib/fontisan/ucd/index.rb', line 16

def initialize(entries)
  @entries = entries.sort
end

Instance Attribute Details

#entriesArray<RangeEntry> (readonly)

Returns:



21
22
23
# File 'lib/fontisan/ucd/index.rb', line 21

def entries
  @entries
end

Class Method Details

.from_triples(triples) ⇒ Index

Build an Index from raw [first_cp, last_cp, name] triples.

Parameters:

  • triples (Array<Array(Integer, Integer, String)>)

Returns:



76
77
78
# File 'lib/fontisan/ucd/index.rb', line 76

def self.from_triples(triples)
  new(triples.map { |first, last, name| RangeEntry.new(first, last, name) })
end

.load(path) ⇒ Index

Load from a YAML file previously written by #save.

Parameters:

  • path (String, Pathname)

    (required)

Returns:



68
69
70
71
# File 'lib/fontisan/ucd/index.rb', line 68

def self.load(path)
  hashes = YAML.load_file(path)
  new(hashes.map { |h| RangeEntry.from_h(h) })
end

Instance Method Details

#eachObject



23
24
25
# File 'lib/fontisan/ucd/index.rb', line 23

def each(&)
  @entries.each(&)
end

#each_overlapping(first, last) ⇒ Enumerator<RangeEntry>

Enumerate every range whose [first_cp, last_cp] overlaps the inclusive query range.

Parameters:

  • first (Integer)
  • last (Integer)

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fontisan/ucd/index.rb', line 43

def each_overlapping(first, last, &)
  return enum_for(:each_overlapping, first, last) unless block_given?

  start_idx = bsearch_first_overlap(first)
  return if start_idx.nil?

  @entries[start_idx..].each do |entry|
    break if entry.first_cp > last

    yield entry if entry.last_cp >= first
  end
end

#lookup(codepoint) ⇒ String?

Returns the name of the range covering codepoint, or nil.

Parameters:

  • codepoint (Integer)

    Unicode codepoint

Returns:

  • (String, nil)

    the name of the range covering codepoint, or nil



33
34
35
36
# File 'lib/fontisan/ucd/index.rb', line 33

def lookup(codepoint)
  idx = bsearch_index(codepoint)
  idx && @entries[idx].name
end

#save(path) ⇒ void

This method returns an undefined value.

Serialize to a YAML file.

Parameters:

  • path (String, Pathname)


59
60
61
62
63
# File 'lib/fontisan/ucd/index.rb', line 59

def save(path)
  File.open(path, "w") do |file|
    YAML.dump(@entries.map(&:to_h), file)
  end
end

#sizeObject



27
28
29
# File 'lib/fontisan/ucd/index.rb', line 27

def size
  @entries.size
end