Class: Fontist::Import::Google::Models::FontFamily

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/fontist/import/google/models/font_family.rb

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ FontFamily

Initialize with support for files as Hash



41
42
43
44
45
46
47
# File 'lib/fontist/import/google/models/font_family.rb', line 41

def initialize(**attributes)
  if attributes.key?(:files)
    files_value = attributes.delete(:files)
    attributes[:files_data] = files_value
  end
  super(**attributes)
end

Instance Method Details

#axes_countInteger

Get the number of axes

Returns:

  • (Integer)

    the number of axes



154
155
156
# File 'lib/fontist/import/google/models/font_family.rb', line 154

def axes_count
  variable_font? ? axes.length : 0
end

#axis_by_tag(tag) ⇒ Axis?

Find an axis by its tag

Parameters:

  • tag (String)

    the axis tag (e.g., “wght”, “wdth”)

Returns:

  • (Axis, nil)

    the axis if found, nil otherwise



109
110
111
112
113
# File 'lib/fontist/import/google/models/font_family.rb', line 109

def axis_by_tag(tag)
  return nil unless variable_font?

  axes.find { |axis| axis.tag == tag }
end

#custom_axesArray<Axis>

Get all custom (non-standard) axes

Returns:

  • (Array<Axis>)

    array of custom axes



145
146
147
148
149
# File 'lib/fontist/import/google/models/font_family.rb', line 145

def custom_axes
  return [] unless variable_font?

  axes.select(&:custom_axis?)
end

#file_urlsArray<String>

Get all file URLs

Returns:

  • (Array<String>)

    array of file URLs



168
169
170
# File 'lib/fontist/import/google/models/font_family.rb', line 168

def file_urls
  files ? files.values : []
end

#filesHash

Get files as a Hash

Returns:

  • (Hash)

    hash of variant names to URLs



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fontist/import/google/models/font_family.rb', line 52

def files
  return {} if files_data.nil?
  return {} if files_data.respond_to?(:empty?) && files_data.empty?
  return files_data if files_data.is_a?(Hash)

  # Try to parse as JSON first
  begin
    JSON.parse(files_data)
  rescue JSON::ParserError, TypeError
    # If it fails, convert Ruby hash syntax to JSON and parse
    if files_data.is_a?(String)
      # Convert Ruby hash syntax (=>) to JSON syntax (:)
      json_str = files_data.gsub("=>", ":")
      begin
        return JSON.parse(json_str)
      rescue JSON::ParserError
        {}
      end
    end
    {}
  end
end

#files=(value) ⇒ Object

Set files from a Hash or JSON string

Parameters:

  • value (Hash, String)

    the files data



78
79
80
# File 'lib/fontist/import/google/models/font_family.rb', line 78

def files=(value)
  self.files_data = value
end

#slant_axesArray<Axis>

Get all slant axes

Returns:

  • (Array<Axis>)

    array of slant axes



136
137
138
139
140
# File 'lib/fontist/import/google/models/font_family.rb', line 136

def slant_axes
  return [] unless variable_font?

  axes.select(&:slant_axis?)
end

#summaryString

Get a human-readable summary

Returns:

  • (String)

    summary of the font family



193
194
195
196
197
# File 'lib/fontist/import/google/models/font_family.rb', line 193

def summary
  parts = [family, version]
  parts << "(#{axes_count} axes)" if variable_font?
  parts.join(" ")
end

#variable_font?Boolean

Check if this is a variable font family Variable fonts have axes defined

Returns:

  • (Boolean)

    true if axes are present and not empty



86
87
88
# File 'lib/fontist/import/google/models/font_family.rb', line 86

def variable_font?
  !axes.nil? && !axes.empty?
end

#variant_exists?(variant_name) ⇒ Boolean

Check if a specific variant exists

Parameters:

  • variant_name (String)

    the variant name

Returns:

  • (Boolean)

    true if variant exists



176
177
178
# File 'lib/fontist/import/google/models/font_family.rb', line 176

def variant_exists?(variant_name)
  variant_names.include?(variant_name)
end

#variant_namesArray<String>

Get all variant names

Returns:

  • (Array<String>)

    array of variant names



161
162
163
# File 'lib/fontist/import/google/models/font_family.rb', line 161

def variant_names
  variants || []
end

#variant_url(variant_name) ⇒ String?

Get URL for a specific variant

Parameters:

  • variant_name (String)

    the variant name

Returns:

  • (String, nil)

    the URL if found, nil otherwise



184
185
186
187
188
# File 'lib/fontist/import/google/models/font_family.rb', line 184

def variant_url(variant_name)
  return nil if files.nil? || files.empty?

  files[variant_name]
end

#variants_by_format(format) ⇒ Hash

Get all variants in a specific format Note: This requires the files hash to contain URLs with the format extension

Parameters:

  • format (Symbol, String)

    the format (:ttf or :woff2)

Returns:

  • (Hash)

    hash of variant name to URL for the format



96
97
98
99
100
101
102
103
# File 'lib/fontist/import/google/models/font_family.rb', line 96

def variants_by_format(format)
  return {} if files.nil?

  extension = format_extension(format)
  files.select do |_variant_name, url|
    url.end_with?(extension)
  end
end

#weight_axesArray<Axis>

Get all weight axes

Returns:

  • (Array<Axis>)

    array of weight axes



118
119
120
121
122
# File 'lib/fontist/import/google/models/font_family.rb', line 118

def weight_axes
  return [] unless variable_font?

  axes.select(&:weight_axis?)
end

#width_axesArray<Axis>

Get all width axes

Returns:

  • (Array<Axis>)

    array of width axes



127
128
129
130
131
# File 'lib/fontist/import/google/models/font_family.rb', line 127

def width_axes
  return [] unless variable_font?

  axes.select(&:width_axis?)
end