comparison TOOLS/subfont-c/subfont.c @ 1607:7336606415f2

Implemented new Gaussian blur algorithm, extended font.desc format, optimized for speed and memory usage.
author zybi
date Tue, 21 Aug 2001 14:00:57 +0000
parents 47a49d6d7132
children 8ccac62ddfb7
comparison
equal deleted inserted replaced
1606:daec36af6c01 1607:7336606415f2
1 /* 1 /*
2 * Renders antialiased fonts for mplayer using freetype library. 2 * Renders antialiased fonts for mplayer using freetype library.
3 * Should work with TrueType, Type1 and any other font supported by libfreetype. 3 * Should work with TrueType, Type1 and any other font supported by libfreetype.
4 * 4 * Can generate font.desc for any encoding.
5 * Goals:
6 * - internationalization: supports any 8 bit encoding (uses iconv).
7 * - nice look: creates glyph `shadows' using algorithm derived from gaussian blur.
8 * 5 *
9 * 6 *
10 * Artur Zaprzala <zybi@fanthom.irc.pl> 7 * Artur Zaprzala <zybi@fanthom.irc.pl>
11 * 8 *
12 */ 9 */
64 char *encoding_name; 61 char *encoding_name;
65 char *font_path; 62 char *font_path;
66 //char *font_metrics; 63 //char *font_metrics;
67 int append_mode = 0; 64 int append_mode = 0;
68 65
69 unsigned char *buffer, *abuffer; 66 unsigned char *bbuffer, *abuffer;
70 int width, height; 67 int width, height;
71 static FT_ULong charset[max_charset_size]; /* characters we want to render; Unicode */ 68 static FT_ULong charset[max_charset_size]; /* characters we want to render; Unicode */
72 static FT_ULong charcodes[max_charset_size]; /* character codes in 'encoding' */ 69 static FT_ULong charcodes[max_charset_size]; /* character codes in 'encoding' */
73 iconv_t cd; // iconv conversion descriptor 70 iconv_t cd; // iconv conversion descriptor
74 71
78 #define ERROR_(msg, ...) (eprintf("%s: error: " msg "\n", command, __VA_ARGS__), exit(1)) 75 #define ERROR_(msg, ...) (eprintf("%s: error: " msg "\n", command, __VA_ARGS__), exit(1))
79 #define WARNING_(msg, ...) eprintf("%s: warning: " msg "\n", command, __VA_ARGS__) 76 #define WARNING_(msg, ...) eprintf("%s: warning: " msg "\n", command, __VA_ARGS__)
80 #define ERROR(...) ERROR_(__VA_ARGS__, NULL) 77 #define ERROR(...) ERROR_(__VA_ARGS__, NULL)
81 #define WARNING(...) WARNING_(__VA_ARGS__, NULL) 78 #define WARNING(...) WARNING_(__VA_ARGS__, NULL)
82 79
83 #define f266toInt(x) (((x)+32)>>6) // round fractional fixed point number to integer 80
81 #define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer
84 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels) 82 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels)
85 #define ALIGN(x) (((x)+7)&~7) // 8 byte align 83 #define f266CeilToInt(x) (((x)+63)>>6) // ceiling
84 #define f266FloorToInt(x) ((x)>>6) // floor
85 #define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16
86
87 #define ALIGN(x) (((x)+7)&~7) // 8 byte align
86 88
87 89
88 90
89 void paste_bitmap(FT_Bitmap *bitmap, int x, int y) { 91 void paste_bitmap(FT_Bitmap *bitmap, int x, int y) {
90 int drow = x+y*width; 92 int drow = x+y*width;
91 int srow = 0; 93 int srow = 0;
92 int sp, dp, w, h; 94 int sp, dp, w, h;
93 if (bitmap->pixel_mode==ft_pixel_mode_mono) 95 if (bitmap->pixel_mode==ft_pixel_mode_mono)
94 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch) 96 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch)
95 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp) 97 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp)
96 buffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0; 98 bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0;
97 else 99 else
98 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch) 100 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch)
99 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp) 101 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp)
100 buffer[drow+dp] = bitmap->buffer[srow+sp]; 102 bbuffer[drow+dp] = bitmap->buffer[srow+sp];
101 } 103 }
102 104
103 105
104 void write_header(FILE *f) { 106 void write_header(FILE *f) {
105 static unsigned char header[800] = "mhwanh"; 107 static unsigned char header[800] = "mhwanh";
111 for (i = 32; i<800; ++i) header[i] = (i-32)/3; 113 for (i = 32; i<800; ++i) header[i] = (i-32)/3;
112 fwrite(header, 1, 800, f); 114 fwrite(header, 1, 800, f);
113 } 115 }
114 116
115 117
116 void write_bitmap() { 118 void write_bitmap(void *buffer, char type) {
117 FILE *f; 119 FILE *f;
118 int const max_name = 128; 120 int const max_name = 128;
119 char name[max_name]; 121 char name[max_name];
120 122
121 snprintf(name, max_name, "%s-b.raw", encoding_name); 123 snprintf(name, max_name, "%s-%c.raw", encoding_name, type);
122 f = fopen(name, "wb"); 124 f = fopen(name, "wb");
123 if (f==NULL) ERROR("fopen failed."); 125 if (f==NULL) ERROR("fopen failed.");
124 write_header(f); 126 write_header(f);
125 fwrite(buffer, 1, width*height, f); 127 fwrite(buffer, 1, width*height, f);
126 fclose(f); 128 fclose(f);
127
128 snprintf(name, max_name, "%s-a.raw", encoding_name);
129 f = fopen(name, "wb");
130 if (f==NULL) ERROR("fopen failed.");
131 write_header(f);
132 fwrite(abuffer, 1, width*height, f);
133 fclose(f);
134 } 129 }
135 130
136 131
137 void render() { 132 void render() {
138 FT_Library library; 133 FT_Library library;
139 FT_Face face; 134 FT_Face face;
140 FT_Error error; 135 FT_Error error;
141 FT_GlyphSlot slot; 136 FT_Glyph *glyphs;
142 FT_ULong glyph_index, character, code; 137 FT_BitmapGlyph glyph;
143 //FT_Glyph glyphs[max_charset_size];
144 FT_Glyph glyph;
145 FILE *f; 138 FILE *f;
146 int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; 139 int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
147 int pen_x, pen_xa, pen_y, ymin, ymax; 140 int pen_x = 0, pen_xa;
141 int ymin = INT_MAX, ymax = INT_MIN;
148 int i, uni_charmap = 1; 142 int i, uni_charmap = 1;
149 int baseline, space_advance = 20; 143 int baseline, space_advance = 20;
144 int glyphs_count = 0;
150 145
151 146
152 /* initialize freetype */ 147 /* initialize freetype */
153 error = FT_Init_FreeType(&library); 148 error = FT_Init_FreeType(&library);
154 if (error) ERROR("Init_FreeType failed."); 149 if (error) ERROR("Init_FreeType failed.");
155 error = FT_New_Face(library, font_path, 0, &face); 150 error = FT_New_Face(library, font_path, 0, &face);
156 if (error) ERROR("New_Face failed."); 151 if (error) ERROR("New_Face failed. Maybe the font path `%s' is wrong.", font_path);
157 152
158 /* 153 /*
159 if (font_metrics) { 154 if (font_metrics) {
160 error = FT_Attach_File(face, font_metrics); 155 error = FT_Attach_File(face, font_metrics);
161 if (error) WARNING("Attach_File failed."); 156 if (error) WARNING("FT_Attach_File failed.");
162 } 157 }
163 */ 158 */
164 159
165 160
166 #if 0 161 #if 0
171 166
172 //error = FT_Select_Charmap(face, ft_encoding_unicode); 167 //error = FT_Select_Charmap(face, ft_encoding_unicode);
173 //error = FT_Select_Charmap(face, ft_encoding_adobe_standard); 168 //error = FT_Select_Charmap(face, ft_encoding_adobe_standard);
174 //error = FT_Select_Charmap(face, ft_encoding_adobe_custom); 169 //error = FT_Select_Charmap(face, ft_encoding_adobe_custom);
175 //error = FT_Set_Charmap(face, face->charmaps[1]); 170 //error = FT_Set_Charmap(face, face->charmaps[1]);
176 //if (error) WARNING("Select_Charmap failed."); 171 //if (error) WARNING("FT_Select_Charmap failed.");
177 #endif 172 #endif
173
174
178 #if 0 175 #if 0
179 /************************************************************/ 176 /************************************************************/
180 if (FT_HAS_GLYPH_NAMES(face)) { 177 if (FT_HAS_GLYPH_NAMES(face)) {
181 int const max_gname = 128; 178 int const max_gname = 128;
182 char gname[max_gname]; 179 char gname[max_gname];
212 } 209 }
213 } 210 }
214 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height); 211 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height);
215 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height); 212 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height);
216 } 213 }
217 if (error) WARNING("Set_Pixel_Sizes failed."); 214 if (error) WARNING("FT_Set_Pixel_Sizes failed.");
218 215
219 216
220 if (FT_IS_FIXED_WIDTH(face)) 217 if (FT_IS_FIXED_WIDTH(face))
221 WARNING("Selected font is fixed-width."); 218 WARNING("Selected font is fixed-width.");
222 219
223 220
224 /* compute space advance */ 221 /* compute space advance */
225 error = FT_Load_Char(face, ' ', load_flags); 222 error = FT_Load_Char(face, ' ', load_flags);
226 if (error) WARNING("spacewidth set to default."); 223 if (error) WARNING("spacewidth set to default.");
227 else space_advance = f266toInt(face->glyph->advance.x); 224 else space_advance = f266ToInt(face->glyph->advance.x);
228 225
229 226
230 /* create font.desc */ 227 /* create font.desc */
231 f = fopen(font_desc, append_mode ? "a":"w"); 228 f = fopen(font_desc, append_mode ? "a":"w");
232 if (f==NULL) ERROR("fopen failed."); 229 if (f==NULL) ERROR("fopen failed.");
230
233 231
234 /* print font.desc header */ 232 /* print font.desc header */
235 if (append_mode) { 233 if (append_mode) {
236 fprintf(f, "\n\n# Subtitle font for %s encoding, face \"%s%s%s\", ppem=%i\n", 234 fprintf(f, "\n\n# Subtitle font for %s encoding, face \"%s%s%s\", ppem=%i\n",
237 encoding_name, 235 encoding_name,
244 fprintf(f, "name 'Subtitle font for %s encoding, face \"%s%s%s\", ppem=%i'\n", 242 fprintf(f, "name 'Subtitle font for %s encoding, face \"%s%s%s\", ppem=%i'\n",
245 encoding_name, 243 encoding_name,
246 face->family_name, 244 face->family_name,
247 face->style_name ? " ":"", face->style_name ? face->style_name:"", 245 face->style_name ? " ":"", face->style_name ? face->style_name:"",
248 ppem); 246 ppem);
247 #ifdef NEW_DESC
248 fprintf(f, "descversion 2\n");
249 #else
249 fprintf(f, "descversion 1\n"); 250 fprintf(f, "descversion 1\n");
251 #endif
250 fprintf(f, "spacewidth %i\n", 2*padding + space_advance); 252 fprintf(f, "spacewidth %i\n", 2*padding + space_advance);
253 #ifndef NEW_DESC
251 fprintf(f, "charspace %i\n", -2*padding); 254 fprintf(f, "charspace %i\n", -2*padding);
252 fprintf(f, "height %i\n", f266toInt(face->size->metrics.height)); 255 #endif
256 fprintf(f, "height %i\n", f266ToInt(face->size->metrics.height));
257 #ifdef NEW_DESC
258 fprintf(f, "ascender %i\n", f266CeilToInt(face->size->metrics.ascender));
259 fprintf(f, "descender %i\n", f266FloorToInt(face->size->metrics.descender));
260 #endif
253 } 261 }
254 fprintf(f, "\n[files]\n"); 262 fprintf(f, "\n[files]\n");
255 fprintf(f, "alpha %s-a.raw\n", encoding_name); 263 fprintf(f, "alpha %s-a.raw\n", encoding_name);
256 fprintf(f, "bitmap %s-b.raw\n", encoding_name); 264 fprintf(f, "bitmap %s-b.raw\n", encoding_name);
257 fprintf(f, "\n[characters]\n"); 265 fprintf(f, "\n[characters]\n");
258 266
259 267
260 /* compute bbox and [characters] section*/ 268 // render glyphs, compute bitmap size and [characters] section
261 pen_x = 0; 269 glyphs = (FT_Glyph*)malloc(charset_size*sizeof(FT_Glyph*));
262 pen_y = 0;
263 ymin = INT_MAX;
264 ymax = INT_MIN;
265 for (i= 0; i<charset_size; ++i) { 270 for (i= 0; i<charset_size; ++i) {
266 FT_UInt glyph_index; 271 FT_GlyphSlot slot;
267 FT_BBox bbox; 272 FT_ULong character, code;
273 FT_UInt glyph_index;
274 FT_BBox bbox;
275
268 character = charset[i]; 276 character = charset[i];
269 code = charcodes[i]; 277 code = charcodes[i];
270 278
279 // get glyph index
271 if (character==0) 280 if (character==0)
272 glyph_index = 0; 281 glyph_index = 0;
273 else { 282 else {
274 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code); 283 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code);
275 if (glyph_index==0) { 284 if (glyph_index==0) {
277 code<' '||code>255 ? '.':code); 286 code<' '||code>255 ? '.':code);
278 continue; 287 continue;
279 } 288 }
280 } 289 }
281 290
291 // load glyph
282 error = FT_Load_Glyph(face, glyph_index, load_flags); 292 error = FT_Load_Glyph(face, glyph_index, load_flags);
283 if (error) { 293 if (error) {
284 WARNING("Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); 294 WARNING("FT_Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
285 continue; 295 continue;
286 } 296 }
287
288 slot = face->glyph; 297 slot = face->glyph;
289 error = FT_Get_Glyph(slot, &glyph); 298
290 299 // render glyph
291 300 if (slot->format != ft_glyph_format_bitmap) {
292 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_pixels, &bbox); 301 error = FT_Render_Glyph(slot, ft_render_mode_normal);
293 if (pen_y+bbox.yMax>ymax) { 302 if (error) {
294 ymax = pen_y+bbox.yMax; 303 WARNING("FT_Render_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
295 // eprintf("%3i: ymax %i (%c)\n", code, ymax, code); 304 continue;
296 } 305 }
297 if (pen_y+bbox.yMin<ymin) { 306 }
298 ymin = pen_y+bbox.yMin; 307
299 // eprintf("%3i: ymin %i (%c)\n", code, ymin, code); 308 // extract glyph image
309 error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph);
310 if (error) {
311 WARNING("FT_Get_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
312 continue;
313 }
314 glyphs[glyphs_count++] = (FT_Glyph)glyph;
315
316 #ifdef NEW_DESC
317 // max height
318 if (glyph->bitmap.rows > height) height = glyph->bitmap.rows;
319
320 // advance pen
321 pen_xa = pen_x + glyph->bitmap.width + 2*padding;
322
323 // font.desc
324 fprintf(f, "0x%02x %i %i %i %i %i %i;\tU+%04X|%c\n", code,
325 pen_x, // bitmap start
326 glyph->bitmap.width + 2*padding, // bitmap width
327 glyph->bitmap.rows + 2*padding, // bitmap height
328 glyph->left - padding, // left bearing
329 glyph->top + padding, // top bearing
330 f266ToInt(slot->advance.x), // advance
331 character, code<' '||code>255 ? '.':code);
332 #else
333 // max height
334 if (glyph->top > ymax) {
335 ymax = glyph->top;
336 //eprintf("%3i: ymax %i (%c)\n", code, ymax, code);
337 }
338 if (glyph->top - glyph->bitmap.rows < ymin) {
339 ymin = glyph->top - glyph->bitmap.rows;
340 //eprintf("%3i: ymin %i (%c)\n", code, ymin, code);
300 } 341 }
301 342
302 /* advance pen */ 343 /* advance pen */
303 pen_xa = pen_x + f266toInt(slot->advance.x) + 2*padding; 344 pen_xa = pen_x + f266ToInt(slot->advance.x) + 2*padding;
304 // pen_y += f266toInt(slot->advance.y); // for vertical layout
305 345
306 /* font.desc */ 346 /* font.desc */
307 fprintf(f, "0x%02x %i %i;\tU+%04X|%c\n", code, pen_x, pen_xa-1, character, code<' '||code>255 ? '.':code); 347 fprintf(f, "0x%02x %i %i;\tU+%04X|%c\n", code, pen_x, pen_xa-1, character, code<' '||code>255 ? '.':code);
348 #endif
308 pen_x = ALIGN(pen_xa); 349 pen_x = ALIGN(pen_xa);
309 350 }
310 } 351
311 352
312 fclose(f); 353 width = pen_x;
313 354 pen_x = 0;
355 #ifdef NEW_DESC
356 if (height<=0) ERROR("Something went wrong. Use the source!");
357 height += 2*padding;
358 #else
314 if (ymax<=ymin) ERROR("Something went wrong. Use the source!"); 359 if (ymax<=ymin) ERROR("Something went wrong. Use the source!");
315
316
317 width = pen_x;
318 height = ymax - ymin + 2*padding; 360 height = ymax - ymin + 2*padding;
319 baseline = ymax + padding; 361 baseline = ymax + padding;
320 eprintf("bitmap size: %ix%i\n", width, height); 362 #endif
321 363
322 buffer = (unsigned char*)malloc(width*height); 364 // end of font.desc
323 abuffer = (unsigned char*)malloc(width*height); 365 if (DEBUG) eprintf("bitmap size: %ix%i\n", width, height);
324 if (buffer==NULL || abuffer==NULL) ERROR("malloc failed."); 366 fprintf(f, "# bitmap size: %ix%i\n", width, height);
325 367 fclose(f);
326 memset(buffer, 0, width*height); 368
327 369 bbuffer = (unsigned char*)malloc(width*height);
328 370 if (bbuffer==NULL) ERROR("malloc failed.");
329 /* render glyphs */ 371 memset(bbuffer, 0, width*height);
330 pen_x = 0; 372
331 pen_y = baseline; 373
332 for (i= 0; i<charset_size; ++i) { 374 /* paste glyphs */
333 FT_UInt glyph_index; 375 for (i= 0; i<glyphs_count; ++i) {
334 character = charset[i]; 376 glyph = (FT_BitmapGlyph)glyphs[i];
335 code = charcodes[i]; 377 #ifdef NEW_DESC
336 378 paste_bitmap(&glyph->bitmap,
337 if (character==0) 379 pen_x + padding,
338 glyph_index = 0; 380 padding);
339 else {
340 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code);
341 if (glyph_index==0)
342 continue;
343 }
344
345 error = FT_Load_Glyph(face, glyph_index, load_flags);
346 if (error) {
347 // WARNING("Load_Glyph failed.");
348 continue;
349 }
350
351 error = FT_Render_Glyph(face->glyph, ft_render_mode_normal);
352 if (error) WARNING("Render_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
353
354 slot = face->glyph;
355
356 paste_bitmap(&slot->bitmap,
357 pen_x + padding + slot->bitmap_left,
358 pen_y - slot->bitmap_top );
359 381
360 /* advance pen */ 382 /* advance pen */
361 pen_x += f266toInt(slot->advance.x) + 2*padding; 383 pen_x += glyph->bitmap.width + 2*padding;
362 // pen_y += f266toInt(slot->advance.y); // for vertical layout 384 #else
385 paste_bitmap(&glyph->bitmap,
386 pen_x + padding + glyph->left,
387 baseline - glyph->top);
388
389 /* advance pen */
390 pen_x += f1616ToInt(glyph->root.advance.x) + 2*padding;
391 #endif
363 pen_x = ALIGN(pen_x); 392 pen_x = ALIGN(pen_x);
364 } 393
394 FT_Done_Glyph((FT_Glyph)glyph);
395 }
396 free(glyphs);
365 397
366 398
367 error = FT_Done_FreeType(library); 399 error = FT_Done_FreeType(library);
368 if (error) ERROR("Done_FreeType failed."); 400 if (error) ERROR("FT_Done_FreeType failed.");
369 } 401 }
370 402
371 403
372 /* decode from 'encoding' to unicode */ 404 /* decode from 'encoding' to unicode */
373 FT_ULong decode_char(char c) { 405 FT_ULong decode_char(char c) {
510 } 542 }
511 for (x = 0; x<width; ++x, ++s, ++t) *t = *s; 543 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
512 } 544 }
513 545
514 546
515 // brute-force gaussian blur 547 // gaussian blur
516 void blur( 548 void blur(
517 unsigned char *s, 549 unsigned char *buffer,
518 unsigned char *t, 550 unsigned char *tmp,
519 int width, 551 int width,
520 int height, 552 int height,
521 int *m, 553 int *m,
522 int r, 554 int r,
523 int mwidth, 555 int mwidth,
524 unsigned volume) { 556 unsigned volume) {
525 557
526 int x, y; 558 int x, y;
527 559
560 unsigned char *s = buffer - r;
561 unsigned char *t = tmp;
528 for (y = 0; y<height; ++y) { 562 for (y = 0; y<height; ++y) {
529 for (x = 0; x<width; ++x, ++s, ++t) { 563 for (x = 0; x<width; ++x, ++s, ++t) {
530 unsigned sum = 0; 564 unsigned sum = 0;
531 unsigned *mrow = m + r; 565 int x1 = (x<r) ? r-x:0;
532 unsigned char *srow = s -r*width; 566 int x2 = (x+r>=width) ? (r+width-x):mwidth;
533 int x1=(x<r)?-x:-r; 567 int mx;
534 int x2=(x+r>=width)?(width-x-1):r; 568 for (mx = x1; mx<x2; ++mx)
569 sum+= s[mx] * m[mx];
570 *t = (sum + volume/2) / volume;
571 //*t = sum;
572 }
573 }
574 tmp -= r*width;
575 for (x = 0; x<width; ++x, ++tmp, ++buffer) {
576 s = tmp;
577 t = buffer;
578 for (y = 0; y<height; ++y, s+= width, t+= width) {
579 unsigned sum = 0;
580 int y1 = (y<r) ? r-y:0;
581 int y2 = (y+r>=height) ? (r+height-y):mwidth;
582 unsigned char *smy = s + y1*width;
535 int my; 583 int my;
536 584 for (my = y1; my<y2; ++my, smy+= width)
537 for (my = -r; my<=r; ++my, srow+= width, mrow+= mwidth) { 585 sum+= *smy * m[my];
538 int mx;
539 if (y+my < 0) continue;
540 if (y+my >= height) break;
541
542 for (mx = x1; mx<=x2; ++mx)
543 sum+= srow[mx] * mrow[mx];
544
545 }
546 *t = (sum + volume/2) / volume; 586 *t = (sum + volume/2) / volume;
547 } 587 }
548 } 588 }
589 }
590
591
592 // Gaussian matrix
593 // Maybe for future use.
594 unsigned gmatrix(unsigned *m, int r, int w, double const A) {
595 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
596 int mx, my;
597
598 for (my = 0; my<w; ++my) {
599 for (mx = 0; mx<w; ++mx) {
600 m[mx+my*w] = (unsigned)(exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * base + .5);
601 volume+= m[mx+my*w];
602 if (DEBUG) eprintf("%3i ", m[mx+my*w]);
603 }
604 if (DEBUG) eprintf("\n");
605 }
606 if (DEBUG) {
607 eprintf("A= %f\n", A);
608 eprintf("volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A));
609 }
610 return volume;
549 } 611 }
550 612
551 613
552 void alpha() { 614 void alpha() {
553 int const g_r = ceil(radius); 615 int const g_r = ceil(radius);
554 int const o_r = ceil(thickness); 616 int const o_r = ceil(thickness);
555 int const g_w = 2*g_r+1; // matrix size 617 int const g_w = 2*g_r+1; // matrix size
556 int const o_w = 2*o_r+1; // matrix size 618 int const o_w = 2*o_r+1; // matrix size
557 double const A = log(1.0/base)/(radius*radius*2); 619 double const A = log(1.0/base)/(radius*radius*2);
558 620
559 int mx, my; 621 int mx, my, i;
560 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A 622 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
561 623
562 unsigned *gm = (unsigned*)malloc(g_w*g_w * sizeof(unsigned)); 624 unsigned *g = (unsigned*)malloc(g_w * sizeof(unsigned));
563 unsigned *om = (unsigned*)malloc(o_w*o_w * sizeof(unsigned)); 625 unsigned *om = (unsigned*)malloc(o_w*o_w * sizeof(unsigned));
564 unsigned char *tbuffer = (unsigned char*)malloc(width*height); 626 if (g==NULL || om==NULL) ERROR("malloc failed.");
565 if (gm==NULL || om==NULL || tbuffer==NULL) ERROR("malloc failed."); 627
566 628 // gaussian curve
567 629 for (i = 0; i<g_w; ++i) {
568 /* Gaussian matrix */ 630 g[i] = (unsigned)(exp(A * (i-g_r)*(i-g_r)) * base + .5);
569 for (my = 0; my<g_w; ++my) { 631 volume+= g[i];
570 for (mx = 0; mx<g_w; ++mx) { 632 if (DEBUG) eprintf("%3i ", g[i]);
571 gm[mx+my*g_w] = (unsigned)(exp(A * ((mx-g_r)*(mx-g_r)+(my-g_r)*(my-g_r))) * base + .5); 633 }
572 volume+= gm[mx+my*g_w]; 634 //volume *= volume;
573 if (DEBUG) eprintf("%3i ", gm[mx+my*g_w]); 635 if (DEBUG) eprintf("\n");
574 }
575 if (DEBUG) eprintf("\n");
576 }
577 if (DEBUG) {
578 eprintf("A= %f\n", A);
579 eprintf("volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A));
580 }
581
582 636
583 /* outline matrix */ 637 /* outline matrix */
584 for (my = 0; my<o_w; ++my) { 638 for (my = 0; my<o_w; ++my) {
585 for (mx = 0; mx<o_w; ++mx) { 639 for (mx = 0; mx<o_w; ++mx) {
586 // antialiased circle would be perfect here, but this one is good enough 640 // antialiased circle would be perfect here, but this one is good enough
592 } 646 }
593 if (DEBUG) eprintf("\n"); 647 if (DEBUG) eprintf("\n");
594 648
595 649
596 if(thickness==1.0) 650 if(thickness==1.0)
597 outline1(buffer, tbuffer, width, height); // FAST solid 1 pixel outline 651 outline1(bbuffer, abuffer, width, height); // FAST solid 1 pixel outline
598 else 652 else
599 outline(buffer, tbuffer, width, height, om, o_r, o_w); // solid outline 653 outline(bbuffer, abuffer, width, height, om, o_r, o_w); // solid outline
600 654
601 //outline(buffer, tbuffer, width, height, gm, g_r, g_w); // Gaussian outline 655 //outline(bbuffer, abuffer, width, height, gm, g_r, g_w); // Gaussian outline
602 656
603 blur(tbuffer, abuffer, width, height, gm, g_r, g_w, volume); 657 blur(abuffer, bbuffer, width, height, g, g_r, g_w, volume);
604 658
605 free(gm); 659 free(g);
606 free(om); 660 free(om);
607 free(tbuffer);
608 } 661 }
609 662
610 663
611 void usage() { 664 void usage() {
612 printf("Usage: %s [--append] [--blur b] [--outline o] encoding ppem font\n", command); 665 printf("Usage: %s [--append] [--blur b] [--outline o] encoding ppem font\n", command);
682 } 735 }
683 736
684 737
685 int main(int argc, char **argv) { 738 int main(int argc, char **argv) {
686 parse_args(argc, argv); 739 parse_args(argc, argv);
740
687 padding = ceil(radius) + ceil(thickness); 741 padding = ceil(radius) + ceil(thickness);
688 742
689 prepare_charset(); 743 prepare_charset();
744
690 render(); 745 render();
746 write_bitmap(bbuffer, 'b');
747
748 abuffer = (unsigned char*)malloc(width*height);
749 if (abuffer==NULL) ERROR("malloc failed.");
691 alpha(); 750 alpha();
692 write_bitmap(); 751 write_bitmap(abuffer, 'a');
693 752
694 free(buffer); 753 free(bbuffer);
695 free(abuffer); 754 free(abuffer);
696 755
697 // fflush(stderr); 756 // fflush(stderr);
698 return 0; 757 return 0;
699 } 758 }