]> git.tdb.fi Git - ttf2png.git/blobdiff - ttf2png.c
Support multiple disjoint code point ranges
[ttf2png.git] / ttf2png.c
index 58b901666a282e4e1aee2805f1d53741a5a10568..2a087ff3ac02eff7acfe403821f6a6704e672f23 100644 (file)
--- a/ttf2png.c
+++ b/ttf2png.c
@@ -61,16 +61,23 @@ typedef struct sFont
        Image image;
 } Font;
 
+typedef struct sRange
+{
+       unsigned first;
+       unsigned last;
+} Range;
+
 typedef int bool;
 
 void usage(void);
 int convert_numeric_option(char, int);
-void convert_code_point_range(char, unsigned *, unsigned *);
+void convert_code_point_range(char, Range *);
 unsigned str_to_code_point(const char *, char **);
 void convert_size(char, unsigned *, unsigned *);
 unsigned round_to_pot(unsigned);
 void *alloc_image_data(size_t, size_t);
-int init_font(Font *, FT_Face, unsigned, unsigned, bool);
+int init_font(Font *, FT_Face, const Range *, unsigned, bool);
+int init_glyphs(Font *, FT_Face, const Range *, bool);
 int render_grid(Font *, unsigned, unsigned, unsigned, bool);
 int render_packed(Font *, unsigned, unsigned);
 int save_defs(const char *, const Font *);
@@ -81,8 +88,8 @@ char verbose = 0;
 int main(int argc, char **argv)
 {
        char *fn;
-       unsigned begin = 0;
-       unsigned end = 255;
+       Range *ranges = NULL;
+       unsigned n_ranges = 0;
        unsigned size = 10;
        unsigned cpl = 0;
        unsigned cellw = 0;
@@ -117,7 +124,8 @@ int main(int argc, char **argv)
                switch(i)
                {
                case 'r':
-                       convert_code_point_range('r', &begin, &end);
+                       ranges = (Range *)realloc(ranges, (++n_ranges)*sizeof(Range));
+                       convert_code_point_range('r', &ranges[n_ranges-1]);
                        break;
                case 's':
                        size = convert_numeric_option('s', 1);
@@ -206,7 +214,7 @@ int main(int argc, char **argv)
        }
 
        font.size = size;
-       err = init_font(&font, face, begin, end, autohinter);
+       err = init_font(&font, face, ranges, n_ranges, autohinter);
        if(err)
                return 1;
 
@@ -288,26 +296,26 @@ int convert_numeric_option(char opt, int min_value)
        return value;
 }
 
-void convert_code_point_range(char opt, unsigned *begin, unsigned *end)
+void convert_code_point_range(char opt, Range *range)
 {
        int value;
        char *ptr;
 
        if(!strcmp(optarg, "all"))
        {
-               *begin = 0;
-               *end = 0x10FFFF;
+               range->first = 0;
+               range->last = 0x10FFFF;
                return;
        }
 
        value = str_to_code_point(optarg, &ptr);
        if(value>0 && *ptr==',')
        {
-               *begin = value;
+               range->first = value;
                value = str_to_code_point(ptr+1, &ptr);
                if(value>0 && !*ptr)
                {
-                       *end = value;
+                       range->last = value;
                        return;
                }
        }
@@ -432,7 +440,7 @@ void *alloc_image_data(size_t a, size_t b)
        return ptr;
 }
 
-int init_font(Font *font, FT_Face face, unsigned first, unsigned last, bool autohinter)
+int init_font(Font *font, FT_Face face, const Range *ranges, unsigned n_ranges, bool autohinter)
 {
        unsigned i, j;
        unsigned size = 0;
@@ -448,7 +456,52 @@ int init_font(Font *font, FT_Face face, unsigned first, unsigned last, bool auto
 
        font->n_glyphs = 0;
        font->glyphs = NULL;
-       for(i=first; i<=last; ++i)
+       for(i=0; i<n_ranges; ++i)
+               if(init_glyphs(font, face, &ranges[i], autohinter))
+                       return -1;
+
+       if(verbose>=1)
+               printf("Loaded %u glyphs\n", font->n_glyphs);
+
+       font->n_kerning = 0;
+       font->kerning = NULL;
+       for(i=0; i<font->n_glyphs; ++i) for(j=0; j<font->n_glyphs; ++j)
+               if(j!=i)
+               {
+                       FT_Vector kerning;
+                       FT_Get_Kerning(face, font->glyphs[i].index, font->glyphs[j].index, FT_KERNING_DEFAULT, &kerning);
+
+                       /* FreeType documentation says that vertical kerning is practically
+                       never used, so we ignore it. */
+                       if(kerning.x)
+                       {
+                               Kerning *kern;
+
+                               if(font->n_kerning>=size)
+                               {
+                                       size += 16;
+                                       font->kerning = (Kerning *)realloc(font->kerning, size*sizeof(Kerning));
+                               }
+
+                               kern = &font->kerning[font->n_kerning++];
+                               kern->left_code = font->glyphs[i].code;
+                               kern->right_code = font->glyphs[j].code;
+                               kern->distance = kerning.x/64;
+                       }
+               }
+
+       if(verbose>=1)
+               printf("Loaded %d kerning pairs\n", font->n_kerning);
+
+       return 0;
+}
+
+int init_glyphs(Font *font, FT_Face face, const Range *range, bool autohinter)
+{
+       unsigned i, j;
+       unsigned size = font->n_glyphs;
+
+       for(i=range->first; i<=range->last; ++i)
        {
                unsigned n;
                FT_Bitmap *bmp = &face->glyph->bitmap;
@@ -528,40 +581,6 @@ int init_font(Font *font, FT_Face face, unsigned first, unsigned last, bool auto
                }
        }
 
-       if(verbose>=1)
-               printf("Loaded %u glyphs\n", font->n_glyphs);
-
-       size = 0;
-       font->n_kerning = 0;
-       font->kerning = NULL;
-       for(i=0; i<font->n_glyphs; ++i) for(j=0; j<font->n_glyphs; ++j)
-               if(j!=i)
-               {
-                       FT_Vector kerning;
-                       FT_Get_Kerning(face, font->glyphs[i].index, font->glyphs[j].index, FT_KERNING_DEFAULT, &kerning);
-
-                       /* FreeType documentation says that vertical kerning is practically
-                       never used, so we ignore it. */
-                       if(kerning.x)
-                       {
-                               Kerning *kern;
-
-                               if(font->n_kerning>=size)
-                               {
-                                       size += 16;
-                                       font->kerning = (Kerning *)realloc(font->kerning, size*sizeof(Kerning));
-                               }
-
-                               kern = &font->kerning[font->n_kerning++];
-                               kern->left_code = font->glyphs[i].code;
-                               kern->right_code = font->glyphs[j].code;
-                               kern->distance = kerning.x/64;
-                       }
-               }
-
-       if(verbose>=1)
-               printf("Loaded %d kerning pairs\n", font->n_kerning);
-
        return 0;
 }