Sha256: 5dc06d877bf9a05cb2cc1e5a23d232262ecda26924ec305c41e32e982907e64a

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

#
# Font helper
#

require 'mini_magick'
require 'pp'

module VCSRuby
  IMFont = Struct.new(:name, :family, :style, :stretch, :weight, :glyphs)

  class Font
    attr_reader :name, :path, :size

    @@fonts = {}

    def initialize name, size
      @name = name
      @path = find_path
      @size = size
    end

    def exists?
      load_font_cache if @@fonts.length == 0

      !!font_by_name(@name)
    end

    def find_path
      load_font_cache if @@fonts.length == 0

      if exists?
        font_by_name(@name).glyphs
      else
        nil
      end
    end

    def font_by_name name
      if name =~ /\./
        key, font = @@fonts.select{ |key, f| f.glyphs =~ /#{name}\z/ }.first
        return font
      else
        @@fonts[name]
      end
    end

    def load_font_cache

      fonts = MiniMagick::Tool::Identify.new(whiny: false) do |identify|
        identify.list 'font'
      end

      parse_fonts(fonts)
    end

    def parse_fonts(fonts)
      font = nil
      fonts.lines.each do |line|
        key, value = line.strip.split(':', 2).map(&:strip)

        next if [nil, 'Path'].include? key

        if key == 'Font'
          @@fonts[value] = font = IMFont.new(value)
        else
          font.send("#{key}=", value)
        end
      end
    end

    def line_height
      MiniMagick::Tool::Convert.new do |convert|
        convert.font path if exists?
        convert.pointsize size
        convert << 'label:F'
        convert.format '%h'
        convert << 'info:'
      end.to_i
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vcs_ruby-1.1.9 lib/font.rb