Class: Castaway::Element::Text

Inherits:
Base
  • Object
show all
Defined in:
lib/castaway/element/text.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #position, #production, #scene, #size

Instance Method Summary collapse

Methods inherited from Base

#_absolute, #_composite, #_convert, #_evaluate_animation_list, #_evaluate_attributes!, #_transform, #alive_at?, #animate, #at, #attribute, #duration, #effect, #enter, #exit, #in, #out, #path, #render_at, #rotate, #scale, #t1, #t2, #tail

Constructor Details

#initialize(production, scene, string) ⇒ Text

Returns a new instance of Text.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/castaway/element/text.rb', line 7

def initialize(production, scene, string)
  super(production, scene)

  @string = string
  @gravity = 'Center'
  @font = 'TimesNewRoman'
  @font_size = 24
  @background = 'transparent'
  @kerning = 0
  @fill = @stroke = nil

  attribute(:font_size, 1) { |memo, value| memo * value }
  attribute(:kerning, -> { @kerning }) { |memo, value| memo + value }
end

Instance Method Details

#_prepare_canvas(t, canvas) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/castaway/element/text.rb', line 57

def _prepare_canvas(t, canvas)
  canvas.xc @background

  font_size = @font_size * attributes[:font_size]
  kerning = attributes[:kerning]

  canvas.pointsize font_size

  commands = [ "gravity #{@gravity}", "font '#{@font}'" ]
  commands << "fill #{@fill}" if @fill
  commands << "stroke #{@stroke}" if @stroke
  commands << format('kerning %.1f', kerning) if kerning
  commands << "text 0,0 '#{@string}'"

  canvas.draw commands.join(' ')
end

#background(color) ⇒ Object



32
33
34
35
# File 'lib/castaway/element/text.rb', line 32

def background(color)
  @background = color
  self
end

#fill(color) ⇒ Object



22
23
24
25
# File 'lib/castaway/element/text.rb', line 22

def fill(color)
  @fill = color
  self
end

#font(font) ⇒ Object



47
48
49
50
# File 'lib/castaway/element/text.rb', line 47

def font(font)
  @font = font
  self
end

#font_size(size) ⇒ Object



52
53
54
55
# File 'lib/castaway/element/text.rb', line 52

def font_size(size)
  @font_size = size
  self
end

#gravity(gravity) ⇒ Object



42
43
44
45
# File 'lib/castaway/element/text.rb', line 42

def gravity(gravity)
  @gravity = gravity
  self
end

#kerning(kerning) ⇒ Object



37
38
39
40
# File 'lib/castaway/element/text.rb', line 37

def kerning(kerning)
  @kerning = kerning
  self
end

#stroke(color) ⇒ Object



27
28
29
30
# File 'lib/castaway/element/text.rb', line 27

def stroke(color)
  @stroke = color
  self
end