0

I am trying to write a plugin that will create a bitmap font. However, this is very frustrating to learn... while I am not familiar with python, it is not that hard to pick up and have not had problems with it outside of GIMP.

Copied some of the code from: https://github.com/sole/snippets/blob/master/gimp/generate_bitmap_font/sole_generate_bitmap_font.py and from http://gimpbook.com/scripting/

Does work:

#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

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

    #Set GLOBAL
    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    # Figure out total width & height
    """twidth = cwidth * 10
    theight = cheight * 10

    # Create Image
    img = gimp.Image(cwidth * 10, cheight * 10, 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

        x_pos = offset * cwidth
        y_pos = offset * cheight

        text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

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

    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_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()

Does not work:

#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

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

    #Set GLOBAL
    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    # Figure out total width & height
    twidth = cwidth * 10
    theight = cheight * 10

    # Create Image
    """img = gimp.Image(cwidth * 10, cheight * 10, 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

        x_pos = offset * cwidth
        y_pos = offset * cheight

        text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

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

    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_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()

It seems that the after changing the beginning comment from line 15 to line 19 that everything goes to hell. And to be honest, I am not even sure how to debug this. I tried using the console under Filters>Python-Fu>Console - however this kept telling me line 1 was the issue... which I think we can all agree is not the case.

I tried running pieces of this code in a python script and works perfectly fine.

What should I do?

morten.c
  • 3,414
  • 5
  • 40
  • 45
Zeveso
  • 1,274
  • 3
  • 21
  • 41
  • 1
    Why are you creating a big string for no reason? Is it your way to comment out the code? About the shebang, have you tried to remove it? Maybe GIMP is actually trying to do something with. Can you post traceback, or some more information about what happens exactly? – Paco Sep 23 '13 at 22:35
  • @Paco What big string? I am trying to create an image with all 95 Chars on it. - The shebang sometimes works, but ill try. (RESULT: WORKED) - Well I guess it worked and I guess line 1 was a problem. Hmm... Let me play with this for the next 24 hours just to be sure. – Zeveso Sep 23 '13 at 22:40
  • Posted an answer to display the big string, and also with my answer I gave in the comments – Paco Sep 23 '13 at 22:49

1 Answers1

1

First of all, try to remove the shebang at line 1.

Then something that has nothing to with the actual problem, but why are you creating such a big string?

# Create Image
"""img = gimp.Image(cwidth * 10, cheight * 10, 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

    x_pos = offset * cwidth
    y_pos = offset * cheight

    text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

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

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()"""

Is this your way to comment out the code?

Paco
  • 4,520
  • 3
  • 29
  • 53
  • Oh, that is because that was all the code, but after it breaking... (bug)... I tried commenting out to see which line was the issue. HAHA! Eventually all of that big block comment would be gone. – Zeveso Sep 23 '13 at 22:52
  • Question: Why is the shebang an issue? – Zeveso Sep 23 '13 at 22:55
  • It shouldn't be an issue, because it is just saying that this the interpreter that should be used. But usually we don't put in the "non executable" python files (such as libraries for example). Maybe it conflicts somehow with GIMP, I actually never made any plugins for GIMP. Triple quotes shouldn't be used to comment out the code, that is a bad practice ;) – Paco Sep 23 '13 at 23:01