Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Created October 16, 2019 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radgeRayden/dd5ad8404ff115106f8320035257bd53 to your computer and use it in GitHub Desktop.
Save radgeRayden/dd5ad8404ff115106f8320035257bd53 to your computer and use it in GitHub Desktop.
FNT -> Scopes S Expressions converter
local filename = arg[1]
local output =
[[
using import struct
struct FNTCharInfo plain
id : i32
x : i32
y : i32
width : i32
height : i32
xoffset : i32
yoffset : i32
xadvance : i32
channel : i32
let font =
arrayof FNTCharInfo
]]
local function makeCharEntry(info)
local indentation = " "
local result =
indentation ..
"FNTCharInfo " ..
"(id = " .. info.id .. ") " ..
"(x = " .. info.x .. ") " ..
"(y = " .. info.y .. ") " ..
"(width = " .. info.width .. ") " ..
"(height = " .. info.height .. ") " ..
"(xoffset = " .. info.xoffset .. ") " ..
"(yoffset = " .. info.yoffset .. ") " ..
"(xadvance = " .. info.xadvance .. ") " ..
"(channel = " .. info.chnl .. ")"
return result
end
local lpeg = require("lpeg")
local re = require("re")
local pattern =
[[
S <- {| 'char id=' id %s+
'x=' x %s+
'y=' y %s+
'width=' width %s+
'height=' height %s+
'xoffset=' xoffset %s+
'yoffset=' yoffset %s+
'xadvance=' xadvance %s+
'page=' page %s+
'chnl=' chnl |}
si32 <- '-'? %d+
id <- {:id: si32 :}
x <- {:x: si32 :}
y <- {:y: si32 :}
width <- {:width: si32 :}
height <- {:height: si32 :}
xoffset <- {:xoffset: si32 :}
yoffset <- {:yoffset: si32 :}
xadvance <- {:xadvance: si32 :}
page <- si32
chnl <- {:chnl: si32 :}
]]
local expression = re.compile(pattern)
local file = io.open(filename)
local line = file:read("*line")
io.write(output)
while (line) do
local cap = lpeg.match(expression, line)
if cap then
print (makeCharEntry(cap))
end
line = file:read("*line")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment