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



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

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



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

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



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

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



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

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



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

def file_urls
  files ? files.values : []
end

#filesHash

Get files as a Hash

Returns:

  • (Hash)

    hash of variant names to URLs



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

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



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

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

#slant_axesArray<Axis>

Get all slant axes

Returns:

  • (Array<Axis>)

    array of slant axes



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

def width_axes
  return [] unless variable_font?

  axes.select(&:width_axis?)
end