pancelor / p8x8

convert PICO-8 carts into picotron carts (some assembly required)
Other
20 stars 0 forks source link

custom font support #4

Closed pancelor closed 4 months ago

pancelor commented 5 months ago

custom fonts in picotron, from /system/lib/head.lua:

    poke(0x5f56, 0x40) -- primary font
    poke(0x5f57, 0x56) -- secondary font
    poke(0x4000,get(fetch"/system/fonts/lil.font"))
    poke(0x5600,get(fetch"/system/fonts/p8.font"))

custom fonts in p8x8, from baked/main.lua:

--swap fonts (see /system/lib/head.lua)
poke(0x5f56, 0x56) -- primary font - p8.font
poke(0x5f57, 0x40) -- secondary font - lil.font

however, this doesn't seem to work -- test case here. the fonst stuff doesn't work because of non-ascii chars, but I'm not sure what's wrong with the numeric version

function _init()
    fontstr="\8\8\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0ᶜᶜᶜᶜ\0ᶜ\0\000666\0\0\0\0\0\0006○6○6\0\0ᶜ>³゛0゜ᶜ\0\0003•ᶜ63\0\0ᵉ⁙⁶/;゛\0\0⁶⁶⁶\0\0\0\0\0「ᶜᶜᶜᶜ「\0\0⁶ᶜᶜᶜᶜ⁶\0\0\0006、○、6\0\0\0ᶜᶜ?ᶜᶜ\0\0\0\0\0\0⁶⁶³\0\0\0\0゛\0\0\0\0\0\0\0\0⁶⁶\0\0`0「ᶜ⁶³\0\0゛3;73゛\0\0ᶜᵉᶜᶜᶜ゛\0\0゛18゛⁷?\0\0゜8゛88゜\0\0008<63○0\0\0゜³゜89゛\0\0゛³゜33゛\0\0?3「ᶜᶜᶜ\0\0゛3゛33゛\0\0゛33>0゛\0\0\0⁶⁶\0⁶⁶\0\0\0⁶⁶\0⁶⁶³\0\0「ᶜ⁶ᶜ「\0\0\0\0>\0>\0\0\0\0⁶ᶜ「ᶜ⁶\0\0、20、\0ᶜ\0\0>c{+•c>\0゛33?33\0\0゜3゜33゜\0\0゛3³³3゛\0\0゜3333゜\0\0?³゜³³?\0\0?³゜³³³\0\0゛3³;3゛\0\00033?333\0\0?ᶜᶜᶜᶜ?\0\0?ᶜᶜᶜ\r⁶\0\00033•ᶠ•3\0\0³³³³³?\0\0cw○kcc\0\00037?;33\0\0゛3333゛\0\0゜33゜³³\0\0゛3333゛8\0゜33゜ᵇ3\0\0>⁷゛88゜\0\0?ᶜᶜᶜᶜᶜ\0\00033333>\0\0cccc6、\0\0ccck○6\0\0cv<゛7c\0\000333゛ᶜᶜ\0\0?8、ᵉ⁷?\0\0゛⁶⁶⁶⁶゛\0\0³⁶ᶜ「0`\0\0゛「「「「゛\0\0ᶜ゛3\0\0\0\0\0\0\0\0\0\0>\0\0⁶ᶜ「\0\0\0\0\0\0゛0>3>\0\0³³゜33゜\0\0\0゛3³3゛\0\00000>33>\0\0\0゛3?³゛\0\0、⁶゜⁶⁶⁶\0\0\0>33>0゛\0³³゜333\0\0ᶜ\0ᶠᶜᶜ?\0\0「\0゛「「¥ᶜ\0³³•ᶠ•3\0\0ᵉᶜᶜᶜᶜ?\0\0\0003○kcc\0\0\0゜3333\0\0\0゛333゛\0\0\0゜33゜³³\0\0>33>00\0\0•7³³³\0\0\0>⁷゛8゜\0\0⁶⁶゜⁶&、\0\0\0003333>\0\0\000333゛ᶜ\0\0\0cck○6\0\0\0003゛ᶜ゛3\0\0\000333>「ᶠ\0\0?「ᶜ⁶?\0\0、ᶜᵉᵉᶜ、\0\0ᶜᶜᶜᶜᶜᶜ\0\0ᵉᶜ、、ᶜᵉ\0\0\0\0n○9\0\0\0\0\0\0\0\0\0\0\0○○○○○\0\0\0u*u*u\0\0\0a○]]>\0\0\0>ccw>\0\0\0■d■d■\0\0\0⁴<、゛▮\0\0\0、.>>、\0\0\0006>>、⁸\0\0\0、6w6、\0\0\0、、>、⁘\0\0\0、>○*:\0\0\0>gcg>\0\0\0○]○a○\0\0\0008⁸⁸ᵉᵉ\0\0\0>ckc>\0\0\0⁸、>、⁸\0\0\0\0\0u\0\0\0\0\0>scs>\0\0\0⁸、○>\"\0\0\0>、⁸、>\0\0\0>wcc>\0\0\0\0⁵r \0\0\0\0\0■*d\0\0\0\0>kwk>\0\0\0○\0○\0○\0\0\0uuuuu\0\0\0「$$~vf~"

--  poke(0x5600,ord(fontstr,1,#fontstr))
    poke(0x5600,split"8,8,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,12,12,12,0,12,0,0,54,54,54,0,0,0,0,0,0,54,127,54,127,54,0,0,12,62,3,30,48,31,12,0,0,51,27,12,54,51,0,0,14,19,6,47,59,30,0,0,6,6,6,0,0,0,0,0,24,12,12,12,12,24,0,0,6,12,12,12,12,6,0,0,0,54,28,127,28,54,0,0,0,12,12,63,12,12,0,0,0,0,0,0,6,6,3,0,0,0,0,30,0,0,0,0,0,0,0,0,6,6,0,0,96,48,24,12,6,3,0,0,30,51,59,55,51,30,0,0,12,14,12,12,12,30,0,0,30,49,56,30,7,63,0,0,31,56,30,56,56,31,0,0,56,60,54,51,127,48,0,0,31,3,31,56,57,30,0,0,30,3,31,51,51,30,0,0,63,51,24,12,12,12,0,0,30,51,30,51,51,30,0,0,30,51,51,62,48,30,0,0,0,6,6,0,6,6,0,0,0,6,6,0,6,6,3,0,0,24,12,6,12,24,0,0,0,0,62,0,62,0,0,0,0,6,12,24,12,6,0,0,28,50,48,28,0,12,0,0,62,99,123,43,27,99,62,0,30,51,51,63,51,51,0,0,31,51,31,51,51,31,0,0,30,51,3,3,51,30,0,0,31,51,51,51,51,31,0,0,63,3,31,3,3,63,0,0,63,3,31,3,3,3,0,0,30,51,3,59,51,30,0,0,51,51,63,51,51,51,0,0,63,12,12,12,12,63,0,0,63,12,12,12,13,6,0,0,51,51,27,15,27,51,0,0,3,3,3,3,3,63,0,0,99,119,127,107,99,99,0,0,51,55,63,59,51,51,0,0,30,51,51,51,51,30,0,0,31,51,51,31,3,3,0,0,30,51,51,51,51,30,56,0,31,51,51,31,11,51,0,0,62,7,30,56,56,31,0,0,63,12,12,12,12,12,0,0,51,51,51,51,51,62,0,0,99,99,99,99,54,28,0,0,99,99,99,107,127,54,0,0,99,118,60,30,55,99,0,0,51,51,51,30,12,12,0,0,63,56,28,14,7,63,0,0,30,6,6,6,6,30,0,0,3,6,12,24,48,96,0,0,30,24,24,24,24,30,0,0,12,30,51,0,0,0,0,0,0,0,0,0,0,62,0,0,6,12,24,0,0,0,0,0,0,30,48,62,51,62,0,0,3,3,31,51,51,31,0,0,0,30,51,3,51,30,0,0,48,48,62,51,51,62,0,0,0,30,51,63,3,30,0,0,28,6,31,6,6,6,0,0,0,62,51,51,62,48,30,0,3,3,31,51,51,51,0,0,12,0,15,12,12,63,0,0,24,0,30,24,24,26,12,0,3,3,27,15,27,51,0,0,14,12,12,12,12,63,0,0,0,51,127,107,99,99,0,0,0,31,51,51,51,51,0,0,0,30,51,51,51,30,0,0,0,31,51,51,31,3,3,0,0,62,51,51,62,48,48,0,0,27,55,3,3,3,0,0,0,62,7,30,56,31,0,0,6,6,31,6,38,28,0,0,0,51,51,51,51,62,0,0,0,51,51,51,30,12,0,0,0,99,99,107,127,54,0,0,0,51,30,12,30,51,0,0,0,51,51,51,62,24,15,0,0,63,24,12,6,63,0,0,28,12,14,14,12,28,0,0,12,12,12,12,12,12,0,0,14,12,28,28,12,14,0,0,0,0,110,127,57,0,0,0,0,0,0,0,0,0,0,0,127,127,127,127,127,0,0,0,117,42,117,42,117,0,0,0,97,127,93,93,62,0,0,0,62,99,99,119,62,0,0,0,17,100,17,100,17,0,0,0,4,60,28,30,16,0,0,0,28,46,62,62,28,0,0,0,54,62,62,28,8,0,0,0,28,54,119,54,28,0,0,0,28,28,62,28,20,0,0,0,28,62,127,42,58,0,0,0,62,103,99,103,62,0,0,0,127,93,127,97,127,0,0,0,56,8,8,14,14,0,0,0,62,99,107,99,62,0,0,0,8,28,62,28,8,0,0,0,0,0,117,0,0,0,0,0,62,115,99,115,62,0,0,0,8,28,127,62,34,0,0,0,62,28,8,28,62,0,0,0,62,119,99,99,62,0,0,0,0,5,114,32,0,0,0,0,0,17,42,100,0,0,0,0,62,107,119,107,62,0,0,0,127,0,127,0,127,0,0,0,117,117,117,117,117,0,0,0,24,36,36,126,118,102,126")
    poke(0x5f58,0x81) --default to custom font

--[[ --p64 default
    poke(0x5f56, 0x40)
    poke(0x5f57, 0x56)
--]]
---[[ --p8x8
    poke(0x5f56, 0x56) -- primary font - p8.font
    poke(0x5f57, 0x40) -- secondary font - lil.font
--]]
end
function _draw()
    cls()
    print(#fontstr)
    print("hello")
    print("\14hello")
    print("\15hello")
end

the numeric version was created in p8 with

    local b=""
    for i=1,#fontstr do
        b..=","..ord(fontstr,i)
    end
    printh(b,"@clip")

todo: compare data format for new font (/system/fonts/p8.font) with old output of fonter on the default font

pancelor commented 4 months ago

ah my bad, you need to poke-unpack-split, not poke-split. (the unpack-split part is necessary b/c 2048 is too many parameters to pass to poke on their own)

here are two snippets you can run inside PICO-8; either one will put a string into your clipboard that can be pasted into your pico8 cart -- p8x8 will properly convert it.

1 - Use this one - it works in pico8, and also in p8x8-converted carts:

local data='poke(unpack(split\"0x5600'
for addr=0x5600,0x5dff do
    data..=","..@addr
end
printh(data..'"))',"@clip")

2 - This one looks fancy but doesn't have much of a real benefit:

local data=""
for addr=0x5600,0x5dff do
    data..=sub(tostr(@addr,1),5,6) --byte to 2-digit hex
end
data='local custom_font = p64env.userdata("u8",0x800,"'..data..'")\npoke(0x5600,custom_font:get())'
printh(data,"@clip")

(kinda neat tho, and maybe there's a better version of this that doesn't involve the slow ish poke. not that it matters much - you poke once during init and then you're good to go. I tried memmap - memmap(0x5600,my_font_userdata) but it didn't do anything; I assume it violated the "all memory under 0x10000 must be memmapped" rule somehow)

merwok commented 4 months ago

I expect the p8scii-based poke to become popular, with the various font tools out there switching to that! Will you consider support? (?"\I-forget-the-code mem-address encoded-bytes")

pancelor commented 4 months ago

Ah, this one? ?"⁶!5600⁸⁸\n\0\0\0\0\0\0\0\0\0..." That's not supported currently. I'll consider it, but it's unlikely (I don't want to be in the business of editing code automatically, and that would be needed here. see my reply about special chars here for some more context/explanation)

However, I can make the warnings nicer. I've already made a snippet you can run in PICO-8 to automatically convert the font memory into a Picotron-safe poke -- I should make this more prominent in the warnings.

pancelor commented 3 months ago

update: you can now use p8x8_datastring() to easily convert binary-data-strings. For instance, you would need to change ?"⁶!5600⁸⁸\n\0\0\0\0\0\0\0\0\0..." in your cart to poke(0x5600,ord(p8x8_datastring"⁸⁸\n\0\0\0\0\0\0\0\0\0...",1,len))

However, note that picotron 010g has a bug where ord(str,i,num) isn't working properly: https://www.lexaloffle.com/bbs/?pid=147830#p