sfml3.rb

Class: SFML::Font

Inherits:
Object
  • Object
show all
Defined in:
ext/graphics/font.c,
ext/graphics/font.c

Overview

A font face used to render text, loaded from a file, memory buffer or stream. Glyphs are rasterized and cached lazily per character size.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(path) ⇒ Font

Loads a font from the font file at filename.

Returns:

Returns:

Raises:

  • (RuntimeError) —

    if the file cannot be loaded



38
39
40
# File 'ext/graphics/font.c', line 38

static VALUE Font_from_file(VALUE klass, VALUE rb_path) {
    return Font_wrap(klass, sfFont_createFromFile(StringValueCStr(rb_path)));
}

.from_memory(data) ⇒ Font

Loads a font from an in-memory font file buffer.

Returns:

Returns:

Raises:

  • (RuntimeError) —

    if the font data cannot be parsed



50
51
52
53
54
55
# File 'ext/graphics/font.c', line 50

static VALUE Font_from_memory(VALUE klass, VALUE rb_data) {
    StringValue(rb_data);

    return Font_wrap(klass,
                     sfFont_createFromMemory(RSTRING_PTR(rb_data), (size_t)RSTRING_LEN(rb_data)));
}

.from_stream(stream) ⇒ Font

stream is any object responding to the InputStream protocol (see InputStream).

Returns:

Returns:

Raises:

  • (RuntimeError) —

    if the font cannot be loaded



66
67
68
69
70
71
72
73
# File 'ext/graphics/font.c', line 66

static VALUE Font_from_stream(VALUE klass, VALUE rb_stream) {
    VALUE holder = Qnil;
    sfInputStream* stream = input_stream_from_rb(rb_stream, &holder);

    (void)holder;

    return Font_wrap(klass, sfFont_createFromStream(stream));
}

Instance Method Details

#bold_kerning(first, second, size) ⇒ Float

Returns the kerning of the bold variant between two code points.

Returns:

  • (Float)

Returns:

  • (Float) —

    the kerning offset between two consecutive bold glyphs at the given character size



142
143
144
145
146
# File 'ext/graphics/font.c', line 142

static VALUE Font_get_bold_kerning(VALUE self, VALUE rb_first, VALUE rb_second, VALUE rb_size) {
    return DBL2NUM(sfFont_getBoldKerning(Get_Font_Struct(self), (uint32_t)NUM2UINT(rb_first),
                                         (uint32_t)NUM2UINT(rb_second),
                                         (unsigned int)NUM2UINT(rb_size)));
}

#copy ⇒ Font

Returns a deep copy of the object.

Returns:

Returns:

  • (Font) —

    an independent copy



81
82
83
# File 'ext/graphics/font.c', line 81

static VALUE Font_copy(VALUE self) {
    return Font_wrap(Get_Klass_Font(), sfFont_copy(Get_Font_Struct(self)));
}

#glyph(codepoint, size, bold = false, outline_thickness = 0) ⇒ Glyph

Returns the glyph for codepoint at the given character_size.

Returns:

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/graphics/font.c', line 92

static VALUE Font_get_glyph(int argc, VALUE* argv, VALUE self) {
    VALUE rb_codepoint, rb_size, rb_bold, rb_outline;
    bool bold = false;
    float outline = 0;

    rb_scan_args(argc, argv, "22", &rb_codepoint, &rb_size, &rb_bold, &rb_outline);

    if (!NIL_P(rb_bold)) {
        bold = RTEST(rb_bold);
    }

    if (!NIL_P(rb_outline)) {
        outline = NUM2DBL(rb_outline);
    }

    return glyph_from_c(sfFont_getGlyph(Get_Font_Struct(self), (uint32_t)NUM2UINT(rb_codepoint),
                                        (unsigned int)NUM2UINT(rb_size), bold, outline));
}

#has_glyph?(codepoint) ⇒ Boolean

Returns true if the font contains a glyph for codepoint.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether this font has a glyph for the given Unicode codepoint



118
119
120
# File 'ext/graphics/font.c', line 118

static VALUE Font_has_glyph(VALUE self, VALUE rb_codepoint) {
    return BOOL2RB(sfFont_hasGlyph(Get_Font_Struct(self), (uint32_t)NUM2UINT(rb_codepoint)));
}

#info ⇒ String

Returns the font's family name.

Returns:

  • (String)

Returns:

  • (String) —

    the font family name



222
223
224
225
226
# File 'ext/graphics/font.c', line 222

static VALUE Font_get_info(VALUE self) {
    sfFontInfo info = sfFont_getInfo(Get_Font_Struct(self));

    return rb_str_new_cstr(info.family != NULL ? info.family : "");
}

#kerning(first, second, size) ⇒ Float

Returns the kerning between two code points at the given size.

Returns:

  • (Float)

Returns:

  • (Float) —

    the kerning offset between two consecutive glyphs at the given character size



129
130
131
132
133
# File 'ext/graphics/font.c', line 129

static VALUE Font_get_kerning(VALUE self, VALUE rb_first, VALUE rb_second, VALUE rb_size) {
    return DBL2NUM(sfFont_getKerning(Get_Font_Struct(self), (uint32_t)NUM2UINT(rb_first),
                                     (uint32_t)NUM2UINT(rb_second),
                                     (unsigned int)NUM2UINT(rb_size)));
}

#line_spacing(size) ⇒ Float

Returns the line spacing for the given character size.

Returns:

  • (Float)

Returns:

  • (Float) —

    the distance between two consecutive lines at the given character size



155
156
157
# File 'ext/graphics/font.c', line 155

static VALUE Font_get_line_spacing(VALUE self, VALUE rb_size) {
    return DBL2NUM(sfFont_getLineSpacing(Get_Font_Struct(self), (unsigned int)NUM2UINT(rb_size)));
}

#smooth=(value) ⇒ Boolean

Enables or disables smooth rendering.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    value



201
202
203
204
# File 'ext/graphics/font.c', line 201

static VALUE Font_set_smooth(VALUE self, VALUE rb_smooth) {
    sfFont_setSmooth((sfFont*)Get_Font_Struct(self), RTEST(rb_smooth));
    return rb_smooth;
}

#smooth? ⇒ Boolean

Returns true if smooth rendering is enabled.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


212
213
214
# File 'ext/graphics/font.c', line 212

static VALUE Font_is_smooth(VALUE self) {
    return BOOL2RB(sfFont_isSmooth(Get_Font_Struct(self)));
}

#texture(size) ⇒ Texture

Returns the texture atlas holding rendered glyphs for the given size.

Returns:

Returns:

  • (Texture) —

    the texture atlas holding rendered glyphs for the given character size. Owned by the font; do not modify or free it directly.



189
190
191
192
# File 'ext/graphics/font.c', line 189

static VALUE Font_get_texture(VALUE self, VALUE rb_size) {
    return texture_from_borrowed(
        sfFont_getTexture((sfFont*)Get_Font_Struct(self), (unsigned int)NUM2UINT(rb_size)));
}

#underline_position(size) ⇒ Float

Returns the position of the underline for the given size.

Returns:

  • (Float)

Returns:

  • (Float) —

    the position of the underline, relative to the baseline, at the given character size



166
167
168
169
# File 'ext/graphics/font.c', line 166

static VALUE Font_get_underline_position(VALUE self, VALUE rb_size) {
    return DBL2NUM(
        sfFont_getUnderlinePosition(Get_Font_Struct(self), (unsigned int)NUM2UINT(rb_size)));
}

#underline_thickness(size) ⇒ Float

Returns the thickness of the underline for the given size.

Returns:

  • (Float)

Returns:

  • (Float) —

    the thickness of the underline at the given character size



177
178
179
180
# File 'ext/graphics/font.c', line 177

static VALUE Font_get_underline_thickness(VALUE self, VALUE rb_size) {
    return DBL2NUM(
        sfFont_getUnderlineThickness(Get_Font_Struct(self), (unsigned int)NUM2UINT(rb_size)));
}