Class: Glimmer::SWT::FontProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/swt/font_proxy.rb

Overview

Proxy for org.eclipse.swt.graphics.Font

This class can be optionally used with WidgetProxy to manipulate an SWT widget font (reusing its FontData but building a new Font)

Otherwise, if no WidgetProxy is passed to constructor, it builds new FontData

Invoking ‘#swt_font` returns the SWT Font object wrapped by this proxy

Follows the Proxy Design Pattern

Constant Summary collapse

ERROR_INVALID_FONT_STYLE =
" is an invalid font style! Valid values are :normal, :bold, and :italic"
FONT_STYLES =
[:normal, :bold, :italic]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widget_proxy = nil, font_properties) ⇒ FontProxy

Builds a new font proxy from passed in widget_proxy and font_properties hash,

It begins with existing SWT widget font and amends it with font properties.

Font properties consist of: :name, :height, and :style (one needed minimum)

Style (:style value) can only be one of FontProxy::FONT_STYLES values: that is :normal, :bold, or :italic



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/glimmer/swt/font_proxy.rb', line 65

def initialize(widget_proxy = nil, font_properties)
  @widget_proxy = widget_proxy
  if font_properties.is_a?(FontData)
    font_datum = font_properties
    @font_properties = {name: font_properties.name, height: font_properties.height, style: font_properties.style}
  elsif font_properties.is_a?(Hash)
    @font_properties = font_properties
    detect_invalid_font_property(font_properties)
    font_properties[:style] = SWTProxy[*font_properties[:style]]
    # TODO consider supporting other properties like locale in the future
    font_data_args = [:name, :height, :style].map do |font_property_name|
      font_properties[font_property_name] || send(font_property_name)
    end
    font_datum = FontData.new(*font_data_args)
  end
  @swt_font = Font.new(DisplayProxy.instance.swt_display, font_datum)
end

Instance Attribute Details

#font_propertiesObject (readonly)

Returns the value of attribute font_properties.



55
56
57
# File 'lib/glimmer/swt/font_proxy.rb', line 55

def font_properties
  @font_properties
end

#swt_fontObject (readonly)

Returns the value of attribute swt_font.



55
56
57
# File 'lib/glimmer/swt/font_proxy.rb', line 55

def swt_font
  @swt_font
end

#widget_proxyObject (readonly)

Returns the value of attribute widget_proxy.



55
56
57
# File 'lib/glimmer/swt/font_proxy.rb', line 55

def widget_proxy
  @widget_proxy
end

Class Method Details

.create(*args) ⇒ Object



40
41
42
# File 'lib/glimmer/swt/font_proxy.rb', line 40

def create(*args)
  flyweight_font_proxies[args] ||= new(*args)
end

.flyweight_font_proxiesObject

Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.



45
46
47
# File 'lib/glimmer/swt/font_proxy.rb', line 45

def flyweight_font_proxies
  @flyweight_font_proxies ||= {}
end

Instance Method Details

#heightObject



87
88
89
# File 'lib/glimmer/swt/font_proxy.rb', line 87

def height
  font_datum.getHeight
end

#nameObject



83
84
85
# File 'lib/glimmer/swt/font_proxy.rb', line 83

def name
  font_datum.getName
end

#styleObject



91
92
93
# File 'lib/glimmer/swt/font_proxy.rb', line 91

def style
  font_datum.getStyle
end