Module: Plushie::Type::Font

Defined in:
lib/plushie/type/font.rb

Overview

Font specification with family, weight, style, and stretch.

Accepts :default, :monospace, a family name string, or a Font struct with detailed properties.

Examples:

Simple

text("msg", "Hello", font: :monospace)
text("msg", "Hello", font: "Fira Code")

Detailed

Font.from_opts(family: "Inter", weight: :bold, style: :italic)

Defined Under Namespace

Classes: Spec

Constant Summary collapse

WEIGHTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Valid font weight values.

%i[thin extra_light light normal medium semi_bold bold extra_bold black].freeze
STYLES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Valid font style values.

%i[normal italic oblique].freeze
STRETCHES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Valid font stretch values.

%i[ultra_condensed extra_condensed condensed semi_condensed
normal semi_expanded expanded extra_expanded ultra_expanded].freeze
FIELD_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Recognized field keys for font specs.

%i[family weight style stretch].freeze

Class Method Summary collapse

Class Method Details

.encode(value) ⇒ String, Hash

Encode a font value for the wire protocol.

Parameters:

  • value (Symbol, String, Spec, Hash)

Returns:

  • (String, Hash)


59
60
61
62
63
64
65
66
67
68
# File 'lib/plushie/type/font.rb', line 59

def encode(value)
  case value
  when :default then "default"
  when :monospace then "monospace"
  when String then {family: value}
  when Spec then encode_spec(value)
  when Hash then encode_spec(Spec.new(**value.slice(*FIELD_KEYS)))
  else raise ArgumentError, "invalid font: #{value.inspect}"
  end
end

.encode_spec(spec) ⇒ Object

Convert a Spec to a wire-ready hash. Weight/style/stretch encode as snake_case strings, matching every other enum in the system.



73
74
75
76
77
78
79
80
# File 'lib/plushie/type/font.rb', line 73

def encode_spec(spec)
  h = {}
  h[:family] = spec.family if spec.family
  h[:weight] = spec.weight.to_s if spec.weight
  h[:style] = spec.style.to_s if spec.style
  h[:stretch] = spec.stretch.to_s if spec.stretch
  h
end

.from_opts(opts) ⇒ Spec

Construct from keyword options.

Parameters:

  • opts (Hash)

    :family, :weight, :style, :stretch

Returns:

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/plushie/type/font.rb', line 49

def from_opts(opts)
  unknown = opts.keys - FIELD_KEYS
  raise ArgumentError, "unknown font fields: #{unknown.inspect}" if unknown.any?
  Spec.new(**opts.slice(*FIELD_KEYS))
end