]> git.tdb.fi Git - ttf2png.git/commitdiff
Improve distance field accuracy
authorMikko Rasa <tdb@tdb.fi>
Fri, 4 May 2018 19:15:07 +0000 (22:15 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 4 May 2018 19:15:07 +0000 (22:15 +0300)
A better initial guess allows the square root function to deal with
larger numbers.

ttf2png.c

index 5e6e351328e30b78f0b641ec867cda66d8313351..bf9f8d2c728a78f41032078661b88de8960c7f46 100644 (file)
--- a/ttf2png.c
+++ b/ttf2png.c
@@ -694,15 +694,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(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;
 }
@@ -740,7 +734,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)