Class: EZDraw::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/ezdraw.rb

Constant Summary collapse

@@instances =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttf_filename, ptheight) ⇒ Font

TODO: better API to distinguish between unsized “typeface” and sized “font”?



450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/ezdraw.rb', line 450

def initialize(ttf_filename, ptheight)
 EZDraw.requires_init

 @font = SDL2::Ttf.TTF_OpenFont(ttf_filename, ptheight)
 raise "TTF_OpenFont: #{SDL2::Ttf.TTF_GetError}" if @font.null? 

 # wrapping in array to make changes shared by all references
 @destroyed_flag = [false]
 ObjectSpace.define_finalizer(self, proc {|id|
  self.class.class._destroy(@font, @destroyed_flag)
 })

 @@instances << self
end

Instance Attribute Details

#fontObject (readonly)

Returns the value of attribute font.



507
508
509
# File 'lib/ezdraw.rb', line 507

def font
  @font
end

Class Method Details

._destroy(font, destroyed_flag) ⇒ Object



482
483
484
485
486
487
488
489
490
491
# File 'lib/ezdraw.rb', line 482

def self._destroy(font, destroyed_flag)
 if destroyed_flag[0]
  #EZDraw.logger.debug "(already destroyed font #{font})"
  return
 end

 EZDraw.logger.debug "destroy font #{font}"
 SDL2::Ttf.TTF_CloseFont(font)
 destroyed_flag[0] = true
end

.cleanupObject



472
473
474
475
476
477
478
479
480
# File 'lib/ezdraw.rb', line 472

def self.cleanup
 EZDraw.requires_init

 # destroy instances explicitly
 # to prevent finalizers from double-destroying after TTF_Quit
 @@instances.each {|font| font.close}
 @@instances = []
 SDL2::Ttf.TTF_Quit
end

.initObject



465
466
467
468
469
470
# File 'lib/ezdraw.rb', line 465

def self.init
 if SDL2::Ttf.TTF_WasInit == 0
  err = SDL2::Ttf.TTF_Init
  raise "TTF_Init: #{SDL2::Ttf.TTF_GetError}" if err != 0
 end
end

Instance Method Details

#closeObject



493
494
495
# File 'lib/ezdraw.rb', line 493

def close
 self.class._destroy(@font, @destroyed_flag)
end

#heightObject



497
498
499
500
# File 'lib/ezdraw.rb', line 497

def height
 # BUG: methods should check whether font was destroyed
 SDL2::Ttf.TTF_FontHeight(@font)
end

#line_pitchObject



502
503
504
# File 'lib/ezdraw.rb', line 502

def line_pitch
 SDL2::Ttf.TTF_FontLineSkip(@font)
end

#render_utf8(text, color) ⇒ Object Also known as: render



509
510
511
512
513
514
515
516
# File 'lib/ezdraw.rb', line 509

def render_utf8(text, color)
 c = SDL2::SDL_Color.new 
 c[:r] = color[0]
 c[:g] = color[1]
 c[:b] = color[2]
 c[:a] = color[3]
 sfc = SDL2::Ttf.TTF_RenderUTF8_Solid(@font, text, c)
end