X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=ttf2png.c;h=84455554eb5c403bbe1c4329fa71b2998af9a4c0;hb=77f478639867f1ff138c0fcc8d566eba5a45469b;hp=5e6e351328e30b78f0b641ec867cda66d8313351;hpb=887d2514f9c7e57898cec5d598cd2a3bcd9ce757;p=ttf2png.git diff --git a/ttf2png.c b/ttf2png.c index 5e6e351..8445555 100644 --- a/ttf2png.c +++ b/ttf2png.c @@ -1,6 +1,6 @@ /* ttf2png - True Type Font to PNG converter -Copyright (c) 2004-2008 Mikko Rasa +Copyright (c) 2004-2018 Mikko Rasa, Mikkosoft Productions This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -78,8 +78,8 @@ void sort_and_compact_ranges(Range *, unsigned *); int range_cmp(const void *, const void *); unsigned round_to_pot(unsigned); void *alloc_image_data(size_t, size_t); -int init_font(Font *, FT_Face, const Range *, unsigned, bool, unsigned); -int init_glyphs(Font *, FT_Face, const Range *, bool, unsigned); +int init_font(Font *, FT_Face, const Range *, unsigned, bool, unsigned, unsigned); +int init_glyphs(Font *, FT_Face, const Range *, bool, unsigned, unsigned); int copy_bitmap(const FT_Bitmap *, Image *); unsigned sqrti(unsigned); unsigned find_distance_to_edge(const Image *, int, int, unsigned); @@ -109,6 +109,7 @@ int main(int argc, char **argv) unsigned padding = 1; bool npot = 0; unsigned distfield = 0; + unsigned border = 0; FT_Library freetype; FT_Face face; @@ -127,7 +128,7 @@ int main(int argc, char **argv) return 1; } - while((i = getopt(argc, argv, "r:s:l:c:o:atvh?ed:pim:n:gf:")) != -1) + while((i = getopt(argc, argv, "r:s:l:c:o:atvh?ed:pim:n:gf:b:")) != -1) { switch(i) { @@ -184,6 +185,9 @@ int main(int argc, char **argv) case 'f': distfield = convert_numeric_option('f', 1); break; + case 'b': + border = convert_numeric_option('b', 1); + break; } } if(!strcmp(out_fn, "-")) @@ -220,7 +224,15 @@ int main(int argc, char **argv) printf("Glyphs: %ld\n", face->num_glyphs); } - err = FT_Set_Pixel_Sizes(face, 0, (distfield ? size*distfield : size)); + font.size = size; + if(distfield) + { + if(!border) + border = sqrti(font.size); + size *= distfield; + } + + err = FT_Set_Pixel_Sizes(face, 0, size); if(err) { fprintf(stderr, "Couldn't set size\n"); @@ -237,8 +249,7 @@ int main(int argc, char **argv) else sort_and_compact_ranges(ranges, &n_ranges); - font.size = size; - err = init_font(&font, face, ranges, n_ranges, autohinter, distfield); + err = init_font(&font, face, ranges, n_ranges, autohinter, distfield, border); if(err) return 1; @@ -505,7 +516,7 @@ void *alloc_image_data(size_t a, size_t b) return ptr; } -int init_font(Font *font, FT_Face face, const Range *ranges, unsigned n_ranges, bool autohinter, unsigned distfield) +int init_font(Font *font, FT_Face face, const Range *ranges, unsigned n_ranges, bool autohinter, unsigned distfield, unsigned border) { unsigned i, j; unsigned size = 0; @@ -523,7 +534,7 @@ int init_font(Font *font, FT_Face face, const Range *ranges, unsigned n_ranges, font->n_glyphs = 0; font->glyphs = NULL; for(i=0; i=1) @@ -562,7 +573,7 @@ int init_font(Font *font, FT_Face face, const Range *ranges, unsigned n_ranges, return 0; } -int init_glyphs(Font *font, FT_Face face, const Range *range, bool autohinter, unsigned distfield) +int init_glyphs(Font *font, FT_Face face, const Range *range, bool autohinter, unsigned distfield, unsigned border) { unsigned i, j; unsigned size = font->n_glyphs; @@ -629,11 +640,9 @@ int init_glyphs(Font *font, FT_Face face, const Range *range, bool autohinter, u start from the bottom. */ if(distfield) { - unsigned margin = 3; - - glyph->offset_x -= margin; - glyph->offset_y -= margin; - create_distance_field(bmp, &glyph->image, distfield, margin); + glyph->offset_x -= border; + glyph->offset_y -= border; + create_distance_field(bmp, &glyph->image, distfield, border); } else { @@ -694,15 +703,9 @@ int copy_bitmap(const FT_Bitmap *bmp, Image *image) unsigned sqrti(unsigned num) { - unsigned result = num; - while(result*result>num) - { - unsigned diff = result*result-num; - if(diff0xFFFF ? 0xFFFF : 0x100); + while(result && result*result>=result+num) + result -= (result*result+result-num)/(result*2); return result; } @@ -740,7 +743,7 @@ unsigned find_distance_to_edge(const Image *image, int origin_x, int origin_y, u } } - return sqrti(closest)*0x7F/range; + return sqrti(closest*0x3F01)/range; } int create_distance_field(const FT_Bitmap *bmp, Image *image, unsigned scale, unsigned margin) @@ -1079,7 +1082,7 @@ int save_png(const char *fn, const Image *image, char alpha) png_info *pngi; png_byte **rows; unsigned i; - png_byte *data2 = 0; + png_byte *data2 = NULL; int color; if(!strcmp(fn, "-"))