; Spaced H-Guides
; This function creates a series of h-guides in the image at the user specified
; spacing
;
; The GIMP -- an image manipulation program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis 
;
; Spaced H Guides script  for GIMP 2.10.18
;
; Tags: spaced horizontal guides
;
; Author statement:
;
; Creates Spaced Horizontal Guides
;
; --------------------------------------------------------------------
; Distributed by ...
; --------------------------------------------------------------------
;   - Changelog -
; Updated to work with Gimp2.4 (11-2007)
; http://www.gimpscripts.com
;
; Updated to work with Gimp2.10.18 (05-2020)
;
; --------------------------------------------------------------------
;
;    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/>.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(define (script-fu-spaced-hguides image spacing origin)

   (let*
      (
            (y 0) ; will be computed h-guide position.
            (height (car (gimp-image-height image)))
            (center (/ height 2))
      )
        
        (gimp-context-push) ; Initiates the temporary state.
        
        ; Calculate starting point
        (set! y (calc-offset origin center height spacing))
        
        ; Iterate and create h-guides until y > height
        (while (< y height)
            (gimp-image-add-hguide image y)
            (set! y (+ y spacing))
        )
        
        ; DEBUG
        ;(gimp-message 
        ;   (string-append "Origin: " (number->string origin) " \nSpacing: " (number->string spacing)
        ;    "\nCenter: " (number->string center) "\nMod: " y
        ;   )
        ;)
        ; DEBUG
        
        ; Deactivates the temporary state and resets the previous user defaults.
        (gimp-context-pop) 
        (gimp-displays-flush)
   ) ; close let*
) ; close define


(define (calc-offset origin center height spacing)
   (cond 
        ((<= origin 0)  spacing) ; Top 
        ((<= origin 1)  (modulo center spacing)) ; Center
        ((<= origin 2)  (modulo height spacing)) ; Bottom
   )
)


; Finally register our script with script-fu.
(script-fu-register
    "script-fu-spaced-hguides"                   ;func name
    "Spaced H-Guides"                            ;menu label
    ;description
    "Create a series of horizontal guides with an even spacing. \nfile:JMB-Spaced-Guides.scm"
    "Jeffrey Boulais"                           ;author
    "copyright 2009, Jeffrey Boulais"           ;copyright notice
    "February 2008"                             ;date created
    "RGB* GRAY* INDEXED*"                       ;image type that the script works on
    SF-IMAGE "Image"                            0
    SF-VALUE "Horizontal Guide Spacing (px)"    "100"
    SF-OPTION "Start from"                      '("Top" "Center" "Bottom")
)

(script-fu-menu-register "script-fu-spaced-hguides" "<Toolbox>/Script-Fu/Setup/Guides")

;end of script