sonic-radiance/sonic-radiance.love/core/modules/assets/fonts.lua

154 lines
4.3 KiB
Lua

local Font = Object:extend()
-- Initilizing and configuring option
function Font:new(filename, size)
local filename = filename
self.font = love.graphics.newFont(filename, size)
self.filter = ""
self:setColor(1, 1, 1, 1)
self:setSpacing(false, 0)
self.align = "left"
end
function Font:set()
love.graphics.setFont(self.font)
end
function Font:setColor(r, g, b, a)
self.color = {}
self.color.r = r
self.color.g = g
self.color.b = b
self.color.a = a
end
function Font:setColorFromTable(color)
self.color = color
end
function Font:setSpacing(use_custom, size)
self.spacing = {}
self.spacing.active = use_custom
self.spacing.size = size
end
function Font:setAlign(align)
self.align = align
end
function Font:setFilter(filter)
self.filter = filter
end
function Font:setLineHeight(height)
self.font:setLineHeight(height)
end
-- get information functions
function Font:getHeight()
local font = self.font
return font:getHeight()
end
function Font:getWidth(string)
local spacing = 0
if (self.spacing.active == true) then
local charNumber = string.len(string)
spacing = self.spacing.size * charNumber
end
local width = self.font:getWidth(string) + spacing
return width
end
function Font:getColor()
return self.color
end
-- print functions
function Font:draw(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
-- draw text with color and effect applied
local limit = limit or 0
local align = align or self.align
self:applyFilter(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
love.graphics.setColor(self.color.r, self.color.g, self.color.b, self.color.a)
self:printf(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
end
function Font:print(text, x, y, align, r, sx, sy, ox, oy, kx, ky)
self:set()
if (self.spacing.active) then
utils.draw.printWithSpacing(text, self.spacing.size, align, x, y, r, sx, sy, ox, oy, kx, ky)
else
utils.draw.print(text, align, x, y, r, sx, sy, ox, oy, kx, ky)
end
end
function Font:printf(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
self:set()
if (limit > 0) then
love.graphics.printf(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
else
self:print(text, x, y, align, r, sx, sy, ox, oy, kx, ky)
end
end
-- FILTER SYSTEM
function Font:applyFilter(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
if self.filter == "shadow" then
self:applyFilterShadow(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
elseif self.filter == "border" then
self:applyFilterBorder(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
elseif self.filter == "doubleborder" then
self:applyFilterDoubleBorder(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
end
end
function Font:applyFilterShadow(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
love.graphics.setColor(0, 0, 0, 1)
self:printf(text, x+1, y+1, limit, align, align, r, sx, sy, ox, oy, kx, ky)
utils.draw.resetColor()
end
function Font:applyFilterBorder(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
love.graphics.setColor(0, 0, 0, 1)
self:printf(text, x-1, y-1, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x , y-1, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x+1, y-1, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x+1, y , limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x-1, y , limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x-1, y+1, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x , y+1, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x+1, y+1, limit, align, r, sx, sy, ox, oy, kx, ky)
utils.draw.resetColor()
end
function Font:applyFilterDoubleBorder(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
love.graphics.setColor(0, 0, 0, 1)
self:printf(text, x-2, y-2, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x , y-2, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x+2, y-2, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x+2, y , limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x-2, y , limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x-2, y+2, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x , y+2, limit, align, r, sx, sy, ox, oy, kx, ky)
self:printf(text, x+2, y+2, limit, align, r, sx, sy, ox, oy, kx, ky)
utils.draw.resetColor()
end
return Font