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