#!/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 image hose. Handy for say, creating alot
#  of brushes from a dingbat font.
#
# TODO:
#      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?
#
#            Modified version for Windows use, submitted 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_image_hose(font, chars, size, selectionmode):
    print font, chars, size, selectionmode

    # 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())
    max_width = 0.0
    max_height = 0.0
    for char in chars:
        (width, height, asc, dsc) = pdb.gimp_text_get_extents_fontname(char, size, 1, font)
        if width > max_width:
            max_width = width
        if height > max_height:
            max_height = height

    img = gimp.Image(max_width, max_height, RGB)

    layer_count = 0
    for char in chars:
        (width, height, asc, dsc) = pdb.gimp_text_get_extents_fontname(char, size, 1, font)

        drawable = gimp.Layer(img, "%s" % char, width, height, RGBA_IMAGE, 100, NORMAL_MODE)
        img.add_layer(drawable, layer_count)
        layer_count = layer_count + 1
        pdb.gimp_edit_clear(drawable)
        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.gimp_image_convert_grayscale(img)

 #       pdb.gimp_image_delete(img)

    name = "%s_%s"  % (font,size)
    filename = "%s/%s.gih" % (brushdir,name)
#    pdb.gimp_image_convert_grayscale(img)
    print img, drawable, filename, filename, 50, name, max_width, max_height, 1,1, 1, [len(chars)], 1, [selectionmode]
    pdb.file_gih_save(img, drawable, filename, filename, 50, name, max_width, max_height, 1,1,1, [len(chars)], 1, [selectionmode])
#    disp = gimp.Display(img)
 #   pdb.gimp_brushes_refresh()
        
    return

register(
    "python_fu_font_to_image_hose",
    "Convert a font to an image hose brush",
    "Convert a font to an image hose brush",
    "Adrian Likins",
    "Adrian Likins",
    "2005",
    "<Toolbox>/Xtns/Python-Fu/Font To Image Hose Brush",
    "",
    [
    (PF_FONT, "font", "Font to convert to brushes", "Sans"),
    (PF_STRING, "glyphs", "Characters", "abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV123456789"),
    (PF_FLOAT, "size", "Size of Font", 20.0),
    (PF_RADIO, "selectionmode",  "The selection method of the brush: incremental, random, velocity",
     "incremental", (("incremental", "incremental"),
                     ("random", "random"),
                     ("angular", "angular"),
                     ("velocity", "velocity"),
                     ("pressure", "pressure"),
                     ("xtilt", "xtilt"),
                     ("ytilt", "ytilt"))),
#    (PF_STRING, "brushdir", "Brush folder", "%s/.gimp-%s/brushes/" % (os.path.expandvars("$HOME"), get_gimp_version())),
    ],
    [],
    font_to_image_hose)

main()

