; v2022-06-18, works with gimp 2.10.32
; Resources:
; https://www.youtube.com/watch?v=n4OBn5DJdjk
; https://docs.gimp.org/en/gimp-using-script-fu-tutorial.html
; https://www.seul.org/~grumbel/gimp/script-fu/script-fu-tut.html
;==================================================
;
; Installation:
; This script should be placed in the user or system-wide script folder.
;
;	Windows 7/10
;	C:\Program Files\GIMP 2\share\gimp\2.0\scripts
;	or
;	C:\Users\YOUR-NAME\AppData\Roaming\GIMP\2.10\scripts
;	
;    
;	Linux
;	/home/yourname/.config/GIMP/2.10/scripts  
;	or
;	Linux system-wide
;	/usr/share/gimp/2.0/scripts
;
;==================================================
;
; LICENSE
;
;    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.
;
;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
;
;==============================================================

(script-fu-register
    "simple-embosed-water-mark-create"             ;func name
    "simple-embosed-water-mark-CREATE"             ;func label
    "Creates a simple water mark, to be embosed in other images. Its text is user choice.\nOne way to control the size of the watermark is by the font size.\nWatermarks created by this acript will be hardly, or not at all, observeable when embosed with the similar name -EMBOSE script to an area with little variation of color."                        ;description
    "Ron Varburg"                    ;author
    "copyright 2019-2022, Ron Varburg"    ;copyright notice
    "October 28, 2019"               ;date created
    ""                     ;image type that the script works on
    SF-TEXT        "Text"          "© www.net9.cf"   ;a string variable
    SF-FONT        "Font"          "Sans"       ;a font variable
    SF-ADJUSTMENT  "Font size"     '(80 1 1000 1 10 0 1)
                                                ;a spin-button
    SF-TOGGLE      "Copy result to clipboard"   TRUE
)
(script-fu-menu-register "simple-embosed-water-mark-create" "<Image>/File/Create/")

(define (simple-embosed-water-mark-create inText inFont inFontSize copyResultToClipboard)

    (let*
        ( 
	      ; define our local variables
              (imageWidth  10)   ; will be adjusted later
              (imageHeight 10)   ; will be adjusted later
              ; create a new image:
              (image (car (gimp-image-new
                           imageWidth
                           imageHeight
                           RGB
                          )
                     )
              )
              (textLayer1 (car (gimp-text-layer-new
                                image
                                inText
                                inFont
                                inFontSize
                                PIXELS
                               )
                          )
              )
              (textLayer2 -1)
              (textLayer3 -1)
              (topLayer -1)
              (bufferAmount 0.1)    ;0.1 for 10%
              (bufferHeight -1)
              (bufferWidth -1)
              (mblurLength 3)  ; value determined by trial and error 
              ; an non essential layer that can help debugging:
              (buttomLayer (car (gimp-layer-new
                                 image
                                 imageWidth
                                 imageHeight
                                 RGB-IMAGE
                                 "buttom layer"
                                 100
                                 LAYER-MODE-NORMAL
                                )
                           )
              )
        ) ; end of our local variables
        (gimp-image-insert-layer image buttomLayer 0 0)
        (gimp-context-set-background '(255 255 255) )
        (gimp-context-set-foreground '(128 128 128) )
        (gimp-drawable-fill buttomLayer BACKGROUND-FILL)
        (gimp-item-set-visible buttomLayer FALSE)
        (gimp-image-insert-layer image textLayer1 0 0)
        (set! imageWidth  (car (gimp-drawable-width  textLayer1) ) )
        (set! imageHeight  (car (gimp-drawable-height textLayer1) ) )
        (set! bufferHeight (* imageHeight bufferAmount) )
        (set! imageHeight  (+ imageHeight bufferHeight bufferHeight) )
        (set! bufferWidth  (* imageWidth bufferAmount) )
        (set! imageWidth  (+ imageWidth  bufferWidth bufferWidth) )
        (gimp-image-resize image imageWidth imageHeight 0 0)
        (gimp-layer-resize buttomLayer imageWidth imageHeight 0 0)
        (gimp-layer-set-offsets textLayer1 bufferWidth bufferHeight)
        (set! textLayer2 (car (gimp-layer-copy textLayer1 FALSE) ) )
        (gimp-image-insert-layer image textLayer2 0 0)
        ;(gimp-image-set-active-layer image textLayer1)
        (gimp-drawable-levels textLayer1
                              HISTOGRAM-VALUE
                              0 1
                              FALSE
                              1.0
                              1 1
                              FALSE
        )
        (plug-in-mblur RUN-NONINTERACTIVE
                       image
                       textLayer1
                       0
                       mblurLength
                       45 ;45 degrees. An arbitrary value.
                       (/ imageWidth 2)
                       (/ imageHeight 2)
        )
        (gimp-layer-set-offsets textLayer1 (* bufferWidth 0.993) (* bufferHeight 0.993) ) 
            ; 0.993 for small, <1, still arbitrary, motion
        (set! textLayer3 (car (gimp-layer-copy textLayer1 FALSE) ) )
        (gimp-image-insert-layer image textLayer3 0 0)
        (gimp-image-lower-item image textLayer3)
        (gimp-drawable-levels textLayer3
                              HISTOGRAM-VALUE
                              0 1
                              FALSE
                              1.0
                              0 0
                              FALSE
        )
        (gimp-layer-set-offsets textLayer3 (* bufferWidth 1.008) (* bufferHeight 1.008) ) 
            ; 1.008 for small, >1, still arbitrary, motion
        (set! topLayer (car (gimp-layer-new-from-visible image image "visible") ) )
        (gimp-image-insert-layer image topLayer 0 0)
        (gimp-display-new image)
        ;(gimp-image-clean-all image) ; Do ask whether or not to discard 
            ; changes that were not saved when closing.
        (if copyResultToClipboard (gimp-edit-copy-visible image) 0)
        (list image topLayer) ; Return value of this script
    )  

)
