indexing description: "[ An EM_TTF_FONT is created from a true-type-font file. The color of the font is determined by the drawing color of the surface where the text is placed on. ]" date: "$Date$" revision: "$Revision$" class EM_TTF_FONT inherit EM_FONT SDL_TTF_FUNCTIONS_EXTERNAL export {NONE} all end MEMORY redefine dispose end EM_SHARED_ERROR_HANDLER export {NONE} all end create make_from_ttf_file feature {NONE} -- Initialisation make_from_ttf_file (a_ttf_file: STRING; point_size: INTEGER) is -- Initialise true type font from `a_ttf_file' with `point_size' size. require a_ttf_file_not_void: a_ttf_file /= Void local file_c_string: EWG_ZERO_TERMINATED_STRING do -- Initalize SDL_ttf init_ttf -- Load font from file create file_c_string.make_unshared_from_string (a_ttf_file) font := ttf_open_font_external (file_c_string.item, point_size) if font = Default_pointer then Error_handler.raise_error (Error_handler.em_error_loading_ttf_font, [a_ttf_file]) end ensure font_loaded: font /= Default_pointer end init_ttf is -- Initialise SDL_ttf. once if ttf_init_external = -1 then Error_handler.raise_error (Error_handler.Em_error_initialising_ttf, []) end end feature -- Measurement width (a_character: CHARACTER): INTEGER is -- Width of `a_character' do Result := string_width (a_character.out) end height (a_character: CHARACTER): INTEGER is -- Height of `a_character' do Result := string_height (a_character.out) end string_width (a_string: STRING): INTEGER is -- Width of `a_string' local tmp: ANY text_c_string: EWG_ZERO_TERMINATED_STRING do create text_c_string.make_unshared_from_string (a_string) tmp := ttf_size_text_external (font.item, text_c_string.item, $Result, default_pointer) end string_height (a_string: STRING): INTEGER is -- Height of `a_string' local tmp: ANY text_c_string: EWG_ZERO_TERMINATED_STRING do create text_c_string.make_unshared_from_string (a_string) tmp := ttf_size_text_external (font.item, text_c_string.item, default_pointer, $Result) end feature -- Drawing draw (a_character: CHARACTER; a_surface: EM_SURFACE; x: INTEGER; y:INTEGER) is -- Draws `a_character' to `a_surface' to position `x' `y'. local surface: EM_TTF_SURFACE dest_box: EM_ORTHOGONAL_RECTANGLE do create surface.make_from_ttf_pointer (ttf_render_glyph_blended_external (font.item, a_character.code, a_surface.drawing_color.sdl_pointer)) create dest_box.make_from_coordinates (x, y, x+surface.width, y+surface.height) a_surface.draw_surface_stretched (surface, dest_box) end draw_part (rect: EM_RECT; a_character: CHARACTER; a_surface: EM_SURFACE; x: INTEGER; y: INTEGER) is -- Draws `rect' part of `a_character' to `a_surface' to position `x' `y'. local surface: EM_TTF_SURFACE dest_box: EM_ORTHOGONAL_RECTANGLE do create surface.make_from_ttf_pointer (ttf_render_glyph_blended_external (font.item, a_character.code, a_surface.drawing_color.sdl_pointer)) create dest_box.make_from_coordinates (x, y, x+surface.width, y+surface.height) a_surface.draw_surface_stretched (surface, dest_box) end draw_string (a_string: STRING; a_surface: EM_SURFACE; x: INTEGER; y: INTEGER) is -- Draws `a_string' to `a_screen' to position `x' ,`y' in `a_surface's drawing_color. require else a_string_not_void: a_string /= Void a_string_not_empty: not a_string.is_empty local surface: EM_TTF_SURFACE dest_box: EM_ORTHOGONAL_RECTANGLE text_c_string: EWG_ZERO_TERMINATED_STRING do create text_c_string.make_unshared_from_string (a_string) create surface.make_from_ttf_pointer (ttf_render_text_blended_external (font.item, text_c_string.item, a_surface.drawing_color.sdl_pointer)) create dest_box.make_from_coordinates (x, y, x+surface.width, y+surface.height) a_surface.draw_surface_stretched (surface, dest_box) end feature {EM_TTF_FONT} -- Implementation font: POINTER -- Font. We have to use `POINTER' because we don't know the size of the struct in `TTF_FONT_STRUCT' dispose is -- Free external resources. do if font /= Default_pointer then free_font end Precursor end free_font is -- Free external resources for `font'. require exists: font /= Default_pointer do ttf_close_font_external (font) end invariant font_loaded: font /= default_pointer end