3 ;;; Time-stamp: <2009-07-18 11:50:00 tony>
4 ;;; Creation: <2008-09-08 08:06:30 tony>
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
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.
22 (asdf:oos
'asdf
:load-op
'cls
)
23 (asdf:oos
'asdf
:load-op
'cl-pdf
)
24 (asdf:oos
'asdf
:load-op
'cl-typesetting
))
26 (defun init-CLS-graphics ()
28 (asdf:oos
'asdf
:load-op
'cl-cairo2-x11
)
29 (asdf:oos
'asdf
:load-op
'cl-2d
))
33 ;;(asdf:oos 'asdf:load-op 'lisp-matrix)
34 ;;(asdf:oos 'asdf:compile-op 'cls :force t)
35 ;;(asdf:oos 'asdf:load-op 'cls)
38 ;;(asdf:oos 'asdf:load-op 'cl-pdf)
39 ;;(asdf:oos 'asdf:load-op 'cl-typesetting)
41 (in-package :lisp-stat-unittests
)
43 ;; tests = 80, failures = 8, errors = 15
44 (run-tests :suite
'lisp-stat-ut
)
45 (describe (run-tests :suite
'lisp-stat-ut
))
47 (describe 'lisp-stat-ut
)
48 (documentation 'lisp-stat-ut
'type
)
50 ;; FIXME: Example: currently not relevant, yet
51 ;; (describe (lift::run-test :test-case 'lisp-stat-unittests::create-proto
52 ;; :suite 'lisp-stat-unittests::lisp-stat-ut-proto))
54 (describe (lift::run-tests
:suite
'lisp-stat-ut-dataframe
))
55 (lift::run-tests
:suite
'lisp-stat-ut-dataframe
)
57 (describe (lift::run-test
58 :test-case
'lisp-stat-unittests
::create-proto
59 :suite
'lisp-stat-unittests
::lisp-stat-ut-proto
))
63 ;;; Tasks to do and consider:
65 ;;; * must contemplate data as being separate from object system,
66 ;;; working out a new package dependency system.
71 ;; REVIEW: general Lisp use guidance
73 (fdefinition 'make-matrix
)
74 (documentation 'make-matrix
'function
)
76 #| Examples from CLHS
, a bit of guidance.
78 ;; This function assumes its callers have checked the types of the
79 ;; arguments, and authorizes the compiler to build in that assumption.
80 (defun discriminant (a b c
)
81 (declare (number a b c
))
82 "Compute the discriminant for a quadratic equation."
83 (- (* b b
) (* 4 a c
))) => DISCRIMINANT
84 (discriminant 1 2/3 -
2) => 76/9
86 ;; This function assumes its callers have not checked the types of the
87 ;; arguments, and performs explicit type checks before making any assumptions.
88 (defun careful-discriminant (a b c
)
89 "Compute the discriminant for a quadratic equation."
93 (locally (declare (number a b c
))
94 (- (* b b
) (* 4 a c
)))) => CAREFUL-DISCRIMINANT
95 (careful-discriminant 1 2/3 -
2) => 76/9
102 (asdf:oos
'asdf
:load-op
'versioned-objects
)
103 (asdf:oos
'asdf
:load-op
'validations
))
109 (with-data dataset
((dsvarname1 [usevarname1])
110 (dsvarname2 [usevarname2]))
117 (defun testme (&key (a 3) (b (+ a 3)))
123 (testme :a 2 :b (* a 5))
126 ;;;; CL-PDF / CL-TYPESETTING experiments
128 (in-package :cl-user)
130 ;;(asdf:oos 'asdf:load-op 'cl-pdf)
132 ;;(asdf:oos 'asdf:load-op 'cl-typesetting)
134 ;;; cl-pdf copyright 2002-2005 Marc Battyani see license.txt for
135 ;;; details of the BSD style license
137 ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
138 ;;; The homepage of cl-pdf is here: http://www.fractalconcept.com/asp/html/cl-pdf.html
140 (defun example1 (&optional (file #P"/tmp/ex1.pdf"))
141 (with-open-file (outstream file
144 :element-type :default)
145 (pdf:with-document ()
147 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
148 (let ((helvetica (pdf:get-font "Helvetica")))
150 (pdf:set-font helvetica 36.0)
151 (pdf:move-text 100 800)
152 (pdf:draw-text "cl-pdf: Example 1"))
153 (pdf:translate 230 500)
155 for i = 0.67 then (* i 1.045)
157 (pdf:set-font helvetica i)
158 (pdf:set-rgb-fill (/ (random 255) 255.0)
159 (/ (random 255) 255.0)
160 (/ (random 255) 255.0))
161 (pdf:move-text (* i 3) 0)
162 (pdf:show-text "cl-typesetting"))
164 (pdf:write-document outstream))))
168 (with-open-file (tony #p"/tmp/tony.pdf"
170 ; :element-type 'signed-byte
171 ; :element-type :default
172 :if-exists :supersede )
175 ;; for the TrueType Example, you need to load the font first:
176 ;; (read the unicode-readme for more info)
178 (pdf:load-ttu-font #P"/tmp/times.ufm" #P"/tmp/times.ttf")
180 (defun example1-ttu (&optional (file #P"/tmp/ex1-ttu.pdf"))
181 (pdf:with-document ()
183 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
184 (let ((helvetica (pdf:get-font "TimesNewRomanPSMT"))) ; The windows times font
186 (pdf:set-font helvetica 36.0)
187 (pdf:move-text 100 800)
188 (pdf:draw-text "cl-pdf: Example 1 with Unicode"))
189 (pdf:translate 230 500)
191 for i = 0.67 then (* i 1.05)
193 (pdf:set-font helvetica i)
194 (pdf:set-rgb-fill (/ (random 255) 255.0)(/ (random 255) 255.0)(/ (random 255) 255.0))
195 (pdf:move-text (* i 3) 0)
196 (pdf:show-text (format nil "Lisp lives! ~cx.~cy.x " (code-char 955)(code-char 955))))
198 (pdf:write-document file)))
200 (defun example2 (&optional (file #P"/tmp/ex2.pdf"))
201 (pdf:with-document ()
203 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
204 (let ((helvetica (pdf:get-font "Helvetica")))
206 (pdf:set-font helvetica 36.0)
207 (pdf:move-text 100 800)
208 (pdf:draw-text "cl-pdf: Example 2"))
209 (pdf:move-to (+ 10 (random 500))(+ 10 (random 400)))
210 (pdf:set-gray-fill 0.5)
212 (pdf:line-to (+ 50 (random 500)) (+ 50 (random 400))))
213 (pdf:close-even-odd-fill-and-stroke)
214 (pdf:move-to (+ 50 (random 500))(+ 400 (random 400)))
215 (pdf:set-rgb-fill 0.5 0.5 0.8)
216 (pdf:set-rgb-stroke 0.9 0.5 0.1)
218 (pdf:bezier2-to (+ 50 (random 500)) (+ 400 (random 400))
219 (+ 50 (random 500)) (+ 400 (random 400))))
220 (pdf:close-even-odd-fill-and-stroke))))
221 (pdf:write-document file)))
223 (defun gen-image-bits ()
224 (with-output-to-string (s)
225 (loop for x from -10 to 10 by 1/10
226 do (loop for y from -10 to 10 by 1/10
227 do (format s "~2,'0x~2,'0x~2,'0x"
228 (round (+ 200 (* 55 (sin x))))
229 (round (+ 200 (* 55 (cos y))))
230 (round (+ 200 (* 55 (sin (+ x y))))))))))
232 (defun example3 (&optional (file #P"/tmp/ex3.pdf"))
233 (pdf:with-document ()
235 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
236 (let* ((helvetica (pdf:get-font "Helvetica"))
237 (image (make-instance 'pdf:image
238 :bits (gen-image-bits)
239 :width 201 :height 201)))
240 (pdf:draw-bar-code128 "30A0033111436" 20 100)
241 (pdf:add-images-to-page image)
243 (pdf:set-font helvetica 36.0)
244 (pdf:move-text 100 800)
245 (pdf:draw-text "cl-pdf: Example 3"))
246 (pdf:with-saved-state
247 (pdf:translate 102 550)
250 (pdf:paint-image image))
251 (pdf:with-saved-state
252 (pdf:translate 100 540)
255 (pdf:paint-image image)))))
256 (pdf:write-document file)))
262 (defparameter *fractal-ratio* 0.8)
263 (defconstant +sin60+ (sin (/ pi 3)))
264 (defconstant +cos60+ (cos (/ pi 3)))
265 (defconstant +tg30+ (tan (/ pi 6)))
266 (defconstant +tg60-tg30+ (- (tan (/ pi 3))(tan (/ pi 6))))
268 (defun %fractal (x y dx dy level)
270 (let ((dx/2 (* dx 0.5))
272 (pdf:move-to (- x dx/2) (- y dy/2))
273 (pdf:line-to x (+ y dy/2))
274 (pdf:line-to (+ x dx/2) (- y dy/2))
275 (pdf:close-fill-and-stroke))
276 (let* ((delta (- 1 *fractal-ratio*))
277 (delta05 (* 0.5 delta))
278 (ratio2 (- 1 delta05))
279 (deltax (* dx 0.25 (+ 1 (* 0.5 +sin60+ (- 1 ratio2)))))
280 (deltay (* dy 0.25 (+ 1 delta05)))
281 (dyf2 (* dy 0.5 (+ 1 delta05 )))
282 (dxf2 (* dx 0.5 (+ 1 delta05 ))))
286 (%down-fractal x (- y (* 1 dy)(* dx +tg30+ -1)(* 0.125 +tg60-tg30+ dxf2)) dxf2 dyf2 level)
287 (%fractal x (+ y (* dy 0.5)) (* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
288 (%fractal (+ x deltax)(- y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
289 (%fractal (- x deltax)(- y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
292 (defun %down-fractal (x y dx dy level)
295 (let ((dx/2 (* dx 0.5))
297 (pdf:move-to (- x dx/2) (+ y dy/2))
298 (pdf:line-to x (- y dy/2))
299 (pdf:line-to (+ x dx/2)(+ y dy/2))
300 (pdf:close-fill-and-stroke))
301 (let* ((delta (- 1 *fractal-ratio*))
302 (delta05 (* 0.5 delta))
303 (ratio2 (- 1 delta05))
304 (deltax (* dx 0.25 (+ 1 (* 0.5 +sin60+ (- 1 ratio2)))))
305 (deltay (* dy 0.25 (+ 1 delta05)))
306 (dyf2 (* dy 0.5 (+ 1 delta05 )))
307 (dxf2 (* dx 0.5 (+ 1 delta05 ))))
311 (%fractal x (+ y (* 1 dy)(* dx +tg30+ -1)(* 0.125 +tg60-tg30+ dxf2)) dxf2 dyf2 level)
312 (%down-fractal x (- y (* dy 0.5)) (* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
313 (%down-fractal (+ x deltax)(+ y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
314 (%down-fractal (- x deltax)(+ y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
318 (defun fractal (x y l level)
321 (%fractal x y dx dy level)))
324 (defun example4 (&optional (file #P"/tmp/ex4.pdf"))
325 (pdf:with-document ()
326 (loop for i from 1 to 7
328 (pdf:with-outline-level ((format nil "Page ~d" i)(pdf:register-page-reference))
329 (let* ((helvetica (pdf:get-font "Helvetica")))
331 (pdf:set-font helvetica 36.0)
332 (pdf:move-text 100 800)
333 (pdf:draw-text (format nil "cl-pdf: Example 4 page ~d" i)))
334 (pdf:set-rgb-stroke 1.0 1.0 1.0)
335 (pdf:set-rgb-fill 0.4 0.4 0.9)
336 (pdf:set-line-width 0.2)
337 (fractal 298 530 600 i)))))
338 (pdf:write-document file)))
340 (defvar *dx* #(1 0 -1 0))
341 (defvar *dy* #(0 1 0 -1))
344 (defun example5 (nx ny &key (size 10) (file #P"/tmp/ex5.pdf"))
347 (visited (make-array (list nx ny) :initial-element nil))
348 (v-walls (make-array (list nx ny) :initial-element t))
349 (h-walls (make-array (list nx ny) :initial-element t))
354 (let ((tested (vector nil nil nil nil))
356 (loop while (< nb-tested 4)
357 for test = (random 4)
358 unless (svref tested test)
360 (setf (svref tested test) t)
361 (setf next-x (+ x (svref *dx* test)))
362 (setf next-y (+ y (svref *dy* test)))
363 (when (and (>= next-x 0)(< next-x nx)(>= next-y 0)(< next-y ny)
364 (not (aref visited next-x next-y)))
365 (return-from find-cell t)))
367 (setf (aref visited x y) t)
368 (loop with nb-visited = 1 and total-cells = (* nx ny)
369 while (< nb-visited total-cells)
371 (progn (push x x-stack)(push y y-stack)
373 (setf (aref h-walls (min x next-x) y) nil)
374 (setf (aref v-walls x (min y next-y)) nil))
375 (setf x next-x y next-y)
376 (setf (aref visited x y) t)
378 (progn (setf x (pop x-stack) y (pop y-stack))))))
379 (pdf:with-document ()
381 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
382 (pdf:translate (* 0.5 (- 595 (* nx size)))(* 0.5 (- 841 (* ny size))))
383 (setf (aref h-walls (1- nx) (random ny)) nil)
385 (pdf:line-to (* nx size) 0)
387 (pdf:line-to 0 (* ny size))
388 (loop for x from 0 below nx
390 for x1 from size by size
391 do (loop for y from 0 below ny
393 for y1 from size by size
395 (when (aref h-walls x y)
398 (when (aref v-walls x y)
400 (pdf:line-to x1 y1)))
402 (pdf:write-document file))))
405 (defun example6 (&optional (file #P"/tmp/ex6.pdf"))
406 (pdf:with-document ()
408 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
409 (let ((helvetica (pdf:get-font "Helvetica")))
411 (pdf:set-font helvetica 36.0)
412 (pdf:move-text 100 800)
413 (pdf:draw-text "cl-pdf: Example 6"))
414 (pdf:set-rgb-stroke 0.1 0.1 0.1)
415 (pdf:set-rgb-fill 0.8 0.8 0.8)
416 (let ((x 50) (y 600))
418 (pdf:rectangle x y 500 140 :radius 10)
419 (pdf:close-fill-and-stroke)
421 (pdf:translate 50 670)
424 for i = 8 then (* i 1.05)
426 (pdf:set-rgb-fill (* 0.1 i) (* 0.01 i) (* 0.02 i))
427 (pdf:circle x y (* 4 i))
428 (pdf:close-fill-and-stroke)
429 (pdf:ellipse (+ x 250) y (* 5 i) (* 4 i))
430 (pdf:close-fill-and-stroke)
432 (pdf:translate 0 -180)
433 (pdf:regular-polygon 150 0 50 7 :fillet-radius 8)
434 (pdf:close-fill-and-stroke)
435 (pdf:star 350 0 50 30 6 :fillet-radius 5)
436 (pdf:close-fill-and-stroke)
438 (pdf:set-rgb-fill 0.8 0.6 0.2)
439 (pdf:regular-polygon 150 0 30 5 :fillet-radius 4)
440 (pdf:close-fill-and-stroke)
441 (pdf:star 350 0 40 20 4 :fillet-radius 6)
442 (pdf:close-fill-and-stroke)
444 (pdf:set-rgb-fill 0.4 0.8 0.7)
445 (pdf:regular-polygon 150 0 15 3 :fillet-radius 3)
446 (pdf:close-fill-and-stroke)
447 (pdf:star 350 0 35 10 12 :fillet-radius 1)
448 (pdf:close-fill-and-stroke)
449 (pdf:set-line-width 0.5)
450 (loop for r from 2 to 100 by 2
451 for start = (* pi 0.001 (random 2000))
452 for length = (* pi 0.001 (random 2000))
453 do (pdf:set-rgb-stroke (* 0.01 (random 100))(* 0.01 (random 100))(* 0.01 (random 100)))
454 (pdf:arc 250 -230 r start length)
456 (pdf:write-document file)))
458 (defvar *test-jpeg-file-path* (when *load-pathname*
459 (merge-pathnames #P"banner.jpg" *load-pathname*)))
461 (unless *test-jpeg-file-path*
462 (error "please set the *test-jpeg-file-path* variable to the banner.jpg file location"))
464 (defvar *test-jpeg* *test-jpeg-file-path*)
466 (defun example7 (&optional (file #P"/tmp/ex7.pdf"))
467 (pdf:with-document ()
468 (let ((jpg-image (pdf:make-jpeg-image *test-jpeg*))
469 (helvetica (pdf:get-font "Helvetica")))
470 (pdf:with-outline-level ("Contents" "page 1")
472 (pdf:register-page-reference "page 1")
473 (pdf:with-outline-level ("Page 1" "page 1")
475 (pdf:set-font helvetica 36.0)
476 (pdf:move-text 100 800)
477 (pdf:draw-text "cl-pdf: Example 7"))
478 (pdf:set-rgb-stroke 0.1 0.1 0.1)
479 (pdf:set-rgb-fill 0.6 0.6 0.8)
481 (pdf:set-font helvetica 13.0)
482 (pdf:move-text 10 700)
483 (pdf:draw-text "Test for bookmarks, JPEG support, internal links, URI links and basic charts"))
484 (pdf:add-images-to-page jpg-image)
485 (pdf:draw-image jpg-image 10 10 239 50 0 t)
486 (pdf:add-URI-link 10 10 239 50 "http://www.fractalconcept.com/asp/html/cl-pdf.html" :border #(1 1 1))
488 (pdf:set-font helvetica 10.0)
489 (pdf:move-text 500 10)
490 (pdf:draw-text "goto page 2"))
491 (pdf:add-link 495 8 80 14 "page 2")
492 (pdf:draw-object (make-instance 'pdf:histogram :x 200 :y 450 :width 300 :height 200
493 :label-names '("Winter" "Spring" "Summer" "Autumn")
494 :labels&colors '(("Serie 1" (1.0 0.0 0.0))
495 ("Serie 2" (0.0 1.0 0.0)))
496 :series '((42 46 48 42)(40 38 51 46))
497 :background-color '(0.9 0.9 0.9)
498 :stacked-series nil ;;; try also with t
502 (pdf:draw-object (make-instance 'pdf:pie-chart :x 200 :y 100 :width 200 :height 200
503 :serie '(12 23 65 33)
504 :labels&colors '(("Winter" (1.0 0.0 0.0))
505 ("Spring" (0.0 1.0 0.0))
506 ("Summer" (0.0 0.0 1.0))
507 ("Autumn" (0.0 1.0 1.0)))))))
509 (pdf:register-page-reference "page 2")
510 (pdf:with-outline-level ("Page 2" "page 2")
512 (pdf:set-font helvetica 36.0)
513 (pdf:move-text 100 800)
514 (pdf:draw-text "Page 2"))
515 (pdf:add-images-to-page jpg-image)
516 (pdf:draw-image jpg-image 10 10 239 50 0 t)
517 (pdf:add-URI-link 10 10 239 50
518 "http://www.fractalconcept.com/asp/html/cl-pdf.html"
521 (pdf:set-font helvetica 10.0)
522 (pdf:move-text 500 10)
523 (pdf:draw-text "goto page 1"))
524 (pdf:add-link 495 8 80 14 "page 1")
526 (make-instance 'pdf:plot-xy :x 100 :y 400 :width 400 :height 200
527 :labels&colors '(("Data 1" (1.0 0.0 0.0))
528 ("Data 2" (0.0 1.0 0.0))
529 ("Data 3" (0.0 0.0 1.0)))
530 :series '(((1 40) (3 38) (5 31) (7 36))
531 ((2 53) (2.5 42) (3.7 46) (6 48))
532 ((1.3 12) (1.6 18) (2 16) (3 27)))
533 :background-color '(0.9 0.9 0.9)
535 :y-axis-options '(:min-value 0)
536 :legend-options ()))))))
537 (pdf:write-document file)))
539 ; Von Koch fractal (brute force ;-))
541 (defun vk-fractal (l level)
542 (pdf:with-saved-state
548 (loop with l3 = (/ l 3.0) and l-1 = (1- level)
549 for angle in '(nil 60 -120 60)
550 do (when angle (pdf:rotate angle))
551 (vk-fractal l3 l-1))))
555 (defun example8 (&optional (file #P"/tmp/ex8.pdf"))
556 (pdf:with-document ()
557 (loop for i from 0 to 6
559 (pdf:with-outline-level ((format nil "Page ~d" i)(pdf:register-page-reference))
560 (let* ((helvetica (pdf:get-font "Helvetica" :win-ansi-encoding)))
561 (pdf:draw-centered-text
563 (format nil "Flocon de Koch (niveau ~d, ~d segments, périmètre ~,1f mm)"
564 i (* 3 (expt 4 i))(/ (* 180 (* 3 (expt 4 i)))(expt 3 i)))
566 (pdf:translate 42 530)
567 (pdf:set-line-width 0.1)
572 (vk-fractal 510 i)))))
573 (pdf:write-document file)))
575 ;;; A Mandelbrot set from Yannick Gingras
577 (defun hsv->rgb (h s v)
578 "channels are in range [0..1]"
581 (let* ((i (floor (* h 6)))
584 (q (* v (- 1 (* s f))))
585 (t_ (* v (- 1 (* s (- 1 f)))))
595 (defun make-color-map (nb-col
603 ;; borowed from Poly-pen --YGingras
604 (let* ((stop-rad (if stop-rad stop-rad start-rad))
605 (angle-inc (* (if clockwise 1.0 -1.0) nb-loop))
606 (val-inc (- stop-rad start-rad)))
607 (coerce (loop for k from 0 to (1- nb-col) collect
608 (let ((i (/ k (1- nb-col))))
609 (mapcar #'(lambda (x) (round x 1/255))
610 (hsv->rgb (mod (+ tilt-angle (* i angle-inc)) 1)
612 (+ start-rad (* i val-inc))))))
616 (defun gen-mandelbrot-bits (w h)
617 ;; Inside a Moth by Lahvak, for other interesting regions see
618 ;; http://fract.ygingras.net/top
621 (declare (optimize speed (debug 0) (safety 0) (space 0))
623 (let* ((nb-cols 30000)
624 (nb-iter (expt 2 11)) ;; crank this if you have a fast box
625 (center #c(-0.7714390420105d0 0.1264514778485d0))
626 (zoom (* (expt (/ h 320) 2) 268436766))
627 (inc (/ h (* 150000d0 zoom)))
628 (cols (make-color-map nb-cols 1 0.2 0.9 0.24 0.21 t))
632 (declare (type double-float inc))
635 (setf c (complex (+ (realpart center)
636 (* inc (+ (the fixnum j) (/ w -2d0))))
638 (* inc (+ (the fixnum i) (/ h -2d0)))))
640 ;; standard Mandelbrot Set formula
641 (push (dotimes (n nb-iter 0)
642 (setf z (+ (* z z) c))
643 (let ((real (realpart z))
647 ;; sub-iter smoothing
648 (- n (log (log (abs (+ (* z z) c)) 10) 2)))))))
650 (with-output-to-string (s)
651 (let ((max (reduce #'max region)))
652 (dolist (x (nreverse region))
653 (destructuring-bind (r g b)
656 ;; pallette stretching
657 (elt cols (floor (expt (/ x max) (/ nb-iter 256))
658 (/ 1 (1- nb-cols)))))
659 (format s "~2,'0x~2,'0x~2,'0x" r g b)))))))
661 ;;; Example 9 is a Mandelbrot set from Yannick Gingras
662 ;;; Takes a long time...
664 (defun example9 (&optional (file #P"/tmp/ex9.pdf"))
665 "draw a nice region of the Mandelbrot Set"
666 (pdf:with-document ()
668 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
671 (helvetica (pdf:get-font "Helvetica"))
672 (image (make-instance 'pdf:image
673 :bits (gen-mandelbrot-bits w h)
674 :width w :height h)))
675 (pdf:add-images-to-page image)
677 (pdf:set-font helvetica 36.0)
678 (pdf:move-text 100 800)
679 (pdf:draw-text "cl-pdf: Example 9"))
680 (pdf:with-saved-state
682 (pdf:scale (/ w 2) (/ h 2))
683 (pdf:paint-image image)))))
684 (pdf:write-document file)))
687 ;; CL-TYPESETTING example
689 ;;; cl-typesetting copyright 2003-2004 Marc Battyani see license.txt for the details
690 ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
691 ;;; The homepage of cl-typesetting is here: http://www.fractalconcept.com/asp/html/cl-typesetting.html
697 (defparameter *boxes* nil) ;for debugging...
699 ; a very simple hello world
700 (defun hello (&optional (file #P"/tmp/hello.pdf"))
701 (with-open-file (outstream file
704 :element-type :default)
706 (pdf:with-document ()
708 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
709 (pdf:set-line-width 0.1)
713 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
714 "cl-typesetting" :eol
717 (with-style (:font "Times-Italic" :font-size 26)
718 "The cool Common Lisp typesetting system")
720 (with-style (:font "Helvetica-Oblique" :font-size 100)
723 (with-style (:font "Helvetica" :font-size 12)
724 "hello" (dotted-hfill) "4.2" :eol
725 "hello world" (dotted-hfill) "4.2.4.2"))
726 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
727 "hello" (dotted-hfill) "4.2" :eol
728 "hello world" (dotted-hfill) "4.2.4.2")
730 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
731 "hello" (dotted-hfill :pattern-spacing 0.6) "4.2" :eol
732 "hello world" (dotted-hfill :pattern-spacing 0.6) "4.2.4.2")
734 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
735 "hello" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2" :eol
736 "hello world" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2.4.2"))))
737 (draw-block content 20 800 545 700))))
738 (pdf:write-document outstream))))
742 ; a multipage simple hello world
743 (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
744 (with-open-file (outstream file
747 :element-type :default)
748 (pdf:with-document ()
752 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
753 "cl-typesetting" :eol
756 (with-style (:font "Times-Italic" :font-size 26)
757 "The cool Common Lisp typesetting system")
759 (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0 0.8))
761 (put-string "Hello World!")(new-line)))))))
762 (loop while (boxes content) do
764 (pdf:set-line-width 0.1)
765 (draw-block content 20 800 545 700))))
766 (pdf:write-document outstream))))
768 ;; (multi-page-hello)
770 ;; The Fancy Example!
773 "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.")
776 "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.")
778 ;; example of extension
780 (defclass rotated-char-box (soft-box h-mode-mixin)
781 ((boxed-char :accessor boxed-char :initarg :boxed-char)
782 (rotation :accessor rotation :initarg :rotation)))
784 (defun put-rotated-char-string (string)
785 (loop for char across string
786 do (add-box (make-instance 'rotated-char-box :dx *font-size*
787 :dy *font-size* :boxed-char char :baseline (* *font-size* 0.8)
788 :rotation (- (random 120) 60)))))
790 (defmethod stroke ((box rotated-char-box) x y)
791 (let ((dx (dx box))(dy (dy box))
792 (width (pdf:get-char-width (boxed-char box) *font* *font-size*)))
793 (pdf:with-saved-state
794 (pdf:translate (+ x (* dx 0.5)) (+ y (* dy 0.3)))
795 (pdf:set-line-width 0.5)
796 (pdf:set-gray-fill 0.8)
797 (pdf:circle 0 0 (* dx 0.45))
798 (pdf:fill-and-stroke)
799 (pdf:set-gray-fill 0)
800 (pdf:rotate (rotation box))
802 (pdf:move-text (* -0.5 width)(* -0.18 *font-size*))
803 (pdf:set-font *font* (* *font-size* 0.8))
804 (pdf:show-text (make-string 1 :initial-element (boxed-char box)))))))
806 ;; a draw function for the functional rule...
808 (defun draw-wavelet-rule (box x0 y0)
809 (let ((dx/2 (* (dx box) 0.5))
810 (dy/2 (* (dy box) 0.5)))
811 (pdf:with-saved-state
812 (pdf:translate (+ x0 dx/2) (- y0 dy/2))
813 (pdf:set-line-width 1)
814 (pdf:set-color-stroke (color box))
815 (pdf:move-to (- dx/2) 0)
816 (loop with a = (/ -0.2 (dx box)) and w = (/ 200.0 (dx box))
817 for x from (- dx/2) by 0.2
818 for y = (* dy/2 (cos (* x w)) (exp (* x x a)))
820 do (pdf:line-to x y))
825 (defun user-drawn-demo (box x y)
828 (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10
829 :font "Times-Italic" :font-size 6.5)
831 x (- y (dy box)) (- (dy box) 10) (dx box) :rotation 90 :border 0.1))
833 ;; a chart (I will have to change this in cl-pdf: it's a real mess!)
835 (defun draw-pie (box x y)
836 (pdf:draw-object (make-instance
837 'pdf:pie-chart :x (+ x 30) :y (- y 100) :width 90 :height 90
838 :serie '(12 23 65 33)
840 '(("Winter" (1.0 0.0 0.0))
841 ("Spring" (0.0 1.0 0.0))
842 ("Summer" (0.0 0.0 1.0))
843 ("Autumn" (0.0 1.0 1.0))))))
847 (defun link-all-a (box x y)
848 (pdf:with-saved-state
852 (when (and (char-box-p box) (char= (boxed-char box)#\a))
853 (push (list (+ x (* 0.5 (dx box)))
854 (+ y (* 0.2 (dy box)))
857 (pdf:set-line-width 1)
858 (pdf:set-rgb-stroke 1.0 0.8 0.4)
859 (loop for (x y box) in all-a
860 for sorted-a = (sort (copy-seq all-a)
861 #'(lambda (item1 item2)
862 (let ((dx1 (- (first item1) x))
863 (dy1 (- (second item1) y))
864 (dx2 (- (first item2) x))
865 (dy2 (- (second item2) y)))
866 (<= (sqrt (+ (* dx1 dx1)
871 for (x2 y2 box) in sorted-a
873 (pdf:set-gray-fill 0.8)
877 (pdf:set-gray-fill 0.8)
878 (pdf:set-rgb-stroke 0.5 0.7 1.0)
879 (loop for (x y box) in all-a
881 (pdf:circle x y (* (dx box) 0.7))
882 (pdf:fill-and-stroke)))))
884 ;;; rivers detection (still brute force not to be used in real life...)
885 (defun link-all-spaces (box x y)
886 (pdf:with-saved-state
887 (let ((all-spaces ()))
890 (when (white-char-box-p box)
891 (push (list (+ x (* 0.5 (dx box)))
892 (+ y (* 0.5 (dx box)))
895 (pdf:set-line-width 1)
896 (pdf:set-rgb-stroke 1.0 0.4 0)
897 (loop for (x y box) in all-spaces
898 for sorted-spaces = (sort (copy-seq all-spaces)
899 #'(lambda (item1 item2)
900 (let ((dx1 (- (first item1) x))
901 (dx2 (- (first item2) x)))
902 (<= (abs dx1)(abs dx2)))))
904 for (x2 y2 box) in sorted-spaces
905 for dy = (abs (- y2 y))
906 for dx = (abs (- x2 x))
908 (when (and (< dx (* 1.5 (+ (dx box)
910 (< 0 dy (* 5 (dx box))))
911 (pdf:set-gray-fill 0.8)
915 (pdf:circle x y (* (dx box) 0.7))
916 (pdf:circle x2 y2 (* (dx box) 0.7))
917 (pdf:fill-and-stroke)))))))
919 (defun link-all-a-and-spaces (box x y)
921 (link-all-spaces box x y))
923 (defun gen-box-graph ()
924 (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
925 :max-dx 500 :max-dy 300 :border-width nil))
926 (n1 (make-instance 'graph-node :data "box" :graph g1))
927 (n2 (make-instance 'graph-node :data "h-mode-mixin" :graph g1))
928 (n3 (make-instance 'graph-node :data "v-mode-mixin" :graph g1))
929 (n5 (make-instance 'graph-node :data "soft-box" :graph g1))
930 (n6 (make-instance 'graph-node :data "container-box" :graph g1))
931 (n7 (make-instance 'graph-node :data "vbox" :graph g1))
932 (n8 (make-instance 'graph-node :data "hbox" :graph g1))
933 (n9 (make-instance 'graph-node :data "glue" :graph g1))
934 (n10 (make-instance 'graph-node :data "hglue" :graph g1))
935 (n11 (make-instance 'graph-node :data "vglue" :graph g1))
936 (n12 (make-instance 'graph-node :data "spacing" :graph g1))
937 (n13 (make-instance 'graph-node :data "h-spacing" :graph g1))
938 (n14 (make-instance 'graph-node :data "v-spacing" :graph g1))
939 (n15 (make-instance 'graph-node :data "char-box" :graph g1))
940 (n16 (make-instance 'graph-node :data "white-char-box" :graph g1)))
941 (add-rank-constraint g1 "same" (list n1 n5 n15))
942 (make-instance 'graph-edge :head n1 :tail n5 :label "subclass" :graph g1)
943 (make-instance 'graph-edge :head n5 :tail n6 :graph g1)
944 (make-instance 'graph-edge :head n6 :tail n7 :graph g1)
945 (make-instance 'graph-edge :head n2 :tail n7 :graph g1)
946 (make-instance 'graph-edge :head n6 :tail n8 :graph g1)
947 (make-instance 'graph-edge :head n3 :tail n8 :graph g1)
948 (make-instance 'graph-edge :head n5 :tail n9 :graph g1)
949 (make-instance 'graph-edge :head n9 :tail n10 :graph g1)
950 (make-instance 'graph-edge :head n2 :tail n10 :graph g1)
951 (make-instance 'graph-edge :head n9 :tail n11 :graph g1)
952 (make-instance 'graph-edge :head n3 :tail n11 :graph g1)
953 (make-instance 'graph-edge :head n5 :tail n12 :graph g1)
954 (make-instance 'graph-edge :head n12 :tail n13 :graph g1)
955 (make-instance 'graph-edge :head n2 :tail n13 :graph g1)
956 (make-instance 'graph-edge :head n12 :tail n14 :graph g1)
957 (make-instance 'graph-edge :head n3 :tail n14 :graph g1)
958 (make-instance 'graph-edge :head n1 :tail n15 :graph g1)
959 (make-instance 'graph-edge :head n2 :tail n15 :graph g1)
960 (make-instance 'graph-edge :head n10 :tail n16 :graph g1)
961 (compute-graph-layout g1)
964 (defun make-color-node-box (graph name color description)
965 (make-instance 'graph-node :graph graph :dx 70 :data
968 (paragraph (:h-align :center :font "Helvetica-Oblique"
969 :font-size 14 :color '(0 0 0))
970 (put-string name) " "
971 (colored-box :dx 9.0 :dy 9.0 :color color :border-width 0.5)
973 (with-style (:font "Times-Italic" :font-size 10)
974 (put-string description))))
977 (defun gen-color-graph ()
978 (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
979 :max-dx 500 :max-dy 300 :border-width nil))
980 (red (make-color-node-box g1 "red" '(1.0 0 0) "(primary color)"))
981 (green (make-color-node-box g1 "green" '(0 1.0 0) "(primary color)"))
982 (blue (make-color-node-box g1 "blue" '(0 0 1.0) "(primary color)"))
983 (cyan (make-color-node-box g1 "cyan" '(0 1.0 1.0) "(green + blue)"))
984 (magenta (make-color-node-box g1 "magenta" '(1.0 0 1.0) "(red + blue)"))
985 (yellow (make-color-node-box g1 "yellow" '(1.0 1.0 0) "(red + green)")))
986 (make-instance 'graph-edge :head blue :tail cyan :graph g1)
987 (make-instance 'graph-edge :head green :tail cyan :graph g1)
988 (make-instance 'graph-edge :head red :tail magenta :graph g1)
989 (make-instance 'graph-edge :head blue :tail magenta :graph g1)
990 (make-instance 'graph-edge :head red :tail yellow :graph g1)
991 (make-instance 'graph-edge :head green :tail yellow :graph g1)
992 (compute-graph-layout g1)
996 ; Copy the files from the directory "files-for-example/" (included in
997 ; the cl-typesetting distribution) to the /tmp directory (or somewhere else).
999 ; Then you need to load the fonts with something like this:
1000 ; (pdf:load-t1-font "/tmp/cmex10.afm" "/tmp/cmex10.pfb")
1001 ; (pdf:load-t1-font "/tmp/cmti10.afm" "/tmp/cmti10.pfb")
1003 (defun full-example (&key (file #P"/tmp/ex.pdf")
1004 (banner-jpg #P"/tmp/banner.jpg")
1005 (fractal-jpg #P"/tmp/fractal.jpg")
1009 (pdf:with-outline-level ("Table of Content" (pdf:register-page-reference))
1013 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 60 :color '(0.0 0 0.8))
1014 "cl-typesetting" :eol
1017 (with-style (:font "Times-Italic" :font-size 28)
1018 "The Cool Common Lisp Typesetting System"))
1020 (hrule :dy 30 :stroke-fn 'draw-wavelet-rule :color '(0.0 0 0.6))
1022 (hrule :dy 0.5 :color '(0.0 0 0.6))
1023 (paragraph (:h-align :center :font "Helvetica" :font-size 16 :color '(0.0 0 0.6))
1025 (hrule :dy 0.1 :color '(0.0 0 0.6))
1026 (paragraph (:h-align :fill :font "Helvetica" :font-size 14 :color '(0.0 0 0.4)
1027 :left-margin 25 :right-margin 25)
1028 "Hello world" (dotted-hfill)
1029 (format-string "~d" (find-ref-point-page-number "hello")) :eol
1030 "Full demo" (dotted-hfill)
1031 (format-string "~d" (find-ref-point-page-number "demo")) :eol
1032 "cl-typegraph" (dotted-hfill)
1033 (format-string "~d" (find-ref-point-page-number "graph")) :eol)
1035 (hrule :dy 0.5 :color '(0.0 0 0.6)))))
1036 (draw-block content 20 800 545 700))))
1038 (pdf:with-outline-level ("Hello world" (pdf:register-page-reference))
1041 (mark-ref-point "hello")
1042 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1043 "cl-typesetting" :eol
1046 (with-style (:font "Times-Italic" :font-size 24)
1047 "The Cool Common Lisp Typesetting System")
1049 ; (with-style (:font "Helvetica-Oblique" :font-size 100)
1052 (with-style (:font "Times-Italic" :font-size 100)
1055 (draw-block content 20 800 545 700))))
1057 (pdf:with-outline-level ("Full demo" (pdf:register-page-reference))
1060 (mark-ref-point "demo")
1061 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1062 "cl-typesetting" :eol
1065 (with-style (:font "Times-Italic" :font-size 13)
1066 "The Cool Common Lisp Typesetting System"))
1067 (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
1068 :font "Times-Italic" :font-size 9)
1069 "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...")
1070 (paragraph (:h-align :center :font "Helvetica-BoldOblique" :font-size 20 :color '(1.0 0 0))
1072 (colored-box :dx 15.0 :dy 15.0 :color "#FFC0C0" :border-width 0.5) " "
1073 (colored-box :dx 15.0 :dy 15.0 :color "#C0FFC0" :border-width 0.5) " "
1074 (colored-box :dx 15.0 :dy 15.0 :color "#C0C0FF" :border-width 0.5))
1075 (paragraph (:h-align :center :font "Times-Italic" :font-size 12 :color '(0.0 0.6 0.3))
1076 "With user defined "
1077 (put-rotated-char-string "extensions") :eol
1078 (with-style (:font "Times-Italic" :font-size 11)
1079 "Support for images and functional rules" :eol
1080 (image :file banner-jpg :dx 100 :dy 20)))
1081 (hrule :dy 15 :stroke-fn 'draw-wavelet-rule)
1083 (table (:col-widths '(60 80 80) :border 0.5 :background-color '(1 1 0.8)
1084 :cell-padding 1 :padding 2)
1086 (cell (:background-color '(0.8 1 0.8) :col-span 3)
1087 (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
1088 "Title with a col-span of 3")))
1090 (cell (:background-color '(0.8 0.8 0.8))
1091 (paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1093 (cell (:background-color '(0.8 0.8 0.8))
1094 (paragraph (:h-align :center :font "Times-Roman" :font-size 9)
1095 "Centered cell content"))
1096 (cell (:background-color '(0.8 0.8 0.8))
1097 (paragraph (:h-align :right :font "Times-Bold" :font-size 9)
1098 "Right cell content")))
1100 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1101 "This cell content should take three lines."))
1102 (cell (:background-color '(1 1 1))
1103 (paragraph (:h-align :center :font "Times-Italic" :font-size 9)
1105 (image :file fractal-jpg :dx 15 :dy 15 :inline t
1108 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 11)
1109 (put-rotated-char-string "common lisp is cool"))))
1111 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1112 "An example of table inside a cell"))
1113 (cell (:background-color '(1 1 1))
1114 (table (:col-widths '(14 14 21) :border 0.2
1115 :background-color '(0.4 0.4 0.8))
1116 (row () (cell () "12")(cell () "34")(cell () "567"))
1117 (row () (cell () "ab")(cell () "cd")(cell () "efg"))))
1118 (cell (:v-align :bottom)(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1119 "You can nest as many tables as you want, like you do in HTML."))))
1120 (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10 :color '(0 0 0)
1121 :left-margin 5 :right-margin 5
1122 :font "Times-Roman" :font-size 10 :text-x-scale 0.7)
1123 (with-style (:color '(0 0.6 0.4))
1124 "This paragraph has been horizontally strechted by a 0.7 ratio. ")
1127 (user-drawn-box :dx 210 :dy 100 :stroke-fn 'user-drawn-demo) :eol
1128 (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 5)
1129 "An example of using cl-typesetting in an user-drawn box.")
1130 (paragraph (:h-align :left :top-margin 15
1131 :left-margin 5 :right-margin 5 :font "courier" :font-size 8)
1133 "(defmethod stroke ((box char-box) x y)
1135 (pdf:move-text x (+ y (offset box)))
1136 (pdf:set-font *font* *font-size*)
1137 (pdf:set-text-x-scale (* *text-x-scale* 100))
1138 (pdf:show-char (boxed-char box))))"))
1139 (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 3)
1140 "An example of verbatim code.")
1141 (paragraph (:h-align :justified :top-margin 9 :font "Helvetica-Oblique"
1142 :left-margin 5 :right-margin 5
1143 :font-size 9 :first-line-indent 20)
1145 (user-drawn-box :dx 240 :dy 100 :stroke-fn 'draw-pie) :eol
1146 (paragraph (:h-align :center :font "Times-Italic" :font-size 8)
1147 "An example of cl-pdf pie chart inserted.")
1148 (paragraph (:h-align :justified :top-margin 9 :font "helvetica" :font-size 9
1149 :left-margin 40 :right-margin 40)
1152 (paragraph (:h-align :center :top-margin 20 :font "Times-Bold" :font-size 20)
1154 (with-style (:font "Helvetica" :font-size 40 :left-margin 20 :right-margin 20)
1156 (paragraph (:h-align :center :top-margin 10 :font "CMTI10"
1157 :font-size 16 :color '(0 0 0))
1158 (with-style (:font "Times-Bold" :font-size 20)
1159 "Basic Math Mode Test" :eol)
1162 (with-style (:font (pdf:get-font "CMEX10" nil) :font-size 30)
1163 (with-offset (23) "H"))
1164 "E"(math-super-and-sub-script () ("n+1") ("k,m"))"="
1166 ("x"(with-superscript () "2")"+x-1")
1168 "-e"(with-superscript () "-x"(with-superscript () "2")))
1170 (with-style (:font "Times-Roman" :font-size 10)
1171 "This test now uses a TeX font (cmti10). Note the italic" :eol "correction for the super/subscript of the E."))
1172 (paragraph (:h-align :center :top-margin 20 :font "Times-Italic" :font-size 18 :color '(0.8 0 0))
1173 "This test pdf file has been typeset " :eol "with cl-typesetting 0.80" :eol
1175 (with-style (:font "Times-Italic" :font-size 14)
1178 (hrule :dy 20 :stroke-fn 'draw-wavelet-rule :color '(0.8 0 0))
1180 (paragraph (:h-align :center :font "Helvetica-Oblique" :font-size 8)
1181 "This project needs contributors. So if you are interested contact "
1182 (with-style (:font "Times-Italic" :font-size 9)
1183 "marc.battyani@fractalconcept.com") "."
1185 (pdf::draw-bar-code128 "CODE128BARCODE" 10 35 :height 25 :width 150 :start-stop-factor 0.25
1186 :font-size 7 :show-string t)
1187 (draw-block content 40 800 250 380 :rotation 5 :border 0.1)
1188 (draw-block content 50 425 250 380 :rotation -5 :border 0.1)
1189 (draw-block content 330 800 250 380 :rotation -2 :border 0.1 :special-fn 'link-all-a-and-spaces)
1190 (draw-block content 310 400 250 380 :v-align :justified :border 0.1))))
1192 (pdf:with-outline-level ("cl-typegraph" (pdf:register-page-reference))
1195 (mark-ref-point "graph")
1196 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1200 (with-style (:font "Times-Italic" :font-size 13)
1201 "The Cool Common Lisp Graph Typesetting System"))
1203 (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
1204 :font "helvetica" :font-size 12)
1205 "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
1207 "In the first graph example below, the nodes contain only strings.")
1209 :hfill (if display-graphs
1210 (graph-box (gen-box-graph))
1211 (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
1212 (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
1213 "The class hierarchy for the boxes in cl-typesetting.")
1215 (paragraph (:h-align :justified :font "helvetica" :font-size 12)
1216 "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.")
1218 :hfill (if display-graphs
1219 (graph-box (gen-color-graph))
1220 (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
1221 (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
1222 "The primary colors"))))
1223 (draw-block content 30 810 530 780))))
1224 (pdf:write-document file)))
1226 ;;; A higher level example
1228 (defun multi-page-example (&optional (file #P"/tmp/multi-page.pdf")
1229 &aux content table (margins '(72 72 72 50)))
1231 ;(pdf:set-line-width 0.1)
1232 (let* ((print-stamp (multiple-value-bind (second minute hour date month year)
1234 (format nil "Printed on ~4D-~2,'0D-~2,'0D ~2,'0D:~2,'0D"
1235 year month date hour minute)))
1236 (header (compile-text ()
1237 (paragraph (:h-align :center
1238 :font "Helvetica-BoldOblique" :font-size 12)
1239 "Multi-page example document")
1242 (footer (lambda (pdf:*page*)
1243 (compile-text (:font "Helvetica" :font-size 10)
1245 (hbox (:align :center :adjustable-p t)
1246 (verbatim print-stamp)
1250 (format nil "Page ~d" pdf:*page-number*)))
1254 (paragraph (:font "Helvetica-Bold" :font-size 16 :top-margin 20)
1255 "1. First paragraph group")
1258 (paragraph (:font "Helvetica" :font-size (+ 6 (random 10)))
1259 (verbatim (format nil "1.~d. " (1+ i)))
1260 (dotimes (j (1+ (random 5)))
1261 (put-string "The quick brown fox jumps over the lazy dog. "))))
1263 (draw-pages content :margins margins :header header :footer footer)
1268 (paragraph (:font "Times-Bold" :font-size 16 :top-margin 20)
1269 "2. Second paragraph group"
1272 (paragraph (:font "Times-Roman" :font-size (+ 6 (random 10)))
1273 (verbatim (format nil "2.~d. " (1+ i)))
1274 (dotimes (j (1+ (random 5)))
1275 (put-string "The quick brown fox jumps over the lazy dog. "))))
1277 (table (:col-widths '(20 100 100 100) :splittable-p nil
1278 :border 0 :background-color '(1 1 0.8))
1281 (paragraph (:h-align :center
1282 :font "Times-Italic" :font-size 12)
1283 "Non-splittable table - row with a col-span of 4")))
1284 (loop for (name tel age) = (list "Dmitri Dmitriev" "555-1234" (+ 25 (random 25)))
1285 and row-number from 1 upto 4
1287 (row (:height (when (evenp row-number) 20)
1288 :background-color (if (zerop (mod row-number 3))
1291 (format nil "~d" row-number)))
1292 (cell (:background-color (when (>= age 40) :blue))
1295 (cell () (paragraph (:h-align :right) age)))))
1297 (draw-pages content :margins margins :header header :footer footer)
1301 (table (:col-widths '(20 100 100 100) :splittable-p t
1302 :border 1/2 :background-color '(1 1 0.8))
1303 (header-row (:background-color :gray)
1304 (cell (:row-span 2) (verbatim "Row #"))
1305 (cell (:row-span 2) "Name")
1306 (cell () "Telephone")
1307 ;(table (:col-widths '(100) :splittable-p nil
1308 ; :padding 0 :border 1/2); :background-color '(1 1 0.8))
1309 ; (row () (cell () "Telephone"))
1310 ; (row () (cell () "Fax"))))
1311 (cell (:row-span 2) (paragraph (:h-align :right) "Age")))
1312 (header-row (:background-color :gray)
1314 (footer-row (:background-color :gray)
1316 (paragraph (:h-align :center
1317 :font "Times-Italic" :font-size 12)
1318 "Table footer with a col-span of 4")))
1319 (loop for (name tel age) = (list "Ivan Ivanov" "555-1234" (+ 25 (random 25)))
1320 and row-number from 1 upto 40
1322 (row (:height (when (evenp row-number) 20)
1323 :background-color (if (zerop (mod row-number 3))
1326 (format nil "~d" row-number)))
1327 (cell (:background-color (when (>= age 40) :blue))
1330 (cell () (paragraph (:h-align :right) age)))))))
1331 (setq table content)
1332 (draw-pages content :margins margins :header header :footer footer) ;:break :after
1334 (setq content ;; Various spans
1336 (table (:col-widths '(20 40 60 80 120)
1337 :background-color :yellow :border 1
1341 (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
1342 "Table with cells spanning more then one row")))
1343 (row (:background-color :green)
1344 (cell (:row-span 2 :background-color :blue)
1345 "1,1 2,1 row-span 2")
1347 (cell (:col-span 2 :row-span 3 :background-color :red)
1348 "1,3 1,4 - 3,3 3,4 col-span 2 row-span 3")
1352 (cell (:row-span 2 :background-color :blue) "2,5 3,5 row-span 2"))
1353 (row (:background-color :green)
1354 (cell (:col-span 2) "3,1 3,2 col-span 2"))
1361 (draw-pages content :margins margins :header header :footer footer) ;:break :after
1363 (draw-block (compile-text () "Test block - line1" :eol "Line 2")
1364 300 300 150 150 :border 1))
1365 (when pdf:*page* (finalize-page pdf:*page*))
1367 ;(draw-page content :margins 72))
1368 (pdf:write-document file))
1372 (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
1373 (pdf:with-document ()
1377 (table (:col-widths '(100 200) :splittable-p t) ;;; start Erik changes
1380 (paragraph () "Header")))
1383 (paragraph () "Footer")))
1387 (paragraph () (put-string (format nil "test ~d" time))))))) ;;; end Erik changes
1390 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50
1391 :color '(0.0 0 0.8))
1392 "cl-typesetting" :eol
1395 (with-style (:font "Times-Italic" :font-size 26)
1396 "The cool Common Lisp typesetting system")
1398 (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0
1401 (put-string "Hello World!")(new-line)))))))
1402 (loop while (boxes content) do
1404 (pdf:set-line-width 0.1)
1405 (draw-block content 20 800 545 700))))
1406 (pdf:write-document file)))
1410 (defparameter *unicode-test-string*
1411 (map unicode-string-type 'code-char
1412 '(8252 8319 8359 8592 8593 8594 8595 8596 8597 8616 915 920 934 945 948 949 963 964 966 32
1413 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 32 65
1414 9568 9650 9658 9660 9668 9675 9688 9689 8364 1027 8218 402 8222 8230 8224 8225 32 66
1415 372 373 374 375 383 506 507 508 509 510 511 903 913 914 916 917 918 919 921 922 923 32
1416 946 947 950 951 952 953 954 955 956 957 958 1101 1102 1103 1105 1106 1107 1108 32
1417 1475 1488 1489 1490 1491 1492 1493 64304 64305 64306 64307 64308 64309 32
1418 7911 7912 7913 7914 7915 7916 7917 7918 1179 1180 1181 1186 1187 1198 1199 1200 32)))
1420 ;; Look at the unicode-readme.txt in cl-pdf to see how to load unicode fonts
1421 (defun unicode-hello (&optional (file #P"/tmp/hello-u.pdf"))
1422 (pdf:with-document ()
1424 (pdf:with-outline-level ("Unicode Example" (pdf:register-page-reference))
1425 (pdf:set-line-width 0.1)
1429 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
1430 "cl-typesetting" :eol
1433 (with-style (:font "Times-Italic" :font-size 26)
1434 "The cool Common Lisp typesetting system")
1436 (with-style (:font "TimesNewRomanPSMT" :font-size 36)
1437 (put-string *unicode-test-string*))))))
1438 (draw-block content 20 800 545 700))))
1439 (pdf:write-document file)))