Class: Tk::Font

Inherits:
Object show all
Defined in:
lib/ffi-tk/command/font.rb

Overview

Create and inspect fonts. The font command provides several facilities for dealing with fonts, such as defining named fonts and inspecting the actual attributes of a font.

Constant Summary collapse

FONT_CONFIGURE_HINTS =
{
  underline:  :boolean,
  overstrike: :boolean,
  weight:     :symbol,
  slant:      :symbol,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_or_hash) ⇒ Font

Returns a new instance of Font.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ffi-tk/command/font.rb', line 6

def initialize(string_or_hash)
  if string_or_hash.respond_to?(:to_str)
    string_or_hash =~ /^(.*)\s+(\d+)?$/

    params = {}
    params[:family] = $1.to_s
    params[:size] = $2.to_i if $2

    @font = Font.create(params)
  elsif string_or_hash.respond_to?(:to_hash)
    @font = Font.create(string_or_hash)
  else
    raise ArgumentError
  end
end

Class Method Details

.actual(font, options = {}) ⇒ Object

NOTE:

the signature has been simplified to a required +font+ argument and a
simple +options+ hash.
The original signature is:
  `font actual font ?-displayof window? ?option? ?--? ?char?`
But it just makes things very painful.


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ffi-tk/command/font.rb', line 71

def self.actual(font, options = {})
  window = options.fetch(:displayof, None)
  option = options.fetch(:option, None)
  char = options.fetch(:char, None)

  args = []
  args << "-displayof" << window unless window == None
  args << option.to_tcl_option unless option == None
  args << "--" << char.to_tcl unless char == None

  array = execute(:actual, font, *args)
  array.tcl_options_to_hash(FONT_CONFIGURE_HINTS)
end

.configure(fontname, argument = None) ⇒ Object



85
86
87
88
# File 'lib/ffi-tk/command/font.rb', line 85

def self.configure(fontname, argument = None)
  Configure.common(
    self, [:configure, fontname], argument, FONT_CONFIGURE_HINTS)
end

.create(fontname, options = None) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ffi-tk/command/font.rb', line 90

def self.create(fontname, options = None)
  if fontname.respond_to?(:to_tcl_options)
    fontname, options = None, fontname
    options = options.to_tcl_options
  end

  execute(:create, fontname, options)
end

.delete(*fontnames) ⇒ Object



99
100
101
# File 'lib/ffi-tk/command/font.rb', line 99

def self.delete(*fontnames)
  execute(:delete, *fontnames)
end

.execute(command, *args) ⇒ Object



53
54
55
# File 'lib/ffi-tk/command/font.rb', line 53

def self.execute(command, *args)
  Tk.execute(:font, command, *args)
end

.execute_only(command, *args) ⇒ Object



57
58
59
# File 'lib/ffi-tk/command/font.rb', line 57

def self.execute_only(command, *args)
  Tk.execute_only(:font, command, *args)
end

.families(options = {}) ⇒ Object

The return value is a list of the case-insensitive names of all font families that exist on window’s display. If the window argument is omitted, it defaults to the main window.



106
107
108
# File 'lib/ffi-tk/command/font.rb', line 106

def self.families(options = {})
  execute(:families, options.to_tcl_options)
end

.measure(font, text, options = {}) ⇒ Object



110
111
112
# File 'lib/ffi-tk/command/font.rb', line 110

def self.measure(font, text, options = {})
  execute(:measure, font, options.to_tcl_options, text)
end

.metrics(font, option, options = {}) ⇒ Object



114
115
116
# File 'lib/ffi-tk/command/font.rb', line 114

def self.metrics(font, option, options = {})
  execute(:metrics, font, options.to_tcl_options, option.to_tcl_option)
end

.namesObject

The return value is a list of all the named fonts that are currently defined.



120
121
122
# File 'lib/ffi-tk/command/font.rb', line 120

def self.names
  execute(:names).to_a
end

Instance Method Details

#actual(options = {}) ⇒ Object



22
23
24
# File 'lib/ffi-tk/command/font.rb', line 22

def actual(options = {})
  Font.actual(@font, options)
end

#actual_hash(options = {}) ⇒ Object



26
27
28
# File 'lib/ffi-tk/command/font.rb', line 26

def actual_hash(options = {})
  Font.actual(@font, options)
end

#configure(argument = None) ⇒ Object



38
39
40
# File 'lib/ffi-tk/command/font.rb', line 38

def configure(argument = None)
  Font.configure(@font, argument)
end

#measure(text, options = {}) ⇒ Object



30
31
32
# File 'lib/ffi-tk/command/font.rb', line 30

def measure(text, options = {})
  Font.measure(@font, text, options)
end

#metrics(option, options = {}) ⇒ Object



34
35
36
# File 'lib/ffi-tk/command/font.rb', line 34

def metrics(option, options = {})
  Font.metrics(@font, option, options)
end

#to_tclObject



42
43
44
# File 'lib/ffi-tk/command/font.rb', line 42

def to_tcl
  TclString.new(@font)
end