From 0e6c291c64e77181851b8a3255a3da8827124080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 6 Feb 2018 00:05:12 +0100 Subject: Initial work on the SDL_ttf functions --- Makefile | 2 +- README.asciidoc | 65 ++++++++- c_src/esdl2.h | 16 +++ c_src/sdl_pixels.c | 31 +++++ c_src/sdl_surface.c | 19 +++ c_src/sdl_ttf.c | 158 ++++++++++++++++++++++ ebin/esdl2.app | 2 +- src/esdl2.erl | 28 ++++ src/sdl_pixels.erl | 8 ++ src/sdl_surface.erl | 5 + src/sdl_ttf.erl | 49 +++++++ vendor/SDL_ttf/include/SDL_ttf.h | 280 +++++++++++++++++++++++++++++++++++++++ 12 files changed, 655 insertions(+), 8 deletions(-) create mode 100644 c_src/sdl_ttf.c create mode 100644 src/sdl_ttf.erl create mode 100644 vendor/SDL_ttf/include/SDL_ttf.h diff --git a/Makefile b/Makefile index 9c6b202..9fea7f0 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ include erlang.mk CFLAGS += $(shell sdl2-config --cflags) # @todo -undefined dynamic_lookup on OSX? -LDLIBS += $(SDL2_LIBS) -lSDL2_image +LDLIBS += $(SDL2_LIBS) -lSDL2_image -lSDL2_ttf check:: cppcheck scan-build diff --git a/README.asciidoc b/README.asciidoc index 073dbb6..cbf5fca 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -2,9 +2,15 @@ SDL2 Erlang NIF. -The following sections list the state of the implementation -as of SDL 2.0.7. The implementation is cut into sections -corresponding to the public headers. +This project tries to implement SDL2 and some of its +extensions in one convenient NIF. The supported versions +and features are listed below. + +* SDL 2.0.7 +* SDL_ttf 2.0.14 + +The implementation is cut into sections corresponding +to the public headers. == Fully implemented @@ -80,6 +86,53 @@ corresponding to the public headers. ** `SDL_RenderReadPixels` (renderer) * 'SDL_stdinc.h': Erlang does not come with the functions `copysign` and `scalbn`. * 'SDL_surface.h': Only surface creation (via `IMG_Load`) and destruction is implemented. Might be better to move IMG_* functions in their own space. +** Everything in this header can be implemented as simple functions (as opposed to cast/call). +** We'll need to add functions to retrieve the pixel format and possibly pitch (though format+dimensions is enough to get it). +** We'll need to add extra functions for direct access to the pixels. +* 'SDL_ttf.h': +** A function to obtain the SDL_ttf version could be useful +** `TTF_Linked_Version` (if it makes sense, depends on how the library is loaded) +** `TTF_ByteSwappedUNICODE` +** `TTF_OpenFontIndex` +** `TTF_OpenFontRW` +** `TTF_OpenFontIndexRW` +** `TTF_GetFontStyle` +** `TTF_SetFontStyle` +** `TTF_GetFontOutline` +** `TTF_SetFontOutline` +** `TTF_GetFontHinting` +** `TTF_SetFontHinting` +** `TTF_FontHeight` +** `TTF_FontAscent` +** `TTF_FontDescent` +** `TTF_FontLineSkip` +** `TTF_GetFontKerning` +** `TTF_SetFontKerning` +** `TTF_FontFaces` +** `TTF_FontFaceIsFixedWidth` +** `TTF_FontFaceFamilyName` +** `TTF_FontFaceStyleName` +** `TTF_GlyphIsProvided` +** `TTF_GlyphMetrics` +** `TTF_SizeText` +** `TTF_SizeUTF8` +** `TTF_SizeUNICODE` +** `TTF_RenderText_Solid` +** `TTF_RenderUNICODE_Solid` +** `TTF_RenderGlyph_Solid` +** `TTF_RenderText_Shaded` +** `TTF_RenderUTF8_Shaded` +** `TTF_RenderUNICODE_Shaded` +** `TTF_RenderGlyph_Shaded` +** `TTF_RenderText_Blended` +** `TTF_RenderUTF8_Blended` +** `TTF_RenderUNICODE_Blended` +** `TTF_RenderText_Blended_Wrapped` +** `TTF_RenderUTF8_Blended_Wrapped` +** `TTF_RenderUNICODE_Blended_Wrapped` +** `TTF_RenderGlyph_Blended` +** `TTF_CloseFont` +** `TTF_GetFontKerningSizeGlyphs` * 'SDL_version.h': `SDL_GetRevisionNumber` must be implemented. The macros may also be useful. * 'SDL_video.h': The following elements are missing: ** `SDL_WINDOWPOS_*` values for different displays @@ -103,9 +156,9 @@ corresponding to the public headers. * 'SDL_timer.h' (unclear if we need it) * 'SDL_touch.h' -SDL extensions also need to be investigated and implemented. -We definitely want at least some of SDL_image, SDL_mixer -and SDL_ttf. We probably do not need SDL_net or SDL_rtf. +Other SDL extensions need to be investigated and implemented. +We definitely want at least some of SDL_image and SDL_mixer. +We probably do not need SDL_net or SDL_rtf. == OpenGL and Vulkan diff --git a/c_src/esdl2.h b/c_src/esdl2.h index e4f0356..cad60a7 100644 --- a/c_src/esdl2.h +++ b/c_src/esdl2.h @@ -16,10 +16,12 @@ #define __ESDL2_H__ #include "SDL.h" +#include "SDL_ttf.h" // List of atoms used by this NIF. #define NIF_ATOMS(A) \ + A(a) \ A(abgr1555) \ A(abgr32) \ A(abgr4444) \ @@ -43,6 +45,7 @@ A(audio) \ A(audio_device_added) \ A(audio_device_removed) \ + A(b) \ A(bgr24) \ A(bgr555) \ A(bgr565) \ @@ -105,6 +108,7 @@ A(format) \ A(fullscreen) \ A(fullscreen_desktop) \ + A(g) \ A(game_controller) \ A(get) \ A(h) \ @@ -179,6 +183,7 @@ A(present_vsync) \ A(pressed) \ A(quit) \ + A(r) \ A(refresh_rate) \ A(released) \ A(render_device_reset) \ @@ -263,9 +268,12 @@ // List of resources used by this NIF. +#define SDL_Font TTF_Font + #define NIF_RES_TYPE(r) SDL_ ## r #define NIF_RESOURCES(R) \ R(Cursor) \ + R(Font) \ R(GLContext) \ R(Renderer) \ R(Surface) \ @@ -405,6 +413,7 @@ /* sdl_stdinc */ \ F(get_num_allocations, 0) \ /* sdl_surface */ \ + F(get_surface_dimensions, 1) \ F(img_load, 1) \ /* sdl_texture */ \ F(create_texture_from_surface, 2) \ @@ -414,6 +423,12 @@ F(set_texture_alpha_mod, 2) \ F(set_texture_blend_mode, 2) \ F(set_texture_color_mod, 4) \ + /* sdl_ttf */ \ + F(ttf_init, 0) \ + F(ttf_open_font, 2) \ + F(ttf_quit, 0) \ + F(ttf_render_utf8_solid, 3) \ + F(ttf_was_init, 0) \ /* sdl_version */ \ F(get_version, 0) \ F(get_revision, 0) \ @@ -504,6 +519,7 @@ NIF_ENUM_TO_ATOM_FUNCTION_DECL(window_event_to_atom, Uint8) NIF_LIST_TO_FLAGS_FUNCTION_DECL(keymod_list_to_flags, Uint16) NIF_FLAGS_TO_LIST_FUNCTION_DECL(keymod_flags_to_list, Uint16) +int map_to_color(ErlNifEnv*, ERL_NIF_TERM, SDL_Color*); int map_to_point(ErlNifEnv*, ERL_NIF_TERM, SDL_Point*); ERL_NIF_TERM point_to_map(ErlNifEnv*, const SDL_Point*); int map_to_rect(ErlNifEnv*, ERL_NIF_TERM, SDL_Rect*); diff --git a/c_src/sdl_pixels.c b/c_src/sdl_pixels.c index aa22329..1bdec60 100644 --- a/c_src/sdl_pixels.c +++ b/c_src/sdl_pixels.c @@ -60,3 +60,34 @@ NIF_ENUM_TO_ATOM_FUNCTION(pixel_format_to_atom, Uint32, PIXEL_FORMAT_ENUM) NIF_ATOM_TO_ENUM_FUNCTION(atom_to_pixel_format, Uint32, PIXEL_FORMAT_ENUM) + +int map_to_color(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Color* color) +{ + ERL_NIF_TERM r, g, b, a; + unsigned int ri, gi, bi, ai; + + if (!enif_get_map_value(env, map, atom_r, &r)) + return 0; + if (!enif_get_map_value(env, map, atom_g, &g)) + return 0; + if (!enif_get_map_value(env, map, atom_b, &b)) + return 0; + if (!enif_get_map_value(env, map, atom_a, &a)) + return 0; + + if (!enif_get_uint(env, r, &ri)) + return 0; + if (!enif_get_uint(env, g, &gi)) + return 0; + if (!enif_get_uint(env, b, &bi)) + return 0; + if (!enif_get_uint(env, a, &ai)) + return 0; + + color->r = ri; + color->g = gi; + color->b = bi; + color->a = ai; + + return 1; +} diff --git a/c_src/sdl_surface.c b/c_src/sdl_surface.c index 22f75ab..a915fec 100644 --- a/c_src/sdl_surface.c +++ b/c_src/sdl_surface.c @@ -20,7 +20,26 @@ void dtor_Surface(ErlNifEnv* env, void* obj) SDL_FreeSurface(NIF_RES_GET(Surface, obj)); } +// get_surface_dimensions + +NIF_FUNCTION(get_surface_dimensions) +{ + void* surface_res; + SDL_Surface* surface; + + BADARG_IF(!enif_get_resource(env, argv[0], res_Surface, &surface_res)); + surface = NIF_RES_GET(Surface, surface_res); + + return enif_make_tuple2(env, + enif_make_int(env, surface->w), + enif_make_int(env, surface->h) + ); +} + // img_load +// @todo We should accept file:filename_all() and convert to binary +// before passing it to the NIF. Then we can support UTF-8 paths. +// @todo This function probably doesn't need to be a call. NIF_CALL_HANDLER(thread_img_load) { diff --git a/c_src/sdl_ttf.c b/c_src/sdl_ttf.c new file mode 100644 index 0000000..7deb729 --- /dev/null +++ b/c_src/sdl_ttf.c @@ -0,0 +1,158 @@ +// Copyright (c) 2018, Loïc Hoguin +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +#include "esdl2.h" + +// @todo These operations should probably occur in the thread. +void dtor_Font(ErlNifEnv* env, void* obj) +{ + TTF_Font* font = NIF_RES_GET(Font, obj); + + TTF_CloseFont(font); +} + +// ttf_init + +NIF_CALL_HANDLER(thread_ttf_init) +{ + if (TTF_Init()) + return sdl_error_tuple(env); + + return atom_ok; +} + +NIF_FUNCTION(ttf_init) +{ + return nif_thread_call(env, thread_ttf_init, 0); +} + +// ttf_openfont + +NIF_CALL_HANDLER(thread_ttf_open_font) +{ + TTF_Font* font; + obj_Font* res; + ERL_NIF_TERM term; + + font = TTF_OpenFont(args[0], (long)args[1]); + + enif_free(args[0]); + + if (!font) + return sdl_error_tuple(env); + + NIF_RES_TO_PTR_AND_TERM(Font, font, res, term); + + return enif_make_tuple2(env, + atom_ok, + term + ); +} + +NIF_FUNCTION(ttf_open_font) +{ + ErlNifBinary bin; + char* filename; + int ptsize; + + BADARG_IF(!enif_get_int(env, argv[1], &ptsize)); + + // Getting the filename last to simplify the code due to memory allocation. + + BADARG_IF(!enif_inspect_binary(env, argv[0], &bin)); + + filename = enif_alloc(bin.size + 1); + memcpy(filename, bin.data, bin.size); + filename[bin.size] = '\0'; + + return nif_thread_call(env, thread_ttf_open_font, 2, + filename, ptsize); +} + +// ttf_quit + +NIF_CAST_HANDLER(thread_ttf_quit) +{ + TTF_Quit(); +} + +NIF_FUNCTION(ttf_quit) +{ + return nif_thread_cast(env, thread_ttf_quit, 0); +} + +// ttf_render_utf8_solid + +NIF_CALL_HANDLER(thread_ttf_render_utf8_solid) +{ + SDL_Surface* surface; + obj_Surface* res; + ERL_NIF_TERM term; + + surface = TTF_RenderUTF8_Solid(args[0], args[1], *(SDL_Color*)args[2]); + + enif_free(args[1]); + enif_free(args[2]); + + if (!surface) + return sdl_error_tuple(env); + + NIF_RES_TO_PTR_AND_TERM(Surface, surface, res, term); + + return enif_make_tuple2(env, + atom_ok, + term + ); +} + +NIF_FUNCTION(ttf_render_utf8_solid) +{ + void* font_res; + ErlNifBinary bin; + char* text; + SDL_Color* color; + + BADARG_IF(!enif_get_resource(env, argv[0], res_Font, &font_res)); + BADARG_IF(!enif_inspect_binary(env, argv[1], &bin)); + + text = enif_alloc(bin.size + 1); + memcpy(text, bin.data, bin.size); + text[bin.size] = '\0'; + + color = enif_alloc(sizeof(SDL_Color)); + if (!map_to_color(env, argv[2], color)) { + enif_free(text); + enif_free(color); + + return enif_make_badarg(env); + } + + return nif_thread_call(env, thread_ttf_render_utf8_solid, 3, + NIF_RES_GET(Font, font_res), text, color); +} + +// ttf_was_init + +NIF_CALL_HANDLER(thread_ttf_was_init) +{ + if (TTF_WasInit()) + return atom_true; + + return atom_false; +} + +NIF_FUNCTION(ttf_was_init) +{ + return nif_thread_call(env, thread_ttf_was_init, 0); +} diff --git a/ebin/esdl2.app b/ebin/esdl2.app index 9dd11ea..13c1b43 100644 --- a/ebin/esdl2.app +++ b/ebin/esdl2.app @@ -1,7 +1,7 @@ {application, 'esdl2', [ {description, "SDL2 Erlang NIF."}, {vsn, "0.1.0"}, - {modules, ['esdl2','esdl2_app','esdl2_callbacks','esdl2_sup','sdl','sdl_blend_mode','sdl_clipboard','sdl_cpu_info','sdl_cursor','sdl_events','sdl_filesystem','sdl_gl','sdl_hints','sdl_keyboard','sdl_keycode','sdl_mouse','sdl_pixels','sdl_platform','sdl_power','sdl_rect','sdl_renderer','sdl_surface','sdl_texture','sdl_version','sdl_video','sdl_window']}, + {modules, ['esdl2','esdl2_app','esdl2_callbacks','esdl2_sup','sdl','sdl_blend_mode','sdl_clipboard','sdl_cpu_info','sdl_cursor','sdl_events','sdl_filesystem','sdl_gl','sdl_hints','sdl_keyboard','sdl_keycode','sdl_mouse','sdl_pixels','sdl_platform','sdl_power','sdl_rect','sdl_renderer','sdl_surface','sdl_texture','sdl_ttf','sdl_version','sdl_video','sdl_window']}, {registered, [esdl2_sup]}, {applications, [kernel,stdlib]}, {mod, {esdl2_app, []}}, diff --git a/src/esdl2.erl b/src/esdl2.erl index 2198d60..52a2ace 100644 --- a/src/esdl2.erl +++ b/src/esdl2.erl @@ -160,6 +160,7 @@ -export([get_num_allocations/0]). %% sdl_surface +-export([get_surface_dimensions/1]). -export([img_load/1]). %% sdl_texture @@ -171,6 +172,13 @@ -export([set_texture_blend_mode/2]). -export([set_texture_color_mod/4]). +%% sdl_ttf +-export([ttf_init/0]). +-export([ttf_open_font/2]). +-export([ttf_quit/0]). +-export([ttf_render_utf8_solid/3]). +-export([ttf_was_init/0]). + %% sdl_version -export([get_version/0]). -export([get_revision/0]). @@ -622,6 +630,9 @@ get_num_allocations() -> %% sdl_surface +get_surface_dimensions(_) -> + erlang:nif_error({not_loaded, ?MODULE}). + img_load(_) -> erlang:nif_error({not_loaded, ?MODULE}). @@ -648,6 +659,23 @@ set_texture_blend_mode(_, _) -> set_texture_color_mod(_, _, _, _) -> erlang:nif_error({not_loaded, ?MODULE}). +%% sdl_ttf + +ttf_init() -> + erlang:nif_error({not_loaded, ?MODULE}). + +ttf_open_font(_, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + +ttf_quit() -> + erlang:nif_error({not_loaded, ?MODULE}). + +ttf_render_utf8_solid(_, _, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + +ttf_was_init() -> + erlang:nif_error({not_loaded, ?MODULE}). + %% sdl_version get_version() -> diff --git a/src/sdl_pixels.erl b/src/sdl_pixels.erl index fef9a55..f9ef96a 100644 --- a/src/sdl_pixels.erl +++ b/src/sdl_pixels.erl @@ -14,6 +14,14 @@ -module(sdl_pixels). +-type color() :: #{ + r := 0..255, + g := 0..255, + b := 0..255, + a := 0..255 +}. +-export_type([color/0]). + -type pixel_format() :: unknown | index1lsb | index1msb | index4lsb | index4msb | index8 | rgb332 | rgb444 | rgb555 | bgr555 diff --git a/src/sdl_surface.erl b/src/sdl_surface.erl index a245b64..5740cc5 100644 --- a/src/sdl_surface.erl +++ b/src/sdl_surface.erl @@ -14,11 +14,16 @@ -module(sdl_surface). +-export([get_dimensions/1]). -export([load/1]). -opaque surface() :: <<>>. -export_type([surface/0]). +-spec get_dimensions(surface()) -> {non_neg_integer(), non_neg_integer()}. +get_dimensions(Surface) -> + esdl2:get_surface_dimensions(Surface). + -spec load(string()) -> {ok, surface()} | sdl:error(). load(Filename) -> esdl2:img_load(Filename), diff --git a/src/sdl_ttf.erl b/src/sdl_ttf.erl new file mode 100644 index 0000000..64a84da --- /dev/null +++ b/src/sdl_ttf.erl @@ -0,0 +1,49 @@ +%% Copyright (c) 2018, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(sdl_ttf). + +-export([is_started/0]). +-export([open_font/2]). +-export([render_solid/3]). +-export([start/0]). +-export([stop/0]). + +-opaque font() :: reference(). +-export_type([font/0]). + +-spec is_started() -> boolean(). +is_started() -> + esdl2:ttf_was_init(), + receive {'_nif_thread_ret_', Ret} -> Ret end. + +-spec open_font(binary(), pos_integer()) -> {ok, font()} | sdl:error(). +open_font(Filename, PointSize) -> + esdl2:ttf_open_font(Filename, PointSize), + receive {'_nif_thread_ret_', Ret} -> Ret end. + +-spec render_solid(font(), binary(), sdl_pixels:color()) + -> {ok, sdl_surface:surface()} | sdl:error(). +render_solid(Font, Text, Color) -> + esdl2:ttf_render_utf8_solid(Font, Text, Color), + receive {'_nif_thread_ret_', Ret} -> Ret end. + +-spec start() -> ok | sdl:error(). +start() -> + esdl2:ttf_init(), + receive {'_nif_thread_ret_', Ret} -> Ret end. + +-spec stop() -> ok. +stop() -> + esdl2:ttf_quit(). diff --git a/vendor/SDL_ttf/include/SDL_ttf.h b/vendor/SDL_ttf/include/SDL_ttf.h new file mode 100644 index 0000000..75e79e0 --- /dev/null +++ b/vendor/SDL_ttf/include/SDL_ttf.h @@ -0,0 +1,280 @@ +/* + SDL_ttf: A companion library to SDL for working with TrueType (tm) fonts + Copyright (C) 2001-2016 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +/* This library is a wrapper around the excellent FreeType 2.0 library, + available at: + http://www.freetype.org/ +*/ + +/* Note: In many places, SDL_ttf will say "glyph" when it means "code point." + Unicode is hard, we learn as we go, and we apologize for adding to the + confusion. */ + +#ifndef _SDL_TTF_H +#define _SDL_TTF_H + +#include "SDL.h" +#include "begin_code.h" + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL +*/ +#define SDL_TTF_MAJOR_VERSION 2 +#define SDL_TTF_MINOR_VERSION 0 +#define SDL_TTF_PATCHLEVEL 14 + +/* This macro can be used to fill a version structure with the compile-time + * version of the SDL_ttf library. + */ +#define SDL_TTF_VERSION(X) \ +{ \ + (X)->major = SDL_TTF_MAJOR_VERSION; \ + (X)->minor = SDL_TTF_MINOR_VERSION; \ + (X)->patch = SDL_TTF_PATCHLEVEL; \ +} + +/* Backwards compatibility */ +#define TTF_MAJOR_VERSION SDL_TTF_MAJOR_VERSION +#define TTF_MINOR_VERSION SDL_TTF_MINOR_VERSION +#define TTF_PATCHLEVEL SDL_TTF_PATCHLEVEL +#define TTF_VERSION(X) SDL_TTF_VERSION(X) + +/* Make sure this is defined (only available in newer SDL versions) */ +#ifndef SDL_DEPRECATED +#define SDL_DEPRECATED +#endif + +/* This function gets the version of the dynamically linked SDL_ttf library. + it should NOT be used to fill a version structure, instead you should + use the SDL_TTF_VERSION() macro. + */ +extern DECLSPEC const SDL_version * SDLCALL TTF_Linked_Version(void); + +/* ZERO WIDTH NO-BREAKSPACE (Unicode byte order mark) */ +#define UNICODE_BOM_NATIVE 0xFEFF +#define UNICODE_BOM_SWAPPED 0xFFFE + +/* This function tells the library whether UNICODE text is generally + byteswapped. A UNICODE BOM character in a string will override + this setting for the remainder of that string. +*/ +extern DECLSPEC void SDLCALL TTF_ByteSwappedUNICODE(int swapped); + +/* The internal structure containing font information */ +typedef struct _TTF_Font TTF_Font; + +/* Initialize the TTF engine - returns 0 if successful, -1 on error */ +extern DECLSPEC int SDLCALL TTF_Init(void); + +/* Open a font file and create a font of the specified point size. + * Some .fon fonts will have several sizes embedded in the file, so the + * point size becomes the index of choosing which size. If the value + * is too high, the last indexed size will be the default. */ +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFont(const char *file, int ptsize); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndex(const char *file, int ptsize, long index); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index); + +/* Set and retrieve the font style */ +#define TTF_STYLE_NORMAL 0x00 +#define TTF_STYLE_BOLD 0x01 +#define TTF_STYLE_ITALIC 0x02 +#define TTF_STYLE_UNDERLINE 0x04 +#define TTF_STYLE_STRIKETHROUGH 0x08 +extern DECLSPEC int SDLCALL TTF_GetFontStyle(const TTF_Font *font); +extern DECLSPEC void SDLCALL TTF_SetFontStyle(TTF_Font *font, int style); +extern DECLSPEC int SDLCALL TTF_GetFontOutline(const TTF_Font *font); +extern DECLSPEC void SDLCALL TTF_SetFontOutline(TTF_Font *font, int outline); + +/* Set and retrieve FreeType hinter settings */ +#define TTF_HINTING_NORMAL 0 +#define TTF_HINTING_LIGHT 1 +#define TTF_HINTING_MONO 2 +#define TTF_HINTING_NONE 3 +extern DECLSPEC int SDLCALL TTF_GetFontHinting(const TTF_Font *font); +extern DECLSPEC void SDLCALL TTF_SetFontHinting(TTF_Font *font, int hinting); + +/* Get the total height of the font - usually equal to point size */ +extern DECLSPEC int SDLCALL TTF_FontHeight(const TTF_Font *font); + +/* Get the offset from the baseline to the top of the font + This is a positive value, relative to the baseline. + */ +extern DECLSPEC int SDLCALL TTF_FontAscent(const TTF_Font *font); + +/* Get the offset from the baseline to the bottom of the font + This is a negative value, relative to the baseline. + */ +extern DECLSPEC int SDLCALL TTF_FontDescent(const TTF_Font *font); + +/* Get the recommended spacing between lines of text for this font */ +extern DECLSPEC int SDLCALL TTF_FontLineSkip(const TTF_Font *font); + +/* Get/Set whether or not kerning is allowed for this font */ +extern DECLSPEC int SDLCALL TTF_GetFontKerning(const TTF_Font *font); +extern DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, int allowed); + +/* Get the number of faces of the font */ +extern DECLSPEC long SDLCALL TTF_FontFaces(const TTF_Font *font); + +/* Get the font face attributes, if any */ +extern DECLSPEC int SDLCALL TTF_FontFaceIsFixedWidth(const TTF_Font *font); +extern DECLSPEC char * SDLCALL TTF_FontFaceFamilyName(const TTF_Font *font); +extern DECLSPEC char * SDLCALL TTF_FontFaceStyleName(const TTF_Font *font); + +/* Check wether a glyph is provided by the font or not */ +extern DECLSPEC int SDLCALL TTF_GlyphIsProvided(const TTF_Font *font, Uint16 ch); + +/* Get the metrics (dimensions) of a glyph + To understand what these metrics mean, here is a useful link: + http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html + */ +extern DECLSPEC int SDLCALL TTF_GlyphMetrics(TTF_Font *font, Uint16 ch, + int *minx, int *maxx, + int *miny, int *maxy, int *advance); + +/* Get the dimensions of a rendered string of text */ +extern DECLSPEC int SDLCALL TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h); +extern DECLSPEC int SDLCALL TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h); +extern DECLSPEC int SDLCALL TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h); + +/* Create an 8-bit palettized surface and render the given text at + fast quality with the given font and color. The 0 pixel is the + colorkey, giving a transparent background, and the 1 pixel is set + to the text color. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Solid(TTF_Font *font, + const char *text, SDL_Color fg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Solid(TTF_Font *font, + const char *text, SDL_Color fg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font, + const Uint16 *text, SDL_Color fg); + +/* Create an 8-bit palettized surface and render the given glyph at + fast quality with the given font and color. The 0 pixel is the + colorkey, giving a transparent background, and the 1 pixel is set + to the text color. The glyph is rendered without any padding or + centering in the X direction, and aligned normally in the Y direction. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Solid(TTF_Font *font, + Uint16 ch, SDL_Color fg); + +/* Create an 8-bit palettized surface and render the given text at + high quality with the given font and colors. The 0 pixel is background, + while other pixels have varying degrees of the foreground color. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Shaded(TTF_Font *font, + const char *text, SDL_Color fg, SDL_Color bg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Shaded(TTF_Font *font, + const char *text, SDL_Color fg, SDL_Color bg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font, + const Uint16 *text, SDL_Color fg, SDL_Color bg); + +/* Create an 8-bit palettized surface and render the given glyph at + high quality with the given font and colors. The 0 pixel is background, + while other pixels have varying degrees of the foreground color. + The glyph is rendered without any padding or centering in the X + direction, and aligned normally in the Y direction. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Shaded(TTF_Font *font, + Uint16 ch, SDL_Color fg, SDL_Color bg); + +/* Create a 32-bit ARGB surface and render the given text at high quality, + using alpha blending to dither the font with the given color. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended(TTF_Font *font, + const char *text, SDL_Color fg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended(TTF_Font *font, + const char *text, SDL_Color fg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font, + const Uint16 *text, SDL_Color fg); + + +/* Create a 32-bit ARGB surface and render the given text at high quality, + using alpha blending to dither the font with the given color. + Text is wrapped to multiple lines on line endings and on word boundaries + if it extends beyond wrapLength in pixels. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended_Wrapped(TTF_Font *font, + const char *text, SDL_Color fg, Uint32 wrapLength); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font, + const char *text, SDL_Color fg, Uint32 wrapLength); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended_Wrapped(TTF_Font *font, + const Uint16 *text, SDL_Color fg, Uint32 wrapLength); + +/* Create a 32-bit ARGB surface and render the given glyph at high quality, + using alpha blending to dither the font with the given color. + The glyph is rendered without any padding or centering in the X + direction, and aligned normally in the Y direction. + This function returns the new surface, or NULL if there was an error. +*/ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font, + Uint16 ch, SDL_Color fg); + +/* For compatibility with previous versions, here are the old functions */ +#define TTF_RenderText(font, text, fg, bg) \ + TTF_RenderText_Shaded(font, text, fg, bg) +#define TTF_RenderUTF8(font, text, fg, bg) \ + TTF_RenderUTF8_Shaded(font, text, fg, bg) +#define TTF_RenderUNICODE(font, text, fg, bg) \ + TTF_RenderUNICODE_Shaded(font, text, fg, bg) + +/* Close an opened font file */ +extern DECLSPEC void SDLCALL TTF_CloseFont(TTF_Font *font); + +/* De-initialize the TTF engine */ +extern DECLSPEC void SDLCALL TTF_Quit(void); + +/* Check if the TTF engine is initialized */ +extern DECLSPEC int SDLCALL TTF_WasInit(void); + +/* Get the kerning size of two glyphs indices */ +/* DEPRECATED: this function requires FreeType font indexes, not glyphs, + by accident, which we don't expose through this API, so it could give + wildly incorrect results, especially with non-ASCII values. + Going forward, please use TTF_GetFontKerningSizeGlyphs() instead, which + does what you probably expected this function to do. */ +extern DECLSPEC int TTF_GetFontKerningSize(TTF_Font *font, int prev_index, int index) SDL_DEPRECATED; + +/* Get the kerning size of two glyphs */ +extern DECLSPEC int TTF_GetFontKerningSizeGlyphs(TTF_Font *font, Uint16 previous_ch, Uint16 ch); + +/* We'll use SDL for reporting errors */ +#define TTF_SetError SDL_SetError +#define TTF_GetError SDL_GetError + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +} +#endif +#include "close_code.h" + +#endif /* _SDL_TTF_H */ -- cgit v1.2.3