You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sortchars.c 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * sortchars analyse ASCII characters
  3. * Copyright (c) 2007 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #include "common.h"
  16. #if !defined(__KERNEL__)
  17. # if defined(HAVE_INTTYPES_H)
  18. # include <inttypes.h>
  19. # endif
  20. # include <stdio.h>
  21. # include <string.h>
  22. # include <stdlib.h>
  23. #endif
  24. #include "cucul.h"
  25. #define GLYPHS 0x7f
  26. #define FONT 1 /* 0 or 1 */
  27. #define DX 2
  28. #define DY 3
  29. #define RANGEBITS 2
  30. #define RANGE (1 << RANGEBITS)
  31. #define FULLRANGE (1 << (RANGEBITS * DX * DY))
  32. int total[GLYPHS][DX][DY];
  33. int16_t allbits[GLYPHS];
  34. int bestchar[FULLRANGE];
  35. static int curve[17] = /* 17 instead of 16 */
  36. {
  37. 0, 4, 6, 8, 9, 10, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15
  38. };
  39. static int distance(uint16_t, uint16_t);
  40. int main(int argc, char *argv[])
  41. {
  42. int count[DX][DY];
  43. char utf8[7];
  44. cucul_canvas_t *cv;
  45. cucul_font_t *f;
  46. char const * const * fonts;
  47. uint8_t *img;
  48. unsigned int w, h, x, y;
  49. int ret, i, max;
  50. /* Load a libcucul internal font */
  51. fonts = cucul_get_font_list();
  52. if(fonts[FONT] == NULL)
  53. {
  54. fprintf(stderr, "error: libcucul was compiled without any fonts\n");
  55. return -1;
  56. }
  57. f = cucul_load_font(fonts[FONT], 0);
  58. if(f == NULL)
  59. {
  60. fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
  61. return -1;
  62. }
  63. cv = cucul_create_canvas(1, 1);
  64. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  65. /* Create our bitmap buffer (32-bit ARGB) */
  66. w = cucul_get_font_width(f);
  67. h = cucul_get_font_height(f);
  68. img = malloc(4 * w * h);
  69. /* Zero our structures */
  70. for(y = 0; y < DY; y++)
  71. for(x = 0; x < DX; x++)
  72. count[x][y] = 0;
  73. for(y = 0; y < h; y++)
  74. for(x = 0; x < w; x++)
  75. count[x * DX / w][y * DY / h]++;
  76. for(i = 0x20; i < GLYPHS; i++)
  77. for(y = 0; y < DY; y++)
  78. for(x = 0; x < DX; x++)
  79. total[GLYPHS][x][y] = 0;
  80. /* Draw all glyphs and count their pixels */
  81. for(i = 0x20; i < GLYPHS; i++)
  82. {
  83. cucul_put_char(cv, 0, 0, i);
  84. /* Render the canvas onto our image buffer */
  85. cucul_render_canvas(cv, f, img, w, h, 4 * w);
  86. for(y = 0; y < h * DY; y++)
  87. for(x = 0; x < w * DX; x++)
  88. total[i][x / w][y / h]
  89. += img[(w * (y / DY) + (x / DX)) * 4 + 1];
  90. }
  91. /* Compute max pixel value */
  92. max = 0;
  93. for(i = 0x20; i < GLYPHS; i++)
  94. for(y = 0; y < DY; y++)
  95. for(x = 0; x < DX; x++)
  96. {
  97. int val = total[i][x][y] * 256 / count[x][y];
  98. if(val > max)
  99. max = val;
  100. }
  101. /* Compute bits for all glyphs */
  102. for(i = 0x20; i < GLYPHS; i++)
  103. {
  104. int bits = 0;
  105. if(i >= 0x7f && i <= 0x9f)
  106. {
  107. allbits[i] = 0;
  108. continue;
  109. }
  110. for(y = 0; y < DY; y++)
  111. {
  112. for(x = 0; x < DX; x++)
  113. {
  114. int t = total[i][x][y] * 16 * 256 / (count[x][y] * max);
  115. bits *= RANGE;
  116. bits |= curve[t] / (16 / RANGE);
  117. }
  118. }
  119. allbits[i] = bits;
  120. }
  121. /* Find a glyph for all combinations */
  122. for(i = 0; i < FULLRANGE; i++)
  123. {
  124. int j, mindist = 0x1000, best = 0;
  125. for(j = 0x20; j < GLYPHS; j++)
  126. {
  127. int d = distance(i, allbits[j]);
  128. if(d < mindist)
  129. {
  130. best = j;
  131. mindist = d;
  132. if(d == 0)
  133. break;
  134. }
  135. }
  136. bestchar[i] = best;
  137. }
  138. /* Print results */
  139. printf("/* Generated by sortchars.c */\n");
  140. printf("static char const lookup_ascii[%i] =\n{\n ", FULLRANGE);
  141. for(i = 0; i < FULLRANGE; i++)
  142. {
  143. ret = cucul_utf32_to_utf8(utf8, bestchar[i]);
  144. utf8[ret] = '\0';
  145. printf("%i, ", bestchar[i]);
  146. if((i % 16) == 15 && i != FULLRANGE - 1)
  147. printf("\n ");
  148. }
  149. printf("\n};\n");
  150. cucul_free_canvas(cv);
  151. return 0;
  152. }
  153. static int distance(uint16_t a, uint16_t b)
  154. {
  155. int i, d = 0;
  156. for(i = 0; i < DX * DY; i++)
  157. {
  158. int x = (int)(a & (RANGE - 1)) - (int)(b & (RANGE - 1));
  159. d += x * x;
  160. a /= RANGE;
  161. b /= RANGE;
  162. }
  163. return d;
  164. }