Skip to content

Commit

Permalink
add baranoki_fnt_psp for non monospace font
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSizuku committed Dec 1, 2024
1 parent 84376e5 commit 6c02793
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Also, it supports for droping file, save decoded image and show cursor moving in tiles. Futhermore, the window is flexible for changing size and support for zooming in and out with converting the client coordinate to logical coordinate.

The main purpose is for analyzing game font or textures.
The main purpose is for analyzing game font or textures. See [TileDB](https://github.com/YuriSizuku/TileViewer/wiki/Font-Database) and [TexDB](https://github.com/YuriSizuku/TileViewer/wiki/Texture-Database) in detail.

![tile_test1](asset/picture/tile_test1.png)
(example of decoding a 2bpp tile font)
Expand Down
72 changes: 72 additions & 0 deletions asset/plugin/baranoki_fnt_psp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---@diagnostic disable : lowercase-global, missing-fields, undefined-global, duplicate-doc-field, undefined-field

version = "v0.1"
description = "[lua_baranoki_fnt::init] lua plugin to decode BaranoKiniBaranoSaku 4bpp non monospace font"

-- struct declear
---@class fontentry_t -- 10 bytes
---@field wchar string
---@field dispw integer
---@field tilew integer
---@field tileh integer
---@field x integer
---@field y integer
---@field offset integer

-- global declear
g_data = nil --- @type string
g_tilecfg = nil ---@type tilecfg_t
g_fontmap = {}

function decode_pre()
--- get neccessory data for decoding
g_tilecfg = get_tilecfg()
g_data = get_rawdata()

--- set the information for decoding
local bpp, w, count = string.unpack("<I1I1I2", g_data, 1)
g_tilecfg.w, g_tilecfg.h, g_tilecfg.bpp = w, w, bpp
g_tilecfg.nbytes = 1
g_tilecfg.size = count
set_tilecfg(g_tilecfg)

log(string.format("[lua_baranoki_fnt::pre] datasize=%d w=%d h=%d bpp=%d nbytes=%d",
g_data:len(), g_tilecfg.w, g_tilecfg.h, g_tilecfg.bpp, g_tilecfg.nbytes))

-- set other intormations
for i=0, count do
local wchar, dispw, tilew, tileh, x, y, offset = string.unpack("<I2I1I1I1I1I1I3", g_data, 0x24 + i*10 + 1)
g_fontmap[i] = {wchar=wchar, dispw=dispw, tilew=tilew, tileh=tileh, x=x, y=y, offset=offset}
end

return true
end

function decode_pixel(i, x, y)
if(g_fontmap[i]==nil) then return 0 end
local bpp = 4 -- supposed 4bpp
local w, h = g_fontmap[i].tilew, g_fontmap[i].tileh
local x1, y1 = x - g_fontmap[i].x, y - g_fontmap[i].y

if(x1 < 0 or y1 < 0 or x1 >= w or y1 >= h) then return 0 end
local pixeli = y1*w + x1
local offset = g_fontmap[i].offset + pixeli * bpp // 8
if(offset >= g_data:len()) then return 0 end

d = string.byte(g_data, offset + 1)
if(pixeli%2 == 0) then d = (d>>4) & 0xf
else d = d & 0xf end
d = 255 * d // (2<<bpp-1)

return d + (d << 8) + (d << 16) + (255 << 24)
end

function decode_post()
log("[lua_baranoki_fnt::post] decode finished")
set_tilenav({index=0, offset=-1})
set_tilestyle({scale=2, style=0})
g_data = nil
return true
end

log(" ", description, version)
5 changes: 3 additions & 2 deletions asset/plugin/lualib/util_declear.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ function set_tilestyle(style) end -- ca pi
function get_rawsize() end -- c api

-- get tiles bytes, usually used in decode_pre, and then use this to decode pixel
---@param offset integer
---@param size integer
---@param offset? integer
---@param size? integer
---@return string ...
---@option
function get_rawdata(offset, size) end --c api

---@return boolean ...
Expand Down
4 changes: 2 additions & 2 deletions src/core_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool TileSolver::LoadDecoder(wxFileName pluginfile)
size_t textsize = 0;
decoder->sendui(decoder->context, &text, &textsize);
wxtext.Append(text);
if(decoder->msg)
if(decoder->msg && decoder->msg[0])
{
wxLogMessage("[TileSolver::LoadDecoder] %s decoder->sendui msg: \n %s",
m_pluginfile.GetFullName(), decoder->msg);
Expand Down Expand Up @@ -262,7 +262,7 @@ int TileSolver::Decode(struct tilecfg_t *tilecfg, wxFileName pluginfile)
if(!wxGetApp().m_usegui) wxtext = m_plugincfg;
else wxtext = wxGetApp().m_configwindow->GetPlugincfg();
decoder->recvui(decoder->context, wxtext.mb_str(), wxtext.size());
if(decoder->msg)
if(decoder->msg && decoder->msg[0])
{
wxLogMessage("[TileSolver::Decode] %s decoder->recvui msg: \n %s",
m_pluginfile.GetFullName(), decoder->msg);
Expand Down
7 changes: 7 additions & 0 deletions src/plugin_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ PLUGIN_STATUS decode_sendui_lua(void *context, const char **buf, size_t *bufsize
if(!lua_isfunction(L, -1))
{
lua_pop(L, 1);
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
return STATUS_CALLBACKERROR;
}
lua_call(L, 0, 1);
Expand All @@ -334,6 +335,7 @@ PLUGIN_STATUS decode_recvui_lua(void *context, const char *buf, size_t bufsize)
if(!lua_isfunction(L, -1))
{
lua_pop(L, 1);
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
return STATUS_CALLBACKERROR;
}

Expand Down Expand Up @@ -372,6 +374,8 @@ PLUGIN_STATUS decode_recvui_lua(void *context, const char *buf, size_t bufsize)
return res ? STATUS_OK : STATUS_FAIL;

decode_recvui_lua_fail:
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';

cJSON_Delete(root);
return STATUS_FAIL;
}
Expand All @@ -388,6 +392,7 @@ PLUGIN_STATUS decode_pixel_lua(void *context,
if(!lua_isfunction(L, -1))
{
lua_pop(L, 1);
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
return STATUS_CALLBACKERROR;
}
lua_pushinteger(L, pos->i);
Expand Down Expand Up @@ -416,6 +421,7 @@ PLUGIN_STATUS decode_pre_lua(void *context,
if(!lua_isfunction(L, -1))
{
lua_pop(L, 1);
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
return STATUS_CALLBACKERROR;
}
lua_call(L, 0, 1);
Expand All @@ -435,6 +441,7 @@ PLUGIN_STATUS decode_post_lua(void *context,
if(!lua_isfunction(L, -1))
{
lua_pop(L, 1);
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
return STATUS_CALLBACKERROR;
}
lua_call(L, 0, 1);
Expand Down

0 comments on commit 6c02793

Please sign in to comment.