Class: Rubydraw::Text

Inherits:
Surface show all
Defined in:
lib/rubydraw/text.rb

Overview

Text objects are instantiazed with the font (can be a TTF file) and with the text to display. They can be drawn at a position on the screen.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Surface

#area, #blit, #fill, #flip, #get_pixel, load_img, load_text, #pixels, #rotozoom, #rotozoom!, #set_pixel, #size, #to_sdl

Constructor Details

#initialize(contents, color, font_name = "Times New Roman", font_size = 25) ⇒ Text

Create a new drawable Text object with the given font and contents.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubydraw/text.rb', line 9

def initialize(contents, color, font_name="Times New Roman", font_size = 25)
  @contents, @font, @font_size, @color = contents, font_name, font_size, color
  if File.exist?(font_name)
    font_path = font_name
  else
    # The font doesn't exist in the program's directory.
    # Check in Rubydraw's font directory (rubydraw-x.x.x/lib/rubydraw/fonts)
    font_path = "#{File.dirname(__FILE__)}/../fonts/#{font_name}.ttf"
  end
  unless File.exists?(font_path)
    raise "Font file '#{font_name}' does not exist; attemped to load from '#{font_path}'"
  end
  sdl_text = SDL::TTF.OpenFont(font_path, font_size)
  raise(SDLError, "Failed to initialize font: #{SDL.GetError}") if sdl_text.pointer.null?

  sdl_color = @color.to_sdl
  @sdl_surface = SDL::TTF.RenderText_Blended(sdl_text, @contents, sdl_color)
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



6
7
8
# File 'lib/rubydraw/text.rb', line 6

def color
  @color
end

#contentsObject (readonly)

Returns the value of attribute contents.



6
7
8
# File 'lib/rubydraw/text.rb', line 6

def contents
  @contents
end

#fontObject (readonly)

Returns the value of attribute font.



6
7
8
# File 'lib/rubydraw/text.rb', line 6

def font
  @font
end

#font_sizeObject (readonly)

Returns the value of attribute font_size.



6
7
8
# File 'lib/rubydraw/text.rb', line 6

def font_size
  @font_size
end

Instance Method Details

#heightObject

See Rubydraw::Text#width



35
36
37
# File 'lib/rubydraw/text.rb', line 35

def height
  super or 0
end

#widthObject

Redefined because if @text is an empty string, it would return nil.



30
31
32
# File 'lib/rubydraw/text.rb', line 30

def width
  super or 0
end