; 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 2 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.

; Pre-conditions:
;   ChunkFive font must be installed
;   There is a directory ~/.gimp-2.8/scripts/data/
;   which contains the file mtg-logo.png
;   and the file creative-commons.png
;

(define (script-fu-sg-mtg-slate episode ; episode number as text
                                episode-title ; title as text
                                day ; "Today" or (1-31)
                                month
                                year 
                                save
                                filename)
  (define (calc-fontsize text font fontsize% width height)
    (let* ((fontsize 6) ;; minimum possible fontsize
           (extents nil)
           (last-extents nil)
           (last-fontsize 3)
           (adjust 2) )
      (set! extents (gimp-text-get-extents-fontname text fontsize PIXELS font))
      (set! width (* width fontsize% 0.01))
      (set! height (* height fontsize% 0.01))
      (while (and (<> last-fontsize fontsize) (not (equal? extents last-extents)))
        (if (or (> (car extents) width) (> (cadr extents) height))
          (begin 
            (set! fontsize last-fontsize)
            (set! adjust (+ (* (- adjust 1) 0.5) 1)) )
          (begin
            (set! last-extents extents)
            (set! last-fontsize fontsize) ) )
        (set! fontsize (truncate (* fontsize adjust)))
        (set! extents (gimp-text-get-extents-fontname text fontsize PIXELS font)) )
      (max fontsize 6) ))
  
  ; Some stuff that doesn't change (very often)
  (define title-fontname "ChunkFive")
  (define mtg-logo-filename "mtg-logo.png")
  (define cc-logo-filename "creative-commons.png")
  (define width 1280)
  (define height 720)
  (define data-dir (string-append gimp-directory
                                  DIR-SEPARATOR
                                  "scripts"
                                  DIR-SEPARATOR
                                  "data"
                                  DIR-SEPARATOR ))
  (define logo-file (string-append data-dir mtg-logo-filename))
  (define cc-file (string-append data-dir cc-logo-filename)) 
  (define bg-color '(9 158 143))
  (define title1-color '(255 151 74))
  (define title2-color '(255 179 122))
  (define shadow-color '(3 103 92))
  (define author "Rolf Steinort")
  (define location "Berlin")
  (define website "meetthegimp.org")
  (define title-font (if (zero? (car (gimp-fonts-get-list title-fontname)))
                       #f
                       title-fontname ))
  (let* ((image (car (gimp-image-new width height RGB)))
         (bg-layer (car (gimp-layer-new image width height RGBA-IMAGE "BG" 100 NORMAL-MODE))) )

    (let ((logo-layer (catch #f (car (gimp-file-load-layer RUN-NONINTERACTIVE image logo-file))))
          (cc-layer (catch #f (car (gimp-file-load-layer RUN-NONINTERACTIVE image cc-file)))) )
      (unless title-font 
        (gimp-message (string-append title-fontname ": font not found!")) )
      (unless logo-layer
        (gimp-message (string-append data-dir mtg-logo-filename ": MTG logo file not found!")) )
      (unless cc-layer
        (gimp-message (string-append data-dir cc-logo-filename ": CC file not found!")) )
      (if (not (and title-font logo-layer cc-layer))
        (gimp-image-delete image) 
        (begin
          (gimp-display-new image)
          (gimp-image-undo-disable image)
          (gimp-context-push)
          (gimp-context-set-foreground bg-color)
          (gimp-drawable-fill bg-layer FOREGROUND-FILL)
          (gimp-image-insert-layer image bg-layer 0 0)
          (gimp-image-insert-layer image logo-layer 0 0)
          (gimp-layer-set-offsets logo-layer
                                  (- (* width 0.25) (/ (car (gimp-drawable-width logo-layer)) 2))
                                  (/ (- height (car (gimp-drawable-height logo-layer))) 2) )
          (gimp-image-insert-layer image cc-layer 0 0)
          (gimp-layer-set-offsets cc-layer
                                  (- width 20 (car (gimp-drawable-width cc-layer)))
                                  (- height 10 (car (gimp-drawable-height cc-layer))) )
          (gimp-context-set-foreground title1-color)
          (let ((header-layer (car (gimp-text-fontname image 
                                                       -1 
                                                       (/ width 2.25) 
                                                       (/ height 10) 
                                                       "Meet the\nGIMP!"
                                                       0
                                                       TRUE
                                                       108
                                                       PIXELS
                                                       title-fontname ))))
            
            (gimp-text-layer-resize header-layer (/ width 1.9) 
                                                 (car (gimp-drawable-height header-layer)) )
            (gimp-text-layer-set-letter-spacing header-layer 5.0)
            (gimp-text-layer-set-justification header-layer TEXT-JUSTIFY-CENTER)
            (plug-in-vpropagate RUN-NONINTERACTIVE
                                image 
                                header-layer 
                                6 ; more opaque
                                15 ; all channels
                                1.0
                                15 
                                0
                                255
                                )
            (let ((header-shadow (car (gimp-layer-copy header-layer TRUE))))
              (gimp-image-add-layer image header-shadow -1)
              (gimp-layer-set-lock-alpha header-shadow TRUE)
              (gimp-context-set-foreground shadow-color)
              (gimp-edit-fill header-shadow FOREGROUND-FILL)
              (gimp-layer-set-offsets header-shadow
                                      (+ (car (gimp-drawable-offsets header-shadow)) 6)
                                      (+ (cadr (gimp-drawable-offsets header-shadow)) 6) )
              (gimp-image-lower-item image header-shadow)
              (gimp-layer-set-lock-alpha header-shadow FALSE)
              )
            )
          (gimp-context-set-foreground title2-color)
          (let ((episode-layer (car (gimp-text-fontname image 
                                                        -1 
                                                        (/ width 2.25) 
                                                        (/ height 2) 
                                                        (string-append "Episode " 
                                                                       (number->string episode) )
                                                        0
                                                        TRUE
                                                        61
                                                        PIXELS
                                                        title-fontname ))))
            (gimp-text-layer-resize episode-layer (/ width 1.9) 
                                                 (car (gimp-drawable-height episode-layer)) )
            (gimp-text-layer-set-justification episode-layer TEXT-JUSTIFY-CENTER)
            (gimp-layer-set-offsets episode-layer
                                    (car (gimp-drawable-offsets episode-layer))
                                    (/ (- height (car (gimp-drawable-height episode-layer)))
                                       2 ))
            (gimp-image-lower-item image episode-layer)
            (let ((episode-shadow (car (gimp-layer-copy episode-layer TRUE))))
              (gimp-image-insert-layer image episode-shadow 0 -1)
              (gimp-layer-set-lock-alpha episode-shadow TRUE)
              (gimp-context-set-foreground shadow-color)
              (gimp-edit-fill episode-shadow FOREGROUND-FILL)
              (gimp-layer-set-offsets episode-shadow
                                      (+ (car (gimp-drawable-offsets episode-shadow)) 6)
                                      (+ (cadr (gimp-drawable-offsets episode-shadow)) 6) )
              (gimp-image-lower-item image episode-shadow)
              (gimp-layer-set-lock-alpha episode-shadow FALSE)
              )
            )
            
          (gimp-context-set-foreground title1-color)
          (let* ((w (/ width 1.9))
                 (h (- (* height 0.9) (/ height 1.65)))
                 (fontsize (calc-fontsize episode-title title-fontname 100 w h)) )
            (let ((title-layer (car (gimp-text-fontname image 
                                                        -1 
                                                        (/ width 2.25) 
                                                        (/ height 1.65) 
                                                        episode-title
                                                        0
                                                        TRUE
                                                        (min fontsize 81)
                                                        PIXELS
                                                        title-fontname ))))
              (gimp-text-layer-resize title-layer (/ width 1.9) 
                                                   (car (gimp-drawable-height title-layer)) )
              (gimp-text-layer-set-justification title-layer TEXT-JUSTIFY-CENTER)
              (gimp-image-lower-item image title-layer)
              (let ((title-shadow (car (gimp-layer-copy title-layer TRUE))))
                  (gimp-image-insert-layer image title-shadow 0 -1)
                  (gimp-layer-set-lock-alpha title-shadow TRUE)
                  (gimp-context-set-foreground shadow-color)
                  (gimp-edit-fill title-shadow FOREGROUND-FILL)
                  (gimp-layer-set-offsets title-shadow
                                          (+ (car (gimp-drawable-offsets title-shadow)) 6)
                                          (+ (cadr (gimp-drawable-offsets title-shadow)) 6) )
                  (gimp-image-lower-item image title-shadow)
                  (gimp-layer-set-lock-alpha title-shadow FALSE)
                  )
              (gimp-context-set-foreground title2-color)
              (let ((date-layer (car (gimp-text-fontname image 
                                                         -1 
                                                         (/ width 2.25) 
                                                         (+ (cadr (gimp-drawable-offsets title-layer))
                                                            (car (gimp-drawable-height title-layer))
                                                            (/ height 70) )
                                                         (string-append (vector-ref #("Jan" "Feb" "Mar"       
                                                                                      "Apr" "May" "Jun"
                                                                                      "Jul" "Aug" "Sep"
                                                                                      "Oct" "Nov" "Dec" )
                                                                                    month )
                                                                        " "
                                                                        (number->string day)
                                                                        " "
                                                                        (number->string year) )
                                                         0
                                                         TRUE
                                                         41
                                                         PIXELS
                                                         title-fontname ))))
                (gimp-text-layer-resize date-layer (/ width 1.9) 
                                                     (car (gimp-drawable-height date-layer)) )
                (gimp-text-layer-set-justification date-layer TEXT-JUSTIFY-CENTER)
                (gimp-image-lower-item image date-layer)
                (let ((date-shadow (car (gimp-layer-copy date-layer TRUE))))
                  (gimp-image-insert-layer image date-shadow 0 -1)
                  (gimp-layer-set-lock-alpha date-shadow TRUE)
                  (gimp-context-set-foreground shadow-color)
                  (gimp-edit-fill date-shadow FOREGROUND-FILL)
                  (gimp-layer-set-offsets date-shadow
                                          (+ (car (gimp-drawable-offsets date-shadow)) 6)
                                          (+ (cadr (gimp-drawable-offsets date-shadow)) 6) )
                  (gimp-image-lower-item image date-shadow)
                  (gimp-layer-set-lock-alpha date-shadow FALSE)
                  )
                )
              )
            )
          ;; Main header 
          (gimp-displays-flush)
          (unless (zero? save)
            (let ((temp-image (car (gimp-edit-named-paste-as-new 
                                     (car (gimp-edit-named-copy-visible image "temp")) ))))
              (file-png-save2 RUN-NONINTERACTIVE 
                                temp-image 
                                (car (gimp-image-get-active-layer temp-image))
                                filename 
                                filename 
                                FALSE ; interlace
                                9
                                FALSE ; bkgd
                                (car (gimp-drawable-has-alpha 
                                       (car (gimp-image-get-active-layer temp-image)) ))
                                FALSE ; offs
                                FALSE ; phys
                                FALSE ; time
                                TRUE  ; comment
                                FALSE ; svtrans
                                )
              (gimp-image-delete temp-image) ))
          (gimp-image-set-active-layer image bg-layer)
          (gimp-image-undo-enable image)
          (gimp-context-pop)
          )
        )
      )
    )
  )
                                   
(script-fu-register "script-fu-sg-mtg-slate"
  "Meet the GIMP slate..."
  "Create slates for MTG videos"
  "Saul Goode"
  "Saul Goode"
  "Feb 2013"
  ""
  SF-ADJUSTMENT "Episode" '(186 1 500 1 10 0 1)
  SF-TEXT "Title" "A New Face"
  SF-ADJUSTMENT "Day" '(1 1 31 1 10 0 1)
  SF-OPTION "Month" '("January"     "February"     "March"       "April" 
                      "May"         "June"         "July"        "August" 
                      "September"   "October"      "November"    "December" )
  SF-ADJUSTMENT "Year" '(2013 2007 2025 1 10 0 1)
  SF-TOGGLE "Export PNG file?" FALSE
  SF-FILENAME "Export to PNG file" "/home/rolf/mtg186.png"
  )

(script-fu-menu-register "script-fu-sg-mtg-slate"
 "<Image>/File/Create/"
 )
