Class: Fontist::Formula
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Fontist::Formula
- Defined in:
- lib/fontist/formula.rb
Constant Summary collapse
- NAMESPACES =
{ "sil" => "SIL", "macos" => "macOS", }.freeze
Class Method Summary collapse
- .all ⇒ Object
- .all_keys ⇒ Object
- .find(font_name) ⇒ Object
- .find_by_font_file(font_file) ⇒ Object
- .find_by_key(key) ⇒ Object
- .find_by_key_or_name(name) ⇒ Object
- .find_by_name(name) ⇒ Object
- .find_fonts(font_name) ⇒ Object
- .find_many(font_name) ⇒ Object
- .find_styles(font_name, style_name) ⇒ Object
- .from_file(path) ⇒ Object
- .name_to_key(name) ⇒ Object
- .titleize(str) ⇒ Object
- .update_formulas_repo ⇒ Object
Instance Method Summary collapse
- #all_fonts ⇒ Object
- #collection_fonts ⇒ Object
- #downloadable? ⇒ Boolean
- #file_size ⇒ Object
- #font_by_name(name) ⇒ Object
- #fonts_by_name(name) ⇒ Object
- #key ⇒ Object
- #key_from_path ⇒ Object
- #license ⇒ Object
- #license_required? ⇒ Boolean
- #manual? ⇒ Boolean
- #source ⇒ Object
- #style_override(font) ⇒ Object
Class Method Details
.all ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/fontist/formula.rb', line 96 def self.all formulas = Dir[Fontist.formulas_path.join("**/*.yml").to_s].map do |path| Formula.from_file(path) end FormulaCollection.new(formulas) end |
.all_keys ⇒ Object
104 105 106 107 108 |
# File 'lib/fontist/formula.rb', line 104 def self.all_keys Dir[Fontist.formulas_path.join("**/*.yml").to_s].map do |path| path.sub("#{Fontist.formulas_path}/", "").sub(".yml", "") end end |
.find(font_name) ⇒ Object
110 111 112 |
# File 'lib/fontist/formula.rb', line 110 def self.find(font_name) Indexes::FontIndex.from_file.load_formulas(font_name).first end |
.find_by_font_file(font_file) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/fontist/formula.rb', line 161 def self.find_by_font_file(font_file) key = Indexes::FilenameIndex.from_file .load_index_formulas(File.basename(font_file)) .flat_map(&:name) .first find_by_key(key) end |
.find_by_key(key) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/fontist/formula.rb', line 144 def self.find_by_key(key) path = Fontist.formulas_path.join("#{key}.yml") return unless File.exist?(path) from_file(path) end |
.find_by_key_or_name(name) ⇒ Object
140 141 142 |
# File 'lib/fontist/formula.rb', line 140 def self.find_by_key_or_name(name) find_by_key(name) || find_by_name(name) end |
.find_by_name(name) ⇒ Object
151 152 153 154 155 |
# File 'lib/fontist/formula.rb', line 151 def self.find_by_name(name) key = name_to_key(name) find_by_key(key) end |
.find_fonts(font_name) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/fontist/formula.rb', line 118 def self.find_fonts(font_name) formulas = Indexes::FontIndex.from_file.load_formulas(font_name) formulas.map do |formula| formula.all_fonts.select do |f| f.name.casecmp?(font_name) end end.flatten end |
.find_many(font_name) ⇒ Object
114 115 116 |
# File 'lib/fontist/formula.rb', line 114 def self.find_many(font_name) Indexes::FontIndex.from_file.load_formulas(font_name) end |
.find_styles(font_name, style_name) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/fontist/formula.rb', line 128 def self.find_styles(font_name, style_name) formulas = Indexes::FontIndex.from_file.load_formulas(font_name) formulas.map do |formula| formula.all_fonts.map do |f| f.styles.select do |s| f.name.casecmp?(font_name) && s.type.casecmp?(style_name) end end end.flatten end |
.from_file(path) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/fontist/formula.rb', line 170 def self.from_file(path) unless File.exist?(path) raise Fontist::Errors::FormulaCouldNotBeFoundError, "Formula file not found: #{path}" end content = File.read(path) from_yaml(content).tap do |formula| formula.path = path formula.name = titleize(formula.key_from_path) if formula.name.nil? end end |
.name_to_key(name) ⇒ Object
157 158 159 |
# File 'lib/fontist/formula.rb', line 157 def self.name_to_key(name) name.downcase.gsub(" ", "_") end |
.titleize(str) ⇒ Object
184 185 186 187 188 |
# File 'lib/fontist/formula.rb', line 184 def self.titleize(str) str.split("/").map do |part| part.tr("_", " ").split.map(&:capitalize).join(" ") end.join("/") end |
.update_formulas_repo ⇒ Object
92 93 94 |
# File 'lib/fontist/formula.rb', line 92 def self.update_formulas_repo Update.call end |
Instance Method Details
#all_fonts ⇒ Object
241 242 243 |
# File 'lib/fontist/formula.rb', line 241 def all_fonts Array(fonts) + collection_fonts end |
#collection_fonts ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/fontist/formula.rb', line 245 def collection_fonts Array(font_collections).flat_map do |c| c.fonts.flat_map do |f| f.styles.each do |s| s.font = c.filename s.source_font = c.source_filename end f end end end |
#downloadable? ⇒ Boolean
194 195 196 |
# File 'lib/fontist/formula.rb', line 194 def downloadable? !resources.nil? && !resources.empty? end |
#file_size ⇒ Object
223 224 225 226 227 |
# File 'lib/fontist/formula.rb', line 223 def file_size return nil if resources.nil? || resources.empty? resources.first.file_size end |
#font_by_name(name) ⇒ Object
229 230 231 232 233 |
# File 'lib/fontist/formula.rb', line 229 def font_by_name(name) all_fonts.find do |font| font.name.casecmp?(name) end end |
#fonts_by_name(name) ⇒ Object
235 236 237 238 239 |
# File 'lib/fontist/formula.rb', line 235 def fonts_by_name(name) all_fonts.select do |font| font.name.casecmp?(name) end end |
#key ⇒ Object
204 205 206 |
# File 'lib/fontist/formula.rb', line 204 def key @key ||= key_from_path end |
#key_from_path ⇒ Object
208 209 210 211 212 213 |
# File 'lib/fontist/formula.rb', line 208 def key_from_path return "" unless @path escaped = Regexp.escape("#{Fontist.formulas_path}/") @path.sub(Regexp.new("^#{escaped}"), "").sub(/\.yml$/, "").to_s end |
#license ⇒ Object
215 216 217 |
# File 'lib/fontist/formula.rb', line 215 def license open_license || requires_license_agreement end |
#license_required? ⇒ Boolean
219 220 221 |
# File 'lib/fontist/formula.rb', line 219 def license_required? requires_license_agreement ? true : false end |
#manual? ⇒ Boolean
190 191 192 |
# File 'lib/fontist/formula.rb', line 190 def manual? !downloadable? end |
#source ⇒ Object
198 199 200 201 202 |
# File 'lib/fontist/formula.rb', line 198 def source return nil if resources.empty? resources.first.source end |
#style_override(font) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/fontist/formula.rb', line 257 def style_override(font) all_fonts .map(&:styles) .flatten .detect { |s| s.family_name == font }&.override || {} end |