From afa6108e1aaf5d962650650b2c801f006ad04337 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sun, 15 Jan 2023 13:30:09 -0500 Subject: [PATCH] fitFont Working as expected --- gui/init.lua | 49 ++++++++++++++++++++++++------------------------- multi | 2 +- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/gui/init.lua b/gui/init.lua index 798729b..8d13afe 100644 --- a/gui/init.lua +++ b/gui/init.lua @@ -57,6 +57,10 @@ gui.Events.OnTouchMoved = multi:newConnection():fastMode() gui.Events.OnTouchPressed = multi:newConnection():fastMode() gui.Events.OnTouchReleased = multi:newConnection():fastMode() +-- Non Love Events + +gui.Events.OnThemeChanged = multi:newConnection():fastMode() + -- Virtual gui init gui.virtual = {} @@ -675,26 +679,25 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) c.textColor = color.black c.OnFontUpdated = multi:newConnection() - function c:calculateFontOffset(font) + function c:calculateFontOffset(font, adjust) + local adjust = adjust or 20 local x, y, width, height = self:getAbsolutes() - local top = height+10 + local top = height + adjust local bottom = 0 - local canvas = love.graphics.newCanvas(width, height+10) + local canvas = love.graphics.newCanvas(width, height + adjust) love.graphics.setCanvas(canvas) love.graphics.clear( 0, 0, 0, .5, false, false) love.graphics.setColor(1,1,1,1) love.graphics.setFont(font) - love.graphics.printf(self.text, 0, 5, width, "left", self.rotation, self.textScaleX, self.textScaleY, 0, 0, self.textShearingFactorX, self.textShearingFactorY) + love.graphics.printf(self.text, 0, adjust/2, width, "left", self.rotation, self.textScaleX, self.textScaleY, 0, 0, self.textShearingFactorX, self.textShearingFactorY) love.graphics.setCanvas() local data = canvas:newImageData() - testIMG:setDualDim(0, 0, width, height) - testIMG:setImage(data) local f_top, f_bot = false, false for yy = 0, height-1 do for xx = 0, width-1 do local r,g,b,a = data:getPixel(xx,yy) if r~=0 or g~=0 or b~=0 then - if yybottom and not f_bot then bottom = yy f_bot = false end + if yy>bottom and not f_bot then bottom = yy f_bot = false break end end end end - return top-10, bottom-10 + return top - adjust, bottom - adjust end function c:setFont(font,size) @@ -719,8 +722,10 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) self.OnFontUpdated:Fire(self) end - function c:fitFont(n) + function c:fitFont(n, max) + local max = max or math.huge local font + local isdefault = false if self.fontFile then if self.fontFile:match("ttf") then font = function(n) @@ -732,6 +737,7 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) end end else + isdefault = true font = function(n) return love.graphics.setNewFont(n) end @@ -740,25 +746,21 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) local Font, text = self.Font, self.text local s = 3 Font = font(s) - while Font:getHeight()