(define (script-fu-random-font image layer 
            font
            font-size		
            characters
            rotate	   ;Toggle rotates characters randomly
			custom     ;Toggle use defined colors (color1 color2)
			color1
			color2
			merge      ;Toggle to merge and cut to selection
			custom-string-toggle ;Toggle to use custom string or not
			custom-string
			active-layer-toggle ;Toggle to to use active layer instead 
         )
	
		(let* 
		   (
		   (width (car (gimp-image-width image)))
		   (height (car (gimp-image-height image)))
		   (count 0)
		   (text-layer 0)
		   (layer-width 0)
		   (layer-height 0)
		   (x 0)
		   (y 0)
		   (layer-count 0)
		   (layer-counter 0)
		   (layers 0)
		   (layer1 0)
		   (channel 0)
		   (c1r (car color1))
		   (c1g (cadr color1))
		   (c1b (caddr color1))
		   (c2r (car color2))
		   (c2g (cadr color2))
		   (c2b (caddr color2))
		   (r 0)
		   (g 0)
		   (b 0)
		   (rfrac (/(- c2r c1r) 256))
		   (gfrac (/(- c2g c1g) 256))
		   (bfrac (/(- c2b c1b) 256))
		   (randvalue 0)
		   (selection 0)
		   (alphabet '("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" "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"))
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			(gimp-context-push)
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			
			(set! selection (car (gimp-selection-bounds image)))
			(if (= selection FALSE)
				(begin
					(gimp-message "You must have an active selection!")
				)
				(begin
					
					
					(set! layer-count (car (gimp-image-get-layers image)))
					(set! layers (cadr (gimp-image-get-layers image)))
					;make all layers invisible
					(set! layer-counter 0)
					(while (< layer-counter layer-count)
						(set! layer1 (vector-ref layers layer-counter))
						(gimp-item-set-visible layer1 FALSE)
						(set! layer-counter (+ layer-counter 1))
					)
					
					(set! channel (car(gimp-selection-save image)))
					
					(while (< count characters)
						(set! x (rand width))
						(set! y (rand height))
						(if (> (car (gimp-selection-value image x y)) 0)
							(begin
						
							(if (= custom FALSE)
								(begin
									(gimp-context-set-foreground (list (rand 256)(rand 256)(rand 256)))
								)
								(begin
									(set! randvalue (rand 256))
									(set! r (+ c1r (* rfrac randvalue)))
									(set! g (+ c1g (* gfrac randvalue)))
									(set! b (+ c1b (* bfrac randvalue)))
									(gimp-context-set-foreground (list r g b))
								)
								c
							)
							(if (= custom-string-toggle FALSE)
								(begin
									(set! custom-string (list-ref alphabet (rand 52)))
								)
								(begin
								)
							)
							(if (= active-layer-toggle TRUE)
								(begin
									(gimp-image-select-item image CHANNEL-OP-REPLACE layer)
									(gimp-edit-copy layer)
									(set! text-layer (car (gimp-edit-paste layer FALSE)))
									(gimp-floating-sel-to-layer text-layer)
									
									
									;(set! text-layer (car (gimp-layer-new-from-drawable layer image)))
								)
								(begin
								
									(set! text-layer (car (gimp-text-layer-new image custom-string
															   font
															   font-size
															   0
														  )
													 )
									)
									(gimp-image-insert-layer image text-layer 0 0)
								)
							)
							
							
							(if (= active-layer-toggle TRUE)
								(begin
									(gimp-image-select-item image CHANNEL-OP-REPLACE text-layer)
									(gimp-edit-fill text-layer FOREGROUND-FILL)
								)
								(begin
								)
							)
							
							(if (= rotate TRUE)
								(begin 
									;select none in order to rotate text-layer
									(gimp-selection-none image)
									
									(set! text-layer (car (gimp-item-transform-rotate text-layer (/(*(*(rand 360)2)3.1415) 360)
																		   TRUE 0 0
														  )
													  )
									)
								)
								(begin
								)
							)
							
							(if (= active-layer-toggle TRUE)
								(begin
									(gimp-layer-translate text-layer (- x (/ width 2)) (- y (/ height 2)))
								)
								(begin
									(set! layer-width (/ (car (gimp-drawable-width text-layer)) 2.0))
									(set! layer-height (/ (car (gimp-drawable-height text-layer)) 2.0))
									(gimp-layer-translate text-layer (- x layer-width) (- y layer-height))
								)
							)
							
							
							(gimp-image-select-item image CHANNEL-OP-REPLACE channel)
							(set! count (+ count 1))
							)
							(begin
							)
						)
					)
					(if (= merge TRUE)
						(begin
							(set! text-layer (car (gimp-image-merge-visible-layers image 0)))
							(gimp-image-set-active-layer image text-layer)
							(gimp-selection-invert image)
							
							(set! selection (car (gimp-selection-bounds image)))
							(if (= selection TRUE)
								(begin
									(gimp-edit-cut text-layer)
								)
								(begin
								)
							)
							
							(gimp-selection-invert image)
						)
						(begin
						)
					)
					;make all original layers visible
					(set! layer-counter 0)
					(while (< layer-counter layer-count)
						(set! layer1 (vector-ref layers layer-counter))
						(gimp-item-set-visible layer1 TRUE)
						(set! layer-counter (+ layer-counter 1))
					)
				)
			) ;end of if selection
			
		   ;(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-random-font"         ;function name
  "<Image>/Script-Fu/Create New/Random Font ..."    ;menu register
  "Creates A random font background"       ;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-FONT "Font" "Sans"
  SF-ADJUSTMENT "Font Size(pixels)" '(50 10 8000 1 10 0 0)
  SF-ADJUSTMENT "Characters/Layers to generate" '(200 10 8000 1 10 0 0)
  SF-TOGGLE     "Randomly rotate" FALSE
  SF-TOGGLE     "Use custom colors instead of random colors (change below colors)" FALSE
  SF-COLOR      "Color 1" '(255 0 0)
  SF-COLOR      "Color 2" '(0 255 0)
  SF-TOGGLE     "Merge Layers and Cut to Selection" TRUE
  SF-TOGGLE     "Use custom string (below)" FALSE
  SF-STRING     "custom string" "Gimp Chat"
  SF-TOGGLE     "Use active layer instead of characters" FALSE
)
