Font Atlas Creator - GIMP (Script FU) Plugin
#!/usr/bin/env python | |
# Mihail Szabolcs - Released under the WTFPL (http://en.wikipedia.org/wiki/WTFPL) license. | |
from gimpfu import * | |
def nx_create_font_atlas(font, font_size, font_weight, antialiasing, uppercase, image_width, image_height, image_type, offset_x, offset_y): | |
gimp.set_foreground(255, 255, 255) | |
gimp.set_background(0, 0, 0) | |
if image_type == GRAY_IMAGE: | |
new_image_type = GRAY | |
else: | |
new_image_type = RGB | |
image = gimp.Image(int(image_width), int(image_height), new_image_type) | |
image.disable_undo() | |
background_layer = gimp.Layer(image, "Background", int(image_width), int(image_height), image_type, 100, NORMAL_MODE) | |
if image_type == RGBA_IMAGE: | |
background_layer.add_alpha() | |
background_layer.fill(TRANSPARENT_FILL) | |
else: | |
background_layer.fill(BACKGROUND_FILL) | |
image.add_layer(background_layer) | |
pdb.gimp_image_grid_set_foreground_color(image, (255, 255, 255)) | |
pdb.gimp_image_grid_set_style(image, GRID_SOLID) | |
pdb.gimp_image_grid_set_spacing(image, font_weight, font_size) | |
disp = gimp.Display(image) | |
chars = [chr(c) for c in xrange(32, 127)] | |
if uppercase: | |
chars = map(lambda c: c.upper(), chars) | |
count = len(chars) | |
rows = int(image_width / font_weight) | |
for i in range(count): | |
c = chars[i] | |
x = int((i % rows) * font_weight) + offset_x | |
y = int((i / rows) * font_size) + offset_y | |
pdb.gimp_text_fontname(image, None, x, y, c, -1, antialiasing, font_size, PIXELS, font) | |
gimp.progress_update(float(i) / float(count)) | |
image.merge_visible_layers(CLIP_TO_IMAGE) | |
image.enable_undo() | |
register( | |
"nx_create_font_atlas", | |
"Creates a font atlas.", | |
"Creates a font atlas.", | |
"Mihail Szabolcs", | |
"WTFPL", | |
"2014-2015", | |
"<Toolbox>/File/Create/_Font Atlas", | |
"", | |
[ | |
(PF_FONT, "font", "Font", "Monospace"), | |
(PF_SPINNER, "font_size", "Size", 32, (4, 512, 1)), | |
(PF_SPINNER, "font_weight", "Weight", 32, (4, 512, 1)), | |
(PF_BOOL, "antialiasing", "Antialiasing", True), | |
(PF_BOOL, "uppercase", "Upper Case (only)", True), | |
(PF_SPINNER, "image_width", "Width", 512, (8, 8192, 1)), | |
(PF_SPINNER, "image_height", "Height", 512, (8, 8192, 1)), | |
(PF_RADIO, "image_type", "Type", GRAY_IMAGE, | |
(("Grayscale (8 bit)", GRAY_IMAGE), ("RGB (24 bit)", RGB_IMAGE), ("RGBA (32 bit)", RGBA_IMAGE))), | |
(PF_SPINNER, "offset_x", "Offset X", 0, (-8192, 8192, 1)), | |
(PF_SPINNER, "offset_y", "Offset Y", 0, (-8192, 8192, 1)) | |
], | |
[], | |
nx_create_font_atlas) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment