From 24ed02bbee76b481480e706cda74ec2feafb0e2c Mon Sep 17 00:00:00 2001 From: chrox Date: Wed, 22 Jan 2014 17:18:20 +0800 Subject: [PATCH] add bold property for text rendering --- frontend/ui/rendertext.lua | 20 ++++++++++---------- frontend/ui/widget/fixedtextwidget.lua | 6 +++--- frontend/ui/widget/textboxwidget.lua | 6 +++--- frontend/ui/widget/textwidget.lua | 9 +++++---- koreader-base | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/frontend/ui/rendertext.lua b/frontend/ui/rendertext.lua index 94c058f9c..d93c66e38 100644 --- a/frontend/ui/rendertext.lua +++ b/frontend/ui/rendertext.lua @@ -61,22 +61,22 @@ local function utf8Chars(input) return read_next_glyph, input, 1 end -function RenderText:getGlyph(face, charcode, bgcolor, fgcolor) +function RenderText:getGlyph(face, charcode, bold, bgcolor, fgcolor) if bgcolor == nil then bgcolor = 0.0 end if fgcolor == nil then fgcolor = 1.0 end - local hash = "glyph|"..face.hash.."|"..charcode.."|"..bgcolor.."|"..fgcolor + local hash = "glyph|"..face.hash.."|"..charcode.."|"..(bold and 1 or 0).."|"..bgcolor.."|"..fgcolor local glyph = GlyphCache:check(hash) if glyph then -- cache hit return glyph[1] end - local rendered_glyph = face.ftface:renderGlyph(charcode, bgcolor, fgcolor) + local rendered_glyph = face.ftface:renderGlyph(charcode, bgcolor, fgcolor, bold) if face.ftface:checkGlyph(charcode) == 0 then for index, font in pairs(Font.fallbacks) do -- rescale face size by DPI since it will be scaled in getFace again local fb_face = Font:getFace(font, Screen:rescaleByDPI(face.size)) if fb_face.ftface:checkGlyph(charcode) ~= 0 then - rendered_glyph = fb_face.ftface:renderGlyph(charcode, bgcolor, fgcolor) + rendered_glyph = fb_face.ftface:renderGlyph(charcode, bgcolor, fgcolor, bold) --DEBUG("fallback to font", font) break end @@ -92,13 +92,13 @@ function RenderText:getGlyph(face, charcode, bgcolor, fgcolor) return rendered_glyph end -function RenderText:getSubTextByWidth(text, face, width, kerning) +function RenderText:getSubTextByWidth(text, face, width, kerning, bold) local pen_x = 0 local prevcharcode = 0 local char_list = {} for _, charcode, uchar in utf8Chars(text) do if pen_x < width then - local glyph = self:getGlyph(face, charcode) + local glyph = self:getGlyph(face, charcode, bold) if kerning and prevcharcode then local kern = face.ftface:getKerning(prevcharcode, charcode) pen_x = pen_x + kern @@ -115,7 +115,7 @@ function RenderText:getSubTextByWidth(text, face, width, kerning) return table.concat(char_list) end -function RenderText:sizeUtf8Text(x, width, face, text, kerning) +function RenderText:sizeUtf8Text(x, width, face, text, kerning, bold) if not text then DEBUG("sizeUtf8Text called without text"); return @@ -129,7 +129,7 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning) local prevcharcode = 0 for _, charcode, uchar in utf8Chars(text) do if pen_x < (width - x) then - local glyph = self:getGlyph(face, charcode) + local glyph = self:getGlyph(face, charcode, bold) if kerning and (prevcharcode ~= 0) then pen_x = pen_x + (face.ftface):getKerning(prevcharcode, charcode) end @@ -143,7 +143,7 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning) return { x = pen_x, y_top = pen_y_top, y_bottom = pen_y_bottom} end -function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bgcolor, fgcolor, width) +function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bold, bgcolor, fgcolor, width) if not text then DEBUG("renderUtf8Text called without text"); return 0 @@ -159,7 +159,7 @@ function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bgcolor, f end for _, charcode, uchar in utf8Chars(text) do if pen_x < text_width then - local glyph = self:getGlyph(face, charcode, bgcolor, fgcolor) + local glyph = self:getGlyph(face, charcode, bold, bgcolor, fgcolor) if kerning and (prevcharcode ~= 0) then pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode) end diff --git a/frontend/ui/widget/fixedtextwidget.lua b/frontend/ui/widget/fixedtextwidget.lua index e5c6e4c71..925dfe0b1 100644 --- a/frontend/ui/widget/fixedtextwidget.lua +++ b/frontend/ui/widget/fixedtextwidget.lua @@ -9,7 +9,7 @@ FixedTextWidget local FixedTextWidget = TextWidget:new{} function FixedTextWidget:getSize() - local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true) + local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold) if not tsize then return Geom:new{} end @@ -22,8 +22,8 @@ function FixedTextWidget:getSize() end function FixedTextWidget:paintTo(bb, x, y) - RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, - true, self.bgcolor, self.fgcolor) + RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, true, self.bold, + self.bgcolor, self.fgcolor) end return FixedTextWidget diff --git a/frontend/ui/widget/textboxwidget.lua b/frontend/ui/widget/textboxwidget.lua index 205f61d32..edf7b6c37 100644 --- a/frontend/ui/widget/textboxwidget.lua +++ b/frontend/ui/widget/textboxwidget.lua @@ -11,6 +11,7 @@ A TextWidget that handles long text wrapping local TextBoxWidget = Widget:new{ text = nil, face = nil, + bold = nil, bgcolor = 0.0, -- [0.0, 1.0] fgcolor = 1.0, -- [0.0, 1.0] width = 400, -- in pixels @@ -115,7 +116,7 @@ function TextBoxWidget:_getVerticalList(alg) for w in word:gsplit("%p+", true) do local word_box = {} word_box.word = w - word_box.width = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, w, true).x + word_box.width = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, w, true, self.bold).x table.insert(h_list, word_box) end end @@ -215,8 +216,7 @@ function TextBoxWidget:_render(v_list) for _,w in ipairs(l) do --@TODO Don't use kerning for monospaced fonts. (houqp) -- refert to cb25029dddc42693cc7aaefbe47e9bd3b7e1a750 in master tree - RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true, - self.bgcolor, self.fgcolor) + RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true, self.bold, self.bgcolor, self.fgcolor) pen_x = pen_x + w.width end y = y + line_height_px + font_height diff --git a/frontend/ui/widget/textwidget.lua b/frontend/ui/widget/textwidget.lua index fa9c0a958..46ece35cb 100644 --- a/frontend/ui/widget/textwidget.lua +++ b/frontend/ui/widget/textwidget.lua @@ -9,6 +9,7 @@ A TextWidget puts a string on a single line local TextWidget = Widget:new{ text = nil, face = nil, + bold = nil, bgcolor = 0.0, -- [0.0, 1.0] fgcolor = 1.0, -- [0.0, 1.0] _bb = nil, @@ -20,7 +21,7 @@ local TextWidget = Widget:new{ --function TextWidget:_render() --local h = self.face.size * 1.3 --self._bb = Blitbuffer.new(self._maxlength, h) - --self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, self.color) + --self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, true, self.bold) --end function TextWidget:getSize() @@ -28,7 +29,7 @@ function TextWidget:getSize() --self:_render() --end --return { w = self._length, h = self._bb:getHeight() } - local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true) + local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold) if not tsize then return Geom:new{} end @@ -46,8 +47,8 @@ function TextWidget:paintTo(bb, x, y) --end --bb:blitFrom(self._bb, x, y, 0, 0, self._length, self._bb:getHeight()) --@TODO Don't use kerning for monospaced fonts. (houqp) - RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text, - true, self.bgcolor, self.fgcolor, self.width) + RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text, true, self.bold, + self.bgcolor, self.fgcolor, self.width) end function TextWidget:free() diff --git a/koreader-base b/koreader-base index 04a517ad2..f35cbdff9 160000 --- a/koreader-base +++ b/koreader-base @@ -1 +1 @@ -Subproject commit 04a517ad2734fb7357924f082f479c63ae4c4f49 +Subproject commit f35cbdff906b265712afffc8831bafe5fbb21f3b