/xbmc/visualizations/Goom/goom2k4-0/src/gfontlib.c

http://github.com/xbmc/xbmc · C · 231 lines · 195 code · 30 blank · 6 comment · 37 complexity · 42362a88122ccbf50b978aec0ecf10f3 MD5 · raw file

  1. #include "goom_config.h"
  2. #include "gfontrle.h"
  3. #include "gfontlib.h"
  4. #include <string.h>
  5. #include <stdlib.h>
  6. static Pixel ***font_chars;
  7. static int *font_width;
  8. static int *font_height;
  9. static Pixel ***small_font_chars;
  10. static int *small_font_width;
  11. static int *small_font_height;
  12. void gfont_load (void) {
  13. unsigned char *gfont;
  14. unsigned int i = 0, j = 0;
  15. unsigned int nba = 0;
  16. unsigned int current = 32;
  17. int *font_pos;
  18. /* decompress le rle */
  19. gfont = malloc (the_font.width*the_font.height*the_font.bytes_per_pixel);
  20. while (i<the_font.rle_size) {
  21. unsigned char c = the_font.rle_pixel [i++];
  22. if (c == 0) {
  23. unsigned int nb = the_font.rle_pixel [i++];
  24. while (nb--)
  25. gfont[j++] = 0;
  26. }
  27. else
  28. gfont [j++] = c;
  29. }
  30. /* determiner les positions de chaque lettre. */
  31. font_height = calloc (256,sizeof(int));
  32. small_font_height = calloc (256,sizeof(int));
  33. font_width = calloc (256,sizeof(int));
  34. small_font_width = calloc (256,sizeof(int));
  35. font_chars = calloc (256,sizeof(int**));
  36. small_font_chars = calloc (256,sizeof(int**));
  37. font_pos = calloc (256,sizeof(int));
  38. for (i=0;i<the_font.width;i++) {
  39. unsigned char a = gfont [i*4 + 3];
  40. if (a)
  41. nba ++;
  42. else
  43. nba = 0;
  44. if (nba == 2) {
  45. font_width [current] = i - font_pos [current];
  46. small_font_width [current] = font_width [current]/2;
  47. font_pos [++current] = i;
  48. font_height [current] = the_font.height - 2;
  49. small_font_height [current] = font_height [current]/2;
  50. }
  51. }
  52. font_pos [current] = 0;
  53. font_height [current] = 0;
  54. small_font_height [current] = 0;
  55. /* charger les lettres et convertir au format de la machine */
  56. for (i=33;i<current;i++) {
  57. int x; int y;
  58. font_chars [i] = malloc (font_height[i]*sizeof(int *));
  59. small_font_chars [i] = malloc (font_height[i]*sizeof(int *)/2);
  60. for (y = 0; y < font_height[i]; y++) {
  61. font_chars [i][y] = malloc (font_width[i]*sizeof(int));
  62. for (x = 0; x < font_width[i]; x++) {
  63. unsigned int r,g,b,a;
  64. r = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4)];
  65. g = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4+1)];
  66. b = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4+2)];
  67. a = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4+3)];
  68. font_chars [i][y][x].val =
  69. (r<<(ROUGE*8))|(g<<(VERT*8))|(b<<(BLEU*8))|(a<<(ALPHA*8));
  70. }
  71. }
  72. for (y = 0; y < font_height[i]/2; y++) {
  73. small_font_chars [i][y] = malloc (font_width[i]*sizeof(int)/2);
  74. for (x = 0; x < font_width[i]/2; x++) {
  75. unsigned int r1,g1,b1,a1,r2,g2,b2,a2,r3,g3,b3,a3,r4,g4,b4,a4;
  76. r1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4)];
  77. g1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+1)];
  78. b1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+2)];
  79. a1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+3)];
  80. r2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+4)];
  81. g2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+5)];
  82. b2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+6)];
  83. a2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+7)];
  84. r3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4)];
  85. g3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+1)];
  86. b3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+2)];
  87. a3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+3)];
  88. r4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+4)];
  89. g4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+5)];
  90. b4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+6)];
  91. a4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+7)];
  92. small_font_chars [i][y][x].val =
  93. (((r1 + r2 + r3 + r4)>>2)<<(ROUGE*8))|
  94. (((g1 + g2 + g3 + g4)>>2)<<(VERT*8))|
  95. (((b1 + b2 + b3 + b4)>>2)<<(BLEU*8))|
  96. (((a1 + a2 + a3 + a4)>>2)<<(ALPHA*8));
  97. }
  98. }
  99. }
  100. /* definir les lettres restantes */
  101. for (i=0;i<256;i++) {
  102. if (font_chars[i]==0) {
  103. font_chars[i]=font_chars[42];
  104. small_font_chars[i]=small_font_chars[42];
  105. font_width[i]=font_width[42];
  106. font_pos[i]=font_pos[42];
  107. font_height[i]=font_height[42];
  108. small_font_width[i]=small_font_width[42];
  109. small_font_height[i]=small_font_height[42];
  110. }
  111. }
  112. font_width [32] = (the_font.height / 2) - 1;
  113. small_font_width [32] = font_width [32]/2;
  114. font_chars [32] = 0;
  115. small_font_chars [32] = 0;
  116. free( gfont );
  117. free( font_pos );
  118. }
  119. void goom_draw_text (Pixel * buf,int resolx,int resoly,
  120. int x, int y,
  121. const char *str, float charspace, int center) {
  122. float fx = (float) x;
  123. int fin = 0;
  124. Pixel ***cur_font_chars;
  125. int *cur_font_width;
  126. int *cur_font_height;
  127. if (resolx>320)
  128. {
  129. /* printf("use big\n"); */
  130. cur_font_chars = font_chars;
  131. cur_font_width = font_width;
  132. cur_font_height = font_height;
  133. }
  134. else
  135. {
  136. /* printf ("use small\n"); */
  137. cur_font_chars = small_font_chars;
  138. cur_font_width = small_font_width;
  139. cur_font_height = small_font_height;
  140. }
  141. if (cur_font_chars == NULL)
  142. return ;
  143. if (center) {
  144. unsigned char *tmp = (unsigned char*)str;
  145. float lg = -charspace;
  146. while (*tmp != '\0')
  147. lg += cur_font_width[*(tmp++)] + charspace;
  148. fx -= lg / 2;
  149. }
  150. while (!fin) {
  151. unsigned char c = *str;
  152. x = (int) fx;
  153. if (c == '\0')
  154. fin = 1;
  155. else if (cur_font_chars[c]==0) {
  156. fx += cur_font_width[c] + charspace;
  157. }
  158. else {
  159. int xx, yy;
  160. int xmin = x;
  161. int xmax = x + cur_font_width[c];
  162. int ymin = y - cur_font_height[c];
  163. int ymax = y;
  164. yy = ymin;
  165. if (xmin < 0)
  166. xmin = 0;
  167. if (xmin >= resolx - 1)
  168. return;
  169. if (xmax >= (int) resolx)
  170. xmax = resolx - 1;
  171. if (yy < 0)
  172. yy = 0;
  173. if (yy <= (int) resoly - 1) {
  174. if (ymax >= (int) resoly - 1)
  175. ymax = resoly - 1;
  176. for (; yy < ymax; yy++)
  177. for (xx = xmin; xx < xmax; xx++)
  178. {
  179. Pixel color = cur_font_chars[c][yy - ymin][xx - x];
  180. Pixel transparency;
  181. transparency.val = color.val & A_CHANNEL;
  182. if (transparency.val)
  183. {
  184. if (transparency.val==A_CHANNEL) buf[yy * resolx + xx] = color;
  185. else
  186. {
  187. Pixel back = buf[yy * resolx + xx];
  188. unsigned int a1 = color.channels.a;
  189. unsigned int a2 = 255 - a1;
  190. buf[yy * resolx + xx].channels.r = (unsigned char)((((unsigned int)color.channels.r * a1) + ((unsigned int)back.channels.r * a2)) >> 8);
  191. buf[yy * resolx + xx].channels.g = (unsigned char)((((unsigned int)color.channels.g * a1) + ((unsigned int)back.channels.g * a2)) >> 8);
  192. buf[yy * resolx + xx].channels.b = (unsigned char)((((unsigned int)color.channels.b * a1) + ((unsigned int)back.channels.b * a2)) >> 8);
  193. }
  194. }
  195. }
  196. }
  197. fx += cur_font_width[c] + charspace;
  198. }
  199. str++;
  200. }
  201. }