From 228e15f4a8452e3120e1fca4f5cdcda3bdf8bf82 Mon Sep 17 00:00:00 2001
From: Yan Zhou <zhouyan@me.com>
Date: Thu, 2 Feb 2017 19:50:32 +0800
Subject: use design size to calculate the distance for `inrange`

---
 src/luaotfload-database.lua | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/luaotfload-database.lua b/src/luaotfload-database.lua
index 5e4fd7a..c494318 100644
--- a/src/luaotfload-database.lua
+++ b/src/luaotfload-database.lua
@@ -938,10 +938,19 @@ local choose_size = function (sizes, askedsize)
                 norange [#norange + 1] = { d, index }
             else
                 --- range match
-                local d = ((low + high) / 2) - askedsize
+                local d = 0
+
+                -- should always be true. Just in case there's some
+                -- weried fonts out there
+                if dsnsize > low and dsnsize < high then
+                    d = dsnsize - askedsize
+                else
+                    d = ((low + high) / 2) - askedsize
+                end
                 if d < 0 then
                     d = -d
                 end
+
                 inrange [#inrange + 1] = { d, index }
             end
         end
-- 
cgit v1.2.3