From 2c100ae2b2eb7dcabc2abc0963c8ae72a04322d3 Mon Sep 17 00:00:00 2001
From: Ekaitz Zarraga <ekaitz@elenq.tech>
Date: Thu, 23 Jul 2020 20:11:32 +0200
Subject: Massive rework on makefiles and cover generator:

    - New style in cover
    - Support for call from main Makefile
    - Reorder directory structure
---
 Makefile.scm            |  81 ++++--
 utils/cover/Makefile    |  14 -
 utils/cover/barcode.scm |  24 +-
 utils/cover/barcode.sld |   7 +
 utils/cover/cover.scm   | 716 +++++++++++++++++++++++-------------------------
 utils/cover/cover.sld   |   9 +
 6 files changed, 443 insertions(+), 408 deletions(-)
 delete mode 100644 utils/cover/Makefile
 create mode 100644 utils/cover/barcode.sld
 create mode 100644 utils/cover/cover.sld

diff --git a/Makefile.scm b/Makefile.scm
index cc36c8b..628b6a9 100644
--- a/Makefile.scm
+++ b/Makefile.scm
@@ -1,11 +1,16 @@
+#|
+#!/usr/bin/env sh
+exec chibi-scheme -A utils/ $0 $@
+|#
 (import (chibi)
         (chibi json)
-        (chibi filesystem)
+        (only (chibi filesystem) directory-files create-directory*)
         (chibi process)
-        (chibi pathname)
-        (chibi app)
-        (chibi string)
-        (chibi io)
+        (only (chibi pathname) path-strip-directory path-extension make-path)
+        ;(chibi app)
+        (only (chibi string) string-prefix? string-trim-right string-join)
+        (prefix (chibi io) io:)
+        (utils cover cover)
         (srfi 1)
         (srfi 16)
         (srfi 95))
@@ -35,7 +40,7 @@
         (begin
           (if debug
             (display
-              (string-append "\nDEBUG: In call: " strcall "\n" err)
+              (string-append "\nDEBUG: Called: " strcall "\n" err)
               (current-error-port)))
           out)
         (error (string-append
@@ -45,13 +50,13 @@
                  "\n" err)))))
 
 (define (read-meta file)
-  (parse-json
+  (string->json
     (process->string-or-fail
       `("./utils/yaml2json.py" ,file)
       "Error parsing Metadata file")))
 
 (define (pandoc-web-simple lang output input)
-  (let ((outpath  (make-path output "WebSimple"))
+  (let ((outpath  (make-path output lang))
         (files    (directory-sources lang input))
         (metafile (directory-meta lang input)))
     (create-directory* outpath)
@@ -59,7 +64,7 @@
       (append
         `("pandoc"
           "--to=html"
-          "-o" ,(make-path outpath (string-append lang ".html"))
+          "-o" ,(make-path outpath "web-simple.html")
           "--standalone"
           "--no-highlight"
           "--data-dir=."
@@ -75,7 +80,7 @@
 
 
 (define (pandoc-web lang output input)
-  (let ((outpath  (make-path output "Web"))
+  (let ((outpath  (make-path output lang))
         (files    (directory-sources lang input))
         (metafile (directory-meta lang input)))
     (create-directory* outpath)
@@ -83,7 +88,7 @@
       (append
         `("pandoc"
           "--to=html"
-          "-o" ,(make-path outpath (string-append lang ".html"))
+          "-o" ,(make-path outpath "web.html")
           "--standalone"
           "--self-contained"
           "--base-header-level=2" ;"--shift-heading-level-by=-1" is the new way
@@ -97,17 +102,17 @@
           "--lua-filter=appendixes.lua")
         files
         `("--metadata-file" ,metafile))
-      "Error in web-page creation")))
+      "Error in simple web-page creation")))
 
 (define (pandoc-book lang output input)
-  (let ((outpath  (make-path output "Book"))
+  (let ((outpath  (make-path output lang))
         (files    (directory-sources lang input))
         (metafile (directory-meta lang input)))
     (create-directory* outpath)
     (process->string-or-fail
       (append
         `("pandoc"
-          "-o" ,(make-path outpath (string-append lang ".pdf"))
+          "-o" ,(make-path outpath "book.pdf")
           "--standalone"
           "--pdf-engine=xelatex"
           "--data-dir=."
@@ -118,15 +123,34 @@
         `("--metadata-file" ,metafile))
       "Error in book creation")))
 
+(define (pandoc-book-simple lang output input)
+  (let ((outpath  (make-path output lang))
+        (files    (directory-sources lang input))
+        (metafile (directory-meta lang input)))
+    (create-directory* outpath)
+    (process->string-or-fail
+      (append
+        `("pandoc"
+          "-o" ,(make-path outpath "book-simple.pdf")
+          "--standalone"
+          "--pdf-engine=xelatex"
+          "--data-dir=."
+          ,(string-append "--resource-path=" input)
+          "--template=book-simple.latex"
+          "--lua-filter=appendixes.lua")
+        files
+        `("--metadata-file" ,metafile))
+      "Error in simple book creation")))
+
 (define (pandoc-epub lang output input)
-  (let ((outpath  (make-path output "Epub"))
+  (let ((outpath  (make-path output lang))
         (files    (directory-sources lang input))
         (metafile (directory-meta lang input)))
     (create-directory* outpath)
     (process->string-or-fail
       (append
         `("pandoc"
-          "-o" ,(make-path outpath (string-append lang ".epub"))
+          "-o" ,(make-path outpath "book.epub")
           "--standalone"
           "--data-dir=."
           ,(string-append "--resource-path=" input)
@@ -135,7 +159,27 @@
           "--lua-filter=appendixes.lua")
         files
         `("--metadata-file" ,metafile))
-      "Error in book creation")))
+      "Error in ebook creation")))
+
+(define (make-cover lang out input)
+  (let* ((outpath  (make-path output lang))
+         (files    (directory-sources lang input))
+         (metafile (directory-meta lang input))
+         (metadata (read-meta metafile))
+         (svg-file (make-path outpath "cover.svg")))
+    (create-directory* outpath)
+    (call-with-output-file
+      svg-file
+      (lambda (out)
+        (metadata->svg metadata out)))
+    (process->string-or-fail
+      (append
+        `("inkscape"
+          "--export-filename" ,(make-path outpath "cover.pdf")
+          "--export-type=pdf"
+          ,svg-file))
+      "Error in cover creation")))
+
 ;; RUN THIS LIKE:
 ;; chibi-scheme Makefile.scm src/BOOK_FOLDER
 ;;
@@ -150,7 +194,8 @@
 (pandoc-web "es" output input)
 (pandoc-web-simple "es" output input)
 (pandoc-book "es" output input)
+(pandoc-book-simple "es" output input)
 (pandoc-epub "es" output input)
+(make-cover "es" output input)
 
-;(display (process->string-or-fail '("./utils/yaml2json.py"  "src/Programming_in_Python/es/Metadata.yaml")  ""))
 ;(display (cdr (assoc 'title (read-meta "src/Programming_in_Python/es/Metadata.yaml"))))
diff --git a/utils/cover/Makefile b/utils/cover/Makefile
deleted file mode 100644
index 2fddab0..0000000
--- a/utils/cover/Makefile
+++ /dev/null
@@ -1,14 +0,0 @@
-TARGET_PDF  = cover.pdf
-
-.PHONY: pdf
-pdf: $(TARGET_PDF)
-
-$(TARGET_PDF): cover.svg
-	inkscape $^ --export-pdf=$@
-
-cover.svg: cover.scm # Cover data too
-	chibi-scheme cover.scm > cover.svg
-
-.PHONY: clean
-clean:
-	rm $(TARGET_PDF) cover.svg
diff --git a/utils/cover/barcode.scm b/utils/cover/barcode.scm
index 85f28c2..b016e27 100644
--- a/utils/cover/barcode.scm
+++ b/utils/cover/barcode.scm
@@ -86,7 +86,8 @@
                                  (number->string height)))
          (encoded (barcode code)))
     `(g
-       (@ (transform ,(string-append "translate(" (number->string x) "," (number->string y) ")"
+       (@ (transform ,(string-append "translate(" (number->string x) ","
+                                     (number->string y) ")"
                                      "scale(" (number->string scale) ")")))
 
        ,(map
@@ -105,14 +106,17 @@
 
        ,(map
           (lambda (char pos)
-            `(text (@ (style ,(string-append "font-size: " (number->string text-size) ";"
-                                             "text-anchor: middle"))
-                      (x ,(cond ((= 0 pos)    (* 3.5 colwidth))
-                                ((<= 1 pos 6) (+ border (* (+ 3 (* (- pos 0.5) 7)) colwidth)))
-                                ((< 6 pos)    (+ border (* (+ 3 5 (* (- pos 0.5) 7)) colwidth)))))
-                      (y  ,height))
-                   ,(list->string (list char))))
+            `(text
+               (@ (style ,(string-append "font-size: "
+                                         (number->string text-size) ";"
+                                         "text-anchor: middle"))
+                  (x ,(cond
+                        ((= 0 pos)    (* 3.5 colwidth))
+                        ((<= 1 pos 6) (+ border (* (+ 3 (* (- pos 0.5) 7))
+                                                   colwidth)))
+                        ((< 6 pos)    (+ border (* (+ 3 5 (* (- pos 0.5) 7))
+                                                   colwidth)))))
+                  (y  ,height))
+               ,(list->string (list char))))
           (string->list code)
           (iota (string-length code))))))
-
-#;(display (barcode-svg "9780201379624"))
diff --git a/utils/cover/barcode.sld b/utils/cover/barcode.sld
new file mode 100644
index 0000000..05fabc3
--- /dev/null
+++ b/utils/cover/barcode.sld
@@ -0,0 +1,7 @@
+(define-library
+  (utils cover barcode)
+  (import (chibi)
+          (chibi sxml)
+          (srfi 1))
+  (export barcode-sxml)
+  (include "barcode.scm"))
diff --git a/utils/cover/cover.scm b/utils/cover/cover.scm
index aa2035c..cde9c10 100644
--- a/utils/cover/cover.scm
+++ b/utils/cover/cover.scm
@@ -1,53 +1,3 @@
-(import (scheme base)
-        (scheme char)
-        (chibi)
-        (chibi string)
-        (chibi sxml)
-        (srfi 1))
-
-(include "barcode.scm")
-; GUILE
-#;(use-modules (sxml simple)
-             (srfi srfi-1))
-
-;; CONTENTS
-(define isbn "9780201379624")
-(define book-title "Programación en Python")
-(define book-subtitle "Introducción a la programación y al lenguaje")
-(define book-title-before "") ; TODO: Thinking about removing this
-(define book-title-word  book-title)
-(define book-title-after book-subtitle)
-(define book-category "Básico")
-(define book-authors '("Giacomo Tesio" "Ekaitz Zarraga"))
-(define book-summary "
-Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus vitae rhoncus
-augue. Donec blandit ligula dui, vitae commodo sem sodales ac. Donec varius sed
-ipsum vitae gravida. Morbi nec ipsum quis sapien commodo lacinia. Sed at risus
-at nisl mollis consectetur ac non mauris. Pellentesque ipsum leo, accumsan
-scelerisque convallis ut, condimentum nec libero. Class aptent taciti sociosqu
-ad litora torquent per conubia nostra, per inceptos himenaeos. Cras pharetra
-rhoncus eros, eu auctor nibh sollicitudin quis. Praesent sodales ultricies ex.
-Aliquam erat volutpat.
-
-Donec aliquam faucibus enim sit amet facilisis. Pellentesque at pretium risus.
-Nullam ac vehicula purus, sed pharetra magna. Morbi mollis luctus nisi vel
-porta. Duis tempus erat eu ante congue consequat. In ac leo sem. Quisque
-bibendum bibendum rutrum. Fusce interdum in metus sed congue. Sed nunc mauris,
-mattis vitae fermentum eu, dignissim vitae nulla. Donec blandit at mi eu
-luctus. Fusce pharetra ipsum fringilla quam faucibus, ut lobortis risus varius.
-Ut nec sem lectus. Integer porta justo a augue sodales, nec malesuada libero
-varius. Sed convallis cursus efficitur. Cras maximus, ligula et dapibus
-ultrices, sapien lectus pellentesque purus, nec dignissim augue dolor eu
-ligula. Morbi consequat fringilla mauris, vitae aliquet metus sodales in.
-  ")
-(define company-info "Lorem ipsum dolor sit amet, consectetur adipiscing elit,
-  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
-  minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
-  commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
-  esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
-  cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
-  laborum.")
-
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; UTILS                                                                      ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -60,45 +10,12 @@ ligula. Morbi consequat fringilla mauris, vitae aliquet metus sodales in.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 ; SIZE
-; Take spine from the outside because it's related to the amount of pages
-(define spine-width  5) ;mm
-(define spine-title-margin-min (* 0.1 spine-width))
-(define spine-title-max-size   10)
-(define spine-title-size
-  (let ((size (- spine-width (* 2 spine-title-margin-min))))
-    (if (> size spine-title-max-size)
-        spine-title-max-size
-        size)))
-
-;B5 Paper size
-(define page-height 250) ;mm
-(define page-width  176) ;mm
-(define margin        3) ;mm
-
-(define text-size   (exact->inexact (/ page-height 70))); mm
-(define category-size   (* 1.5 text-size)); mm
-(define authors-size (* 1.8 text-size))
-(define title-size   (* 2.5 text-size)); mm
-(define main-title-size ; mm
-  (let ((size (exact->inexact
-               (* 3
-                  (/ page-width
-                     (length (string->list book-title-word)))))))
-    (if (< (/ page-height 4) size)
-        (exact->inexact (/ page-height 4))
-        size)))
-(define main-subtitle-size (* 2 text-size))
-
-(define width  (+ (* 2 margin) (* 2 page-width) spine-width)) ;mm
-(define height (+ (* 2 margin) page-height)) ;mm
-(define viewBox (string-append "0 0 "
-                               (num width)
-                               " "
-                               (num height)))
 ; INFO: Setting viewbox and size to the same it makes the measurements real
 ; world ones, in this case millimeters
 ; Read more: https://mpetroff.net/2013/08/analysis-of-svg-units/
 
+
+
 (define (elenq-logo sub size x y . extra) ; 10 x 4mm
   `(g (@ (id "elenq-logo")
         (transform
@@ -121,85 +38,7 @@ ligula. Morbi consequat fringilla mauris, vitae aliquet metus sodales in.
 ;(define elenq-technology (elenq-logo "TECHNOLOGY" 1 0 0))
 ;(define elenq-publishing (elenq-logo "PUBLISHING" 1 0 0))
 
-(define style
-  (string-append "
-  .margin-mark {
-    stroke-width: 0.1;
-    stroke-linecap: butt;
-    stroke: red;
-  }
-  .title-back {
-    font-family: 'Pathway Gothic One';
-    font-size: "(num title-size)";
-    fill: white;
-  }
-  .title-spine {
-    font-family: 'Pathway Gothic One';
-    font-size: "(num spine-title-size)";
-    dominant-baseline: mathematical;
-    fill: black;
-  }
-  .authors-spine {
-    font-family: 'Lato';
-    font-size: "(num text-size)";
-    dominant-baseline: mathematical;
-    text-anchor: end;
-  }
-  #black-part {
-    fill: black;
-  }
-  .text-elenq{
-    font-family:Lato;
-    -inkscape-font-specification:'Lato Light Italic';
-    font-style: italic;
-    font-weight: 300;
-    fill: black;
-    font-size: "(num text-size)";
-    text-align: justify;
-  }
-  .text-summary {
-    font-family:Lato;
-    font-size: "(num text-size)";
-    fill: white;
-    text-align: justify;
-  }
-  rect.category{
-    fill: black;
-  }
-  text.category{
-    fill: white;
-    font-size: "(num category-size)";
-    -inkscape-font-specification:'Lato Light';
-    font-family:Lato;
-    font-weight: 300;
-  }
-  .main-title {
-    font-size: "(num main-title-size)";
-    font-family: 'Pathway Gothic One';
-  }
-  .main-title-before {
-    font-size: "(num (* 1 title-size))";
-    font-family: 'Pathway Gothic One';
-    font-weight:300;
-  }
-  .main-title-after {
-    font-size: "(num main-subtitle-size)";
-    -inkscape-font-specification:'Lato Light Italic';
-    line-height: 0.9;
-    font-style: italic;
-    font-family:Lato;
-    font-weight: 300;
-    text-anchor: end;
-  }
-  .author {
-    font-size: "(num authors-size)";
-    font-family:Lato;
-    font-style:normal;
-    font-stretch:normal;
-    font-variant:normal;
-    text-anchor: end;
-  }
-  "))
+
 
 ; Text area, Inkscape-only svg parameter
 (define (textArea text class x y w h)
@@ -229,205 +68,350 @@ ligula. Morbi consequat fringilla mauris, vitae aliquet metus sodales in.
                  text
                  #\newline))))))
 
-(display "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>")
-(display (sxml->xml
-  `(svg
-     (@ (width ,(mm width))
-        (height ,(mm height))
-        (viewBox ,viewBox))
-     (defs
-       (style ,style))
-
-
-     ; BLACK BACKGROUND FOR SUMMARY
-     (g (@ (transform
-             ,(string-append
-                ; TODO extender lo negro en el margen
-                "translate( 0," (num (+ margin (* 0.05 page-height))) ")
-                scale(" (num (+ margin (* 0.95 page-width))) ")")))
-        (path (@ (id "black-part")
-                 (d "M 1.00, 0.10 L 0.30, 0.00 L 0.00, 0.16 L 0.00, 0.67 L 0.45, 0.76 L 0.97, 0.67 Z"))))
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-     ;; CUTTING MARKS                                                         ;;
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-     ,(map (lambda (posx posy rot)
-             `(g (@ (id ,(string-append "marks-"
-                                        (num posx)
-                                        "-"
-                                        (num posy)))
-                    (transform ,rot))
-                 (line (@
-                         (x1 ,(+ posx))
-                         (y1 ,(+ posy margin))
-                         (x2 ,(+ posx (* 0.6 margin)))
-                         (y2 ,(+ posy margin))
-                         (class "margin-mark")))
-                 (line (@
-                         (x1 ,(+ posx margin))
-                         (y1 ,(+ posy))
-                         (x2 ,(+ posx margin))
-                         (y2 ,(+ posy (* 0.6 margin)))
-                         (class "margin-mark")))))
-
-           (list 0 0      width width)
-           (list 0 height 0     height)
-           (list "rotate(0,0,0)"
-                 (string-append "rotate(-90,   0," (num height) ")")
-                 (string-append "rotate(-270," (num width) ", 0)")
-                 (string-append "rotate(-180," (num width) "," (num height) ")")))
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-     ;; BACK SIDE                                                             ;;
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-     ; TITLE + BOOK SUMMARY
-     (text (@ (class "title-back")
-              (x ,(+ margin (* 0.15 page-width)))
-              (y ,(+ margin (* page-height 0.20))))
-           ,(string-upcase book-title))
-
-
-     ,(textArea book-summary
-                "text-summary"
-                (+ margin (* 0.15 page-width))
-                (+ margin (* page-height 0.20))
-                (* 0.7 page-width)
-                (* 0.25 page-height))
-
-
-     ; LOGO + ELENQ SUMMARY
-     ,(elenq-logo "TECHNOLOGY"
-                  3
-                  (* (+ (+ margin (* 0.5 page-width))
-                        (+ margin (* 0.5 page-width)
-                           (- (* 0.5 page-width) (* 0.1 page-width)))) 0.5)
-                  (+ margin (* 0.70 page-height) -10))
-     ,(textArea company-info
-                "text-elenq"
-                (+ margin (* 0.5 page-width))
-                (+ margin (* 0.70 page-height))
-                (- (* 0.5 page-width) (* 0.1 page-width))
-                (- (* 0.25 page-height) (* 0.1 page-height)))
-
-
-     ; BARCODE
-     ,(barcode-sxml isbn
-                    (+ margin (* 0.1 page-width))
-                    (+ margin (* 0.75 page-height))
-                    (* 0.25 page-width))
-
-
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-     ;; SPINE                                                                 ;;
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-     ,(let ((x (+ margin page-width))
-            (y height)
-            (width (+ margin margin page-height))
-            (height spine-width))
-        `(g (@ (transform
-                 ,(string-append "translate(" (num x) "," (num y) ") rotate (-90)")))
-            (rect (@ (height ,height)
-                     (width ,width)
-                     (style "fill: white;")))
-
-            (text (@ (class "title-spine")
-                     (x      ,(* 0.2 width))
-                     (y      ,(* 0.5 spine-width))
-                     (width  ,(* 0.3 width))
-                     (height ,spine-title-size))
-                  ,book-title)
-            (text (@ (class "authors-spine")
-                     (x      ,(* 0.9 width))
-                     (y      ,(* 0.5 spine-width))
-                     (width  ,(* 0.3 width))
-                     (height ,spine-title-size))
-                  ,(string-join book-authors ", "))))
-
-
-     ,(if (< spine-width 10)
-          ; Smaller than 10mm -> Renders just a Q
-          `(text (@ (style ,(string-append "font-family: armata;
-                                           text-anchor: middle;
-                                           font-size: " (num spine-width)))
-                   (x ,(+ margin page-width (* 0.5 spine-width)))
-                   (y ,(+ margin (* 0.95 page-height))))
-                "Q")
-          (elenq-logo "PUBLISHING"
-                      ; Max logo size is as the spine was 40mm thick
-                      (* (if (< 40 spine-width) 40 spine-width) 0.1 0.75)
-                      (+ margin page-width (* 0.5 spine-width)) ;anchor is centered
-                      (+ margin (* 0.95 page-height))))
-
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-     ;; FRONT SIDE                                                            ;;
-     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-     ,(let* ((win (* 0.35 page-width))
-             (w (+ win margin))
-             (h (* 1.5 category-size))
-             (x (- width margin win))
-             (y (+ margin (* 0.15 page-height))))
-        `(g
-           (rect (@ (class "category")
-                    (x ,x)
-                    (y ,y)
-                    (width ,w)
-                    (height ,h)))
-
-           (text
-             (@ (class "category")
-                (x ,(+ x (* 0.10 w)))
-                (y ,(+ y (* 0.75 h)))
-                (w ,w)
-                (h ,h))
-             ,book-category)))
-
-     (text (@ (class "main-title-before")
-              (x ,(exact->inexact (- width
-                                     (- page-width (/ page-width 8))
-                                     margin)))
-              (y ,(+ margin (* 0.30 page-height))))
-           ,book-title-before)
-
-     (text (@ (class "main-title")
-              (textLength ,(* page-width 6/8))
-              (lengthAdjust ,"spacingAndGlyphs")
-              (x ,(exact->inexact (- width
-                                     (- page-width (/ page-width 8))
-                                     margin)))
-              (y ,(+ main-title-size margin (* 0.30 page-height))))
-           ,(string-upcase book-title-word))
-
-     #;(text (@ (class "main-title-after")
-              (x ,(exact->inexact (- width  (/ page-width 8) margin)))
-              (y ,(+ main-title-size
-                     (* 1.25 title-size)
-                     margin
-                     (* 0.30 page-height))))
-           ,book-title-after)
-
-     ; TODO WORK ON THIS
-     ,(textArea book-title-after
-                "main-title-after"
-                (exact->inexact (- width  (- page-width (/ page-width 8)) margin))
-                (+ main-title-size
-                   (* main-subtitle-size 0.5)
-                   margin
-                   (* 0.30 page-height))
-                (* page-width 6/8)
-                (* 2 1.25 main-subtitle-size))
-
-     ,(map (lambda (author pos)
-             `(text (@ (class "author")
-                       (x ,(exact->inexact (- width (/ page-width 8) margin)))
-                       (y ,(+ main-title-size
-                              margin
-                              (* 0.35 page-height)
-                              (* (+ 2 pos) (* 1.5 authors-size)))))
-                    ,author))
-           book-authors
-           (iota (length book-authors)))
-
-     ,(elenq-logo "PUBLISHING"
-                  3
-                  (exact->inexact (- width (/ page-width 2) margin))
-                  (+ margin (* 0.95 page-height))))))
+
+(define (metadata->svg metadata outport)
+
+  (let* (;B5 Paper size
+         (page-height 250) ;mm
+         (page-width  176) ;mm
+         (margin        3) ;mm
+
+         (isbn "9780201379624")
+         (book-title "Programación en Python")
+         (book-subtitle "Introducción a la programación y al lenguaje")
+         (book-title-before "") ; TODO: Thinking about removing this
+         (book-title-after book-subtitle)
+         (book-category "Básico")
+         (book-authors '("Giacomo Tesio" "Ekaitz Zarraga"))
+         (book-summary " ")
+         (company-info "")
+
+         (main-title-size
+           (let ((size (exact->inexact
+                         (* 3
+                            (/ page-width
+                               (length (string->list book-title)))))))
+             (if (< (/ page-height 5) size)
+                 (exact->inexact (/ page-height 5))
+                 size))) ;mm
+
+         (spine-width  10) ;mm TODO
+         (spine-title-margin-min (* 0.1 spine-width))
+         (spine-title-max-size   10)
+         (spine-title-size
+           (let ((size (- spine-width (* 2 spine-title-margin-min))))
+             (if (> size spine-title-max-size)
+                 spine-title-max-size
+                 size)))
+
+
+         (text-size       (exact->inexact (/ page-height 70))); mm
+         (category-size   (* 1.5 text-size)); mm
+         (authors-size    (* 1.8 text-size))
+         (title-size      (* 2.5 text-size)); mm
+
+         (main-subtitle-size (* 2 text-size))
+         (main-title-width   (exact->inexact (* 6/8 page-width)))
+         (main-title-margin  (/ (- page-width main-title-width) 2))
+
+         (width   (+ (* 2 margin) (* 2 page-width) spine-width)) ;mm
+         (height  (+ (* 2 margin) page-height)) ;mm
+         (viewBox (string-append "0 0 " (num width) " " (num height)))
+         (style
+           (string-append
+             "
+             .margin-mark {
+             stroke-width: 0.1;
+             stroke-linecap: butt;
+             stroke: red;
+             }
+             .title-back {
+             font-family: 'Pathway Gothic One';
+             font-size: "(num title-size)";
+             fill: white;
+             }
+             .title-spine {
+             font-family: 'Pathway Gothic One';
+             font-size: "(num spine-title-size)";
+             dominant-baseline: mathematical;
+             fill: black;
+             }
+             .authors-spine {
+             font-family: 'Lato';
+             font-size: "(num text-size)";
+             dominant-baseline: mathematical;
+             text-anchor: end;
+             }
+             #black-part {
+             fill: black;
+             }
+             .text-elenq{
+             font-family:Lato;
+             -inkscape-font-specification:'Lato Light Italic';
+             font-style: italic;
+             font-weight: 300;
+             fill: black;
+             font-size: "(num text-size)";
+             text-align: justify;
+             }
+             .text-summary {
+             font-family:Lato;
+             font-size: "(num text-size)";
+             fill: white;
+             text-align: justify;
+             }
+             rect.category{
+             fill: black;
+             }
+             text.category{
+             fill: white;
+             font-size: "(num category-size)";
+             -inkscape-font-specification:'Lato Light';
+             font-family:Lato;
+             font-weight: 300;
+             }
+             .main-title {
+             font-size: "(num main-title-size)";
+             font-family: 'Pathway Gothic One';
+             }
+             .main-title-before {
+             font-size: "(num (* 1 title-size))";
+             font-family: 'Pathway Gothic One';
+             font-weight:300;
+             }
+             .main-title-after {
+             font-size: "(num main-subtitle-size)";
+             -inkscape-font-specification:'Lato Light Italic';
+             line-height: 0.9;
+             font-style: italic;
+             font-family:Lato;
+             font-weight: 300;
+             text-anchor: middle;
+             }
+             .author {
+             font-size: "(num authors-size)";
+             font-family:Lato;
+             text-anchor: end;
+             }
+             ")))
+
+    (display "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" outport)
+    (display (sxml->xml
+     `(svg
+        (@ (width ,(mm width))
+           (height ,(mm height))
+           (viewBox ,viewBox))
+        (defs
+          (style ,style))
+
+
+        ; BLACK BACKGROUND FOR SUMMARY
+        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+        (g (@ (transform
+                ,(string-append
+                   ; TODO extender lo negro en el margen
+                   "translate( 0," (num (+ margin (* 0.05 page-height))) ")
+                   scale(" (num (+ margin (* 0.95 page-width))) ")")))
+           (path
+             (@ (id "black-part")
+                (d "M 1.00,
+                    0.10 L 0.30,
+                    0.00 L 0.00,
+                    0.16 L 0.00,
+                    0.67 L 0.45,
+                    0.76 L 0.97,
+                    0.67 Z"))))
+
+        ;; CUTTING MARKS
+        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+        ,(map (lambda (posx posy rot)
+                `(g (@ (id ,(string-append "marks-"
+                                           (num posx)
+                                           "-"
+                                           (num posy)))
+                       (transform ,rot))
+                    (line (@
+                            (x1 ,(+ posx))
+                            (y1 ,(+ posy margin))
+                            (x2 ,(+ posx (* 0.6 margin)))
+                            (y2 ,(+ posy margin))
+                            (class "margin-mark")))
+                    (line (@
+                            (x1 ,(+ posx margin))
+                            (y1 ,(+ posy))
+                            (x2 ,(+ posx margin))
+                            (y2 ,(+ posy (* 0.6 margin)))
+                            (class "margin-mark")))))
+
+              (list 0 0      width width)
+              (list 0 height 0     height)
+              (list "rotate(0,0,0)"
+                    (string-append "rotate(-90,   0," (num height) ")")
+                    (string-append "rotate(-270," (num width) ", 0)")
+                    (string-append "rotate(-180," (num width) ","
+                                                  (num height) ")")))
+
+
+        ;; BACK SIDE 
+        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+        ; TITLE + BOOK SUMMARY
+        (text (@ (class "title-back")
+                 (x ,(+ margin (* 0.15 page-width)))
+                 (y ,(+ margin (* page-height 0.20))))
+              ,(string-upcase book-title))
+
+
+        ,(textArea book-summary
+                   "text-summary"
+                   (+ margin (* 0.15 page-width))
+                   (+ margin (* page-height 0.20))
+                   (* 0.7 page-width)
+                   (* 0.25 page-height))
+
+
+        ; LOGO + ELENQ SUMMARY
+        ,(elenq-logo "TECHNOLOGY"
+                     3
+                     (* (+ (+ margin (* 0.5 page-width))
+                           (+ margin (* 0.5 page-width)
+                              (- (* 0.5 page-width) (* 0.1 page-width)))) 0.5)
+                     (+ margin (* 0.70 page-height) -10))
+        ,(textArea company-info
+                   "text-elenq"
+                   (+ margin (* 0.5 page-width))
+                   (+ margin (* 0.70 page-height))
+                   (- (* 0.5 page-width) (* 0.1 page-width))
+                   (- (* 0.25 page-height) (* 0.1 page-height)))
+
+
+        ; BARCODE
+        ,(barcode-sxml isbn
+                       (+ margin (* 0.1 page-width))
+                       (+ margin (* 0.75 page-height))
+                       (* 0.25 page-width))
+
+
+        ;; SPINE
+        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+        ,(let ((x (+ margin page-width))
+               (y height)
+               (width (+ margin margin page-height))
+               (height spine-width))
+           `(g (@ (transform
+                    ,(string-append "translate(" (num x) "," (num y) ")
+                                    rotate (-90)")))
+               (rect (@ (height ,height)
+                        (width ,width)
+                        (style "fill: white;")))
+
+               (text (@ (class "title-spine")
+                        (x      ,(* 0.2 width))
+                        (y      ,(* 0.5 spine-width))
+                        (width  ,(* 0.3 width))
+                        (height ,spine-title-size))
+                     ,book-title)
+               (text (@ (class "authors-spine")
+                        (x      ,(* 0.9 width))
+                        (y      ,(* 0.5 spine-width))
+                        (width  ,(* 0.3 width))
+                        (height ,spine-title-size))
+                     ,(string-join book-authors ", "))))
+
+
+        ,(if (< spine-width 10)
+             ; Smaller than 10mm -> Renders just a Q
+             `(text (@ (style ,(string-append "font-family: armata;
+                                              text-anchor: middle;
+                                              font-size: " (num spine-width)))
+                      (x ,(+ margin page-width (* 0.5 spine-width)))
+                      (y ,(+ margin (* 0.95 page-height))))
+                   "Q")
+             (elenq-logo "PUBLISHING"
+                         ; Max logo size is as the spine was 40mm thick
+                         (* (if (< 40 spine-width) 40 spine-width) 0.1 0.75)
+                         (+ margin page-width (* 0.5 spine-width)) ;anchor is centered
+                         (+ margin (* 0.95 page-height))))
+
+
+        ;; FRONT SIDE
+        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+        ,(let* ((win (* 0.35 page-width))
+                (w (+ win margin))
+                (h (* 1.5 category-size))
+                (x (- width margin win))
+                (y (+ margin (* 0.15 page-height))))
+           `(g
+              (rect (@ (class "category")
+                       (x ,x)
+                       (y ,y)
+                       (width ,w)
+                       (height ,h)))
+
+              (text
+                (@ (class "category")
+                   (x ,(+ x (* 0.10 w)))
+                   (y ,(+ y (* 0.75 h)))
+                   (w ,w)
+                   (h ,h))
+                ,book-category)))
+
+        (text (@ (class "main-title-before")
+                 (x ,(exact->inexact
+                       (- width
+                          (- page-width
+                             (/ (- page-width main-title-width) 2))
+                          margin)))
+                 (y ,(+ margin (* 0.30 page-height))))
+              ,book-title-before)
+
+        (text (@ (class "main-title")
+                 (textLength ,main-title-width)
+                 (lengthAdjust ,"spacingAndGlyphs")
+                 (x ,(exact->inexact (- width
+                                        main-title-width
+                                        main-title-margin
+                                        margin)))
+                 (y ,(+ main-title-size margin (* 0.30 page-height))))
+              ,(string-upcase book-title))
+
+        #;(text (@ (class "main-title-after")
+                 (x ,(exact->inexact (- width  (/ page-width 8) margin)))
+                 (y ,(+ main-title-size
+                        (* 1.25 title-size)
+                        margin
+                        (* 0.30 page-height))))
+              ,book-title-after)
+
+        ; TODO WORK ON THIS
+        ,(textArea book-title-after
+                   "main-title-after"
+                   (exact->inexact (- width
+                                      margin
+                                      main-title-margin
+                                      main-title-width))
+                   (+ main-title-size
+                      (* main-subtitle-size 0.5)
+                      margin
+                      (* 0.30 page-height))
+                   main-title-width
+                   (* 2 1.25 main-subtitle-size))
+
+        ,(map (lambda (author pos)
+                `(text (@ (class "author")
+                          (x ,(exact->inexact
+                                (- width main-title-margin margin)))
+                          (y ,(+ main-title-size
+                                 margin
+                                 (* 0.35 page-height)
+                                 (* (+ 2 pos) (* 1.3 authors-size)))))
+                       ,author))
+              book-authors
+              (iota (length book-authors)))
+
+        ,(elenq-logo "PUBLISHING"
+                     3
+                     (exact->inexact (- width (/ page-width 2) margin))
+                     (+ margin (* 0.95 page-height))))) outport)))
+
+
diff --git a/utils/cover/cover.sld b/utils/cover/cover.sld
new file mode 100644
index 0000000..7a83f8e
--- /dev/null
+++ b/utils/cover/cover.sld
@@ -0,0 +1,9 @@
+(define-library (utils cover cover)
+                (import (scheme char)
+                        (chibi)
+                        (chibi string)
+                        (chibi sxml)
+                        (utils cover barcode)
+                        (srfi 1))
+                (export metadata->svg)
+                (include "cover.scm"))
-- 
cgit v1.2.3