Sha256: bdd79edd46d0545f5d6a697eb7eb570d804178545d9271dd20d60c34cd29040f

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

class Shoes
  module Swt
    class TextFontFactory
      def initialize
        @fonts = []
      end

      def create_font(font_style)
        name = font_style[:name]
        size = font_style[:size]
        styles = styles_bitmask(font_style[:styles])

        existing_font = find_existing_font(name, size, styles)
        if existing_font
          existing_font
        else
          build_font(name, size, styles)
        end
      end

      def dispose
        @fonts.each { |font| font.dispose unless font.disposed? }
        @fonts.clear
      end

      def find_existing_font(name, size, styles)
        # Bit odd, but fonts on some OS's have multiple font_data elements,
        # so check if any of them match.
        @fonts.find do |font|
          font.font_data.any? do |font_data|
            font_data.name == name &&
              # Windows seems to create fonts of height 15.75 when requesting 16
              font_data.height.round == size.round &&
              font_data.style == styles
          end
        end
      end

      def build_font(name, size, styles)
        font = ::Swt::Graphics::Font.new @display, name, size, styles
        @fonts << font
        font
      end

      def styles_bitmask(styles)
        styles.reduce { |acc, elem| acc | elem }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shoes-swt-4.0.0.pre8 lib/shoes/swt/text_block/text_font_factory.rb
shoes-swt-4.0.0.pre7 lib/shoes/swt/text_block/text_font_factory.rb