#!/bin/bash
# -*- scheme -*-
exec guile -e main -s "$0" "$@"
!#

;;; Commentary:
;;
;; @section Overview
;;
;; @code{texi-to-pdf-presentation} is a command-line script offered by
;; Guile-Present that can transform a file written in a subset of
;; Texinfo into a presentation as a PDF file, suitable for presenting
;; with a PDF viewer.
;;
;; @code{texi-to-pdf-presentation} works by rendering each slide using
;; Guile-Cairo. You will need Guile-Cairo, and optionally guile-rsvg if
;; you include SVG images.
;;
;; @section Usage
;;
;; @deffn Command texi-to-pdf-presentation in-texi-file out-pdf-file
;;
;; Convert the Texinfo file @var{in-texi-file} into a PDF suitable for
;; presentations.
;;
;; This command is subject to the limitations of
;; @code{stexi->presentation}. Namely, only a subset of all Texinfo
;; constructs are supported. @xref{present texinfo
;; stexi->presentation,,stexi->presentation}, for more information.
;; @end deffn
;;
;;; Code:

(use-modules (present cairo)
             (present texinfo)
             (texinfo)
             (cairo)
             (sxml simple))

(define *pdf-width* 1024)
(define *pdf-height* 768)

(define (texi->pdf in out)
  (let* ((presentation (stexi->presentation
                        (call-with-file-and-dir in texi->stexi)))
         (surf (cairo-pdf-surface-create *pdf-width* *pdf-height* out))
         (cr (cairo-create surf)))
    (cairo-set-source-rgb cr 0 0 0)
    (presentation-render-cairo presentation cr)
    (cairo-surface-finish surf)))

(define (main args)
  (or (= (length args) 3)
      (begin
        (format (current-error-port) "usage: ~A TEXI-FILE OUTPUT-PDF-FILE\n"
                (car args))
        (exit 1)))
  (setlocale LC_ALL "")
  (apply texi->pdf (cdr args)))
