Class: SDL2::TTF

Inherits:
Object
  • Object
show all
Defined in:
ttf.c,
ttf.c

Overview

This class represents font information.

You can render TrueType fonts using SDL_ttf and this class.

Defined Under Namespace

Modules: Hinting, Style

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ascentInteger (readonly)

The maximum pixel ascent of all glyphs of the font. The distance from the top of the font to the baseline.

Returns:

  • (Integer)

See Also:

#descentInteger (readonly)

The maximum pixel descent of all glyphs of the font. The distance from the bottom of the font to the baseline.

Returns:

  • (Integer)

See Also:

#face_family_nameString (readonly)

The current font face family name of the font.

Returns:

  • (String)

#face_style_nameString (readonly)

The current font face style name of the font.

Returns:

  • (String)

#heightInteger (readonly)

The maximum pixel height of all glyphs of the font. You can use this height to render text as close together vertically as possible.

Returns:

  • (Integer)

See Also:

#hintingInteger

Font hinting. One of the constants of Hinting.

Returns:

  • (Integer)

#kerningBooelan

True if kerning is enabled.

Returns:

  • (Booelan)

#line_skipInteger (readonly)

The recommended pixel height of rendered line of text of the font. Normally, this value is larger than #height.

Returns:

  • (Integer)

#num_facesInteger (readonly)

The number of faces available in the font.

Returns:

  • (Integer)

#outlineInteger

The outline pixel width of the font.

Returns:

  • (Integer)

#styleInteger

Font style. The OR’d bits of the constants of Style.

Returns:

  • (Integer)

Class Method Details

.initnil

Initialize TrueType font rendering submodule.

This function must be called before calling other methods/class methods of SDL2::TTF.

Returns:

  • (nil)


127
128
129
130
131
# File 'ttf.c', line 127

static VALUE TTF_s_init(VALUE self)
{
    HANDLE_TTF_ERROR(TTF_Init());
    return Qnil;
}

.open(fname, ptsize, index = 0) ⇒ SDL2::TTF

Open a font data from file.

Parameters:

  • fname (String)

    the path of the font file

  • ptsize (Integer)

    the point size of the font (72DPI).

  • index (Integer) (defaults to: 0)

    the index of the font faces. Some font files have multiple font faces, and you can select one of them using index. 0 origin. If a font have only one font face, 0 is the only valid index.

Returns:

Raises:

  • (SDL2::Error)

    occurs when you fail to open a file.



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ttf.c', line 149

static VALUE TTF_s_open(int argc, VALUE* argv, VALUE self)
{
    TTF_Font* font;
    VALUE fname, ptsize, index;
    rb_scan_args(argc, argv, "21", &fname, &ptsize, &index);

    font = TTF_OpenFontIndex(StringValueCStr(fname), NUM2INT(ptsize),
                             index == Qnil ? 0 : NUM2LONG(index));
    if (!font)
        TTF_ERROR();
    
    return TTF_new(font);
}

Instance Method Details

#destroynil

Destroy the font data and release memory.

Returns:

  • (nil)


168
169
170
171
172
173
174
175
# File 'ttf.c', line 168

static VALUE TTF_destroy(VALUE self)
{
    TTF* f = Get_TTF(self);
    if (f->font)
        TTF_CloseFont(f->font);
    f->font = NULL;
    return Qnil;
}

#destroy?Boolean

Return true if the font is destroyed by #destroy.

Returns:

  • (Boolean)

#face_is_fixed_width?Boolean

Return true if the font is fixed width.

Returns:

  • (Boolean)

#render_blended(text, fg) ⇒ SDL2::Surface

Render text using the font with fg color on a new surface, using Blended mode.

Blended mode rendering is very slow but very nice. The rendered surface has an alpha channel,

Parameters:

  • text (String)

    the text to render

  • fg ([Integer, Integer, Integer])

    the color to render. An array of r, g, and b components.

Returns:

Raises:



291
292
293
294
# File 'ttf.c', line 291

static VALUE TTF_render_blended(VALUE self, VALUE text, VALUE fg)
{
    return render(render_blended, self, text, fg, Qnil);
}

#render_shaded(text, fg, bg) ⇒ SDL2::Surface

Render text using the font with fg color on a new surface, using Shaded mode.

Shaded mode rendering is slow and nice, but with a solid box filled by the background color.

Parameters:

  • text (String)

    the text to render

  • fg ([Integer, Integer, Integer])

    the color to render. An array of r, g, and b components.

  • bg ([Integer, Integer, Integer])

    the background color. An array of r, g, and b components.

Returns:

Raises:



270
271
272
273
# File 'ttf.c', line 270

static VALUE TTF_render_shaded(VALUE self, VALUE text, VALUE fg, VALUE bg)
{
    return render(TTF_RenderUTF8_Shaded, self, text, fg, bg);
}

#render_solid(text, fg) ⇒ SDL2::Surface

Render text using the font with fg color on a new surface, using Solid mode.

Solid mode rendering is quick but dirty.

Parameters:

  • text (String)

    the text to render

  • fg ([Integer, Integer, Integer])

    the color to render. An array of r, g, and b components.

Returns:

Raises:



247
248
249
250
# File 'ttf.c', line 247

static VALUE TTF_render_solid(VALUE self, VALUE text, VALUE fg)
{
    return render(render_solid, self, text, fg, Qnil);
}

#size_text(text) ⇒ [Integer, Integer]

Returns a pair of width and height of the rendered surface

Calculate the size of rendered surface of text using the font.

Parameters:

  • text (String)

    the string to size up

Returns:

  • ([Integer, Integer])

    a pair of width and height of the rendered surface

Raises:

  • (SDL2::Error)

    It is raised when an error occurs, such as a glyph ins the string not being found.



201
202
203
204
205
206
207
# File 'ttf.c', line 201

static VALUE TTF_size_text(VALUE self, VALUE text)
{
    int w, h;
    text = rb_str_export_to_enc(text, rb_utf8_encoding());
    HANDLE_TTF_ERROR(TTF_SizeUTF8(Get_TTF_Font(self), StringValueCStr(text), &w, &h));
    return rb_ary_new3(2, INT2NUM(w), INT2NUM(h));
}