first near final draft of talk -- just have to decide if I should include or exclude...
[CommonLispStat.git] / TODO.lisp
blob5bba8eea4e291249e501271cb99f3cd54f876040
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2009-07-11 14:20:39 tony>
4 ;;; Creation: <2008-09-08 08:06:30 tony>
5 ;;; File: TODO.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c) 2007-2008, AJ Rossini <blindglobe@gmail.com>. BSD.
8 ;;; Purpose: Stuff that needs to be made working sits inside the
9 ;;; progns... This file contains the current challenges to
10 ;;; solve, including a description of the setup and the work
11 ;;; to solve....
13 ;;; What is this talk of 'release'? Klingons do not make software
14 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
15 ;;; designers and quality assurance people in its wake.
17 ;;; SET UP
19 (in-package :cl-user)
21 (defun init-CLS ()
22 (asdf:oos 'asdf:load-op 'lispstat))
24 (defun init-CLS-graphics ()
25 (init-CLS)
26 (asdf:oos 'asdf:load-op 'cl-cairo2-x11)
27 (asdf:oos 'asdf:load-op 'cl-2d))
29 (init-CLS-graphics)
31 ;;(asdf:oos 'asdf:load-op 'lisp-matrix)
32 ;;(asdf:oos 'asdf:compile-op 'lispstat :force t)
33 ;;(asdf:oos 'asdf:load-op 'lispstat)
36 ;;(asdf:oos 'asdf:load-op 'cl-pdf)
37 ;;(asdf:oos 'asdf:load-op 'cl-typesetting)
39 (in-package :lisp-stat-unittests)
41 ;; tests = 80, failures = 8, errors = 15
42 (run-tests :suite 'lisp-stat-ut)
43 (describe (run-tests :suite 'lisp-stat-ut))
45 (describe 'lisp-stat-ut)
46 (documentation 'lisp-stat-ut 'type)
48 ;; FIXME: Example: currently not relevant, yet
49 ;; (describe (lift::run-test :test-case 'lisp-stat-unittests::create-proto
50 ;; :suite 'lisp-stat-unittests::lisp-stat-ut-proto))
52 (describe (lift::run-tests :suite 'lisp-stat-ut-dataframe))
53 (lift::run-tests :suite 'lisp-stat-ut-dataframe)
55 (describe (lift::run-test
56 :test-case 'lisp-stat-unittests::create-proto
57 :suite 'lisp-stat-unittests::lisp-stat-ut-proto))
59 (in-package :ls-user)
61 ;;; DSC2009 examples in ./examples/*.lisp
64 #+nil(progn
65 ;; REVIEW: general Lisp use guidance
67 (fdefinition 'make-matrix)
68 (documentation 'make-matrix 'function)
70 #| Examples from CLHS, a bit of guidance.
72 ;; This function assumes its callers have checked the types of the
73 ;; arguments, and authorizes the compiler to build in that assumption.
74 (defun discriminant (a b c)
75 (declare (number a b c))
76 "Compute the discriminant for a quadratic equation."
77 (- (* b b) (* 4 a c))) => DISCRIMINANT
78 (discriminant 1 2/3 -2) => 76/9
80 ;; This function assumes its callers have not checked the types of the
81 ;; arguments, and performs explicit type checks before making any assumptions.
82 (defun careful-discriminant (a b c)
83 "Compute the discriminant for a quadratic equation."
84 (check-type a number)
85 (check-type b number)
86 (check-type c number)
87 (locally (declare (number a b c))
88 (- (* b b) (* 4 a c)))) => CAREFUL-DISCRIMINANT
89 (careful-discriminant 1 2/3 -2) => 76/9
94 #+nil
95 (progn
96 (asdf:oos 'asdf:load-op 'versioned-objects)
97 (asdf:oos 'asdf:load-op 'validations))
103 (with-data dataset ((dsvarname1 [usevarname1])
104 (dsvarname2 [usevarname2]))
105 @body)
111 (defun testme (&key (a 3) (b (+ a 3)))
114 (testme)
115 (testme :a 2)
116 (testme :b 4)
117 (testme :a 2 :b (* a 5))
120 ;;;; CL-PDF / CL-TYPESETTING experiments
122 (in-package :cl-user)
124 ;;(asdf:oos 'asdf:load-op 'cl-pdf)
126 ;;(asdf:oos 'asdf:load-op 'cl-typesetting)
128 ;;; cl-pdf copyright 2002-2005 Marc Battyani see license.txt for
129 ;;; details of the BSD style license
131 ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
132 ;;; The homepage of cl-pdf is here: http://www.fractalconcept.com/asp/html/cl-pdf.html
134 (defun example1 (&optional (file #P"/tmp/ex1.pdf"))
135 (with-open-file (outstream file
136 :direction :output
137 :if-exists :rename
138 :element-type :default)
139 (pdf:with-document ()
140 (pdf:with-page ()
141 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
142 (let ((helvetica (pdf:get-font "Helvetica")))
143 (pdf:in-text-mode
144 (pdf:set-font helvetica 36.0)
145 (pdf:move-text 100 800)
146 (pdf:draw-text "cl-pdf: Example 1"))
147 (pdf:translate 230 500)
148 (loop repeat 150
149 for i = 0.67 then (* i 1.045)
150 do (pdf:in-text-mode
151 (pdf:set-font helvetica i)
152 (pdf:set-rgb-fill (/ (random 255) 255.0)
153 (/ (random 255) 255.0)
154 (/ (random 255) 255.0))
155 (pdf:move-text (* i 3) 0)
156 (pdf:show-text "cl-typesetting"))
157 (pdf:rotate 13)))))
158 (pdf:write-document outstream))))
160 (example1)
162 (with-open-file (tony #p"/tmp/tony.pdf"
163 :direction :output
164 ; :element-type 'signed-byte
165 ; :element-type :default
166 :if-exists :supersede )
167 (example1 tony))
169 ;; for the TrueType Example, you need to load the font first:
170 ;; (read the unicode-readme for more info)
171 #+nil
172 (pdf:load-ttu-font #P"/tmp/times.ufm" #P"/tmp/times.ttf")
174 (defun example1-ttu (&optional (file #P"/tmp/ex1-ttu.pdf"))
175 (pdf:with-document ()
176 (pdf:with-page ()
177 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
178 (let ((helvetica (pdf:get-font "TimesNewRomanPSMT"))) ; The windows times font
179 (pdf:in-text-mode
180 (pdf:set-font helvetica 36.0)
181 (pdf:move-text 100 800)
182 (pdf:draw-text "cl-pdf: Example 1 with Unicode"))
183 (pdf:translate 230 500)
184 (loop repeat 150
185 for i = 0.67 then (* i 1.05)
186 do (pdf:in-text-mode
187 (pdf:set-font helvetica i)
188 (pdf:set-rgb-fill (/ (random 255) 255.0)(/ (random 255) 255.0)(/ (random 255) 255.0))
189 (pdf:move-text (* i 3) 0)
190 (pdf:show-text (format nil "Lisp lives! ~cx.~cy.x " (code-char 955)(code-char 955))))
191 (pdf:rotate 13)))))
192 (pdf:write-document file)))
194 (defun example2 (&optional (file #P"/tmp/ex2.pdf"))
195 (pdf:with-document ()
196 (pdf:with-page ()
197 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
198 (let ((helvetica (pdf:get-font "Helvetica")))
199 (pdf:in-text-mode
200 (pdf:set-font helvetica 36.0)
201 (pdf:move-text 100 800)
202 (pdf:draw-text "cl-pdf: Example 2"))
203 (pdf:move-to (+ 10 (random 500))(+ 10 (random 400)))
204 (pdf:set-gray-fill 0.5)
205 (dotimes (i 50)
206 (pdf:line-to (+ 50 (random 500)) (+ 50 (random 400))))
207 (pdf:close-even-odd-fill-and-stroke)
208 (pdf:move-to (+ 50 (random 500))(+ 400 (random 400)))
209 (pdf:set-rgb-fill 0.5 0.5 0.8)
210 (pdf:set-rgb-stroke 0.9 0.5 0.1)
211 (dotimes (i 50)
212 (pdf:bezier2-to (+ 50 (random 500)) (+ 400 (random 400))
213 (+ 50 (random 500)) (+ 400 (random 400))))
214 (pdf:close-even-odd-fill-and-stroke))))
215 (pdf:write-document file)))
217 (defun gen-image-bits ()
218 (with-output-to-string (s)
219 (loop for x from -10 to 10 by 1/10
220 do (loop for y from -10 to 10 by 1/10
221 do (format s "~2,'0x~2,'0x~2,'0x"
222 (round (+ 200 (* 55 (sin x))))
223 (round (+ 200 (* 55 (cos y))))
224 (round (+ 200 (* 55 (sin (+ x y))))))))))
226 (defun example3 (&optional (file #P"/tmp/ex3.pdf"))
227 (pdf:with-document ()
228 (pdf:with-page ()
229 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
230 (let* ((helvetica (pdf:get-font "Helvetica"))
231 (image (make-instance 'pdf:image
232 :bits (gen-image-bits)
233 :width 201 :height 201)))
234 (pdf:draw-bar-code128 "30A0033111436" 20 100)
235 (pdf:add-images-to-page image)
236 (pdf:in-text-mode
237 (pdf:set-font helvetica 36.0)
238 (pdf:move-text 100 800)
239 (pdf:draw-text "cl-pdf: Example 3"))
240 (pdf:with-saved-state
241 (pdf:translate 102 550)
242 (pdf:rotate 20)
243 (pdf:scale 200 125)
244 (pdf:paint-image image))
245 (pdf:with-saved-state
246 (pdf:translate 100 540)
247 (pdf:rotate -70)
248 (pdf:scale 300 200)
249 (pdf:paint-image image)))))
250 (pdf:write-document file)))
252 (example1)
254 ;; logo
256 (defparameter *fractal-ratio* 0.8)
257 (defconstant +sin60+ (sin (/ pi 3)))
258 (defconstant +cos60+ (cos (/ pi 3)))
259 (defconstant +tg30+ (tan (/ pi 6)))
260 (defconstant +tg60-tg30+ (- (tan (/ pi 3))(tan (/ pi 6))))
262 (defun %fractal (x y dx dy level)
263 (if (zerop level)
264 (let ((dx/2 (* dx 0.5))
265 (dy/2 (* dy 0.5)))
266 (pdf:move-to (- x dx/2) (- y dy/2))
267 (pdf:line-to x (+ y dy/2))
268 (pdf:line-to (+ x dx/2) (- y dy/2))
269 (pdf:close-fill-and-stroke))
270 (let* ((delta (- 1 *fractal-ratio*))
271 (delta05 (* 0.5 delta))
272 (ratio2 (- 1 delta05))
273 (deltax (* dx 0.25 (+ 1 (* 0.5 +sin60+ (- 1 ratio2)))))
274 (deltay (* dy 0.25 (+ 1 delta05)))
275 (dyf2 (* dy 0.5 (+ 1 delta05 )))
276 (dxf2 (* dx 0.5 (+ 1 delta05 ))))
277 (decf level)
278 (setf dx (* dx 0.5))
279 (setf dy (* dy 0.5))
280 (%down-fractal x (- y (* 1 dy)(* dx +tg30+ -1)(* 0.125 +tg60-tg30+ dxf2)) dxf2 dyf2 level)
281 (%fractal x (+ y (* dy 0.5)) (* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
282 (%fractal (+ x deltax)(- y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
283 (%fractal (- x deltax)(- y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
286 (defun %down-fractal (x y dx dy level)
287 (setf level 0)
288 (if (zerop level)
289 (let ((dx/2 (* dx 0.5))
290 (dy/2 (* dy 0.5)))
291 (pdf:move-to (- x dx/2) (+ y dy/2))
292 (pdf:line-to x (- y dy/2))
293 (pdf:line-to (+ x dx/2)(+ y dy/2))
294 (pdf:close-fill-and-stroke))
295 (let* ((delta (- 1 *fractal-ratio*))
296 (delta05 (* 0.5 delta))
297 (ratio2 (- 1 delta05))
298 (deltax (* dx 0.25 (+ 1 (* 0.5 +sin60+ (- 1 ratio2)))))
299 (deltay (* dy 0.25 (+ 1 delta05)))
300 (dyf2 (* dy 0.5 (+ 1 delta05 )))
301 (dxf2 (* dx 0.5 (+ 1 delta05 ))))
302 (decf level)
303 (setf dx (* dx 0.5))
304 (setf dy (* dy 0.5))
305 (%fractal x (+ y (* 1 dy)(* dx +tg30+ -1)(* 0.125 +tg60-tg30+ dxf2)) dxf2 dyf2 level)
306 (%down-fractal x (- y (* dy 0.5)) (* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
307 (%down-fractal (+ x deltax)(+ y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
308 (%down-fractal (- x deltax)(+ y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
312 (defun fractal (x y l level)
313 (let ((dx l)
314 (dy (* l +sin60+)))
315 (%fractal x y dx dy level)))
317 ;the logo
318 (defun example4 (&optional (file #P"/tmp/ex4.pdf"))
319 (pdf:with-document ()
320 (loop for i from 1 to 7
321 do (pdf:with-page ()
322 (pdf:with-outline-level ((format nil "Page ~d" i)(pdf:register-page-reference))
323 (let* ((helvetica (pdf:get-font "Helvetica")))
324 (pdf:in-text-mode
325 (pdf:set-font helvetica 36.0)
326 (pdf:move-text 100 800)
327 (pdf:draw-text (format nil "cl-pdf: Example 4 page ~d" i)))
328 (pdf:set-rgb-stroke 1.0 1.0 1.0)
329 (pdf:set-rgb-fill 0.4 0.4 0.9)
330 (pdf:set-line-width 0.2)
331 (fractal 298 530 600 i)))))
332 (pdf:write-document file)))
334 (defvar *dx* #(1 0 -1 0))
335 (defvar *dy* #(0 1 0 -1))
337 ;make-maze
338 (defun example5 (nx ny &key (size 10) (file #P"/tmp/ex5.pdf"))
339 (let ((x-stack '())
340 (y-stack '())
341 (visited (make-array (list nx ny) :initial-element nil))
342 (v-walls (make-array (list nx ny) :initial-element t))
343 (h-walls (make-array (list nx ny) :initial-element t))
344 (x (random nx))
345 (y (random ny))
346 next-x next-y)
347 (flet ((find-cell ()
348 (let ((tested (vector nil nil nil nil))
349 (nb-tested 0))
350 (loop while (< nb-tested 4)
351 for test = (random 4)
352 unless (svref tested test)
353 do (incf nb-tested)
354 (setf (svref tested test) t)
355 (setf next-x (+ x (svref *dx* test)))
356 (setf next-y (+ y (svref *dy* test)))
357 (when (and (>= next-x 0)(< next-x nx)(>= next-y 0)(< next-y ny)
358 (not (aref visited next-x next-y)))
359 (return-from find-cell t)))
360 nil)))
361 (setf (aref visited x y) t)
362 (loop with nb-visited = 1 and total-cells = (* nx ny)
363 while (< nb-visited total-cells)
364 do (if (find-cell)
365 (progn (push x x-stack)(push y y-stack)
366 (if (/= next-x x)
367 (setf (aref h-walls (min x next-x) y) nil)
368 (setf (aref v-walls x (min y next-y)) nil))
369 (setf x next-x y next-y)
370 (setf (aref visited x y) t)
371 (incf nb-visited))
372 (progn (setf x (pop x-stack) y (pop y-stack))))))
373 (pdf:with-document ()
374 (pdf:with-page ()
375 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
376 (pdf:translate (* 0.5 (- 595 (* nx size)))(* 0.5 (- 841 (* ny size))))
377 (setf (aref h-walls (1- nx) (random ny)) nil)
378 (pdf:move-to 0 0)
379 (pdf:line-to (* nx size) 0)
380 (pdf:move-to 0 size)
381 (pdf:line-to 0 (* ny size))
382 (loop for x from 0 below nx
383 for x0 = 0 then x1
384 for x1 from size by size
385 do (loop for y from 0 below ny
386 for y0 = 0 then y1
387 for y1 from size by size
389 (when (aref h-walls x y)
390 (pdf:move-to x1 y0)
391 (pdf:line-to x1 y1))
392 (when (aref v-walls x y)
393 (pdf:move-to x0 y1)
394 (pdf:line-to x1 y1)))
395 (pdf:stroke))))
396 (pdf:write-document file))))
399 (defun example6 (&optional (file #P"/tmp/ex6.pdf"))
400 (pdf:with-document ()
401 (pdf:with-page ()
402 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
403 (let ((helvetica (pdf:get-font "Helvetica")))
404 (pdf:in-text-mode
405 (pdf:set-font helvetica 36.0)
406 (pdf:move-text 100 800)
407 (pdf:draw-text "cl-pdf: Example 6"))
408 (pdf:set-rgb-stroke 0.1 0.1 0.1)
409 (pdf:set-rgb-fill 0.8 0.8 0.8)
410 (let ((x 50) (y 600))
411 (dotimes (i 2)
412 (pdf:rectangle x y 500 140 :radius 10)
413 (pdf:close-fill-and-stroke)
414 (setf y (- y 180))))
415 (pdf:translate 50 670)
416 (let ((x 50) (y 0))
417 (loop repeat 4
418 for i = 8 then (* i 1.05)
420 (pdf:set-rgb-fill (* 0.1 i) (* 0.01 i) (* 0.02 i))
421 (pdf:circle x y (* 4 i))
422 (pdf:close-fill-and-stroke)
423 (pdf:ellipse (+ x 250) y (* 5 i) (* 4 i))
424 (pdf:close-fill-and-stroke)
425 (setf x (+ x 50))))
426 (pdf:translate 0 -180)
427 (pdf:regular-polygon 150 0 50 7 :fillet-radius 8)
428 (pdf:close-fill-and-stroke)
429 (pdf:star 350 0 50 30 6 :fillet-radius 5)
430 (pdf:close-fill-and-stroke)
432 (pdf:set-rgb-fill 0.8 0.6 0.2)
433 (pdf:regular-polygon 150 0 30 5 :fillet-radius 4)
434 (pdf:close-fill-and-stroke)
435 (pdf:star 350 0 40 20 4 :fillet-radius 6)
436 (pdf:close-fill-and-stroke)
438 (pdf:set-rgb-fill 0.4 0.8 0.7)
439 (pdf:regular-polygon 150 0 15 3 :fillet-radius 3)
440 (pdf:close-fill-and-stroke)
441 (pdf:star 350 0 35 10 12 :fillet-radius 1)
442 (pdf:close-fill-and-stroke)
443 (pdf:set-line-width 0.5)
444 (loop for r from 2 to 100 by 2
445 for start = (* pi 0.001 (random 2000))
446 for length = (* pi 0.001 (random 2000))
447 do (pdf:set-rgb-stroke (* 0.01 (random 100))(* 0.01 (random 100))(* 0.01 (random 100)))
448 (pdf:arc 250 -230 r start length)
449 (pdf:stroke)))))
450 (pdf:write-document file)))
452 (defvar *test-jpeg-file-path* (when *load-pathname*
453 (merge-pathnames #P"banner.jpg" *load-pathname*)))
455 (unless *test-jpeg-file-path*
456 (error "please set the *test-jpeg-file-path* variable to the banner.jpg file location"))
458 (defvar *test-jpeg* *test-jpeg-file-path*)
460 (defun example7 (&optional (file #P"/tmp/ex7.pdf"))
461 (pdf:with-document ()
462 (let ((jpg-image (pdf:make-jpeg-image *test-jpeg*))
463 (helvetica (pdf:get-font "Helvetica")))
464 (pdf:with-outline-level ("Contents" "page 1")
465 (pdf:with-page ()
466 (pdf:register-page-reference "page 1")
467 (pdf:with-outline-level ("Page 1" "page 1")
468 (pdf:in-text-mode
469 (pdf:set-font helvetica 36.0)
470 (pdf:move-text 100 800)
471 (pdf:draw-text "cl-pdf: Example 7"))
472 (pdf:set-rgb-stroke 0.1 0.1 0.1)
473 (pdf:set-rgb-fill 0.6 0.6 0.8)
474 (pdf:in-text-mode
475 (pdf:set-font helvetica 13.0)
476 (pdf:move-text 10 700)
477 (pdf:draw-text "Test for bookmarks, JPEG support, internal links, URI links and basic charts"))
478 (pdf:add-images-to-page jpg-image)
479 (pdf:draw-image jpg-image 10 10 239 50 0 t)
480 (pdf:add-URI-link 10 10 239 50 "http://www.fractalconcept.com/asp/html/cl-pdf.html" :border #(1 1 1))
481 (pdf:in-text-mode
482 (pdf:set-font helvetica 10.0)
483 (pdf:move-text 500 10)
484 (pdf:draw-text "goto page 2"))
485 (pdf:add-link 495 8 80 14 "page 2")
486 (pdf:draw-object (make-instance 'pdf:histogram :x 200 :y 450 :width 300 :height 200
487 :label-names '("Winter" "Spring" "Summer" "Autumn")
488 :labels&colors '(("Serie 1" (1.0 0.0 0.0))
489 ("Serie 2" (0.0 1.0 0.0)))
490 :series '((42 46 48 42)(40 38 51 46))
491 :background-color '(0.9 0.9 0.9)
492 :stacked-series nil ;;; try also with t
493 :x-axis-options ()
494 :y-axis-options ()
495 :legend-options ()))
496 (pdf:draw-object (make-instance 'pdf:pie-chart :x 200 :y 100 :width 200 :height 200
497 :serie '(12 23 65 33)
498 :labels&colors '(("Winter" (1.0 0.0 0.0))
499 ("Spring" (0.0 1.0 0.0))
500 ("Summer" (0.0 0.0 1.0))
501 ("Autumn" (0.0 1.0 1.0)))))))
502 (pdf:with-page ()
503 (pdf:register-page-reference "page 2")
504 (pdf:with-outline-level ("Page 2" "page 2")
505 (pdf:in-text-mode
506 (pdf:set-font helvetica 36.0)
507 (pdf:move-text 100 800)
508 (pdf:draw-text "Page 2"))
509 (pdf:add-images-to-page jpg-image)
510 (pdf:draw-image jpg-image 10 10 239 50 0 t)
511 (pdf:add-URI-link 10 10 239 50
512 "http://www.fractalconcept.com/asp/html/cl-pdf.html"
513 :border #(1 1 1))
514 (pdf:in-text-mode
515 (pdf:set-font helvetica 10.0)
516 (pdf:move-text 500 10)
517 (pdf:draw-text "goto page 1"))
518 (pdf:add-link 495 8 80 14 "page 1")
519 (pdf:draw-object
520 (make-instance 'pdf:plot-xy :x 100 :y 400 :width 400 :height 200
521 :labels&colors '(("Data 1" (1.0 0.0 0.0))
522 ("Data 2" (0.0 1.0 0.0))
523 ("Data 3" (0.0 0.0 1.0)))
524 :series '(((1 40) (3 38) (5 31) (7 36))
525 ((2 53) (2.5 42) (3.7 46) (6 48))
526 ((1.3 12) (1.6 18) (2 16) (3 27)))
527 :background-color '(0.9 0.9 0.9)
528 :x-axis-options ()
529 :y-axis-options '(:min-value 0)
530 :legend-options ()))))))
531 (pdf:write-document file)))
533 ; Von Koch fractal (brute force ;-))
535 (defun vk-fractal (l level)
536 (pdf:with-saved-state
537 (if (zerop level)
538 (progn
539 (pdf:move-to 0 0)
540 (pdf:line-to l 0)
541 (pdf:stroke))
542 (loop with l3 = (/ l 3.0) and l-1 = (1- level)
543 for angle in '(nil 60 -120 60)
544 do (when angle (pdf:rotate angle))
545 (vk-fractal l3 l-1))))
546 (pdf:translate l 0))
549 (defun example8 (&optional (file #P"/tmp/ex8.pdf"))
550 (pdf:with-document ()
551 (loop for i from 0 to 6
552 do (pdf:with-page ()
553 (pdf:with-outline-level ((format nil "Page ~d" i)(pdf:register-page-reference))
554 (let* ((helvetica (pdf:get-font "Helvetica" :win-ansi-encoding)))
555 (pdf:draw-centered-text
556 297 800
557 (format nil "Flocon de Koch (niveau ~d, ~d segments, périmètre ~,1f mm)"
558 i (* 3 (expt 4 i))(/ (* 180 (* 3 (expt 4 i)))(expt 3 i)))
559 helvetica 18.0)
560 (pdf:translate 42 530)
561 (pdf:set-line-width 0.1)
562 (vk-fractal 510 i)
563 (pdf:rotate -120)
564 (vk-fractal 510 i)
565 (pdf:rotate -120)
566 (vk-fractal 510 i)))))
567 (pdf:write-document file)))
569 ;;; A Mandelbrot set from Yannick Gingras
571 (defun hsv->rgb (h s v)
572 "channels are in range [0..1]"
573 (if (eql 0 s)
574 (list v v v)
575 (let* ((i (floor (* h 6)))
576 (f (- (* h 6) i))
577 (p (* v (- 1 s)))
578 (q (* v (- 1 (* s f))))
579 (t_ (* v (- 1 (* s (- 1 f)))))
580 (hint (mod i 6)))
581 (case hint
582 (0 (list v t_ p))
583 (1 (list q v p))
584 (2 (list p v t_))
585 (3 (list p q v))
586 (4 (list t_ p v))
587 (5 (list v p q))))))
589 (defun make-color-map (nb-col
590 start-rad
591 &optional
592 stop-rad
593 (sat .85)
594 (tilt-angle 0)
595 (nb-loop 1)
596 (clockwise t))
597 ;; borowed from Poly-pen --YGingras
598 (let* ((stop-rad (if stop-rad stop-rad start-rad))
599 (angle-inc (* (if clockwise 1.0 -1.0) nb-loop))
600 (val-inc (- stop-rad start-rad)))
601 (coerce (loop for k from 0 to (1- nb-col) collect
602 (let ((i (/ k (1- nb-col))))
603 (mapcar #'(lambda (x) (round x 1/255))
604 (hsv->rgb (mod (+ tilt-angle (* i angle-inc)) 1)
606 (+ start-rad (* i val-inc))))))
607 'vector)))
610 (defun gen-mandelbrot-bits (w h)
611 ;; Inside a Moth by Lahvak, for other interesting regions see
612 ;; http://fract.ygingras.net/top
614 ;; TODO:AA
615 (declare (optimize speed (debug 0) (safety 0) (space 0))
616 (type fixnum w h))
617 (let* ((nb-cols 30000)
618 (nb-iter (expt 2 11)) ;; crank this if you have a fast box
619 (center #c(-0.7714390420105d0 0.1264514778485d0))
620 (zoom (* (expt (/ h 320) 2) 268436766))
621 (inc (/ h (* 150000d0 zoom)))
622 (cols (make-color-map nb-cols 1 0.2 0.9 0.24 0.21 t))
623 (c #c(0d0 0d0))
624 (z #c(0d0 0d0))
625 (region nil))
626 (declare (type double-float inc))
627 (dotimes (i h)
628 (dotimes (j w)
629 (setf c (complex (+ (realpart center)
630 (* inc (+ (the fixnum j) (/ w -2d0))))
631 (+ (imagpart center)
632 (* inc (+ (the fixnum i) (/ h -2d0)))))
633 z #c(0d0 0d0))
634 ;; standard Mandelbrot Set formula
635 (push (dotimes (n nb-iter 0)
636 (setf z (+ (* z z) c))
637 (let ((real (realpart z))
638 (imag (imagpart z)))
639 (when (< 2 (abs z))
640 (return (- nb-iter
641 ;; sub-iter smoothing
642 (- n (log (log (abs (+ (* z z) c)) 10) 2)))))))
643 region)))
644 (with-output-to-string (s)
645 (let ((max (reduce #'max region)))
646 (dolist (x (nreverse region))
647 (destructuring-bind (r g b)
648 (if (zerop x)
649 '(0 0 0)
650 ;; pallette stretching
651 (elt cols (floor (expt (/ x max) (/ nb-iter 256))
652 (/ 1 (1- nb-cols)))))
653 (format s "~2,'0x~2,'0x~2,'0x" r g b)))))))
655 ;;; Example 9 is a Mandelbrot set from Yannick Gingras
656 ;;; Takes a long time...
658 (defun example9 (&optional (file #P"/tmp/ex9.pdf"))
659 "draw a nice region of the Mandelbrot Set"
660 (pdf:with-document ()
661 (pdf:with-page ()
662 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
663 (let* ((w 600)
664 (h 750)
665 (helvetica (pdf:get-font "Helvetica"))
666 (image (make-instance 'pdf:image
667 :bits (gen-mandelbrot-bits w h)
668 :width w :height h)))
669 (pdf:add-images-to-page image)
670 (pdf:in-text-mode
671 (pdf:set-font helvetica 36.0)
672 (pdf:move-text 100 800)
673 (pdf:draw-text "cl-pdf: Example 9"))
674 (pdf:with-saved-state
675 (pdf:translate 0 0)
676 (pdf:scale (/ w 2) (/ h 2))
677 (pdf:paint-image image)))))
678 (pdf:write-document file)))
681 ;; CL-TYPESETTING example
683 ;;; cl-typesetting copyright 2003-2004 Marc Battyani see license.txt for the details
684 ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
685 ;;; The homepage of cl-typesetting is here: http://www.fractalconcept.com/asp/html/cl-typesetting.html
687 (in-package typeset)
689 ;;; Low level tests
691 (defparameter *boxes* nil) ;for debugging...
693 ; a very simple hello world
694 (defun hello (&optional (file #P"/tmp/hello.pdf"))
695 (with-open-file (outstream file
696 :direction :output
697 :if-exists :rename
698 :element-type :default)
700 (pdf:with-document ()
701 (pdf:with-page ()
702 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
703 (pdf:set-line-width 0.1)
704 (let ((content
705 (compile-text ()
706 (vspace 100)
707 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
708 "cl-typesetting" :eol
709 (vspace 2)
710 (hrule :dy 1)
711 (with-style (:font "Times-Italic" :font-size 26)
712 "The cool Common Lisp typesetting system")
713 (vspace 50)
714 (with-style (:font "Helvetica-Oblique" :font-size 100)
715 "Hello World!")
716 (vspace 50)
717 (with-style (:font "Helvetica" :font-size 12)
718 "hello" (dotted-hfill) "4.2" :eol
719 "hello world" (dotted-hfill) "4.2.4.2"))
720 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
721 "hello" (dotted-hfill) "4.2" :eol
722 "hello world" (dotted-hfill) "4.2.4.2")
723 (vspace 50)
724 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
725 "hello" (dotted-hfill :pattern-spacing 0.6) "4.2" :eol
726 "hello world" (dotted-hfill :pattern-spacing 0.6) "4.2.4.2")
727 (vspace 50)
728 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
729 "hello" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2" :eol
730 "hello world" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2.4.2"))))
731 (draw-block content 20 800 545 700))))
732 (pdf:write-document outstream))))
734 ;;(hello)
736 ; a multipage simple hello world
737 (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
738 (with-open-file (outstream file
739 :direction :output
740 :if-exists :rename
741 :element-type :default)
742 (pdf:with-document ()
743 (let ((content
744 (compile-text ()
745 (vspace 100)
746 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
747 "cl-typesetting" :eol
748 (vspace 2)
749 (hrule :dy 1)
750 (with-style (:font "Times-Italic" :font-size 26)
751 "The cool Common Lisp typesetting system")
752 (vspace 50)
753 (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0 0.8))
754 (dotimes (i 100)
755 (put-string "Hello World!")(new-line)))))))
756 (loop while (boxes content) do
757 (pdf:with-page ()
758 (pdf:set-line-width 0.1)
759 (draw-block content 20 800 545 700))))
760 (pdf:write-document outstream))))
762 ;; (multi-page-hello)
764 ;; The Fancy Example!
766 (defparameter *par1*
767 "Lisp is a family of languages with a long history. Early key ideas in Lisp were developed by John McCarthy during the 1956 Dartmouth Summer Research Project on Artificial Intelligence. McCarthy's motivation was to develop an algebraic list processing language for artificial intelligence work. Implementation efforts for early dialects of Lisp were undertaken on the IBM 704, the IBM 7090, the Digital Equipment Corporation (DEC) PDP-1, the DEC PDP-6, and the PDP-10. The primary dialect of Lisp between 1960 and 1965 was Lisp 1.5. By the early 1970's there were two predominant dialects of Lisp, both arising from these early efforts: MacLisp and Interlisp. For further information about very early Lisp dialects, see The Anatomy of Lisp or Lisp 1.5 Programmer's Manual.")
769 (defparameter *par2*
770 "MacLisp improved on the Lisp 1.5 notion of special variables and error handling. MacLisp also introduced the concept of functions that could take a variable number of arguments, macros, arrays, non-local dynamic exits, fast arithmetic, the first good Lisp compiler, and an emphasis on execution speed. By the end of the 1970's, MacLisp was in use at over 50 sites. For further information about Maclisp, see Maclisp Reference Manual, Revision 0 or The Revised Maclisp Manual.")
772 ;; example of extension
774 (defclass rotated-char-box (soft-box h-mode-mixin)
775 ((boxed-char :accessor boxed-char :initarg :boxed-char)
776 (rotation :accessor rotation :initarg :rotation)))
778 (defun put-rotated-char-string (string)
779 (loop for char across string
780 do (add-box (make-instance 'rotated-char-box :dx *font-size*
781 :dy *font-size* :boxed-char char :baseline (* *font-size* 0.8)
782 :rotation (- (random 120) 60)))))
784 (defmethod stroke ((box rotated-char-box) x y)
785 (let ((dx (dx box))(dy (dy box))
786 (width (pdf:get-char-width (boxed-char box) *font* *font-size*)))
787 (pdf:with-saved-state
788 (pdf:translate (+ x (* dx 0.5)) (+ y (* dy 0.3)))
789 (pdf:set-line-width 0.5)
790 (pdf:set-gray-fill 0.8)
791 (pdf:circle 0 0 (* dx 0.45))
792 (pdf:fill-and-stroke)
793 (pdf:set-gray-fill 0)
794 (pdf:rotate (rotation box))
795 (pdf:in-text-mode
796 (pdf:move-text (* -0.5 width)(* -0.18 *font-size*))
797 (pdf:set-font *font* (* *font-size* 0.8))
798 (pdf:show-text (make-string 1 :initial-element (boxed-char box)))))))
800 ;; a draw function for the functional rule...
802 (defun draw-wavelet-rule (box x0 y0)
803 (let ((dx/2 (* (dx box) 0.5))
804 (dy/2 (* (dy box) 0.5)))
805 (pdf:with-saved-state
806 (pdf:translate (+ x0 dx/2) (- y0 dy/2))
807 (pdf:set-line-width 1)
808 (pdf:set-color-stroke (color box))
809 (pdf:move-to (- dx/2) 0)
810 (loop with a = (/ -0.2 (dx box)) and w = (/ 200.0 (dx box))
811 for x from (- dx/2) by 0.2
812 for y = (* dy/2 (cos (* x w)) (exp (* x x a)))
813 while (< x dx/2)
814 do (pdf:line-to x y))
815 (pdf:stroke))))
817 ;; user-drawn box
819 (defun user-drawn-demo (box x y)
820 (draw-block
821 (compile-text ()
822 (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10
823 :font "Times-Italic" :font-size 6.5)
824 *par1*))
825 x (- y (dy box)) (- (dy box) 10) (dx box) :rotation 90 :border 0.1))
827 ;; a chart (I will have to change this in cl-pdf: it's a real mess!)
829 (defun draw-pie (box x y)
830 (pdf:draw-object (make-instance
831 'pdf:pie-chart :x (+ x 30) :y (- y 100) :width 90 :height 90
832 :serie '(12 23 65 33)
833 :labels&colors
834 '(("Winter" (1.0 0.0 0.0))
835 ("Spring" (0.0 1.0 0.0))
836 ("Summer" (0.0 0.0 1.0))
837 ("Autumn" (0.0 1.0 1.0))))))
839 ;; a stupid trick
840 ;; brute force!!!
841 (defun link-all-a (box x y)
842 (pdf:with-saved-state
843 (let ((all-a ()))
844 (map-boxes box 0 0
845 #'(lambda (box x y)
846 (when (and (char-box-p box) (char= (boxed-char box)#\a))
847 (push (list (+ x (* 0.5 (dx box)))
848 (+ y (* 0.2 (dy box)))
849 box)
850 all-a))))
851 (pdf:set-line-width 1)
852 (pdf:set-rgb-stroke 1.0 0.8 0.4)
853 (loop for (x y box) in all-a
854 for sorted-a = (sort (copy-seq all-a)
855 #'(lambda (item1 item2)
856 (let ((dx1 (- (first item1) x))
857 (dy1 (- (second item1) y))
858 (dx2 (- (first item2) x))
859 (dy2 (- (second item2) y)))
860 (<= (sqrt (+ (* dx1 dx1)
861 (* dy1 dy1)))
862 (sqrt (+ (* dx2 dx2)
863 (* dy2 dy2)))))))
864 do (loop repeat 4
865 for (x2 y2 box) in sorted-a
867 (pdf:set-gray-fill 0.8)
868 (pdf::move-to x y)
869 (pdf::line-to x2 y2)
870 (pdf::stroke)))
871 (pdf:set-gray-fill 0.8)
872 (pdf:set-rgb-stroke 0.5 0.7 1.0)
873 (loop for (x y box) in all-a
875 (pdf:circle x y (* (dx box) 0.7))
876 (pdf:fill-and-stroke)))))
878 ;;; rivers detection (still brute force not to be used in real life...)
879 (defun link-all-spaces (box x y)
880 (pdf:with-saved-state
881 (let ((all-spaces ()))
882 (map-boxes box 0 0
883 #'(lambda (box x y)
884 (when (white-char-box-p box)
885 (push (list (+ x (* 0.5 (dx box)))
886 (+ y (* 0.5 (dx box)))
887 box)
888 all-spaces))))
889 (pdf:set-line-width 1)
890 (pdf:set-rgb-stroke 1.0 0.4 0)
891 (loop for (x y box) in all-spaces
892 for sorted-spaces = (sort (copy-seq all-spaces)
893 #'(lambda (item1 item2)
894 (let ((dx1 (- (first item1) x))
895 (dx2 (- (first item2) x)))
896 (<= (abs dx1)(abs dx2)))))
897 do (loop repeat 5
898 for (x2 y2 box) in sorted-spaces
899 for dy = (abs (- y2 y))
900 for dx = (abs (- x2 x))
902 (when (and (< dx (* 1.5 (+ (dx box)
903 (delta-size box))))
904 (< 0 dy (* 5 (dx box))))
905 (pdf:set-gray-fill 0.8)
906 (pdf::move-to x y)
907 (pdf::line-to x2 y2)
908 (pdf::stroke)
909 (pdf:circle x y (* (dx box) 0.7))
910 (pdf:circle x2 y2 (* (dx box) 0.7))
911 (pdf:fill-and-stroke)))))))
913 (defun link-all-a-and-spaces (box x y)
914 (link-all-a box x y)
915 (link-all-spaces box x y))
917 (defun gen-box-graph ()
918 (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
919 :max-dx 500 :max-dy 300 :border-width nil))
920 (n1 (make-instance 'graph-node :data "box" :graph g1))
921 (n2 (make-instance 'graph-node :data "h-mode-mixin" :graph g1))
922 (n3 (make-instance 'graph-node :data "v-mode-mixin" :graph g1))
923 (n5 (make-instance 'graph-node :data "soft-box" :graph g1))
924 (n6 (make-instance 'graph-node :data "container-box" :graph g1))
925 (n7 (make-instance 'graph-node :data "vbox" :graph g1))
926 (n8 (make-instance 'graph-node :data "hbox" :graph g1))
927 (n9 (make-instance 'graph-node :data "glue" :graph g1))
928 (n10 (make-instance 'graph-node :data "hglue" :graph g1))
929 (n11 (make-instance 'graph-node :data "vglue" :graph g1))
930 (n12 (make-instance 'graph-node :data "spacing" :graph g1))
931 (n13 (make-instance 'graph-node :data "h-spacing" :graph g1))
932 (n14 (make-instance 'graph-node :data "v-spacing" :graph g1))
933 (n15 (make-instance 'graph-node :data "char-box" :graph g1))
934 (n16 (make-instance 'graph-node :data "white-char-box" :graph g1)))
935 (add-rank-constraint g1 "same" (list n1 n5 n15))
936 (make-instance 'graph-edge :head n1 :tail n5 :label "subclass" :graph g1)
937 (make-instance 'graph-edge :head n5 :tail n6 :graph g1)
938 (make-instance 'graph-edge :head n6 :tail n7 :graph g1)
939 (make-instance 'graph-edge :head n2 :tail n7 :graph g1)
940 (make-instance 'graph-edge :head n6 :tail n8 :graph g1)
941 (make-instance 'graph-edge :head n3 :tail n8 :graph g1)
942 (make-instance 'graph-edge :head n5 :tail n9 :graph g1)
943 (make-instance 'graph-edge :head n9 :tail n10 :graph g1)
944 (make-instance 'graph-edge :head n2 :tail n10 :graph g1)
945 (make-instance 'graph-edge :head n9 :tail n11 :graph g1)
946 (make-instance 'graph-edge :head n3 :tail n11 :graph g1)
947 (make-instance 'graph-edge :head n5 :tail n12 :graph g1)
948 (make-instance 'graph-edge :head n12 :tail n13 :graph g1)
949 (make-instance 'graph-edge :head n2 :tail n13 :graph g1)
950 (make-instance 'graph-edge :head n12 :tail n14 :graph g1)
951 (make-instance 'graph-edge :head n3 :tail n14 :graph g1)
952 (make-instance 'graph-edge :head n1 :tail n15 :graph g1)
953 (make-instance 'graph-edge :head n2 :tail n15 :graph g1)
954 (make-instance 'graph-edge :head n10 :tail n16 :graph g1)
955 (compute-graph-layout g1)
956 g1))
958 (defun make-color-node-box (graph name color description)
959 (make-instance 'graph-node :graph graph :dx 70 :data
960 (make-filled-vbox
961 (compile-text ()
962 (paragraph (:h-align :center :font "Helvetica-Oblique"
963 :font-size 14 :color '(0 0 0))
964 (put-string name) " "
965 (colored-box :dx 9.0 :dy 9.0 :color color :border-width 0.5)
966 :eol
967 (with-style (:font "Times-Italic" :font-size 10)
968 (put-string description))))
969 70 +huge-number+)))
971 (defun gen-color-graph ()
972 (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
973 :max-dx 500 :max-dy 300 :border-width nil))
974 (red (make-color-node-box g1 "red" '(1.0 0 0) "(primary color)"))
975 (green (make-color-node-box g1 "green" '(0 1.0 0) "(primary color)"))
976 (blue (make-color-node-box g1 "blue" '(0 0 1.0) "(primary color)"))
977 (cyan (make-color-node-box g1 "cyan" '(0 1.0 1.0) "(green + blue)"))
978 (magenta (make-color-node-box g1 "magenta" '(1.0 0 1.0) "(red + blue)"))
979 (yellow (make-color-node-box g1 "yellow" '(1.0 1.0 0) "(red + green)")))
980 (make-instance 'graph-edge :head blue :tail cyan :graph g1)
981 (make-instance 'graph-edge :head green :tail cyan :graph g1)
982 (make-instance 'graph-edge :head red :tail magenta :graph g1)
983 (make-instance 'graph-edge :head blue :tail magenta :graph g1)
984 (make-instance 'graph-edge :head red :tail yellow :graph g1)
985 (make-instance 'graph-edge :head green :tail yellow :graph g1)
986 (compute-graph-layout g1)
987 g1))
989 ;;; Example document
990 ; Copy the files from the directory "files-for-example/" (included in
991 ; the cl-typesetting distribution) to the /tmp directory (or somewhere else).
993 ; Then you need to load the fonts with something like this:
994 ; (pdf:load-t1-font "/tmp/cmex10.afm" "/tmp/cmex10.pfb")
995 ; (pdf:load-t1-font "/tmp/cmti10.afm" "/tmp/cmti10.pfb")
997 (defun full-example (&key (file #P"/tmp/ex.pdf")
998 (banner-jpg #P"/tmp/banner.jpg")
999 (fractal-jpg #P"/tmp/fractal.jpg")
1000 display-graphs)
1001 (with-document ()
1002 (pdf:with-page ()
1003 (pdf:with-outline-level ("Table of Content" (pdf:register-page-reference))
1004 (let ((content
1005 (compile-text ()
1006 (vspace 100)
1007 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 60 :color '(0.0 0 0.8))
1008 "cl-typesetting" :eol
1009 (vspace 2)
1010 (hrule :dy 1)
1011 (with-style (:font "Times-Italic" :font-size 28)
1012 "The Cool Common Lisp Typesetting System"))
1013 (vspace 100)
1014 (hrule :dy 30 :stroke-fn 'draw-wavelet-rule :color '(0.0 0 0.6))
1015 (vspace 250)
1016 (hrule :dy 0.5 :color '(0.0 0 0.6))
1017 (paragraph (:h-align :center :font "Helvetica" :font-size 16 :color '(0.0 0 0.6))
1018 "Table of content")
1019 (hrule :dy 0.1 :color '(0.0 0 0.6))
1020 (paragraph (:h-align :fill :font "Helvetica" :font-size 14 :color '(0.0 0 0.4)
1021 :left-margin 25 :right-margin 25)
1022 "Hello world" (dotted-hfill)
1023 (format-string "~d" (find-ref-point-page-number "hello")) :eol
1024 "Full demo" (dotted-hfill)
1025 (format-string "~d" (find-ref-point-page-number "demo")) :eol
1026 "cl-typegraph" (dotted-hfill)
1027 (format-string "~d" (find-ref-point-page-number "graph")) :eol)
1028 (vspace 2)
1029 (hrule :dy 0.5 :color '(0.0 0 0.6)))))
1030 (draw-block content 20 800 545 700))))
1031 (pdf:with-page ()
1032 (pdf:with-outline-level ("Hello world" (pdf:register-page-reference))
1033 (let ((content
1034 (compile-text ()
1035 (mark-ref-point "hello")
1036 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1037 "cl-typesetting" :eol
1038 (vspace 2)
1039 (hrule :dy 1)
1040 (with-style (:font "Times-Italic" :font-size 24)
1041 "The Cool Common Lisp Typesetting System")
1042 ; (vspace 50)
1043 ; (with-style (:font "Helvetica-Oblique" :font-size 100)
1044 ; "Hello World!")
1045 :vfill
1046 (with-style (:font "Times-Italic" :font-size 100)
1047 "Hello World!")
1048 :vfill))))
1049 (draw-block content 20 800 545 700))))
1050 (pdf:with-page ()
1051 (pdf:with-outline-level ("Full demo" (pdf:register-page-reference))
1052 (let ((content
1053 (compile-text ()
1054 (mark-ref-point "demo")
1055 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1056 "cl-typesetting" :eol
1057 (vspace 2)
1058 (hrule :dy 1)
1059 (with-style (:font "Times-Italic" :font-size 13)
1060 "The Cool Common Lisp Typesetting System"))
1061 (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
1062 :font "Times-Italic" :font-size 9)
1063 "This typesetting system's goal is to be an alternative to the TeX typesetting system. It is written in Common Lisp and uses cl-pdf as its backend. This enables it to be powerful, extensible, programmable and fast. Though it is not considered very difficult, it is already much better than Word...")
1064 (paragraph (:h-align :center :font "Helvetica-BoldOblique" :font-size 20 :color '(1.0 0 0))
1065 "Now in Color! "
1066 (colored-box :dx 15.0 :dy 15.0 :color "#FFC0C0" :border-width 0.5) " "
1067 (colored-box :dx 15.0 :dy 15.0 :color "#C0FFC0" :border-width 0.5) " "
1068 (colored-box :dx 15.0 :dy 15.0 :color "#C0C0FF" :border-width 0.5))
1069 (paragraph (:h-align :center :font "Times-Italic" :font-size 12 :color '(0.0 0.6 0.3))
1070 "With user defined "
1071 (put-rotated-char-string "extensions") :eol
1072 (with-style (:font "Times-Italic" :font-size 11)
1073 "Support for images and functional rules" :eol
1074 (image :file banner-jpg :dx 100 :dy 20)))
1075 (hrule :dy 15 :stroke-fn 'draw-wavelet-rule)
1076 (vspace 3)
1077 (table (:col-widths '(60 80 80) :border 0.5 :background-color '(1 1 0.8)
1078 :cell-padding 1 :padding 2)
1079 (row ()
1080 (cell (:background-color '(0.8 1 0.8) :col-span 3)
1081 (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
1082 "Title with a col-span of 3")))
1083 (row ()
1084 (cell (:background-color '(0.8 0.8 0.8))
1085 (paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1086 "Left aligned"))
1087 (cell (:background-color '(0.8 0.8 0.8))
1088 (paragraph (:h-align :center :font "Times-Roman" :font-size 9)
1089 "Centered cell content"))
1090 (cell (:background-color '(0.8 0.8 0.8))
1091 (paragraph (:h-align :right :font "Times-Bold" :font-size 9)
1092 "Right cell content")))
1093 (row ()
1094 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1095 "This cell content should take three lines."))
1096 (cell (:background-color '(1 1 1))
1097 (paragraph (:h-align :center :font "Times-Italic" :font-size 9)
1098 "A jpeg "
1099 (image :file fractal-jpg :dx 15 :dy 15 :inline t
1100 :offset 9)
1101 " in the text"))
1102 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 11)
1103 (put-rotated-char-string "common lisp is cool"))))
1104 (row ()
1105 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1106 "An example of table inside a cell"))
1107 (cell (:background-color '(1 1 1))
1108 (table (:col-widths '(14 14 21) :border 0.2
1109 :background-color '(0.4 0.4 0.8))
1110 (row () (cell () "12")(cell () "34")(cell () "567"))
1111 (row () (cell () "ab")(cell () "cd")(cell () "efg"))))
1112 (cell (:v-align :bottom)(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1113 "You can nest as many tables as you want, like you do in HTML."))))
1114 (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10 :color '(0 0 0)
1115 :left-margin 5 :right-margin 5
1116 :font "Times-Roman" :font-size 10 :text-x-scale 0.7)
1117 (with-style (:color '(0 0.6 0.4))
1118 "This paragraph has been horizontally strechted by a 0.7 ratio. ")
1119 *par1*)
1120 (vspace 10)
1121 (user-drawn-box :dx 210 :dy 100 :stroke-fn 'user-drawn-demo) :eol
1122 (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 5)
1123 "An example of using cl-typesetting in an user-drawn box.")
1124 (paragraph (:h-align :left :top-margin 15
1125 :left-margin 5 :right-margin 5 :font "courier" :font-size 8)
1126 (verbatim
1127 "(defmethod stroke ((box char-box) x y)
1128 (pdf:in-text-mode
1129 (pdf:move-text x (+ y (offset box)))
1130 (pdf:set-font *font* *font-size*)
1131 (pdf:set-text-x-scale (* *text-x-scale* 100))
1132 (pdf:show-char (boxed-char box))))"))
1133 (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 3)
1134 "An example of verbatim code.")
1135 (paragraph (:h-align :justified :top-margin 9 :font "Helvetica-Oblique"
1136 :left-margin 5 :right-margin 5
1137 :font-size 9 :first-line-indent 20)
1138 *par1*)
1139 (user-drawn-box :dx 240 :dy 100 :stroke-fn 'draw-pie) :eol
1140 (paragraph (:h-align :center :font "Times-Italic" :font-size 8)
1141 "An example of cl-pdf pie chart inserted.")
1142 (paragraph (:h-align :justified :top-margin 9 :font "helvetica" :font-size 9
1143 :left-margin 40 :right-margin 40)
1144 *par2*)
1145 (vspace 10)
1146 (paragraph (:h-align :center :top-margin 20 :font "Times-Bold" :font-size 20)
1147 "Kerning test" :eol
1148 (with-style (:font "Helvetica" :font-size 40 :left-margin 20 :right-margin 20)
1149 "Yes, AWAY"))
1150 (paragraph (:h-align :center :top-margin 10 :font "CMTI10"
1151 :font-size 16 :color '(0 0 0))
1152 (with-style (:font "Times-Bold" :font-size 20)
1153 "Basic Math Mode Test" :eol)
1154 (vspace 5)
1155 (display-formula ()
1156 (with-style (:font (pdf:get-font "CMEX10" nil) :font-size 30)
1157 (with-offset (23) "H"))
1158 "E"(math-super-and-sub-script () ("n+1") ("k,m"))"="
1159 (fraction ()
1160 ("x"(with-superscript () "2")"+x-1")
1161 ("F(x)+b-3"))
1162 "-e"(with-superscript () "-x"(with-superscript () "2")))
1163 (vspace 5)
1164 (with-style (:font "Times-Roman" :font-size 10)
1165 "This test now uses a TeX font (cmti10). Note the italic" :eol "correction for the super/subscript of the E."))
1166 (paragraph (:h-align :center :top-margin 20 :font "Times-Italic" :font-size 18 :color '(0.8 0 0))
1167 "This test pdf file has been typeset " :eol "with cl-typesetting 0.80" :eol
1168 (vspace 10)
1169 (with-style (:font "Times-Italic" :font-size 14)
1170 "Marc Battyani"))
1171 :vfill
1172 (hrule :dy 20 :stroke-fn 'draw-wavelet-rule :color '(0.8 0 0))
1173 :vfill
1174 (paragraph (:h-align :center :font "Helvetica-Oblique" :font-size 8)
1175 "This project needs contributors. So if you are interested contact "
1176 (with-style (:font "Times-Italic" :font-size 9)
1177 "marc.battyani@fractalconcept.com") "."
1178 ))))
1179 (pdf::draw-bar-code128 "CODE128BARCODE" 10 35 :height 25 :width 150 :start-stop-factor 0.25
1180 :font-size 7 :show-string t)
1181 (draw-block content 40 800 250 380 :rotation 5 :border 0.1)
1182 (draw-block content 50 425 250 380 :rotation -5 :border 0.1)
1183 (draw-block content 330 800 250 380 :rotation -2 :border 0.1 :special-fn 'link-all-a-and-spaces)
1184 (draw-block content 310 400 250 380 :v-align :justified :border 0.1))))
1185 (pdf:with-page ()
1186 (pdf:with-outline-level ("cl-typegraph" (pdf:register-page-reference))
1187 (let ((content
1188 (compile-text ()
1189 (mark-ref-point "graph")
1190 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1191 "cl-typegraph" :eol
1192 (vspace 2)
1193 (hrule :dy 1)
1194 (with-style (:font "Times-Italic" :font-size 13)
1195 "The Cool Common Lisp Graph Typesetting System"))
1196 (vspace 20)
1197 (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
1198 :font "helvetica" :font-size 12)
1199 "cl-typegraph is a cl-typesetting extension to typeset graphs. It uses GraphViz for the graph layout and then draws it with cl-pdf and cl-typesetting. The nodes can contain strings or a full cl-typesetting layout." :eol
1200 (vspace 20)
1201 "In the first graph example below, the nodes contain only strings.")
1202 (vspace 20)
1203 :hfill (if display-graphs
1204 (graph-box (gen-box-graph))
1205 (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
1206 (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
1207 "The class hierarchy for the boxes in cl-typesetting.")
1208 (vspace 20)
1209 (paragraph (:h-align :justified :font "helvetica" :font-size 12)
1210 "In the next graph, each node contains a full cl-typesetting layout. All the cl-typesetting features can be used in a node, even another graph.")
1211 (vspace 20)
1212 :hfill (if display-graphs
1213 (graph-box (gen-color-graph))
1214 (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
1215 (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
1216 "The primary colors"))))
1217 (draw-block content 30 810 530 780))))
1218 (pdf:write-document file)))
1220 ;;; A higher level example
1222 (defun multi-page-example (&optional (file #P"/tmp/multi-page.pdf")
1223 &aux content table (margins '(72 72 72 50)))
1224 (with-document ()
1225 ;(pdf:set-line-width 0.1)
1226 (let* ((print-stamp (multiple-value-bind (second minute hour date month year)
1227 (get-decoded-time)
1228 (format nil "Printed on ~4D-~2,'0D-~2,'0D ~2,'0D:~2,'0D"
1229 year month date hour minute)))
1230 (header (compile-text ()
1231 (paragraph (:h-align :center
1232 :font "Helvetica-BoldOblique" :font-size 12)
1233 "Multi-page example document")
1234 ;(vspace 1) ;:vfill
1235 (hrule :dy 1/2)))
1236 (footer (lambda (pdf:*page*)
1237 (compile-text (:font "Helvetica" :font-size 10)
1238 (hrule :dy 1/2)
1239 (hbox (:align :center :adjustable-p t)
1240 (verbatim print-stamp)
1241 ;(hspace 100)
1242 :hfill
1243 (verbatim
1244 (format nil "Page ~d" pdf:*page-number*)))
1245 ))))
1246 (setq content
1247 (compile-text ()
1248 (paragraph (:font "Helvetica-Bold" :font-size 16 :top-margin 20)
1249 "1. First paragraph group")
1250 (hrule :dy 2)
1251 (dotimes (i 40)
1252 (paragraph (:font "Helvetica" :font-size (+ 6 (random 10)))
1253 (verbatim (format nil "1.~d. " (1+ i)))
1254 (dotimes (j (1+ (random 5)))
1255 (put-string "The quick brown fox jumps over the lazy dog. "))))
1256 :eop))
1257 (draw-pages content :margins margins :header header :footer footer)
1259 (setq content
1260 (compile-text ()
1261 ;(vbox ()
1262 (paragraph (:font "Times-Bold" :font-size 16 :top-margin 20)
1263 "2. Second paragraph group"
1264 (hrule :dy 2))
1265 (dotimes (i 40)
1266 (paragraph (:font "Times-Roman" :font-size (+ 6 (random 10)))
1267 (verbatim (format nil "2.~d. " (1+ i)))
1268 (dotimes (j (1+ (random 5)))
1269 (put-string "The quick brown fox jumps over the lazy dog. "))))
1271 (table (:col-widths '(20 100 100 100) :splittable-p nil
1272 :border 0 :background-color '(1 1 0.8))
1273 (row ()
1274 (cell (:col-span 4)
1275 (paragraph (:h-align :center
1276 :font "Times-Italic" :font-size 12)
1277 "Non-splittable table - row with a col-span of 4")))
1278 (loop for (name tel age) = (list "Dmitri Dmitriev" "555-1234" (+ 25 (random 25)))
1279 and row-number from 1 upto 4
1281 (row (:height (when (evenp row-number) 20)
1282 :background-color (if (zerop (mod row-number 3))
1283 :red nil))
1284 (cell () (verbatim
1285 (format nil "~d" row-number)))
1286 (cell (:background-color (when (>= age 40) :blue))
1287 name)
1288 (cell () tel)
1289 (cell () (paragraph (:h-align :right) age)))))
1291 (draw-pages content :margins margins :header header :footer footer)
1293 (setq content
1294 (compile-text ()
1295 (table (:col-widths '(20 100 100 100) :splittable-p t
1296 :border 1/2 :background-color '(1 1 0.8))
1297 (header-row (:background-color :gray)
1298 (cell (:row-span 2) (verbatim "Row #"))
1299 (cell (:row-span 2) "Name")
1300 (cell () "Telephone")
1301 ;(table (:col-widths '(100) :splittable-p nil
1302 ; :padding 0 :border 1/2); :background-color '(1 1 0.8))
1303 ; (row () (cell () "Telephone"))
1304 ; (row () (cell () "Fax"))))
1305 (cell (:row-span 2) (paragraph (:h-align :right) "Age")))
1306 (header-row (:background-color :gray)
1307 (cell () "Fax"))
1308 (footer-row (:background-color :gray)
1309 (cell (:col-span 4)
1310 (paragraph (:h-align :center
1311 :font "Times-Italic" :font-size 12)
1312 "Table footer with a col-span of 4")))
1313 (loop for (name tel age) = (list "Ivan Ivanov" "555-1234" (+ 25 (random 25)))
1314 and row-number from 1 upto 40
1316 (row (:height (when (evenp row-number) 20)
1317 :background-color (if (zerop (mod row-number 3))
1318 :red nil))
1319 (cell () (verbatim
1320 (format nil "~d" row-number)))
1321 (cell (:background-color (when (>= age 40) :blue))
1322 name)
1323 (cell () tel)
1324 (cell () (paragraph (:h-align :right) age)))))))
1325 (setq table content)
1326 (draw-pages content :margins margins :header header :footer footer) ;:break :after
1328 (setq content ;; Various spans
1329 (compile-text ()
1330 (table (:col-widths '(20 40 60 80 120)
1331 :background-color :yellow :border 1
1332 :splittable-p t)
1333 (header-row ()
1334 (cell (:col-span 5)
1335 (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
1336 "Table with cells spanning more then one row")))
1337 (row (:background-color :green)
1338 (cell (:row-span 2 :background-color :blue)
1339 "1,1 2,1 row-span 2")
1340 (cell () "1,2")
1341 (cell (:col-span 2 :row-span 3 :background-color :red)
1342 "1,3 1,4 - 3,3 3,4 col-span 2 row-span 3")
1343 (cell () "1,5"))
1344 (row ()
1345 (cell () "2,2")
1346 (cell (:row-span 2 :background-color :blue) "2,5 3,5 row-span 2"))
1347 (row (:background-color :green)
1348 (cell (:col-span 2) "3,1 3,2 col-span 2"))
1349 (row ()
1350 (cell () "4,1")
1351 (cell () "4,2")
1352 (cell () "4,3")
1353 (cell () "4,4")
1354 (cell () "4,5")))))
1355 (draw-pages content :margins margins :header header :footer footer) ;:break :after
1357 (draw-block (compile-text () "Test block - line1" :eol "Line 2")
1358 300 300 150 150 :border 1))
1359 (when pdf:*page* (finalize-page pdf:*page*))
1360 ;(pdf:with-page ()
1361 ;(draw-page content :margins 72))
1362 (pdf:write-document file))
1363 table)
1365 #+nil
1366 (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
1367 (pdf:with-document ()
1368 (let ((content
1369 (compile-text ()
1370 (vspace 100)
1371 (table (:col-widths '(100 200) :splittable-p t) ;;; start Erik changes
1372 (header-row ()
1373 (cell ()
1374 (paragraph () "Header")))
1375 (footer-row ()
1376 (cell ()
1377 (paragraph () "Footer")))
1378 (dotimes (time 50)
1379 (row ()
1380 (cell ()
1381 (paragraph () (put-string (format nil "test ~d" time))))))) ;;; end Erik changes
1382 (vspace 10)
1383 :eol
1384 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50
1385 :color '(0.0 0 0.8))
1386 "cl-typesetting" :eol
1387 (vspace 2)
1388 (hrule :dy 1)
1389 (with-style (:font "Times-Italic" :font-size 26)
1390 "The cool Common Lisp typesetting system")
1391 (vspace 50)
1392 (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0
1393 0.8))
1394 (dotimes (i 100)
1395 (put-string "Hello World!")(new-line)))))))
1396 (loop while (boxes content) do
1397 (pdf:with-page ()
1398 (pdf:set-line-width 0.1)
1399 (draw-block content 20 800 545 700))))
1400 (pdf:write-document file)))
1402 ;;; Unicode test
1404 (defparameter *unicode-test-string*
1405 (map unicode-string-type 'code-char
1406 '(8252 8319 8359 8592 8593 8594 8595 8596 8597 8616 915 920 934 945 948 949 963 964 966 32
1407 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 32 65
1408 9568 9650 9658 9660 9668 9675 9688 9689 8364 1027 8218 402 8222 8230 8224 8225 32 66
1409 372 373 374 375 383 506 507 508 509 510 511 903 913 914 916 917 918 919 921 922 923 32
1410 946 947 950 951 952 953 954 955 956 957 958 1101 1102 1103 1105 1106 1107 1108 32
1411 1475 1488 1489 1490 1491 1492 1493 64304 64305 64306 64307 64308 64309 32
1412 7911 7912 7913 7914 7915 7916 7917 7918 1179 1180 1181 1186 1187 1198 1199 1200 32)))
1414 ;; Look at the unicode-readme.txt in cl-pdf to see how to load unicode fonts
1415 (defun unicode-hello (&optional (file #P"/tmp/hello-u.pdf"))
1416 (pdf:with-document ()
1417 (pdf:with-page ()
1418 (pdf:with-outline-level ("Unicode Example" (pdf:register-page-reference))
1419 (pdf:set-line-width 0.1)
1420 (let ((content
1421 (compile-text ()
1422 (vspace 100)
1423 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
1424 "cl-typesetting" :eol
1425 (vspace 2)
1426 (hrule :dy 1)
1427 (with-style (:font "Times-Italic" :font-size 26)
1428 "The cool Common Lisp typesetting system")
1429 (vspace 50)
1430 (with-style (:font "TimesNewRomanPSMT" :font-size 36)
1431 (put-string *unicode-test-string*))))))
1432 (draw-block content 20 800 545 700))))
1433 (pdf:write-document file)))