From 8d6030819b0aed14bbfc28cf7d9536051571633a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 17 Sep 2015 14:35:09 +0200 Subject: [PATCH] textbox: Honor per-screen DPI This makes the textbox' :draw() and :fit() callbacks use the DPI that is specified in the given drawing context. With this, the textbox now scales correctly if different screens have different DPI values. Idea originally from Daniel. Closes https://github.com/awesomeWM/awesome/pull/457. Signed-off-by: Uli Schlachter --- lib/wibox/widget/textbox.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/wibox/widget/textbox.lua b/lib/wibox/widget/textbox.lua index dfa39e522..fc0d0cfbe 100644 --- a/lib/wibox/widget/textbox.lua +++ b/lib/wibox/widget/textbox.lua @@ -20,16 +20,21 @@ local error = error local textbox = { mt = {} } --- Setup a pango layout for the given textbox and cairo context -local function setup_layout(box, width, height) +local function setup_layout(box, width, height, dpi) local layout = box._layout layout.width = Pango.units_from_double(width) layout.height = Pango.units_from_double(height) + if box.dpi ~= dpi then + box.dpi = dpi + box._ctx:set_resolution(dpi) + box._layout:context_changed() + end end --- Draw the given textbox on the given cairo context in the given geometry function textbox:draw(context, cr, width, height) + setup_layout(self, width, height, context.dpi) cr:update_layout(self._layout) - setup_layout(self, width, height) local ink, logical = self._layout:get_pixel_extents() local offset = 0 if self._valign == "center" then @@ -43,7 +48,7 @@ end --- Fit the given textbox function textbox:fit(context, width, height) - setup_layout(self, width, height) + setup_layout(self, width, height, context.dpi) local ink, logical = self._layout:get_pixel_extents() if logical.width == 0 or logical.height == 0 then @@ -161,9 +166,9 @@ local function new(text, ignore_markup) end end - local ctx = PangoCairo.font_map_get_default():create_context() - ctx:set_resolution(beautiful.xresources.get_dpi()) - ret._layout = Pango.Layout.new(ctx) + ret._dpi = -1 + ret._ctx = PangoCairo.font_map_get_default():create_context() + ret._layout = Pango.Layout.new(ret._ctx) ret:set_ellipsize("end") ret:set_wrap("word_char")