; Font to Image Hose rel 0.04
; Created by Graechan
; Comments directed to http://gimpchat.com or http://gimpscripts.com
;
; License: GPLv3
;    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 3 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.
;
;    To view a copy of the GNU General Public License
;    visit: http://www.gnu.org/licenses/gpl.html
;
;
; ------------
;| Change Log |
; ------------ 
; Rel 0.01 - Initial Release
; Rel 0.02 - Added toggle control for Capitols and included Bugfixes
; Rel 0.03 - Added option to also save the layers as .gbr brushes
; Rel 0.04 - added ability to use active font and added menu location to <Fonts>/
;
(define items (quote (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)))
(define lcase (quote (a b c d e f g h i j k l m n o p q r s t u v w x y z)))
(define nums (quote (1 2 3 4 5 6 7 8 9 0)))
(define chars (quote ("~" "!" "@" "#" "$" "%" "^" "&" "*" "(" ")" "_" "+" "-" "=" ";" ":" "'" "," "<" "." ">" "/" "?" "{" "}" "[" "]" "|")))
(define (script-fu-font-to-image-hose
                               active
							   font-in-out
							   font-size
							   gbr-save
							   folder
							   uclets-in
							   lclets
							   numeric
							   chatr
							   conserve)							  

 (let* (
            (width 50)
			(height 50)
			(image (car (gimp-image-new width height RGB)))
			(font-in (if (= active TRUE) (car (gimp-context-get-font)) font-in-out))
		    (font (if (> (string-length font-in) 0) font-in (car (gimp-context-get-font))))
			(layer 0)
			(sym-list 0)
			(uclets uclets-in)
			(text 0)
			(path 0)
			(layerList 0)
			(layerId 0)
			(cnt 1)
			(layerWidth 0)
			(layerHeight 0)
			(offx 0) 
			(offy 0)
			(layer-name 0)
			(handler (car (gimp-message-get-handler)))
			(ver 2.8)
        )
	(cond ((not (defined? 'gimp-image-get-item-position)) (set! ver 2.6))) ;define the gimp version
	
	(gimp-context-push)
	(gimp-context-set-paint-method "gimp-paintbrush")
	(if (= ver 2.8) (gimp-context-set-dynamics "Dynamics Off"))
	(gimp-context-set-foreground '(0 0 0))
	(gimp-context-set-background '(255 255 255))	
	
;;;;begin the script
    (if (and (= lclets FALSE) (= numeric FALSE) (= chatr FALSE)) (set! uclets TRUE)) 
;;;;caps---------------------------------------------------------------------------------------------------------------------	
	(if (= uclets TRUE) (begin
	(set! sym-list items)	
	
	(while (not (null? sym-list))
	(set! text (car sym-list))	
	(set! layer (car (gimp-text-fontname image -1 0 0 (symbol->string text) 0 TRUE font-size PIXELS font)))
	(gimp-layer-flatten layer)
	(gimp-layer-add-alpha layer)
	(set! sym-list (cdr sym-list))
	) ;endwhile
	)) ;endif

;;;;lower case--------------------------------------------------------------------------------------------------------------------	
	(if (= lclets TRUE) (begin
	(set! sym-list lcase)	
	
	(while (not (null? sym-list))
	(set! text (car sym-list))	
	(set! layer (car (gimp-text-fontname image -1 0 0 (symbol->string text) 0 TRUE font-size PIXELS font)))
	(gimp-layer-flatten layer)
	(gimp-layer-add-alpha layer)
	(set! sym-list (cdr sym-list))
	) ;endwhile
	)) ;endif
	
;;;;numerals------------------------------------------------------------------------------------------------------------------     
	(if (= numeric TRUE) (begin
	(set! sym-list nums)
	
	(while (not (null? sym-list))
	(set! text (car sym-list))	
	(set! layer (car (gimp-text-fontname image -1 0 0 (number->string text) 0 TRUE font-size PIXELS font)))
	(gimp-layer-flatten layer)
	(gimp-layer-add-alpha layer)
	(set! sym-list (cdr sym-list))
	) ;endwhile
	)) ;endif
	
;;;;control characters------------------------------------------------------------------------------------------------------
    (if (= chatr TRUE) (begin
	(set! sym-list chars)
	
	(while (not (null? sym-list))
	(set! text (car sym-list))	
	(set! layer (car (gimp-text-fontname image -1 0 0 text 0 TRUE font-size PIXELS font)))
	(gimp-layer-flatten layer)
	(gimp-layer-add-alpha layer)
	(set! sym-list (cdr sym-list))
	) ;endwhile
	)) ;endif
	
	
	
	(gimp-image-resize-to-layers image)
	(set! width (car (gimp-image-width image)))
	(set! height (car (gimp-image-height image)))	
	(gimp-image-convert-grayscale image)
	
	(set! layerList (vector->list (cadr (gimp-image-get-layers image))))
    (while (not (null? layerList))
    (set! layerId (car layerList))
    (set! layerWidth (car (gimp-drawable-width layerId)))
    (set! layerHeight (car (gimp-drawable-height layerId)))
    (set! offx (/ (- width layerWidth) 2))
    (set! offy (/ (- height layerHeight) 2))    
    (gimp-layer-set-offsets layerId offx offy)
    (gimp-layer-resize-to-image-size layerId)
    (gimp-layer-flatten layerId)
	(gimp-layer-add-alpha layerId)
    (set! layer-name (cond ((defined? 'gimp-item-get-name) (car (gimp-item-get-name layerId)))
		             (else (car (gimp-drawable-get-name layerId)))))
    (cond ((defined? 'gimp-item-set-name) (car (gimp-item-set-name layerId (string-append layer-name "D'bat"))))
		             (else (car (gimp-drawable-set-name layerId (string-append layer-name "D'bat")))))				 
    (set! layerList (cdr layerList)))
	
	(set! path (string-append gimp-directory "/brushes/" font ".gih"))	
	
	(file-gih-save RUN-NONINTERACTIVE image 
	                                  (car (gimp-image-get-active-layer image)) ;drawable 
							          path ;filename [The name of the file to save the brush pipe in] 
							          path ;raw-filename [The name of the file to save the brush pipe in] 
							          100 ;spacing 
							          font ;description 
							          width ;cell-width 
							          height ;cell-height 
							          1 ;display-cols 
							          1 ;display-rows 
							          1 ;dimension 
							          (vector (car (gimp-image-get-layers image))) ;rank 
							          1 ;dimension 
							          (list "incremental")) ;sel
									  
	(if (= gbr-save TRUE) (begin
	(set! layerList (vector->list (cadr (gimp-image-get-layers image))))
    
	(while (not (null? layerList))
    (set! layerId (car layerList))
	
	(file-gbr-save RUN-NONINTERACTIVE 
               image 
			   layerId 
			   (string-append folder DIR-SEPARATOR font "-" (number->string cnt) ".gbr") ;filename 
			   (string-append folder DIR-SEPARATOR font "-" (number->string cnt) ".gbr") ;raw-filename 
			   100 ;spacing 
			   (string-append font (number->string cnt))) ;description)
			   (set! cnt (+ cnt 1))
			   (set! layerList (cdr layerList))
	) ;endwhile
	)) ;endif
	
	
	
	
	
	
	
	
	
	

	(gimp-brushes-refresh)
	(cond ((= conserve FALSE) (gimp-image-delete image))
	(else (gimp-display-new image))
	) ;endcond
	(gimp-context-pop)
    (gimp-message-set-handler 0)	
    (gimp-message (string-append "Brush Saved :\n" font ".gih" "\nto :\n" path))	
    (gimp-message-set-handler handler)
	
 )
)

(script-fu-register "script-fu-font-to-image-hose"        		    
  "Font to Image Hose..."
  "Creates a .gih brush from a font or dingbat Images"
  "Graechan"
  "Graechan - http://gimpchat.com"
  "2013"
  ""
  SF-TOGGLE     "Use the Active Font"   FALSE
  SF-FONT       "Symbol Font [enter your Dingbat]"               "Sans"
  SF-ADJUSTMENT "Symbol Font Size (pixels)" '(100 6 1000 1 1 0 1)
  SF-TOGGLE     "Save as Brushes to Dierectory below"   FALSE
  SF-DIRNAME   "Directory" (string-append gimp-directory "/brushes/") 
  SF-TOGGLE     "Apply to Upper Case"   TRUE
  SF-TOGGLE     "Apply to Lower Case"   FALSE
  SF-TOGGLE     "Apply to Numerals"     FALSE
  SF-TOGGLE     "Apply to Characters"   FALSE
  SF-TOGGLE     "Keep the Layers"       FALSE
)

(script-fu-menu-register "script-fu-font-to-image-hose" "<Image>/Script-Fu/Brushes")
(script-fu-menu-register "script-fu-font-to-image-hose" "<Fonts>/")


