(define (script-fu-custom-font image layer 
            text
         )
	
		(let* 
		   (
		   (width (car (gimp-image-width image)))
		   (height (car (gimp-image-height image)))
		   (charList ())
		   (char 0)
		   (layer-name 0)
		   (new-image 0)
		   (source-layer 0)
		   (background-layer 0)
		   (new-display 0)
		   (floating-sel 0)
		   (layer-width-half 0)
		   (count 0)
		   (offset 0)
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			(gimp-context-push)
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			
			;creates a new image with a single background layer to work with
			(set! new-image (car (gimp-image-new width height RGB)))      ;creates new image
			(set! background-layer (car (gimp-layer-new new-image width height
	                           RGBA-IMAGE "background" 100 NORMAL-MODE)))
			(gimp-image-insert-layer new-image background-layer 0 0)
			
			
			(set! charList (map (lambda (x) (make-string 1 x)) (string->list text))) ;create the list of letters

			(while (not (null? charList))
   				(set! char (car charList))
				
				;default layer-name to whatever character is appended with .png
				(set! layer-name (string-append char ".png"))
				
				;if it's a space we'll look for space.png layer
				(if (string=? char " ") (begin 
					(set! layer-name "space.png")
				))
				
				
				; --------------------------
				; copying and pasting is done below
				; --------------------------
				;copying from the correct layer
				(set! source-layer (car (gimp-image-get-layer-by-name image layer-name)))
				(if (< source-layer 0) (begin 
				    (gimp-message (string-append "Layer not found: " layer-name ))
				))
				
				(gimp-selection-all image)
				(gimp-edit-copy source-layer)
				
				;paste it in new image as a floating selection then make it a layer
				(set! floating-sel (car (gimp-edit-paste background-layer TRUE)))
				(gimp-floating-sel-to-layer floating-sel)
				
				;move it to the right place
				
				(if (= count 0) ;first time don't move it just calculate layer-width-half
					(begin
						(set! layer-width-half (/ (car (gimp-drawable-width floating-sel)) 2.0))
						
					)
					(begin ;second time and onward after that we add last calculated half
					       ;recalculate half add that then move it 
					    (set! offset (+ offset layer-width-half))
						(set! layer-width-half (/ (car (gimp-drawable-width floating-sel)) 2.0))
						(set! offset (+ offset layer-width-half))
						(gimp-layer-translate floating-sel offset 0)
					)
				)
				
				
				(set! charList (cdr charList))
				(set! count (+ count 1))
			) ;endwhile
			(gimp-image-resize-to-layers new-image)
			;create new display for our new-image
			(set! new-display (car (gimp-display-new new-image)))      ;creates new display for image
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-context-pop)
			(gimp-displays-flush)
	    )
	
	
    
) ;end of define
(script-fu-register
  "script-fu-custom-font"         ;function name
  "<Image>/Script-Fu/Create New/Custom Font ..."    ;menu register
  "Creates an image of text using the custom font layers"       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2016"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
  SF-STRING     "Text" "Gimp Chat"
)
