From cdd17906f6e874823d0d5d160177268824d98272 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Tue, 8 Sep 2015 20:10:17 -0700 Subject: [PATCH] refactor: ui/font:getFace --- frontend/ui/font.lua | 23 ++++++++++++++--------- spec/unit/font_spec.lua | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 spec/unit/font_spec.lua diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index 99b4ef381..b1b9d96f4 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -49,18 +49,17 @@ local Font = { function Font:getFace(font, size) - if not font then - -- default to content font - font = self.cfont - end + -- default to content font + if not font then font = self.cfont end -- original size before scaling by screen DPI local orig_size = size local size = Screen:scaleBySize(size) - local face = self.faces[font..size] + local hash = font..size + local face_obj = self.faces[hash] -- build face if not found - if not face then + if not face_obj then local realname = self.fontmap[font] if not realname then realname = font @@ -71,10 +70,16 @@ function Font:getFace(font, size) DEBUG("#! Font "..font.." ("..realname..") not supported: "..face) return nil end - self.faces[font..size] = face - --DEBUG("getFace, found: "..realname.." size:"..size) + face_obj = { + size = size, + orig_size = orig_size, + ftface = face, + hash = hash + } + self.faces[hash] = face_obj + -- DEBUG("getFace, found: "..realname.." size:"..size) end - return { size = size, orig_size = orig_size, ftface = face, hash = font..size } + return face_obj end function Font:_readList(target, dir) diff --git a/spec/unit/font_spec.lua b/spec/unit/font_spec.lua new file mode 100644 index 000000000..4d6c155bd --- /dev/null +++ b/spec/unit/font_spec.lua @@ -0,0 +1,15 @@ +require("commonrequire") +local DEBUG = require("dbg") +local Font = require("ui/font") + +describe("Font module", function() + local f = nil + it("should get face", function() + f = Font:getFace('cfont', 18) + assert.are_not.equals(f.ftface, nil) + f = Font:getFace('tfont', 16) + assert.are_not.equals(f.ftface, nil) + f = Font:getFace('hfont', 12) + assert.are_not.equals(f.ftface, nil) + end) +end)