]> git.tdb.fi Git - ttf2png.git/commitdiff
Support multiple disjoint code point ranges
authorMikko Rasa <tdb@tdb.fi>
Sat, 14 Apr 2018 13:45:10 +0000 (16:45 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 14 Apr 2018 13:46:03 +0000 (16:46 +0300)
Readme
ttf2png.c

diff --git a/Readme b/Readme
index 7ce6e70badfe2a479c7b87b2b84d548b8aa9b659..9d10f6c52f85b599349e028fa45766b9fdb16713 100644 (file)
--- a/Readme
+++ b/Readme
@@ -15,7 +15,8 @@ Command-line options
     Range of code points to convert, specified as unicode code points.  The
     default is 0,255, matching the ISO-8859-1 (Latin-1) character set.  Code
     points can be specified as plain numbers, unicode code points (U+xxxx) or
-    UTF-8 characters.
+    UTF-8 characters.  This option can be specified multiple times to add
+    more ranges to be converted.
 
   -s <pixels>
     Font size to use.  The default is 10 pixels.
@@ -137,6 +138,7 @@ Changelog
 
 next
 - Alternate ways of specifying code point ranges
+- Multiple code point ranges can be specified
 
 1.1
 - Controllable margin and padding in packed mode
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;
 }