Any Python experts? Need help with HB Menu font converter

Discussion in '3DS - Homebrew Development and Emulators' started by mashers, Sep 26, 2015.

  1. mashers
    OP

    mashers Stubborn ape

    Member
    3,837
    5,154
    Jun 10, 2015
    Kongo Jungle
    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, Sep 26, 2015
  2. 730

    730 Professional Shitposter

    Member
    465
    376
    Apr 2, 2015
    Argentina
    Ummm maybe you're missing a module? I dunno.
     
  3. mashers
    OP

    mashers Stubborn ape

    Member
    3,837
    5,154
    Jun 10, 2015
    Kongo Jungle
    Well the script needs a module called PIL which is installed.
     
  4. 730

    730 Professional Shitposter

    Member
    465
    376
    Apr 2, 2015
    Argentina
    Are you importing it?
     
  5. olshrimpeyes

    olshrimpeyes Your question is bad and you should feel bad

    Member
    454
    283
    Jul 30, 2015
    United States
    GNU/Somewhere
    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.
     
  6. mashers
    OP

    mashers Stubborn ape

    Member
    3,837
    5,154
    Jun 10, 2015
    Kongo Jungle
    Yes it's imported at the beginning of the script.

    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 :/
     
  7. 730

    730 Professional Shitposter

    Member
    465
    376
    Apr 2, 2015
    Argentina
    That obfuscation doesn't help very much either... look at those variable names.
     
  8. suloku

    suloku GBAtemp Advanced Fan

    Member
    862
    491
    Apr 28, 2008
    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.
     
    Garcia98 and 730 like this.
  9. 730

    730 Professional Shitposter

    Member
    465
    376
    Apr 2, 2015
    Argentina
    That verb doesn't exist :P Contribute sounds better.
     
  10. mashers
    OP

    mashers Stubborn ape

    Member
    3,837
    5,154
    Jun 10, 2015
    Kongo Jungle
    Yeah I know. It's not a very user-friendly piece of code.

    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.
     
  11. mashers
    OP

    mashers Stubborn ape

    Member
    3,837
    5,154
    Jun 10, 2015
    Kongo Jungle
    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.
     
  12. suloku

    suloku GBAtemp Advanced Fan

    Member
    862
    491
    Apr 28, 2008
    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.

    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)
     
    cearp likes this.
  13. mashers
    OP

    mashers Stubborn ape

    Member
    3,837
    5,154
    Jun 10, 2015
    Kongo Jungle
    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.

    It does exist in English as a noun, but it means something quite different ;)