From 865b2a8f90fe81761d123bf023db0c668b2dc305 Mon Sep 17 00:00:00 2001 From: Zijie He Date: Thu, 14 Apr 2016 17:42:54 -0700 Subject: [PATCH] Update ImageWidget to support more stretch settings, and enable screensaver on kobo --- defaults.lua | 2 +- frontend/ui/screensaver.lua | 69 ++++++++++++------------------ frontend/ui/widget/imagewidget.lua | 47 ++++++++++++++++++-- reader.lua | 7 ++- 4 files changed, 77 insertions(+), 48 deletions(-) diff --git a/defaults.lua b/defaults.lua index 8991175ce..f546c9c65 100644 --- a/defaults.lua +++ b/defaults.lua @@ -178,7 +178,7 @@ KOBO_LIGHT_ON_START = -2 -- -1, -2 or 0-100. -1 leaves light as it -- sets light on start/wake up KOBO_SYNC_BRIGHTNESS_WITH_NICKEL = true -- Save brightness set in KOreader -- with nickel's 'Kobo eReader.conf' -KOBO_SCREEN_SAVER = "" -- image or directory with pictures or "-" +KOBO_SCREEN_SAVER = "" -- image or directory with pictures or empty KOBO_SCREEN_SAVER_LAST_BOOK = true -- get screensaver from last book if possible -- Network proxy settings diff --git a/frontend/ui/screensaver.lua b/frontend/ui/screensaver.lua index b219eafa6..ffb79da28 100644 --- a/frontend/ui/screensaver.lua +++ b/frontend/ui/screensaver.lua @@ -25,26 +25,6 @@ function Screensaver:getCoverImage(file) local data = DocSettings:open(lastfile) local proportional_cover = data:readSetting("proportional_screensaver") if image then - if proportional_cover then - image_height = image:getHeight() - image_width = image:getWidth() - local image_ratio = image_width / image_height - if image_ratio < 1 then - image_height = screen_height - image_width = image_height * image_ratio - else - image_width = screen_width - image_height = image_width / image_ratio - end - else - image_height = screen_height - image_width = screen_width - end - local image_widget = ImageWidget:new{ - image = image, - width = image_width, - height = image_height, - } return AlphaContainer:new{ alpha = 1, height = screen_height, @@ -56,7 +36,12 @@ function Screensaver:getCoverImage(file) padding = 0, height = screen_height, width = screen_width, - image_widget + ImageWidget:new{ + image = image, + height = screen_height, + width = screen_width, + overflow = proportional_cover, + } } } } @@ -64,8 +49,23 @@ function Screensaver:getCoverImage(file) end end -function Screensaver:getRandomImage(dir) - local ImageWidget = require("ui/widget/imagewidget") +local function createWidget(file) + if lfs.attributes(file, "mode") == "file" then + local ImageWidget = require("ui/widget/imagewidget") + return ImageWidget:new{ + file = file, + width = Screen:getWidth(), + height = Screen:getHeight(), + overflow = true, + centering = true, + } + end +end + +local function getRandomImage(dir) + if string.sub(dir, string.len(dir)) ~= "/" then + dir = dir .. "/" + end local pics = {} local i = 0 math.randomseed(os.time()) @@ -80,14 +80,7 @@ function Screensaver:getRandomImage(dir) end local image = pics[math.random(i)] if image then - image = dir .. image - if lfs.attributes(image, "mode") == "file" then - return ImageWidget:new{ - file = image, - width = Screen:getWidth(), - height = Screen:getHeight(), - } - end + return createWidget(dir .. image) end end @@ -110,17 +103,9 @@ function Screensaver:show() if type(KOBO_SCREEN_SAVER) == "string" then local file = KOBO_SCREEN_SAVER if lfs.attributes(file, "mode") == "directory" then - if string.sub(file,string.len(file)) ~= "/" then - file = file .. "/" - end - self.suspend_msg = self:getRandomImage(file) - elseif lfs.attributes(file, "mode") == "file" then - local ImageWidget = require("ui/widget/imagewidget") - self.suspend_msg = ImageWidget:new{ - file = file, - width = Screen:getWidth(), - height = Screen:getHeight(), - } + self.suspend_msg = getRandomImage(file) + else + self.suspend_msg = createWidget(file) end end end diff --git a/frontend/ui/widget/imagewidget.lua b/frontend/ui/widget/imagewidget.lua index 5d93fa88a..e2a4efd7f 100644 --- a/frontend/ui/widget/imagewidget.lua +++ b/frontend/ui/widget/imagewidget.lua @@ -39,6 +39,21 @@ local ImageWidget = Widget:new{ autoscale = false, -- when alpha is set to true, alpha values from the image will be honored alpha = false, + -- when autostretch is set to true, image will be stretched to best fit the + -- widget size. i.e. either fit the width or fit the height according to the + -- original image size. + autostretch = false, + -- when overflow is set to true, image will be stretched to fit the widget + -- size vertically and horizontally, without impact original aspect ratio. + -- But overflow part will be ignored. + overflow = false, + -- when nostretch is set to true, image won't be stretched. + nostretch = false, + -- when centering is set to true, image will be placed in the middle of the + -- widget. This setting takes effect only with overflow, autostretch and + -- nostretch. i.e. only when the image size is not consistent with widget + -- size. + centering = false, _bb = nil } @@ -79,15 +94,35 @@ function ImageWidget:_render() error("cannot render image") end local native_w, native_h = self._bb:getWidth(), self._bb:getHeight() - local scaled_w, scaled_h = self.width, self.height + local w, h if self.autoscale then local dpi_scale = Screen:getDPI() / 167 -- rounding off to power of 2 to avoid alias with pow(2, floor(log(x)/log(2)) local scale = math.pow(2, math.max(0, math.floor(math.log(dpi_scale)/0.69))) - scaled_w, scaled_h = scale * native_w, scale * native_h + w, h = scale * native_w, scale * native_h + elseif self.autostretch then + local ratio = native_w / self.width / native_h * self.height + if ratio < 1 then + h = self.height + w = self.width * ratio + else + h = self.height * ratio + w = self.width + end + elseif self.nostretch then + w, h = native_w, native_h + elseif self.overflow then + local ratio = native_w / self.width / native_h * self.height + if ratio < 1 then + h = self.height / ratio + w = self.width + else + h = self.height + w = self.width / ratio + end end - if (scaled_w and scaled_w ~= native_w) or (scaled_h and scaled_h ~= native_h) then - self._bb = self._bb:scale(scaled_w or native_w, scaled_h or native_h) + if (w and w ~= native_w) or (h and h ~= native_h) then + self._bb = self._bb:scale(w or native_w, h or native_h) end end @@ -110,6 +145,10 @@ function ImageWidget:paintTo(bb, x, y) w = size.w, h = size.h } + if self.centering then + x = x - (size.w - self.width) / 2 + y = y - (size.h - self.height) / 2 + end if self.alpha == true then bb:alphablitFrom(self._bb, x, y, 0, 0, size.w, size.h) else diff --git a/reader.lua b/reader.lua index 117dac0c3..f3a64144a 100755 --- a/reader.lua +++ b/reader.lua @@ -97,7 +97,12 @@ local UIManager = require("ui/uimanager") local Device = require("device") local Font = require("ui/font") --- read some global reader setting here: +-- change some global default values according to the device types +if Device:isKobo() then + KOBO_SCREEN_SAVER = "/mnt/onboard/screensaver" +end + +-- read some global reader settings here: -- font local fontmap = G_reader_settings:readSetting("fontmap") if fontmap ~= nil then