#!/usr/bin/env python

#   Copyright (C) 2005 Adrian Likins <adrian@likins.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  Renders each glyph of a font into a brush. Handy for say, creating alot
#  of brushes from a dingbat font.
#
# TODO:
#      Need a better way to find where to save brushes (stuff coming in devel gimp)
#      Would be cool to be able to query the font info and find out which glyphs are in the font
#            since a lot of dingbats are incomplete. Maybe use pygtk->pango/etc?
#      Would be cool to make some image hoses. Maybe one brush for a font with the char changing
#            with direction/etc. 
#
#             MODIFIED VERSION FOR WINDOWS by Mahvin <mahvin@gmail.com> August 25, 2009


import os
import string

import gimp
from gimpfu import *


def get_gimp_version():
    # assume version like "gimp-2.2.8"
    ver = pdb.gimp_version()
    parts = ver.split(".")
    #    print parts
    major_minor_ver = string.join(parts[0:2], ".")
    return major_minor_ver
    

def font_to_brushes(font, chars, size):
    #print font, chars, size, brushdir

    # ugh, the font selector widget seems to want to return a PDB_STRING of format "fontname fontsize"
    #  so, we just strip that bit off...
    parts = font.split(" ")
    if parts[-1].isdigit():
        fontparts  = parts[:-1]
        font = string.join(fontparts, " ")

    brushdir = "%s/.gimp-%s/brushes" % (os.path.expanduser('~'), get_gimp_version())
    for char in chars:
        (width, height, asc, dsc) = pdb.gimp_text_get_extents_fontname(char, size, 1, font)
        img = gimp.Image(width, height, RGB)

        drawable = gimp.Layer(img, "test", width, height, RGB_IMAGE, 100, NORMAL_MODE)
        img.add_layer(drawable, 0)
        pdb.gimp_edit_fill(drawable, BACKGROUND_FILL)
        font_layer = pdb.gimp_text_fontname(img, drawable,  0.0, 0.0, char, 0, 1, size, 1, font)

        pdb.gimp_floating_sel_anchor(font_layer)

        pdb.plug_in_autocrop(img, drawable)
        pdb.gimp_image_convert_grayscale(img)

        name = "%s_%s_%s"  % (font, char, size)
        filename = "%s/%s.gbr" % (brushdir,name)
        pdb.file_gbr_save(img, drawable, filename, filename, 20, name)

        pdb.gimp_image_delete(img)

    pdb.gimp_brushes_refresh()
        
    return

register(
    "python_fu_font_to_brushes",
    "Convert a font to brushes",
    "Convert a font to brushes",
    "Adrian Likins",
    "Adrian Likins",
    "2005",
    "<Toolbox>/Xtns/Python-Fu/Font To Brushes",
    "",
    [
    (PF_FONT, "font", "Font to convert to brushes", "Sans"),
    (PF_STRING, "glyphs", "Characters", "abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV123456789"),
    (PF_FLOAT, "size", "Size of Font", 20.0),
 #   (PF_STRING, "brushdir", "Brush folder", "%s/.gimp-%s/brushes/" % (os.path.expandvars("$HOME"), get_gimp_version())),
    ],
    [],
    font_to_brushes)

main()

