carol.gimp.org

gimp2

Python:Font Map

There is a Script-fu located at Xtns -->Script-Fu -->Utils -->Font Map... called Font Map. If you have a lot of fonts and are not careful with the search stuff, you might get a huge image displayed in a way that it becomes difficult to see what has happened. Don't be frightened and play with the search strings.

I wanted to use this script-fu via a python script that I was writing to have TheGIMP write web pages displaying the fonts it had access to. The script was fairly complicated, and one of the things about script-fu is that it makes images that gimp doesn't know about.

fontmap

Read it on the stylish web page below. Download it and install it.

font-map.py

File   Edit   Search   Preferences




#!/usr/bin/env python

from gimpfu import *

def max_font_dims(text, use_name, font_list, font_size):
    max_width = 0
    max_height = 0

    for font in font_list:
	if use_name:
	    text = font

	extents = pdb.gimp_text_get_extents_fontname(text,
						     font_size, PIXELS,
						     font)
	width, height = extents[0:2]

        max_width = max(max_width, width)
        max_height = max(max_height, height)

    return max_width, max_height

def font_map(text, use_name, labels, font_filter, font_size, border, colors):
    num_fonts, font_list = pdb.gimp_fonts_get_list(font_filter)

    if labels:
	label_size = font_size / 2
	border += label_size / 2

    max_width, max_height = max_font_dims(text, use_name, font_list, font_size)

    width = max_width + 2 * border
    height = max_height * num_fonts + 2 * border

    if labels:
	height += label_size * num_fonts

    if colors:
	image_type = RGB
	bg_type = RGB_IMAGE
	lbl_type = RGBA_IMAGE
    else:
	image_type = GRAY
	bg_type = GRAY_IMAGE
	lbl_type = GRAYA_IMAGE

    img = gimp.Image(width, height, image_type)
    drawable = gimp.Layer(img, "Background", width, height,
			  bg_type, 100, NORMAL_MODE)

    old_fg = gimp.get_foreground()
    old_bg = gimp.get_background()

    img.disable_undo()

    if not colors:
	gimp.set_background((255, 255, 255))
	gimp.set_foreground((0, 0, 0))

    pdb.gimp_edit_clear(drawable)
    img.add_layer(drawable, 0)

    if labels:
        drawable = gimp.Layer(img, "Labels", width, height,
			      lbl_type, 100, NORMAL_MODE)
	pdb.gimp_edit_clear(drawable)
	img.add_layer(drawable, -1)

    y = border 

    for font in font_list:
	if use_name:
	    text = font

	pdb.gimp_text_fontname(img, None, border, y, text, 0, True,
			       font_size, PIXELS, font)

	y += max_height

	if labels:
	    sel = pdb.gimp_text_fontname(img, drawable,
					 border - label_size / 2,
					 y - label_size / 2,
					 font, 0, True,
					 label_size, PIXELS, "Sans")

	    pdb.gimp_floating_sel_anchor(sel)

	    y += label_size

    img.active_layer = drawable

    if not colors:
	gimp.set_background(old_bg)
	gimp.set_foreground(old_fg)

    img.enable_undo()

    if __name__ == '__main__':
        gimp.Display(img)
    else:
        return img

if __name__ == '__main__':
    register(
	"python_fu_font_map",
	"Generate a listing of fonts matching a filter",
	"Generate a listing of fonts matching a filter (based on script-fu version)",
	"Spencer Kimball (ported to python by Manish Singh)",
	"Spencer Kimball (ported to python by Manish Singh)",
	"1997, 2004",
	"<Toolbox>/Xtns/Python-Fu/Test/Font Map",
	"*",
	[
	    (PF_STRING, "text", "Text", "How quickly daft jumping zebras vex."),
	    (PF_TOGGLE, "use_name", "Use font name as text", False),
	    (PF_TOGGLE, "labels", "Labels", True),
	    (PF_STRING, "filter", "Filter (regexp)", "Sans"),
	    (PF_SPINNER, "font_size", "Font size (pixels)", 32, (2, 1000, 1, 10)),
	    (PF_SPINNER, "border", "Border (pixels)", 10, (0, 200, 1, 10)),
	    (PF_TOGGLE, "colors", "Use active colors", False)
	],
	[],
	font_map)

    main()





  

Use it in another script:

font-map.py

File   Edit   Search   Preferences



#at the top of the script
from gimpfu import *

from fontmap import font_map

#use it in the script
img = font_map("", True, True, single, font_size, border_size, False)



  

another-gnu-type

Most all of my software is gnu. thanks!

Valid XHTML 1.1!

CC-GNU GPL
This software is licensed under the CC-GNU GPL.