Homebrew Any Python experts? Need help with HB Menu font converter

mashers

Stubborn ape
OP
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
40
Location
Kongo Jungle
XP
5,074
Country
In the source for @smealum's HB menu, there's a Python script called font.py. This seems to take a font in .fnt format, which specifies the parameters of each character and their coordinates within an accompanying .png file containing the actual characters.

I'm trying to use this script to create new fonts for the launcher. To make sure it's working I'm using the original font which comes in .fnt in the HB Menu source archive. I'm getting an error I don't understand in the scrips and I'm hoping someone who is familiar with Python can help. Here's the error:

'str' object has no attribute 'getpixel'

It occurs on the line shown in bold.

Code:
def outputChar(p, c):
    global fontData, fontDesc, fontName
    im = p[c["page"]][0]
    x, y = int(c["x"]), int(c["y"])
    w, h = int(c["width"]), int(c["height"])
    xo, yo = int(c["xoffset"]), int(c["yoffset"])
    id, xa = int(c["id"]), int(c["xadvance"])
    if id<164:
        c = chr(id)
        c = c if c!="'" else "\\'"
        c = c if c!="\\" else "\\\\"
        c = "\'"+c+"\'"
    else:
        c = str(id)
    data = []
    for i in range(w):
        data.extend([im.getpixel((x+i,y+h-1-j)) for j in range(h)])
    fontDesc[id] = ("    (charDesc_s){%s, %d, %d, %d, %d, %d, %d, %d, &%sData[%d]},"%(c,x,y,w,h,xo,yo,xa,fontName,len(fontData)))
fontData.extend(data)

Thanks in advance guys.

Edit - the bold line doesn't show up in the code block. The error occurs on the line starting data.extend.
 
Last edited by mashers,

730

Professional Shitposter
Member
Joined
Apr 2, 2015
Messages
485
Trophies
0
XP
628
Country
Argentina
In the source for @smealum's HB menu, there's a Python script called font.py. This seems to take a font in .fnt format, which specifies the parameters of each character and their coordinates within an accompanying .png file containing the actual characters.

I'm trying to use this script to create new fonts for the launcher. To make sure it's working I'm using the original font which comes in .fnt in the HB Menu source archive. I'm getting an error I don't understand in the scrips and I'm hoping someone who is familiar with Python can help. Here's the error:

'str' object has no attribute 'getpixel'

It occurs on the line shown in bold.

Code:
def outputChar(p, c):
    global fontData, fontDesc, fontName
    im = p[c["page"]][0]
    x, y = int(c["x"]), int(c["y"])
    w, h = int(c["width"]), int(c["height"])
    xo, yo = int(c["xoffset"]), int(c["yoffset"])
    id, xa = int(c["id"]), int(c["xadvance"])
    if id<164:
        c = chr(id)
        c = c if c!="'" else "\\'"
        c = c if c!="\\" else "\\\\"
        c = "\'"+c+"\'"
    else:
        c = str(id)
    data = []
    for i in range(w):
        data.extend([im.getpixel((x+i,y+h-1-j)) for j in range(h)])
    fontDesc[id] = ("    (charDesc_s){%s, %d, %d, %d, %d, %d, %d, %d, &%sData[%d]},"%(c,x,y,w,h,xo,yo,xa,fontName,len(fontData)))
fontData.extend(data)

Thanks in advance guys.

Edit - the bold line doesn't show up in the code block. The error occurs on the line starting data.extend.
Ummm maybe you're missing a module? I dunno.
 

olshrimpeyes

Your question is bad and you should feel bad
Member
Joined
Jul 30, 2015
Messages
454
Trophies
0
Location
GNU/Somewhere
XP
312
Country
United States
It looks like the script is trying to load an image, but instead is just getting a string. Possibly the filename for the image?

Try changing:
Code:
im = p[c["page"]][0]

To:
Code:
im =Image.open(p[c["page"]][0])

I'll honestly be surprised if that works because I think there's a much bigger problem here.
 

mashers

Stubborn ape
OP
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
40
Location
Kongo Jungle
XP
5,074
Country
Are you importing it?
Yes it's imported at the beginning of the script.

It looks like the script is trying to load an image, but instead is just getting a string. Possibly the filename for the image?

Try changing:
Code:
im = p[c["page"]][0]

To:
Code:
im =Image.open(p[c["page"]][0])

I'll honestly be surprised if that works because I think there's a much bigger problem here.

Well, the function causing the problem is called as follows:
Code:
f = open(fntfn, "r")
pages = {}
for l in f:
    l=readLine(l)
    if len(l)>0:
        if l[0]=="page":
            pages[l[1]["id"]]=(l[1]["file"], Image.open(l[1]["file"]))
        elif l[0]=="char":
outputChar(pages, l[1])

As far as I can tell, the variable pages is an array and the result of Image.open gets stored in there somehow and is then passed to the outputChar function. I can't really be sure how this works though as the syntax of this language makes no sense to me.

The change you suggested resulted in different errors that a hex() argument can't be converts to hex.

I'm actually considering trying to work out what this script is actually doing and then rewriting it in a different language. I have never used Python before and it makes absolutely no sense to me. It doesn't help that I seem to have four different versions of Python which all appear to behave in different ways :/
 

730

Professional Shitposter
Member
Joined
Apr 2, 2015
Messages
485
Trophies
0
XP
628
Country
Argentina
That obfuscation doesn't help very much either... look at those variable names.
 

suloku

Well-Known Member
Member
Joined
Apr 28, 2008
Messages
883
Trophies
0
XP
866
Country
Smea should be using python 3, since his aemstro python scripts use python 3. Pyhon 2 and python 3 are incompatible, so check you are using python 3. That's all I can apport to this.
 
  • Like
Reactions: Garcia98 and 730

mashers

Stubborn ape
OP
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
40
Location
Kongo Jungle
XP
5,074
Country
That obfuscation doesn't help very much either... look at those variable names.
Yeah I know. It's not a very user-friendly piece of code.

Smea should be using python 3, since his aemstro python scripts use python 3. Pyhon 2 and python 3 are incompatible, so check you are using python 3. That's all I can apport to this.
Thanks mate. I've tried in Python 2.6, 2.7, 3.0 and 3.5. They all behave slightly differently and give errors at different points in the code. It seems like Python is more of a mess than cross-browser CSS :wacko: My last effort with this will be to try installing Python in a Windows VM in case the issue is with running the script on a Mac. If that doesn't work then I'm either going to have to try to unpick what exactly the script is doing and try to replicate it in a language I understand (and works), or devise a completely new way of loading and using bitmap fonts in the HB Menu.
 

mashers

Stubborn ape
OP
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
40
Location
Kongo Jungle
XP
5,074
Country
Right, I'm officially giving up on Python. I've tried four different versions of Python under Ubuntu, each with their own corresponding version of PIL and Pillow, each of which installed by apt and also by Python's own package installer, and most of which required me to install headers for zlib, freetype and a load of other things. Every combination gave me a different issue, and none of them worked. This is completely ridiculous. At this point it would have been quicker to render the entire TTF to a PNG and manually map it so the characters can be blitted onto the screen. That's probably all that ctrulib is doing behind the scenes anyway since the .c file for the font seems to contain a load of hex values for each character, and the .bin file I suspect contains the image data. Looks like I'm writing my own font engine.
 

suloku

Well-Known Member
Member
Joined
Apr 28, 2008
Messages
883
Trophies
0
XP
866
Country
I'll try later under windows, see if I can make the script work, but I don't think I'll have any more luck.

That verb doesn't exist :P Contribute sounds better.
I knew something didn't sound right... it was late night and was lazy to use the translator, so I took my chances (bad decission :P)
 
  • Like
Reactions: cearp

mashers

Stubborn ape
OP
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
40
Location
Kongo Jungle
XP
5,074
Country
I'll try later under windows, see if I can make the script work, but I don't think I'll have any more luck.
Thanks buddy. For now I'm working on my own font rendering method, which is a bit of a headfuck but should work. I'm half way there.

I knew something didn't sound right... it was late night and was lazy to use the translator, so I took my chances (bad decission :P)
It does exist in English as a noun, but it means something quite different ;)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Xdqwerty @ Xdqwerty: