+# Class to load and use TTF_Font
+extern class SDLFont `{TTF_Font *`}
+ # Load a font with a specified name and size
+ new (name: String, points: Int) import String.to_cstring `{
+ char * cname = String_to_cstring(name);
+
+ TTF_Font *font = TTF_OpenFont(cname, (int)points);
+ if(!font) {
+ printf("TTF_OpenFont: %s\n", TTF_GetError());
+ exit(1);
+ }
+
+ return font;
+ `}
+
+ fun destroy `{ TTF_CloseFont(recv); `}
+
+ # Create a String with the specified color, return an SDLImage
+ fun render(text: String, r, g, b: Int): SDLImage import String.to_cstring `{
+ SDL_Color color;
+ SDL_Surface *text_surface;
+ char *ctext;
+
+ color.r = r;
+ color.g = g;
+ color.b = b;
+
+ ctext = String_to_cstring(text);
+ if(!(text_surface=TTF_RenderText_Blended(recv, ctext, color)))
+ {
+ fprintf(stderr, "SDL TFF error: %s\n", TTF_GetError());
+ exit(1);
+ }
+ else
+ return text_surface;
+ `}
+
+ # TODO reactivate fun below when updating libsdl_ttf to 2.0.10 or above
+ #fun outline: Int # TODO check to make inline/nitside only
+ #fun outline=(v: Int) is extern
+
+ #fun kerning: Bool is extern
+ #fun kerning=(v: Bool) is extern
+
+ # Maximum pixel height of all glyphs of this font.
+ fun height: Int `{
+ return TTF_FontHeight(recv);
+ `}
+
+ fun ascent: Int `{
+ return TTF_FontAscent(recv);
+ `}
+
+ fun descent: Int `{
+ return TTF_FontDescent(recv);
+ `}
+
+ # Get the recommended pixel height of a rendered line of text of the loaded font. This is usually larger than the Font.height.
+ fun line_skip: Int `{
+ return TTF_FontLineSkip(recv);
+ `}
+
+ # Return true is the font used fixed width for each char
+ fun is_fixed_width: Bool `{
+ return TTF_FontFaceIsFixedWidth(recv);
+ `}
+
+ # Return the family name of the font
+ fun family_name: nullable String import String.to_cstring, String.as nullable `{
+ char *fn = TTF_FontFaceFamilyName(recv);
+
+ if (fn == NULL)
+ return null_String();
+ else
+ return String_as_nullable(NativeString_to_s(fn));
+ `}
+
+ # Return the style name of the font
+ fun style_name: nullable String import String.to_cstring, String.as nullable `{
+ char *sn = TTF_FontFaceStyleName(recv);
+
+ if (sn == NULL)
+ return null_String();
+ else
+ return String_as_nullable(NativeString_to_s(sn));
+ `}
+
+ # Return the estimated width of a String if used with the current font
+ fun width_of(text: String): Int import NativeString.to_s `{
+ char *ctext = String_to_cstring(text);
+ int w;
+ if (TTF_SizeText(recv, ctext, &w, NULL))
+ {
+ fprintf(stderr, "SDL TFF error: %s\n", TTF_GetError());
+ exit(1);
+ }
+ else
+ return w;
+ `}
+end