Class: Shoes::Swt::TextFontFactory

Inherits:
Object
  • Object
show all
Defined in:
shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb

Instance Method Summary collapse

Constructor Details

#initializeTextFontFactory

Returns a new instance of TextFontFactory.



6
7
8
9
# File 'shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb', line 6

def initialize
  @fonts = []
  @display = nil
end

Instance Method Details

#build_font(name, size, styles) ⇒ Object



42
43
44
45
46
# File 'shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb', line 42

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

#create_font(font_style) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb', line 11

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

#disposeObject



24
25
26
27
# File 'shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb', line 24

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

#find_existing_font(name, size, styles) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb', line 29

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

#styles_bitmask(styles) ⇒ Object



48
49
50
# File 'shoes-swt/lib/shoes/swt/text_block/text_font_factory.rb', line 48

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