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; a_point_size: INTEGER) is -- Initialise true type font from `a_ttf_file' with `a_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 point_size := a_point_size -- Load font from file create file_c_string.make_unshared_from_string (a_ttf_file) font := ttf_open_font_external (file_c_string.item, a_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 point_size: INTEGER -- Size of font 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 font_height: INTEGER is -- Max glyph height do Result := ttf_font_height_external (font) end font_line_skip: INTEGER is -- Recommended line spacing do Result := ttf_font_line_skip_external (font) end font_ascent: INTEGER is -- Max ascent do Result := ttf_font_ascent_external (font) end font_descent: INTEGER is -- Max descent do Result := ttf_font_descent_external (font) end glyph_metrics (unicode: INTEGER): EM_TTF_GLYPH_METRICS is -- Metrics for `unicode' character do Result := create {EM_TTF_GLYPH_METRICS}.make (font, unicode) end feature -- Drawing create_glyph_surface (unicode: INTEGER; color: EM_COLOR): EM_SURFACE is -- Create new surface depicting `unicode' character with text-`color'. -- Result has alpha channel. local sp: POINTER surf: EM_TTF_SURFACE do sp := ttf_render_glyph_blended_external(font, unicode, color.sdl_pointer) create surf.make_from_ttf_pointer (sp) Result := surf end create_string_surface (a_string: STRING; color: EM_COLOR): EM_SURFACE is -- Create a new surface local surf: EM_TTF_SURFACE text_c_string: EWG_ZERO_TERMINATED_STRING do create text_c_string.make_unshared_from_string (a_string) create surf.make_from_ttf_pointer( ttf_render_text_blended_external(font, text_c_string.item, color.sdl_pointer)) Result := surf end 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_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_BLITTING_RECTANGLE; 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_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_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