Skip to content

Commit

Permalink
feat: new font api in ginga
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Nov 25, 2024
1 parent 9f1dacd commit d981bbc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 45 deletions.
43 changes: 0 additions & 43 deletions ee/engine/core/ginga/draw.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
local math = require('math')
local util_decorator = require('src/lib/util/decorator')

local deafult_font_name = 'Tiresias'
local current_font_name = deafult_font_name
local current_font_size = 8

local function color(std, engine, canvas, tint)
local c = tint
local R = math.floor(c/0x1000000)
Expand All @@ -29,37 +25,6 @@ local function rect(std, engine, canvas, mode, pos_x, pos_y, width, height)
canvas:drawRect(mode == 0 and 'fill' or 'frame', x, y, width, height)
end

local function text(std, engine, canvas, pos_x, pos_y, text)
if pos_x and pos_y then
local x = engine.offset_x + pos_x
local y = engine.offset_y + pos_y
canvas:drawText(x, y, text)
end
return canvas:measureText(text or pos_x)
end

local function tui_text(std, engine, canvas, pos_x, pos_y, size, text)
local hem = engine.current.data.width / 80
local vem = engine.current.data.height / 24
local x = engine.offset_x + (pos_x * hem)
local y = engine.offset_y + (pos_y * vem)
local font_size = hem * size

canvas:attrFont(current_font_name, font_size)
canvas:drawText(x, y, text)
canvas:attrFont(current_font_name, current_font_size)
end

local function font(std, engine, canvas, name, size)
if type(name) == 'number' and not size then
size = name
name = current_font_name
end
current_font_name = name
current_font_size = size
canvas:attrFont(name, size)
end

local function line(std, engine, canvas, x1, y1, x2, y2)
local ox = engine.offset_x
local oy = engine.offset_y
Expand All @@ -82,21 +47,13 @@ local function image(std, engine, canvas, src, pos_x, pos_y)
end

local function install(std, engine)
std = std or {}
std.draw = std.draw or {}

std.draw.image = util_decorator.prefix3(std, engine, engine.canvas, image)
std.draw.clear = util_decorator.prefix3(std, engine, engine.canvas, clear)
std.draw.color = util_decorator.prefix3(std, engine, engine.canvas, color)
std.draw.rect = util_decorator.prefix3(std, engine, engine.canvas, rect)
std.draw.text = util_decorator.prefix3(std, engine, engine.canvas, text)
std.draw.font = util_decorator.prefix3(std, engine, engine.canvas, font)
std.draw.line = util_decorator.prefix3(std, engine, engine.canvas, line)
std.text.put = util_decorator.prefix3(std, engine, engine.canvas, tui_text)

return {
draw=std.draw
}
end

local P = {
Expand Down
8 changes: 8 additions & 0 deletions ee/engine/core/ginga/main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local zeebo_module = require('src/lib/common/module')
--
local core_draw = require('ee/engine/core/ginga/draw')
local core_text = require('ee/engine/core/ginga/text')
local core_keys = require('ee/engine/core/ginga/keys')
--
local engine_encoder = require('src/lib/engine/api/encoder')
Expand All @@ -12,6 +13,7 @@ local engine_keys = require('src/lib/engine/api/key')
local engine_math = require('src/lib/engine/api/math')
local engine_draw_ui = require('src/lib/engine/draw/ui')
local engine_draw_fps = require('src/lib/engine/draw/fps')
local engine_draw_text = require('src/lib/engine/draw/text')
local engine_draw_poly = require('src/lib/engine/draw/poly')
local engine_bus = require('src/lib/engine/raw/bus')
local engine_fps = require('src/lib/engine/raw/fps')
Expand Down Expand Up @@ -68,6 +70,10 @@ local cfg_fps_control = {
uptime=event.uptime
}

local cfg_text = {
font_previous = core_text.font_previous
}

local system_language = function()
return 'pt-BR'
end
Expand Down Expand Up @@ -107,6 +113,8 @@ local function install(evt, gamefile)
:package('@keys1', engine_keys)
:package('@keys2', core_keys)
:package('@draw', core_draw)
:package('@draw.text', core_text)
:package('@draw.text2', engine_draw_text, cfg_text)
:package('@draw.ui', engine_draw_ui)
:package('@draw.fps', engine_draw_fps)
:package('@draw.poly', engine_draw_poly, cfg_poly)
Expand Down
63 changes: 63 additions & 0 deletions ee/engine/core/ginga/text.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local util_decorator = require('src/lib/util/decorator')

local deafult_font_name = 'Tiresias'
local previous_font_name = deafult_font_name
local previous_font_size = 9
local current_font_name = deafult_font_name
local current_font_size = 8

local function apply_font()
previous_font_name = current_font_name
previous_font_size = current_font_size
canvas:attrFont(current_font_name, current_font_size)
end

local function text_print(std, engine, canvas, pos_x, pos_y, text)
if previous_font_name ~= current_font_name or previous_font_size ~= current_font_size then
apply_font()
end

local x = engine.offset_x + pos_x
local y = engine.offset_y + pos_y
canvas:drawText(x, y, text)
end

local function text_mensure(canvas, text)
apply_font()
local w, h = canvas:measureText(text)
return w, h
end

local function font_size(std, engine, size)
current_font_size = size
end

local function font_name(std, engine, name)
current_font_name = name
end

local function font_default(std, engine, font_id)
current_font_name = deafult_font_name
end

local function font_previous()
current_font_name = previous_font_name
current_font_size = previous_font_size
canvas:attrFont(current_font_name, current_font_size)
end

local function install(std, engine)
std.text = std.text or {}
std.text.print = util_decorator.prefix3(std, engine, engine.canvas, text_print)
std.text.font_size = util_decorator.prefix2(std, engine, font_size)
std.text.font_name = util_decorator.prefix2(std, engine, font_name)
std.text.font_default = util_decorator.prefix2(std, engine, font_default)
std.text.mensure = util_decorator.prefix1(canvas, text_mensure)
end

local P = {
install=install,
font_previous=font_previous
}

return P
1 change: 0 additions & 1 deletion src/engine/core/love/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ end

local function install(std, engine)
std.text = std.text or {}
std.text.put = util_decorator.prefix2(std, engine, text_put)
std.text.print = util_decorator.prefix2(std, engine, text_print)
std.text.font_size = util_decorator.prefix2(std, engine, font_size)
std.text.font_name = util_decorator.prefix2(std, engine, function() end)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/engine/draw/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ local util_decorator = require('src/lib/util/decorator')

--! @short std.text.put
--! @renamefunc put
--! @hideparam std
--! @hideparam engine
--! @hideparam font_previous
--! @brief print text grid in based 80x24
--! @par Example
--! @code{.java}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/object/std.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local P = {
mensure = function() end,
font_size = function() end,
font_name = function() end,
font_defaullt = function() end
font_default = function() end
},
app = {
width = 1280,
Expand Down

0 comments on commit d981bbc

Please sign in to comment.