Class: Ray::Font
- Inherits:
-
Object
- Object
- Ray::Font
- Extended by:
- ResourceSet
- Defined in:
- lib/ray/font.rb,
ext/font.c
Constant Summary collapse
- STYLE_NORMAL =
INT2FIX(TTF_STYLE_NORMAL)
- STYLE_ITALIC =
INT2FIX(TTF_STYLE_ITALIC)
- STYLE_BOLD =
INT2FIX(TTF_STYLE_BOLD)
- STYLE_UNDERLINE =
INT2FIX(TTF_STYLE_UNDERLINE)
Instance Method Summary collapse
-
#bold? ⇒ Boolean
@return [true, false] True if the font is bold.
- #draw(string, opts = {}) ⇒ Object
-
#height ⇒ Integer
The height of the font.
-
#initialize(arg, size) ⇒ Object
constructor
Creates a new font.
-
#italic? ⇒ true, false
True if the font is italic.
-
#line_skip ⇒ Integer
The recommended spacing between lines for this font.
-
#normal? ⇒ true, false
True if the font has no style.
- #size_of(text, encoding = nil) ⇒ Ray::Rect
-
#style ⇒ Integer
Bitmask describing the style of the font.
-
#style=(style) ⇒ Object
Sets the font style.
-
#underlined? ⇒ true, false
True if the font is underlined.
Methods included from ResourceSet
[], add_set, missing_pattern, need_argument_count, reject!, required_argument_count, select!, set_hash
Constructor Details
#initialize(arg, size) ⇒ Object
Creates a new font.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'ext/font.c', line 77
VALUE ray_init_font(VALUE self, VALUE arg, VALUE size) {
if (rb_respond_to(arg, RAY_METH("to_str")))
ray_init_font_with_filename(self, rb_String(arg), NUM2INT(size));
else if (rb_respond_to(arg, RAY_METH("read")))
ray_init_font_with_io(self, arg, NUM2INT(size));
else {
rb_raise(rb_eTypeError, "Can't convert %s into String",
RAY_OBJ_CLASSNAME(arg));
}
return Qnil;
}
|
Instance Method Details
#bold? ⇒ Boolean
@return [true, false] True if the font is bold
18 19 20 |
# File 'lib/ray/font.rb', line 18 def bold? (style & STYLE_BOLD) != 0 end |
#draw(string, opts = {}) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'ext/font.c', line 166
VALUE ray_font_draw(int argc, VALUE *argv, VALUE self) {
VALUE string, hash;
rb_scan_args(argc, argv, "11", &string, &hash);
if (NIL_P(hash))
hash = rb_hash_new();
TTF_Font *font = ray_rb2font(self);
char *c_string = StringValuePtr(string);
/* solid, shaded, blended */
VALUE drawing_mode = rb_hash_aref(hash, RAY_SYM("mode"));
VALUE encoding = rb_hash_aref(hash, RAY_SYM("encoding"));
int c_encoding = 0;
if (NIL_P(encoding) || encoding == RAY_SYM("latin1"))
c_encoding = RAY_ENCODING_LATIN1;
else if (encoding == RAY_SYM("utf8"))
c_encoding = RAY_ENCODING_UTF8;
else if (encoding == RAY_SYM("unicode"))
c_encoding = RAY_ENCODING_UNICODE;
else
rb_raise(rb_eArgError, "Invalid encoding.");
VALUE rb_color = rb_hash_aref(hash, RAY_SYM("color"));
ray_color c_color = {255, 255, 255, 255};
if (!NIL_P(rb_color))
c_color = ray_rb2col(rb_color);
SDL_Color color = {c_color.r, c_color.g, c_color.b};
SDL_Surface *surface = NULL;
if (NIL_P(drawing_mode) || drawing_mode == RAY_SYM("solid")) {
if (c_encoding == RAY_ENCODING_LATIN1)
surface = TTF_RenderText_Solid(font, c_string, color);
else if (c_encoding == RAY_ENCODING_UTF8)
surface = TTF_RenderUTF8_Solid(font, c_string, color);
else
surface = TTF_RenderUNICODE_Solid(font, (uint16_t*)c_string, color);
}
else if (drawing_mode == RAY_SYM("shaded")) {
VALUE rb_bg = rb_hash_aref(hash, RAY_SYM("background"));
ray_color c_bg = {0, 0, 0, 255};
if (!NIL_P(rb_bg))
c_bg = ray_rb2col(rb_bg);
SDL_Color bg = {c_bg.r, c_bg.g, c_bg.b};
if (c_encoding == RAY_ENCODING_LATIN1)
surface = TTF_RenderText_Shaded(font, c_string, color, bg);
else if (c_encoding == RAY_ENCODING_UTF8)
surface = TTF_RenderUTF8_Shaded(font, c_string, color, bg);
else
surface = TTF_RenderUNICODE_Shaded(font, (uint16_t*)c_string, color, bg);
}
else if (drawing_mode == RAY_SYM("blended")) {
if (c_encoding == RAY_ENCODING_LATIN1)
surface = TTF_RenderText_Blended(font, c_string, color);
else if (c_encoding == RAY_ENCODING_UTF8)
surface = TTF_RenderUTF8_Blended(font, c_string, color);
else
surface = TTF_RenderUNICODE_Blended(font, (uint16_t*)c_string, color);
}
else
rb_raise(rb_eArgError, "Invalid drawing mode.");
if (!surface) {
rb_raise(rb_eRuntimeError, "Could not drawstring (%s)",
TTF_GetError());
}
VALUE on = rb_hash_aref(hash, RAY_SYM("on"));
if (NIL_P(on))
return ray_create_gc_image(surface);
SDL_Surface *target = ray_rb2surface(on);
VALUE rb_rect = rb_hash_aref(hash, RAY_SYM("at"));
if (NIL_P(rb_rect))
rb_rect = rb_hash_aref(hash, RAY_SYM("to"));
SDL_Rect rect;
if (RTEST(rb_obj_is_kind_of(rb_rect, ray_cRect)))
rect = ray_rb2rect(rb_rect);
else if (RTEST(rb_obj_is_kind_of(rb_rect, rb_cArray)))
rect = ray_rb2rect(rb_apply(ray_cRect, RAY_METH("new"), rb_rect));
else {
rb_raise(rb_eTypeError, "Can't convert %s into Ray::Rect",
RAY_OBJ_CLASSNAME(rb_rect));
}
SDL_BlitSurface(surface, NULL, target, &rect);
SDL_FreeSurface(surface);
return on;
}
|
#height ⇒ Integer
Returns the height of the font.
107 108 109 110 |
# File 'ext/font.c', line 107 VALUE ray_font_height(VALUE self) { TTF_Font *font = ray_rb2font(self); return INT2FIX(TTF_FontHeight(font)); } |
#italic? ⇒ true, false
Returns True if the font is italic.
13 14 15 |
# File 'lib/ray/font.rb', line 13 def italic? (style & STYLE_ITALIC) != 0 end |
#line_skip ⇒ Integer
Returns the recommended spacing between lines for this font.
113 114 115 116 |
# File 'ext/font.c', line 113 VALUE ray_font_line_skip(VALUE self) { TTF_Font *font = ray_rb2font(self); return INT2FIX(TTF_FontLineSkip(font)); } |
#normal? ⇒ true, false
Returns True if the font has no style.
8 9 10 |
# File 'lib/ray/font.rb', line 8 def normal? style == STYLE_NORMAL end |
#size_of(text, encoding = nil) ⇒ Ray::Rect
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'ext/font.c', line 123
VALUE ray_font_size_of(int argc, VALUE *argv, VALUE self) {
TTF_Font *font = ray_rb2font(self);
VALUE string = Qnil, encoding = Qnil;
rb_scan_args(argc, argv, "11", &string, &encoding);
char *c_string = StringValuePtr(string);
int w, h;
if (NIL_P(encoding) || encoding == RAY_SYM("latin1"))
TTF_SizeText(font, c_string, &w, &h);
else if (encoding == RAY_SYM("utf8"))
TTF_SizeUTF8(font, c_string, &w, &h);
else if (encoding == RAY_SYM("unicode"))
TTF_SizeUNICODE(font, (uint16_t*)c_string, &w, &h);
else
rb_raise(rb_eArgError, "Invalid encoding.");
SDL_Rect rect = {0, 0, w, h};
return ray_rect2rb(rect);
}
|
#style ⇒ Integer
Returns Bitmask describing the style of the font. STYLE_NORMAL for a normal font.
94 95 96 97 |
# File 'ext/font.c', line 94 VALUE ray_font_style(VALUE self) { TTF_Font *font = ray_rb2font(self); return INT2FIX(TTF_GetFontStyle(font)); } |
#style=(style) ⇒ Object
Sets the font style.
100 101 102 103 104 |
# File 'ext/font.c', line 100
VALUE ray_font_set_style(VALUE self, VALUE style) {
TTF_Font *font = ray_rb2font(self);
TTF_SetFontStyle(font, NUM2INT(style));
return style;
}
|
#underlined? ⇒ true, false
Returns True if the font is underlined.
23 24 25 |
# File 'lib/ray/font.rb', line 23 def underlined? (style & STYLE_UNDERLINE) != 0 end |