Sha256: 80a81079b672cbe2361865c615afd7e53f5f36f78154e42822e278b98fb54308

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true
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

4 entries across 4 versions & 1 rubygems

Version Path
shoes-swt-4.0.0.pre12 lib/shoes/swt/text_block/text_font_factory.rb
shoes-swt-4.0.0.pre11 lib/shoes/swt/text_block/text_font_factory.rb
shoes-swt-4.0.0.pre10 lib/shoes/swt/text_block/text_font_factory.rb
shoes-swt-4.0.0.pre9 lib/shoes/swt/text_block/text_font_factory.rb