Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

143 wiersze
3.4 KiB

  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 RANGE 4
  30. int total[GLYPHS][DX][DY];
  31. static int curve[17] = /* 17 instead of 16 */
  32. {
  33. 0, 4, 6, 8, 9, 10, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15
  34. };
  35. int main(int argc, char *argv[])
  36. {
  37. int count[DX][DY];
  38. char utf8[7];
  39. cucul_canvas_t *cv;
  40. cucul_font_t *f;
  41. char const * const * fonts;
  42. uint8_t *img;
  43. unsigned int w, h, x, y;
  44. int ret, i, max;
  45. /* Load a libcucul internal font */
  46. fonts = cucul_get_font_list();
  47. if(fonts[FONT] == NULL)
  48. {
  49. fprintf(stderr, "error: libcucul was compiled without any fonts\n");
  50. return -1;
  51. }
  52. f = cucul_load_font(fonts[FONT], 0);
  53. if(f == NULL)
  54. {
  55. fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
  56. return -1;
  57. }
  58. cv = cucul_create_canvas(1, 1);
  59. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  60. /* Create our bitmap buffer (32-bit ARGB) */
  61. w = cucul_get_font_width(f);
  62. h = cucul_get_font_height(f);
  63. img = malloc(4 * w * h);
  64. /* Zero our structures */
  65. for(y = 0; y < DY; y++)
  66. for(x = 0; x < DX; x++)
  67. count[x][y] = 0;
  68. for(y = 0; y < h; y++)
  69. for(x = 0; x < w; x++)
  70. count[x * DX / w][y * DY / h]++;
  71. for(i = 0x20; i < GLYPHS; i++)
  72. for(y = 0; y < DY; y++)
  73. for(x = 0; x < DX; x++)
  74. total[GLYPHS][x][y] = 0;
  75. /* Draw all glyphs and count their pixels */
  76. for(i = 0x20; i < GLYPHS; i++)
  77. {
  78. cucul_put_char(cv, 0, 0, i);
  79. /* Render the canvas onto our image buffer */
  80. cucul_render_canvas(cv, f, img, w, h, 4 * w);
  81. for(y = 0; y < h * DY; y++)
  82. for(x = 0; x < w * DX; x++)
  83. total[i][x / w][y / h]
  84. += img[(w * (y / DY) + (x / DX)) * 4 + 1];
  85. }
  86. /* Compute max value */
  87. max = 0;
  88. for(i = 0x20; i < GLYPHS; i++)
  89. for(y = 0; y < DY; y++)
  90. for(x = 0; x < DX; x++)
  91. {
  92. int val = total[i][x][y] * 256 / count[x][y];
  93. if(val > max)
  94. max = val;
  95. }
  96. for(i = 0x20; i < GLYPHS; i++)
  97. {
  98. int bits = 0;
  99. if(i >= 0x7f && i <= 0x9f)
  100. continue;
  101. ret = cucul_utf32_to_utf8(utf8, i);
  102. utf8[ret] = '\0';
  103. for(y = 0; y < DY; y++)
  104. {
  105. for(x = 0; x < DX; x++)
  106. {
  107. int t = total[i][x][y] * 16 * 256 / (count[x][y] * max);
  108. t = curve[t] / (16 / RANGE);
  109. printf("%d ", t);
  110. bits *= RANGE;
  111. bits |= t;
  112. }
  113. }
  114. printf(" 0x%04d (%s)\n", bits, utf8);
  115. }
  116. cucul_free_canvas(cv);
  117. return 0;
  118. }