Class: Okab::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/okab/font.rb,
sig/okab.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(face) ⇒ Font

Returns a new instance of Font.

Parameters:

  • face (Alhena::Font)

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/okab/font.rb', line 12

def initialize(face)
  raise ArgumentError, "expected an Alhena::Font" unless face.is_a?(Alhena::Font)

  @face = face
end

Instance Attribute Details

#face ⇒ Alhena::Font (readonly)

Returns the value of attribute face.

Returns:

  • (Alhena::Font)


7
8
9
# File 'lib/okab/font.rb', line 7

def face
  @face
end

Class Method Details

.embed(document, font, subset: true) ⇒ EmbeddedFont

Parameters:

  • document (Document)
  • font (Font, Alhena::Font)
  • subset: (Boolean) (defaults to: true)

Returns:



10
# File 'lib/okab/font.rb', line 10

def self.embed(document, font, subset: true) = document.embed_font(font, subset: subset)

.load(path) ⇒ Font

Parameters:

  • path (String)

Returns:



9
# File 'lib/okab/font.rb', line 9

def self.load(path) = new(Alhena::Font.open(path))

Instance Method Details

#measure(text, size:) ⇒ Float

Parameters:

  • text (String)
  • size: (Numeric)

Returns:

  • (Float)

Raises:

  • (ArgumentError)


18
19
20
21
22
23
# File 'lib/okab/font.rb', line 18

def measure(text, size:)
  raise ArgumentError, "text must be valid UTF-8" unless text.is_a?(String) && text.encoding == Encoding::UTF_8 && text.valid_encoding?
  raise ArgumentError, "size must be finite and positive" unless size.is_a?(Numeric) && size.finite? && size.positive?

  @face.glyph_ids(text).sum { |glyph| @face.advance_width(glyph) } * size.to_f / @face.units_per_em
end