summaryrefslogtreecommitdiff
path: root/ksh-report.el
blob: 722e4f601a7b8d2a526ba2eb267a10c95421309b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(require 'seq)

(defun draw-biometry (param-name param param-age percentile)
  (let ((temp-script-file (make-temp-script
			   (format "%s %s" param-name param)
			   param-age (string-to-number percentile))))
    (message "Drawing %s..." temp-script-file)
    (shell-command
     (format "asy -f pdf -o %s %s" (file-name-directory temp-script-file) temp-script-file))
    (delete-file temp-script-file)
    (format "%s.pdf" temp-script-file)))

(defun make-temp-script (param param-age percentile)
  (let ((temp-script-file (make-temp-file "biometry")))
    (with-temp-file temp-script-file
      (insert "include \"biometry\";\n")
      (insert (format
	       "draw_biometry(\"%s\", \"%s\", %d);"
	       param param-age percentile)))
    temp-script-file))

(defun insert-biometry (readings)
  (princ
   (format "[[%s]]\n"
	   (crop-pdf
	    (combine-pdfs
	     (mapcar (lambda (reading)
		       (apply 'draw-biometry
			      (mapcar 'get-field reading)))
                     readings))))))

(defun combine-pdfs (pdfs)
  (let ((temp-combination (make-temp-file "pdfjam" nil ".pdf")))
    (apply 'call-process "pdfjam" nil nil nil
	   "--nup" "4x1"
	   "--outfile" temp-combination pdfs)
    (mapc 'delete-file pdfs)
    temp-combination))

(defun crop-pdf (pdf)
  (message "Cropping %s..." pdf)
  (let ((output-pdf (format "%s-crop.pdf" (file-name-sans-extension pdf))))
    (shell-command (format "pdfcrop %s %s" pdf output-pdf))
    (delete-file pdf) output-pdf))

(defun latex-use-package (package &optional arguments)
  (if arguments
      (format "\\usepackage[%s]{%s}" arguments package)
    (format "\\usepackage{%s}" package)))

(defun make-report-header (title)
  (seq-do 'ksh-forms-org-keyword
          '(("TITLE" . "Kuzhanthai Sanjeevi Hospital")
            ("AUTHOR" . "Dr. Serene Isaac, MD, DGO, DNB")))
  (seq-do 'ksh-forms-org-latex-header
          (list (latex-use-package "fullpage")
                (latex-use-package "nopageno")
                (latex-use-package "datetime" "ddmmyyyy")
                "\\renewcommand{\\dateseparator}{-}"
                "\\setlength{\\parindent}{0cm}"
                (latex-use-package "titlesec" "tiny")
                (latex-use-package "wasysym")
                (latex-use-package "titling")
                (format "\\pretitle{\\hrule \\begin{center} {\\Large \\textbf{%s}} \\par \\small \\sc}" title)
                "\\posttitle{\\par 17, Jawahar Street, Ramavarmapuram, Nagercoil - 629001 \\par \\phone \\, 223374 \\end{center}}"
                "\\preauthor{}"
                "\\postauthor{\\par Obstetrician and Gynaecologist}"
                "\\predate{\\hfill \\textbf{Date: }}"
                "\\date{\\today}"
                "\\postdate{\\vspace{1em} \\hrule \\par}")))

(provide 'ksh-report)