Sha256: 690b781b2a94252a4580fea551ad477981b621bd63572551054881823ff280b8

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 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 ['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-0.8.5 lib/font.rb