From 913937277fec521a704fc29253198da1a7dbeb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 13 Feb 2018 14:42:06 +0100 Subject: Add functions for obtaining glyph informations --- README.asciidoc | 3 -- c_src/esdl2.h | 3 ++ c_src/sdl_ttf.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/esdl2.erl | 12 ++++++++ src/sdl_ttf.erl | 24 ++++++++++++++++ 5 files changed, 126 insertions(+), 3 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 70975ec..6ff2b74 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -94,12 +94,9 @@ to the public headers. ** `TTF_Linked_Version` (if it makes sense, depends on how the library is loaded) ** `TTF_OpenFontRW` (unclear if we need it) ** `TTF_OpenFontIndexRW` (unclear if we need it) -** `TTF_GlyphIsProvided` -** `TTF_GlyphMetrics` ** `TTF_RenderGlyph_Solid` ** `TTF_RenderGlyph_Shaded` ** `TTF_RenderGlyph_Blended` -** `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 diff --git a/c_src/esdl2.h b/c_src/esdl2.h index e82278a..60835a7 100644 --- a/c_src/esdl2.h +++ b/c_src/esdl2.h @@ -440,8 +440,11 @@ F(ttf_font_line_skip, 1) \ F(ttf_get_font_hinting, 1) \ F(ttf_get_font_kerning, 1) \ + F(ttf_get_font_kerning_size_glyphs, 3) \ F(ttf_get_font_outline, 1) \ F(ttf_get_font_style, 1) \ + F(ttf_glyph_is_provided, 2) \ + F(ttf_glyph_metrics, 2) \ F(ttf_init, 0) \ F(ttf_open_font, 2) \ F(ttf_open_font_index, 3) \ diff --git a/c_src/sdl_ttf.c b/c_src/sdl_ttf.c index 917c9d9..ccde11c 100644 --- a/c_src/sdl_ttf.c +++ b/c_src/sdl_ttf.c @@ -239,6 +239,36 @@ NIF_FUNCTION(ttf_get_font_kerning) NIF_RES_GET(Font, font_res)); } +// ttf_get_font_kerning_size_glyphs + +NIF_CALL_HANDLER(thread_ttf_get_font_kerning_size_glyphs) +{ + int size; + + size = TTF_GetFontKerningSizeGlyphs(args[0], (long)args[1], (long)args[2]); + + if (size < 0) + return sdl_error_tuple(env); + + return enif_make_tuple2(env, + atom_ok, + enif_make_int(env, size) + ); +} + +NIF_FUNCTION(ttf_get_font_kerning_size_glyphs) +{ + void* font_res; + unsigned int previous_ch, ch; + + BADARG_IF(!enif_get_resource(env, argv[0], res_Font, &font_res)); + BADARG_IF(!enif_get_uint(env, argv[1], &previous_ch)); + BADARG_IF(!enif_get_uint(env, argv[2], &ch)); + + return nif_thread_call(env, thread_ttf_get_font_kerning_size_glyphs, 3, + NIF_RES_GET(Font, font_res), previous_ch, ch); +} + // ttf_get_font_outline NIF_CALL_HANDLER(thread_ttf_get_font_outline) @@ -273,6 +303,63 @@ NIF_FUNCTION(ttf_get_font_style) NIF_RES_GET(Font, font_res)); } +// ttf_glyph_is_provided + +NIF_CALL_HANDLER(thread_ttf_glyph_is_provided) +{ + if (TTF_GlyphIsProvided(args[0], (long)args[1])) + return atom_true; + + return atom_false; +} + +NIF_FUNCTION(ttf_glyph_is_provided) +{ + void* font_res; + unsigned int ch; + + BADARG_IF(!enif_get_resource(env, argv[0], res_Font, &font_res)); + BADARG_IF(!enif_get_uint(env, argv[1], &ch)); + + return nif_thread_call(env, thread_ttf_glyph_is_provided, 2, + NIF_RES_GET(Font, font_res), ch); +} + +// ttf_glyph_metrics + +NIF_CALL_HANDLER(thread_ttf_glyph_metrics) +{ + int minx, maxx, miny, maxy, advance; + + if (TTF_GlyphMetrics(args[0], (long)args[1], + &minx, &maxx, &miny, &maxy, &advance)) { + return sdl_error_tuple(env); + } + + return enif_make_tuple2(env, + atom_ok, + enif_make_tuple5(env, + enif_make_int(env, minx), + enif_make_int(env, maxx), + enif_make_int(env, miny), + enif_make_int(env, maxy), + enif_make_int(env, advance) + ) + ); +} + +NIF_FUNCTION(ttf_glyph_metrics) +{ + void* font_res; + unsigned int ch; + + BADARG_IF(!enif_get_resource(env, argv[0], res_Font, &font_res)); + BADARG_IF(!enif_get_uint(env, argv[1], &ch)); + + return nif_thread_call(env, thread_ttf_glyph_metrics, 2, + NIF_RES_GET(Font, font_res), ch); +} + // ttf_init NIF_CALL_HANDLER(thread_ttf_init) diff --git a/src/esdl2.erl b/src/esdl2.erl index 308ffe6..a5c901e 100644 --- a/src/esdl2.erl +++ b/src/esdl2.erl @@ -183,8 +183,11 @@ -export([ttf_font_line_skip/1]). -export([ttf_get_font_hinting/1]). -export([ttf_get_font_kerning/1]). +-export([ttf_get_font_kerning_size_glyphs/3]). -export([ttf_get_font_outline/1]). -export([ttf_get_font_style/1]). +-export([ttf_glyph_is_provided/2]). +-export([ttf_glyph_metrics/2]). -export([ttf_init/0]). -export([ttf_open_font/2]). -export([ttf_open_font_index/3]). @@ -712,12 +715,21 @@ ttf_get_font_hinting(_) -> ttf_get_font_kerning(_) -> erlang:nif_error({not_loaded, ?MODULE}). +ttf_get_font_kerning_size_glyphs(_, _, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + ttf_get_font_outline(_) -> erlang:nif_error({not_loaded, ?MODULE}). ttf_get_font_style(_) -> erlang:nif_error({not_loaded, ?MODULE}). +ttf_glyph_is_provided(_, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + +ttf_glyph_metrics(_, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + ttf_init() -> erlang:nif_error({not_loaded, ?MODULE}). diff --git a/src/sdl_ttf.erl b/src/sdl_ttf.erl index 314f70a..acacdcd 100644 --- a/src/sdl_ttf.erl +++ b/src/sdl_ttf.erl @@ -19,6 +19,8 @@ -export([get_ascent/1]). -export([get_descent/1]). -export([get_family_name/1]). +-export([get_glyph_metrics/2]). +-export([get_glyphs_kerning_size/3]). -export([get_height/1]). -export([get_hinting/1]). -export([get_line_skip/1]). @@ -26,6 +28,7 @@ -export([get_outline/1]). -export([get_style/1]). -export([get_style_name/1]). +-export([has_glyph/2]). -export([is_fixed_width/1]). -export([is_kerning_enabled/1]). -export([is_started/0]). @@ -45,6 +48,9 @@ -opaque font() :: reference(). -export_type([font/0]). +-type glyph() :: 0..65535. +-export_type([glyph/0]). + -type hinting() :: normal | light | mono | none. -export_type([hinting/0]). @@ -74,6 +80,19 @@ get_family_name(Font) -> esdl2:ttf_font_face_family_name(Font), receive {'_nif_thread_ret_', Ret} -> Ret end. +-spec get_glyph_metrics(font(), glyph()) + -> {ok, {integer(), integer(), integer(), integer(), integer()}} + | sdl:error(). +get_glyph_metrics(Font, Glyph) -> + esdl2:ttf_glyph_metrics(Font, Glyph), + receive {'_nif_thread_ret_', Ret} -> Ret end. + +-spec get_glyphs_kerning_size(font(), glyph(), glyph()) + -> {ok, integer()} | sdl:error(). +get_glyphs_kerning_size(Font, PreviousGlyph, Glyph) -> + esdl2:ttf_get_font_kerning_size_glyphs(Font, PreviousGlyph, Glyph), + receive {'_nif_thread_ret_', Ret} -> Ret end. + -spec get_height(font()) -> non_neg_integer(). get_height(Font) -> esdl2:ttf_font_height(Font), @@ -109,6 +128,11 @@ get_style_name(Font) -> esdl2:ttf_font_face_style_name(Font), receive {'_nif_thread_ret_', Ret} -> Ret end. +-spec has_glyph(font(), glyph()) -> boolean(). +has_glyph(Font, Glyph) -> + esdl2:ttf_glyph_is_provided(Font, Glyph), + receive {'_nif_thread_ret_', Ret} -> Ret end. + -spec is_fixed_width(font()) -> boolean(). is_fixed_width(Font) -> esdl2:ttf_font_face_is_fixed_width(Font), -- cgit v1.2.3