(define (script-fu-custom-font image layer 
            text
			justification
			letter-spacing
			line-spacing
			merge
         )
	
		(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)
		   (layer-height 0)
		   (count 0)
		   (countline 0)
		   (offset 0)
		   (offsetline 0)
		   (linewidth 0)
		   (characters-layers ())
		   (move-layer 0)
		   (move-x 0)
		   (lines ())
		   (widths ())
		   (maxwidth 0)
		   (character-counts ())
		   (character-count 0)
		   (item-index 0)
		   (character-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
            (set! charList (append charList (list "\n"))) ;make it end with newline character
			(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")
				))
				
				
				; if its' new line character we do what needs to be done like shifting the line to justification and reset variables to initial states
				(if (string=? char "\n") 
					(begin
					
						(if (= justification 0) ;LEFT
							(begin
								(set! move-x 0)
							)
							(begin
								(if (= justification 1) ;RIGHT
									(begin
										(set! move-x (- 0 linewidth))
									)
									(begin  
                                        (if (= justification 2) ;CENTER\
											(begin
												(set! move-x (- 0 (/ linewidth 2)))
											)
									    )
									)
								)
							)
						)
						
						(set! lines (append lines (list characters-layers)))
						
					    ;loop through our characters-layers and translate it aoordingly based on move-x for justification
						(while (not (null? characters-layers))
							(set! move-layer (car characters-layers))
							(gimp-layer-translate move-layer move-x 0)
							(set! characters-layers (cdr characters-layers))
						)
						
						(set! character-counts (append character-counts (list count)))
					    (set! count 0)
						(set! offset 0)
						
						(set! widths (append widths (list linewidth)))
						(set! maxwidth (max linewidth maxwidth))
						(set! linewidth 0)
						
						
						(set! characters-layers ())
						
						(set! countline (+ countline 1))
						(if (= countline 0) ;first time don't move it just calculate layer-width-half
							(begin
							
							)
							(begin ;second time and onward after that we add last calculated half
								   ;recalculate half add that then move it 
								(set! layer-height (car (gimp-drawable-height floating-sel)))
								(set! offsetline (+ offsetline layer-height))
								(set! offsetline (+ offsetline line-spacing))
							)
						)
					)
					(begin
						(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)
						(gimp-item-set-name floating-sel layer-name)
						;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))
								(set! offset (+ offset layer-width-half))
								(gimp-layer-translate floating-sel offset offsetline)
							)
							(begin ;second time and onward after that we add last calculated half
								   ;recalculate half add that then move it
                                (set! offset (+ offset letter-spacing))								   
								(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 offsetline)
								
								(set! linewidth (+ linewidth letter-spacing))
							)
						)
						(set! characters-layers (append characters-layers (list floating-sel)))
						(set! linewidth (+ linewidth (car (gimp-drawable-width floating-sel))))
						(set! count (+ count 1))
					)
				)
				(set! charList (cdr charList))
				
			) ;endwhile
			
			
			;FILLED JUSTIFICATION is a little more MATH :D
			(if (= justification 3) ;FILLED
				(begin 
					(while (not (null? lines))
						(set! characters-layers (car lines))
						(set! linewidth (list-ref widths item-index))
						(set! character-count (list-ref character-counts item-index))
						(set! move-x (/ (- maxwidth linewidth) (max (- character-count 1) 1)))
						(set! character-offset 0)
						(while (not (null? characters-layers))
							(set! move-layer (car characters-layers))
							
							(gimp-layer-translate move-layer character-offset 0)
						
							(set! character-offset (+ character-offset move-x))
							(set! characters-layers (cdr characters-layers))
						)
								
						(set! lines (cdr lines))
						(set! item-index (+ item-index 1))
					)
				)
			)
			
			
			
			
			
			
			(gimp-image-remove-layer new-image background-layer)
			(gimp-image-resize-to-layers new-image)
			(if (= merge TRUE)
				(begin
					(set! background-layer (car (gimp-image-merge-visible-layers new-image 0)))
					(plug-in-autocrop 1 new-image background-layer)
				)
			)
			;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"
  SF-TEXT "Multiline text" "Gimp Chat\nCustom Font\nIs Awesome"
  SF-OPTION "Justification" '("LEFT" "RIGHT" "CENTER" "FILLED")
  SF-ADJUSTMENT "Letter Spacing" '(0 -1000 1000 1 10 0 0)
  SF-ADJUSTMENT "Line Spacing" '(0 -1000 1000 1 10 0 0)
  SF-TOGGLE     "Merge Layers" TRUE
)
