ruby-miyako 2.1.4 → 2.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/miyako_font.c CHANGED
@@ -20,8 +20,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
20
  */
21
21
 
22
22
  /*
23
- =拡張ライブラリmiyako_no_katana
24
- Authors:: サイロス誠
23
+ =miyako_no_katana
24
+ Authors:: Cyross Makoto
25
25
  Version:: 2.0
26
26
  Copyright:: 2007-2008 Cyross Makoto
27
27
  License:: LGPL2.1
@@ -59,55 +59,65 @@ static GLOBAL_DEFINE_GET_STRUCT(TTFont, Get_TTFont, cTTFFont, "SDL::TT::Font");
59
59
 
60
60
  static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE vy)
61
61
  {
62
+ int font_size, shadow_margin_x, shadow_margin_y, hspace, x, y, a1, a2, margin_x, margin_y;
63
+ TTF_Font *font;
64
+ VALUE *p_font_color, *p_shadow_color;
65
+ VALUE use_shadow, shadow_margin, chr;
66
+ SDL_Color fore_color, shadow_color;
67
+ SDL_Surface *scr, *ssrc, *ssrc2;
68
+ SDL_Rect drect;
69
+ MiyakoBitmap src, dst;
70
+ char *sptr;
71
+ Uint32 sr, sg, sb, sa;
72
+ Uint32 dr, dg, db, da;
73
+ Uint32 *psrc2, *psrc, *ppsrc2, *ppdst;
74
+ int i, n, clen;
75
+ const char *ptr;
76
+ int len;
77
+ rb_encoding *enc;
78
+
62
79
  rb_secure(4);
63
80
  StringValue(str);
64
81
 
65
82
  str = rb_funcall(str, id_encode, 1, rb_const_get(cEncoding, id_utf8));
66
83
 
67
- TTF_Font *font = Get_TTFont(rb_iv_get(self, "@font"))->font;
84
+ font = Get_TTFont(rb_iv_get(self, "@font"))->font;
85
+
86
+ p_font_color = RARRAY_PTR(rb_iv_get(self, "@color"));
68
87
 
69
- VALUE *p_font_color = RARRAY_PTR(rb_iv_get(self, "@color"));
70
- SDL_Color fore_color;
71
88
  fore_color.r = NUM2INT(*(p_font_color+0));
72
89
  fore_color.g = NUM2INT(*(p_font_color+1));
73
90
  fore_color.b = NUM2INT(*(p_font_color+2));
74
91
  fore_color.unused = 0;
75
92
 
76
- VALUE *p_shadow_color = RARRAY_PTR(rb_iv_get(self, "@shadow_color"));
77
- SDL_Color shadow_color;
93
+ p_shadow_color = RARRAY_PTR(rb_iv_get(self, "@shadow_color"));
78
94
  shadow_color.r = NUM2INT(*(p_shadow_color+0));
79
95
  shadow_color.g = NUM2INT(*(p_shadow_color+1));
80
96
  shadow_color.b = NUM2INT(*(p_shadow_color+2));
81
97
  shadow_color.unused = 0;
82
98
 
83
- int font_size = NUM2INT(rb_iv_get(self, "@size"));
84
- VALUE use_shadow = rb_iv_get(self, "@use_shadow");
85
- VALUE shadow_margin = rb_iv_get(self, "@shadow_margin");
86
- int shadow_margin_x = (use_shadow == Qtrue ? NUM2INT(*(RARRAY_PTR(shadow_margin)+0)) : 0);
87
- int shadow_margin_y = (use_shadow == Qtrue ? NUM2INT(*(RARRAY_PTR(shadow_margin)+1)) : 0);
88
- int hspace = NUM2INT(rb_iv_get(self, "@hspace"));
99
+ font_size = NUM2INT(rb_iv_get(self, "@size"));
100
+ use_shadow = rb_iv_get(self, "@use_shadow");
101
+ shadow_margin = rb_iv_get(self, "@shadow_margin");
102
+ shadow_margin_x = (use_shadow == Qtrue ? NUM2INT(*(RARRAY_PTR(shadow_margin)+0)) : 0);
103
+ shadow_margin_y = (use_shadow == Qtrue ? NUM2INT(*(RARRAY_PTR(shadow_margin)+1)) : 0);
104
+ hspace = NUM2INT(rb_iv_get(self, "@hspace"));
89
105
 
90
- SDL_Surface *scr = GetSurface(rb_iv_get(mScreen, "@@screen"))->surface;
91
- SDL_Rect drect;
106
+ scr = GetSurface(rb_iv_get(mScreen, "@@screen"))->surface;
92
107
 
93
- MiyakoBitmap src, dst;
94
108
  _miyako_setup_unit(vdst, scr, &dst, vx, vy, 0);
95
109
 
96
- char *sptr = RSTRING_PTR(str);
110
+ sptr = RSTRING_PTR(str);
97
111
 
98
112
  src.x = dst.x;
99
113
  src.y = dst.y;
100
114
 
101
- int x, y;
102
- Uint32 sr, sg, sb, sa;
103
- Uint32 dr, dg, db, da;
104
-
105
115
  if(use_shadow == Qtrue)
106
116
  {
107
- SDL_Surface *ssrc2 = TTF_RenderUTF8_Blended(font, sptr, shadow_color);
117
+ ssrc2 = TTF_RenderUTF8_Blended(font, sptr, shadow_color);
108
118
 
109
119
  if(ssrc2 == NULL) return INT2NUM(src.x);
110
- Uint32 *psrc2 = (Uint32 *)(ssrc2->pixels);
120
+ psrc2 = (Uint32 *)(ssrc2->pixels);
111
121
 
112
122
  src.x += shadow_margin_x;
113
123
  src.y += shadow_margin_y;
@@ -129,7 +139,7 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
129
139
  }
130
140
  if(size2.w > ssrc2->w){ size2.w = ssrc2->w; }
131
141
 
132
- int margin_x = 0;
142
+ margin_x = 0;
133
143
  if(src.x < 0)
134
144
  {
135
145
  int tmp_w = ssrc2->w + src.x;
@@ -151,7 +161,7 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
151
161
  }
152
162
  if(size2.h > ssrc2->h){ size2.h = ssrc2->h; }
153
163
 
154
- int margin_y = 0;
164
+ margin_y = 0;
155
165
  if(src.y < 0)
156
166
  {
157
167
  int tmp_h = ssrc2->h + src.y;
@@ -170,8 +180,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
170
180
 
171
181
  for(y = 0; y < size2.h; y++)
172
182
  {
173
- Uint32 *ppsrc2 = psrc2 + (y+margin_y) * ssrc2->w + margin_x;
174
- Uint32 *ppdst = dst.ptr + (src.y + y) * dst.surface->w + src.x;
183
+ ppsrc2 = psrc2 + (y+margin_y) * ssrc2->w + margin_x;
184
+ ppdst = dst.ptr + (src.y + y) * dst.surface->w + src.x;
175
185
  for(x = 0; x < size2.w; x++)
176
186
  {
177
187
  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
@@ -191,8 +201,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
191
201
  sr = (Uint32)((*ppsrc2 >> 16)) & 0xff;
192
202
  sg = (Uint32)((*ppsrc2 >> 8)) & 0xff;
193
203
  sb = (Uint32)((*ppsrc2)) & 0xff;
194
- int a1 = sa + 1;
195
- int a2 = 256 - sa;
204
+ a1 = sa + 1;
205
+ a2 = 256 - sa;
196
206
  *ppdst = (((sr * a1 + dr * a2) >> 8)) << 16 |
197
207
  (((sg * a1 + dg * a2) >> 8)) << 8 |
198
208
  (((sb * a1 + db * a2) >> 8)) |
@@ -204,8 +214,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
204
214
  sr = *ppsrc2 & 0xff0000;
205
215
  sg = *ppsrc2 & 0xff00;
206
216
  sb = *ppsrc2 & 0xff;
207
- int a1 = sa + 1;
208
- int a2 = 256 - sa;
217
+ a1 = sa + 1;
218
+ a2 = 256 - sa;
209
219
  *ppdst = ((sr * a1 + dr * a2) & 0xff000000 |
210
220
  (sg * a1 + dg * a2) & 0xff0000 |
211
221
  (sb * a1 + db * a2)) >> 8 |
@@ -227,8 +237,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
227
237
  sr = (Uint32)(((*ppsrc2 & ssrc2->format->Rmask) >> ssrc2->format->Rshift));
228
238
  sg = (Uint32)(((*ppsrc2 & ssrc2->format->Gmask) >> ssrc2->format->Gshift));
229
239
  sb = (Uint32)(((*ppsrc2 & ssrc2->format->Bmask) >> ssrc2->format->Bshift));
230
- int a1 = sa + 1;
231
- int a2 = 256 - sa;
240
+ a1 = sa + 1;
241
+ a2 = 256 - sa;
232
242
  *ppdst = (((sr * a1 + dr * a2) >> 8)) << dst.fmt->Rshift |
233
243
  (((sg * a1 + dg * a2) >> 8)) << dst.fmt->Gshift |
234
244
  (((sb * a1 + db * a2) >> 8)) << dst.fmt->Bshift |
@@ -249,7 +259,7 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
249
259
  src.y = dst.y;
250
260
  }
251
261
 
252
- SDL_Surface *ssrc = TTF_RenderUTF8_Blended(font, sptr, fore_color);
262
+ ssrc = TTF_RenderUTF8_Blended(font, sptr, fore_color);
253
263
 
254
264
  if(ssrc == NULL) return INT2NUM(src.x);
255
265
 
@@ -261,7 +271,7 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
261
271
  }
262
272
  else
263
273
  {
264
- Uint32 *psrc = (Uint32 *)(ssrc->pixels);
274
+ psrc = (Uint32 *)(ssrc->pixels);
265
275
 
266
276
  MiyakoSize size;
267
277
 
@@ -273,7 +283,7 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
273
283
  }
274
284
  if(size.w > ssrc->w){ size.w = ssrc->w; }
275
285
 
276
- int margin_x = 0;
286
+ margin_x = 0;
277
287
  if(src.x < 0)
278
288
  {
279
289
  int tmp_w = ssrc->w + src.x;
@@ -295,7 +305,7 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
295
305
  }
296
306
  if(size.h > ssrc->h){ size.h = ssrc->h; }
297
307
 
298
- int margin_y = 0;
308
+ margin_y = 0;
299
309
  if(src.y < 0)
300
310
  {
301
311
  int tmp_h = ssrc->h + src.y;
@@ -335,8 +345,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
335
345
  sr = (Uint32)(((*ppsrc) >> 16)) & 0xff;
336
346
  sg = (Uint32)(((*ppsrc) >> 8)) & 0xff;
337
347
  sb = (Uint32)(((*ppsrc))) & 0xff;
338
- int a1 = sa + 1;
339
- int a2 = 256 - sa;
348
+ a1 = sa + 1;
349
+ a2 = 256 - sa;
340
350
  *ppdst = (((sr * a1 + dr * a2) >> 8)) << 16 |
341
351
  (((sg * a1 + dg * a2) >> 8)) << 8 |
342
352
  (((sb * a1 + db * a2) >> 8)) |
@@ -348,8 +358,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
348
358
  sr = *ppsrc & 0xff0000;
349
359
  sg = *ppsrc & 0xff00;
350
360
  sb = *ppsrc & 0xff;
351
- int a1 = sa + 1;
352
- int a2 = 256 - sa;
361
+ a1 = sa + 1;
362
+ a2 = 256 - sa;
353
363
  *ppdst = ((sr * a1 + dr * a2) & 0xff000000 |
354
364
  (sg * a1 + dg * a2) & 0xff0000 |
355
365
  (sb * a1 + db * a2)) >> 8 |
@@ -371,8 +381,8 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
371
381
  sr = (Uint32)(((*ppsrc & ssrc->format->Rmask) >> ssrc->format->Rshift));
372
382
  sg = (Uint32)(((*ppsrc & ssrc->format->Gmask) >> ssrc->format->Gshift));
373
383
  sb = (Uint32)(((*ppsrc & ssrc->format->Bmask) >> ssrc->format->Bshift));
374
- int a1 = sa + 1;
375
- int a2 = 256 - sa;
384
+ a1 = sa + 1;
385
+ a2 = 256 - sa;
376
386
  *ppdst = (((sr * a1 + dr * a2) >> 8)) << dst.fmt->Rshift |
377
387
  (((sg * a1 + dg * a2) >> 8)) << dst.fmt->Gshift |
378
388
  (((sb * a1 + db * a2) >> 8)) << dst.fmt->Bshift |
@@ -388,15 +398,14 @@ static VALUE font_draw_text(VALUE self, VALUE vdst, VALUE str, VALUE vx, VALUE v
388
398
  }
389
399
  SDL_FreeSurface(ssrc);
390
400
 
391
- int i, n;
392
- const char *ptr = RSTRING_PTR(str);
393
- int len = RSTRING_LEN(str);
394
- rb_encoding *enc = rb_enc_get(str);
401
+ ptr = RSTRING_PTR(str);
402
+ len = RSTRING_LEN(str);
403
+ enc = rb_enc_get(str);
395
404
  for(i=0; i<len; i+=n)
396
405
  {
397
406
  n = rb_enc_mbclen(ptr+i, ptr+len, enc);
398
- VALUE chr = rb_str_subseq(str, i, n);
399
- int clen = RSTRING_LEN(chr);
407
+ chr = rb_str_subseq(str, i, n);
408
+ clen = RSTRING_LEN(chr);
400
409
  dst.x += (clen==1 ? font_size>>1 : font_size) + shadow_margin_x + hspace;
401
410
  }
402
411
  return INT2NUM(dst.x);
@@ -418,18 +427,19 @@ static VALUE font_text_size(VALUE self, VALUE str)
418
427
  int shadow_margin_x = (use_shadow == Qtrue ? NUM2INT(*(RARRAY_PTR(shadow_margin)+0)) : 0);
419
428
  int hspace = NUM2INT(rb_iv_get(self, "@hspace"));
420
429
 
421
- int i, n, l=0;
430
+ VALUE chr, array;
431
+ int i, n, l=0, clen;
422
432
  const char *ptr = RSTRING_PTR(str);
423
433
  int len = RSTRING_LEN(str);
424
434
  rb_encoding *enc = rb_enc_get(str);
425
435
  for(i=0; i<len; i+=n)
426
436
  {
427
437
  n = rb_enc_mbclen(ptr+i, ptr+len, enc);
428
- VALUE chr = rb_str_subseq(str, i, n);
429
- int clen = RSTRING_LEN(chr);
438
+ chr = rb_str_subseq(str, i, n);
439
+ clen = RSTRING_LEN(chr);
430
440
  l += (clen==1 ? font_size>>1 : font_size) + shadow_margin_x + hspace;
431
441
  }
432
- VALUE array = rb_ary_new();
442
+ array = rb_ary_new();
433
443
  rb_ary_push(array, INT2NUM(l));
434
444
  rb_ary_push(array, font_line_height(self));
435
445
  return array;