class Fontist::Font

Attributes

name[R]

Public Class Methods

all() click to toggle source
# File lib/fontist/font.rb, line 16
def self.all
  new.all
end
find(name) click to toggle source
# File lib/fontist/font.rb, line 20
def self.find(name)
  new(name: name).find
end
install(name, options = {}) click to toggle source
# File lib/fontist/font.rb, line 24
def self.install(name, options = {})
  new(options.merge(name: name)).install
end
list(name) click to toggle source
# File lib/fontist/font.rb, line 36
def self.list(name)
  new(name: name).list
end
new(options = {}) click to toggle source
# File lib/fontist/font.rb, line 6
def initialize(options = {})
  @name = options[:name]
  @confirmation = options[:confirmation] || "no"
  @hide_licenses = options[:hide_licenses]
  @no_progress = options[:no_progress] || false
  @force = options[:force] || false

  check_or_create_fontist_path!
end
status(name) click to toggle source
# File lib/fontist/font.rb, line 32
def self.status(name)
  new(name: name).status
end
uninstall(name) click to toggle source
# File lib/fontist/font.rb, line 28
def self.uninstall(name)
  new(name: name).uninstall
end

Public Instance Methods

all() click to toggle source
# File lib/fontist/font.rb, line 64
def all
  Fontist::Formula.all.map(&:fonts).flatten
end
find() click to toggle source
# File lib/fontist/font.rb, line 40
def find
  find_system_font || downloadable_font || raise_non_supported_font
end
install() click to toggle source
# File lib/fontist/font.rb, line 44
def install
  (find_system_font unless @force) || download_font || raise_non_supported_font
end
list() click to toggle source
# File lib/fontist/font.rb, line 58
def list
  return all_list unless @name

  font_list || raise_non_supported_font
end
status() click to toggle source
# File lib/fontist/font.rb, line 52
def status
  return installed_paths unless @name

  find_system_font || downloadable_font || raise_non_supported_font
end
uninstall() click to toggle source
# File lib/fontist/font.rb, line 48
def uninstall
  uninstall_font || downloadable_font || raise_non_supported_font
end

Private Instance Methods

all_formulas() click to toggle source
# File lib/fontist/font.rb, line 192
def all_formulas
  Fontist::Formula.all
end
all_list() click to toggle source
# File lib/fontist/font.rb, line 206
def all_list
  list_styles(all_formulas)
end
ask_for_agreement() click to toggle source
# File lib/fontist/font.rb, line 147
def ask_for_agreement
  Fontist.ui.ask(
    "\nDo you accept all presented font licenses, and want Fontist " \
    "to download these fonts for you? => TYPE 'Yes' or 'No':"
  )
end
check_and_confirm_required_license(formula) click to toggle source
# File lib/fontist/font.rb, line 129
def check_and_confirm_required_license(formula)
  return @confirmation unless formula.license_required

  show_license(formula.license) unless @hide_licenses
  return @confirmation if @confirmation.casecmp?("yes")

  confirmation = ask_for_agreement
  return confirmation if confirmation&.casecmp?("yes")

  raise Fontist::Errors::LicensingError.new(
    "Fontist will not download these fonts unless you accept the terms."
  )
end
check_or_create_fontist_path!() click to toggle source
# File lib/fontist/font.rb, line 90
def check_or_create_fontist_path!
  unless Fontist.fonts_path.exist?
    require "fileutils"
    FileUtils.mkdir_p(Fontist.fonts_path)
  end
end
download_font() click to toggle source
# File lib/fontist/font.rb, line 115
def download_font
  return if formulas.empty?

  formulas.flat_map do |formula|
    confirmation = check_and_confirm_required_license(formula)
    paths = font_installer(formula).install(confirmation: confirmation)

    Fontist.ui.say("Fonts installed at:")
    paths.each do |path|
      Fontist.ui.say("- #{path}")
    end
  end
end
downloadable_font() click to toggle source
# File lib/fontist/font.rb, line 109
def downloadable_font
  if formula
    raise Fontist::Errors::MissingFontError.new(name)
  end
end
find_fontist_paths() click to toggle source
# File lib/fontist/font.rb, line 179
def find_fontist_paths
  fonts = Fontist::SystemIndex.fontist_index.find(name, nil)
  return unless fonts

  fonts.map do |font|
    font[:path]
  end
end
find_system_font() click to toggle source
# File lib/fontist/font.rb, line 72
def find_system_font
  paths = Fontist::SystemFont.find(name)
  unless paths
    Fontist.ui.say(%(Font "#{name}" not found locally.))
    return
  end

  print_paths(paths)
end
font_installer(formula) click to toggle source
# File lib/fontist/font.rb, line 97
def font_installer(formula)
  FontInstaller.new(formula, no_progress: @no_progress)
end
font_list() click to toggle source
# File lib/fontist/font.rb, line 210
def font_list
  return if formulas.empty?

  list_styles(formulas)
end
font_paths() click to toggle source
# File lib/fontist/font.rb, line 202
def font_paths
  @font_paths ||= Dir.glob(Fontist.fonts_path.join("**"))
end
formula() click to toggle source
# File lib/fontist/font.rb, line 101
def formula
  @formula ||= Fontist::Formula.find(name)
end
formulas() click to toggle source
# File lib/fontist/font.rb, line 105
def formulas
  @formulas ||= Fontist::Formula.find_many(name)
end
installed(style) click to toggle source
# File lib/fontist/font.rb, line 230
def installed(style)
  path(style) ? true : false
end
installed_paths() click to toggle source
# File lib/fontist/font.rb, line 188
def installed_paths
  print_paths(SystemFont.font_paths)
end
license_agrement_message(license) click to toggle source
# File lib/fontist/font.rb, line 154
    def license_agrement_message(license)
      <<~MSG
        FONT LICENSE ACCEPTANCE REQUIRED FOR "#{name}":

        Fontist can install this font if you accept its licensing conditions.

        FONT LICENSE BEGIN ("#{name}")
        -----------------------------------------------------------------------
        #{license}
        -----------------------------------------------------------------------
        FONT LICENSE END ("#{name}")
      MSG
    end
list_styles(formulas) click to toggle source
# File lib/fontist/font.rb, line 216
def list_styles(formulas)
  map_to_hash(formulas) do |formula|
    map_to_hash(formula.fonts) do |font|
      map_to_hash(font.styles) do |style|
        installed(style)
      end
    end
  end
end
map_to_hash(elements) { |e| ... } click to toggle source
# File lib/fontist/font.rb, line 226
def map_to_hash(elements)
  elements.map { |e| [e, yield(e)] }.to_h
end
path(style) click to toggle source
# File lib/fontist/font.rb, line 196
def path(style)
  font_paths.detect do |path|
    File.basename(path) == style.font
  end
end
print_paths(paths) click to toggle source
raise_non_supported_font() click to toggle source
# File lib/fontist/font.rb, line 234
def raise_non_supported_font
  raise Fontist::Errors::UnsupportedFontError.new(@name)
end
show_license(license) click to toggle source
# File lib/fontist/font.rb, line 143
def show_license(license)
  Fontist.ui.say(license_agrement_message(license))
end
uninstall_font() click to toggle source
# File lib/fontist/font.rb, line 168
def uninstall_font
  paths = find_fontist_paths
  return unless paths

  paths.each do |path|
    File.delete(path)
  end

  paths
end