Sha256: 7c37acc00b7de32cbe645bd1f85bbd1c8fff42e1ff4fe8b236e9d101dd32260d

Contents?: true

Size: 1.74 KB

Versions: 18

Compression:

Stored size: 1.74 KB

Contents

module Fontist
  class Fontconfig
    def self.update
      new.update
    end

    def self.remove(options = {})
      new(options).remove
    end

    def initialize(options = {})
      @options = options
    end

    def update
      ensure_fontconfig_installed
      create_config
      regenerate_fontconfig_cache
    end

    def remove
      return handle_file_not_found unless config_exists?

      regenerate_fontconfig_cache if fontconfig_installed?
      remove_config
    end

    private

    def ensure_fontconfig_installed
      raise Errors::FontconfigNotFoundError unless fontconfig_installed?
    end

    def fontconfig_installed?
      Utils::System.fontconfig_installed?
    end

    def create_config
      return if File.exist?(config_path)

      FileUtils.mkdir_p(File.dirname(config_path))
      File.write(config_path, config_content)
    end

    def config_path
      File.join(xdg_config_home, "fontconfig", "conf.d", "10-fontist.conf")
    end

    def xdg_config_home
      ENV["XDG_CONFIG_HOME"] || File.join(Dir.home, ".config")
    end

    def config_content
      <<~CONTENT
        <?xml version='1.0'?>
        <!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
        <fontconfig>
          <dir>#{Fontist.fonts_path}</dir>
        </fontconfig>
      CONTENT
    end

    def regenerate_fontconfig_cache
      Helpers.run("fc-cache -f")
    end

    def ensure_file_exists
      return if @options[:force]

      raise Errors::FontconfigFileNotFoundError unless File.exist?(config_path)
    end

    def config_exists?
      File.exist?(config_path)
    end

    def handle_file_not_found
      return if @options[:force]

      raise Errors::FontconfigFileNotFoundError
    end

    def remove_config
      FileUtils.rm(config_path)
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
fontist-1.21.2 lib/fontist/fontconfig.rb
fontist-1.21.1 lib/fontist/fontconfig.rb
fontist-1.20.0 lib/fontist/fontconfig.rb
fontist-1.19.0 lib/fontist/fontconfig.rb
fontist-1.18.2 lib/fontist/fontconfig.rb
fontist-1.17.1 lib/fontist/fontconfig.rb
fontist-1.17.0 lib/fontist/fontconfig.rb
fontist-1.16.0 lib/fontist/fontconfig.rb
fontist-1.15.2 lib/fontist/fontconfig.rb
fontist-1.15.1 lib/fontist/fontconfig.rb
fontist-1.15.0 lib/fontist/fontconfig.rb
fontist-1.14.6 lib/fontist/fontconfig.rb
fontist-1.14.5 lib/fontist/fontconfig.rb
fontist-1.14.4 lib/fontist/fontconfig.rb
fontist-1.14.3 lib/fontist/fontconfig.rb
fontist-1.14.2 lib/fontist/fontconfig.rb
fontist-1.14.1 lib/fontist/fontconfig.rb
fontist-1.14.0 lib/fontist/fontconfig.rb