smalruby/dxruby_sdl

View on GitHub
lib/dxruby_sdl/font.rb

Summary

Maintainability
A
0 mins
Test Coverage
# -*- coding: utf-8 -*-

module DXRubySDL
  class Font
    attr_reader :size
    attr_reader :_ttf

    @font_cache = {}

    def initialize(size, fontname = '', hash = {})
      @size = size

      if !SDL::TTF.init?
        SDL::TTF.init
      end
      if !(path = (FONTS[fontname] || FONT_ALIASES[fontname]))
        path = FONTS.first.last
      end
      font_cache = Font.instance_variable_get('@font_cache')
      args = [path, size]
      if font_cache.key?(args)
        @_ttf = font_cache[args]
      else
        @_ttf = font_cache[args] = SDL::TTF.open(*args)
      end
    end

    def get_width(string)
      return @_ttf.text_size(string).first
    end

    alias_method :getWidth, :get_width

    private

    FONTS = {}
    private_constant :FONTS

    FONT_ALIASES = {}
    private_constant :FONT_ALIASES

    # :nocov:
    if /darwin/ =~ RUBY_PLATFORM
      font_info =
        [
        %w[
          梅Pゴシック
          /Library/Fonts/ume-pgo4.ttf
          ume-pgo4
          MS\ Pゴシック
          MS\ Pゴシック
          MS\ PGothic
        ],
        %w[
          梅ゴシック
          /Library/Fonts/ume-tgo4.ttf
          ume-tgo4
          MS\ ゴシック
          MS\ ゴシック
          MS\ Gothic
        ],
        %w[
          梅P明朝
          /Library/Fonts/ume-pmo3.ttf
          ume-pmo3
          MS\ P明朝
          MS\ P明朝
          MS\ PMincho
        ],
        %w[
          梅明朝
          /Library/Fonts/ume-tmo3.ttf
          ume-tmo3
          MS\ 明朝
          MS\ 明朝
          MS\ Mincho
        ],
         %w[
           MS\ Pゴシック
           /Library/Fonts/Microsoft/MS\ PGothic.ttf
           MS\ Pゴシック
           MS\ PGothic
         ],
         %w[
           MS\ ゴシック
           /Library/Fonts/Microsoft/MS\ Gothic.ttf
           MS\ ゴシック
           MS\ Gothic
         ],
         %w[
           MS\ P明朝
           /Library/Fonts/Microsoft/MS\ PMincho.ttf
           MS\ P明朝
           MS\ PMincho
         ],
         %w[
           MS\ 明朝
           /Library/Fonts/Microsoft/MS\ Mincho.ttf
           MS\ 明朝
           MS\ Mincho
         ],
         %w[
           Osaka
           /Library/Fonts/osaka.ttf
         ],
         %w[
           ヒラギノ丸ゴ\ ProN\ W4
           /Library/Fonts/ヒラギノ丸ゴ\ ProN\ W4.ttc
         ],
         %w[
           IPA\ Pゴシック
           /Library/Fonts/ipagp.ttf
           IPA\ PGothic
           ipagp
         ],
         %w[
           IPA\ ゴシック
           /Library/Fonts/ipag.ttf
           IPA\ Gothic
           ipag
         ],
        ]
    elsif /linux/ =~ RUBY_PLATFORM
      font_info =
        [
         %w[
           梅Pゴシック
           /usr/share/fonts/truetype/horai-umefont/ume-pgo4.ttf
           ume-pgo4
           MS\ Pゴシック
           MS\ Pゴシック
           MS\ PGothic
         ],
         %w[
           梅ゴシック
           /usr/share/fonts/truetype/horai-umefont/ume-tgo4.ttf
           ume-tgo4
           MS\ ゴシック
           MS\ ゴシック
           MS\ Gothic
         ],
         %w[
           梅P明朝
           /usr/share/fonts/truetype/horai-umefont/ume-pmo3.ttf
           ume-pmo3
           MS\ P明朝
           MS\ P明朝
           MS\ PMincho
         ],
         %w[
           梅明朝
           /usr/share/fonts/truetype/horai-umefont/ume-tmo3.ttf
           ume-tmo3
           MS\ 明朝
           MS\ 明朝
           MS\ Mincho
         ],
         %w[
           IPA\ Pゴシック
           /usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf:/usr/share/fonts/ipa-pgothic/ipagp.ttf
           IPA\ PGothic
           ipagp
         ],
         %w[
           IPA\ ゴシック
           /usr/share/fonts/opentype/ipafont-gothic/ipag.ttf:/usr/share/fonts/ipa-gothic/ipag.ttf
           IPA\ Gothic
           ipag
         ],
         %w[
           IPA\ P明朝
           /usr/share/fonts/opentype/ipafont-mincho/ipamp.ttf:/usr/share/fonts/ipa-pmincho/ipamp.ttf
           IPA\ PMincho
           ipamp
         ],
         %w[
           IPA\ 明朝
           /usr/share/fonts/opentype/ipafont-mincho/ipam.ttf:/usr/share/fonts/ipa-mincho/ipam.ttf
           IPA\ Mincho
           ipam
         ],
        ]
    end
    # :nocov:
    font_info.each do |name, paths, *aliases|
      path = paths.split(':').find { |s| File.exist?(s) }
      if path
        FONTS[name] = path
        aliases.each do |a|
          FONT_ALIASES[a] = path
        end
      end
    end
  end
end