Question

Ok, I am trying to develop a small plugin for gimp that makes a bitmap font. For some reason now that I added the following code it won't work.

current_cell_x = 0
current_cell_y = 0

x_pos = current_cell_x * cwidth
y_pos = current_cell_y * cheight

x_pos += cwidth / 2.0
y_pos += cheight / 2.0

x_pos -= text_layer.width / 2.0
y_pos -= text_layer.height / 2.0

Even just the first line with 'current_cell_x = 0' is enough to make the entire plugin not work.

Here is all the code in a working format:

from gimpfu import *

def create_font(cwidth, cheight, xcells, ycells, font, size, color) :

    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    #current_cell_x = 0
    #current_cell_y = 0

    # Figure out total width & height
    twidth = int(cwidth * xcells)
    theight = int(cheight * ycells)

    # Create Image
    img = gimp.Image(twidth, theight, RGB)
    img.disable_undo()

    # Save the current foreground color:
    pdb.gimp_context_push()

    # Set the text color & background color
    gimp.set_foreground(color)
    gimp.set_background(0, 0, 0)

    # Create All Layers & Position Accordingly
    for i in range(char_begin, char_end):
        string = '%c' % i
        offset = i - char_begin
        text_layer = pdb.gimp_text_fontname(img, None, 0, 0, string, -1, True, size, PIXELS, font)

        #x_pos = current_cell_x * cwidth
        #y_pos = current_cell_y * cheight

        #x_pos += cwidth / 2.0
        #y_pos += cheight / 2.0

        #x_pos -= text_layer.width / 2.0
        #y_pos -= text_layer.height / 2.0

    #pdb.gimp_image_flatten(img)

    img.enable_undo()

    # Create a new image window
    gimp.Display(img)
    # Show the new image window
    gimp.displays_flush()

    # Restore the old foreground color:
    pdb.gimp_context_pop()

register(
    "python_fu_bitmap_font",
    "Bitmap Font",
    "Create a new bitmap font",
    "*****",
    "*****",
    "2013",
    "Bitmap Font (Py)...",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "cwidth", "Cell Width", 24, (1, 3000, 1)),
        (PF_SPINNER, "cheight", "Cell Height", 51, (1, 3000, 1)),
        (PF_SPINNER, "xcells", "Cells on X axis", 10, (1, 3000, 1)),
        (PF_SPINNER, "ycells", "Cells on Y axis", 10, (1, 3000, 1)),
        (PF_FONT, "font", "Font face", "Consolas"),
        (PF_SPINNER, "size", "Font size", 50, (1, 3000, 1)),
        (PF_COLOR, "color", "Text color", (1.0, 0.0, 0.0))
    ],
    [],
    create_font, menu="<Image>/File/Create")

main()

However, when I un-comment lines 9 - 10 & lines 33 - 40 I get the following errors.

enter image description here

enter image description here

enter image description here

I wanted my final code to look something like this:

def create_font(cwidth, cheight, xcells, ycells, font, size, color) :

    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    current_cell_x = 0
    current_cell_y = 0

    # Figure out total width & height
    twidth = int(cwidth * xcells)
    theight = int(cheight * ycells)

    # Create Image
    img = gimp.Image(twidth, theight, RGB)
    img.disable_undo()

    # Save the current foreground color:
    pdb.gimp_context_push()

    # Set the text color & background color
    gimp.set_foreground(color)
    gimp.set_background(0, 0, 0)

    # Create All Layers & Position Accordingly
    for i in range(char_begin, char_end):
        string = '%c' % i

        text_layer = pdb.gimp_text_fontname(img, None, 0, 0, string, -1, True, size, PIXELS, font)
        pdb.plug_in_autocrop_layer(0, img, text_layer)

        x_pos = float(current_cell_x * cwidth)
        y_pos = float(current_cell_y * cheight)

        x_pos += float(cwidth) / 2.0
        y_pos += float(cheight) / 2.0

        x_pos -= float(text_layer.width) / 2.0
        y_pos -= float(text_layer.height) / 2.0

        text_layer.translate(x_pos, y_pos)

        #Set current cell
        current_cell_x += 1
        if current_cell_x > xcells:
            current_cell_x = 0
            current_cell_y += 1
        if current_cell_y > ycells:
            break

        gimp.progress_update(float(offset) / float(num_chars))

    #pdb.gimp_image_flatten(img) # Don't do this

    img.enable_undo()

    # Create a new image window
    gimp.Display(img)
    # Show the new image window
    gimp.displays_flush()

    # Restore the old foreground color:
    pdb.gimp_context_pop()

If you see any suggestions as far as that goes, I wouldn't mind.

Note: Got my code from - https://github.com/sole/snippets/blob/master/gimp/generate_bitmap_font/sole_generate_bitmap_font.py and http://gimpbook.com/scripting/


Ran 'python -tt plugin.py' and got the following:

enter image description here

enter image description here

Was it helpful?

Solution 2

I had inconsistent indentation I needed to go to Settings>Preferences>Language Menu/Tab Settings>Tab Settings and then check Replace by space.

Then I needed to run

python -tt yourscript.py

Which allowed me to get rid of all the indention errors that were caused by my edior.

I also used reindent.py from the Python33/Tools/Scripts folder, but it did not work for me. It may have helped in someway I did not see.

Matthias was halfway right, but he did not submit an answer. I am doing that in his place.

OTHER TIPS

The error on the CMD windows says it: you are mixing tabs and spaces in your source code - make sure the lines you add to the code are prefixed with spaces only, and not tab (\x09) characters.

You should configure the editor program you are using to edit the Python code not to use tab characters at all.

(it could possibly be the other way around, you are editing a file idented with tabs, and your editor is properly configured to use spaces - you should replace all tabs in that case)

Check the Python style guide document: http://www.python.org/dev/peps/pep-0008/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top