/*
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
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);
unsigned padding = 1;
bool npot = 0;
unsigned distfield = 0;
+ unsigned border = 0;
FT_Library freetype;
FT_Face face;
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)
{
case 'f':
distfield = convert_numeric_option('f', 1);
break;
+ case 'b':
+ border = convert_numeric_option('b', 1);
+ break;
}
}
if(!strcmp(out_fn, "-"))
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");
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;
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;
font->n_glyphs = 0;
font->glyphs = NULL;
for(i=0; i<n_ranges; ++i)
- if(init_glyphs(font, face, &ranges[i], autohinter, distfield))
+ if(init_glyphs(font, face, &ranges[i], autohinter, distfield, border))
return -1;
if(verbose>=1)
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;
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
{
unsigned sqrti(unsigned num)
{
- unsigned result = num;
- while(result*result>num)
- {
- unsigned diff = result*result-num;
- if(diff<result)
- break;
-
- result -= (diff+result)/(result*2);
- }
+ unsigned result = (num>0xFFFF ? 0xFFFF : 0x100);
+ while(result && result*result>=result+num)
+ result -= (result*result+result-num)/(result*2);
return result;
}
}
}
- return sqrti(closest)*0x7F/range;
+ return sqrti(closest*0x3F01)/range;
}
int create_distance_field(const FT_Bitmap *bmp, Image *image, unsigned scale, unsigned margin)
png_info *pngi;
png_byte **rows;
unsigned i;
- png_byte *data2 = 0;
+ png_byte *data2 = NULL;
int color;
if(!strcmp(fn, "-"))