3 ;;; Time-stamp: <2009-07-14 19:22:51 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 ;;; * Need to consider a reorg of data package into a single, with
66 ;;; files such as data-frame, data-frame-array, data-frame-listoflist,
67 ;;; data-frame-matrix, listoflist, data-lispstat represending obvious
68 ;;; structures and tools.
70 ;;; * must contemplate data as being separate from object system,
71 ;;; working out a new package dependency system.
76 ;; REVIEW: general Lisp use guidance
78 (fdefinition 'make-matrix
)
79 (documentation 'make-matrix
'function
)
81 #| Examples from CLHS
, a bit of guidance.
83 ;; This function assumes its callers have checked the types of the
84 ;; arguments, and authorizes the compiler to build in that assumption.
85 (defun discriminant (a b c
)
86 (declare (number a b c
))
87 "Compute the discriminant for a quadratic equation."
88 (- (* b b
) (* 4 a c
))) => DISCRIMINANT
89 (discriminant 1 2/3 -
2) => 76/9
91 ;; This function assumes its callers have not checked the types of the
92 ;; arguments, and performs explicit type checks before making any assumptions.
93 (defun careful-discriminant (a b c
)
94 "Compute the discriminant for a quadratic equation."
98 (locally (declare (number a b c
))
99 (- (* b b
) (* 4 a c
)))) => CAREFUL-DISCRIMINANT
100 (careful-discriminant 1 2/3 -
2) => 76/9
107 (asdf:oos
'asdf
:load-op
'versioned-objects
)
108 (asdf:oos
'asdf
:load-op
'validations
))
114 (with-data dataset
((dsvarname1 [usevarname1])
115 (dsvarname2 [usevarname2]))
122 (defun testme (&key (a 3) (b (+ a 3)))
128 (testme :a 2 :b (* a 5))
131 ;;;; CL-PDF / CL-TYPESETTING experiments
133 (in-package :cl-user)
135 ;;(asdf:oos 'asdf:load-op 'cl-pdf)
137 ;;(asdf:oos 'asdf:load-op 'cl-typesetting)
139 ;;; cl-pdf copyright 2002-2005 Marc Battyani see license.txt for
140 ;;; details of the BSD style license
142 ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
143 ;;; The homepage of cl-pdf is here: http://www.fractalconcept.com/asp/html/cl-pdf.html
145 (defun example1 (&optional (file #P"/tmp/ex1.pdf"))
146 (with-open-file (outstream file
149 :element-type :default)
150 (pdf:with-document ()
152 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
153 (let ((helvetica (pdf:get-font "Helvetica")))
155 (pdf:set-font helvetica 36.0)
156 (pdf:move-text 100 800)
157 (pdf:draw-text "cl-pdf: Example 1"))
158 (pdf:translate 230 500)
160 for i = 0.67 then (* i 1.045)
162 (pdf:set-font helvetica i)
163 (pdf:set-rgb-fill (/ (random 255) 255.0)
164 (/ (random 255) 255.0)
165 (/ (random 255) 255.0))
166 (pdf:move-text (* i 3) 0)
167 (pdf:show-text "cl-typesetting"))
169 (pdf:write-document outstream))))
173 (with-open-file (tony #p"/tmp/tony.pdf"
175 ; :element-type 'signed-byte
176 ; :element-type :default
177 :if-exists :supersede )
180 ;; for the TrueType Example, you need to load the font first:
181 ;; (read the unicode-readme for more info)
183 (pdf:load-ttu-font #P"/tmp/times.ufm" #P"/tmp/times.ttf")
185 (defun example1-ttu (&optional (file #P"/tmp/ex1-ttu.pdf"))
186 (pdf:with-document ()
188 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
189 (let ((helvetica (pdf:get-font "TimesNewRomanPSMT"))) ; The windows times font
191 (pdf:set-font helvetica 36.0)
192 (pdf:move-text 100 800)
193 (pdf:draw-text "cl-pdf: Example 1 with Unicode"))
194 (pdf:translate 230 500)
196 for i = 0.67 then (* i 1.05)
198 (pdf:set-font helvetica i)
199 (pdf:set-rgb-fill (/ (random 255) 255.0)(/ (random 255) 255.0)(/ (random 255) 255.0))
200 (pdf:move-text (* i 3) 0)
201 (pdf:show-text (format nil "Lisp lives! ~cx.~cy.x " (code-char 955)(code-char 955))))
203 (pdf:write-document file)))
205 (defun example2 (&optional (file #P"/tmp/ex2.pdf"))
206 (pdf:with-document ()
208 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
209 (let ((helvetica (pdf:get-font "Helvetica")))
211 (pdf:set-font helvetica 36.0)
212 (pdf:move-text 100 800)
213 (pdf:draw-text "cl-pdf: Example 2"))
214 (pdf:move-to (+ 10 (random 500))(+ 10 (random 400)))
215 (pdf:set-gray-fill 0.5)
217 (pdf:line-to (+ 50 (random 500)) (+ 50 (random 400))))
218 (pdf:close-even-odd-fill-and-stroke)
219 (pdf:move-to (+ 50 (random 500))(+ 400 (random 400)))
220 (pdf:set-rgb-fill 0.5 0.5 0.8)
221 (pdf:set-rgb-stroke 0.9 0.5 0.1)
223 (pdf:bezier2-to (+ 50 (random 500)) (+ 400 (random 400))
224 (+ 50 (random 500)) (+ 400 (random 400))))
225 (pdf:close-even-odd-fill-and-stroke))))
226 (pdf:write-document file)))
228 (defun gen-image-bits ()
229 (with-output-to-string (s)
230 (loop for x from -10 to 10 by 1/10
231 do (loop for y from -10 to 10 by 1/10
232 do (format s "~2,'0x~2,'0x~2,'0x"
233 (round (+ 200 (* 55 (sin x))))
234 (round (+ 200 (* 55 (cos y))))
235 (round (+ 200 (* 55 (sin (+ x y))))))))))
237 (defun example3 (&optional (file #P"/tmp/ex3.pdf"))
238 (pdf:with-document ()
240 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
241 (let* ((helvetica (pdf:get-font "Helvetica"))
242 (image (make-instance 'pdf:image
243 :bits (gen-image-bits)
244 :width 201 :height 201)))
245 (pdf:draw-bar-code128 "30A0033111436" 20 100)
246 (pdf:add-images-to-page image)
248 (pdf:set-font helvetica 36.0)
249 (pdf:move-text 100 800)
250 (pdf:draw-text "cl-pdf: Example 3"))
251 (pdf:with-saved-state
252 (pdf:translate 102 550)
255 (pdf:paint-image image))
256 (pdf:with-saved-state
257 (pdf:translate 100 540)
260 (pdf:paint-image image)))))
261 (pdf:write-document file)))
267 (defparameter *fractal-ratio* 0.8)
268 (defconstant +sin60+ (sin (/ pi 3)))
269 (defconstant +cos60+ (cos (/ pi 3)))
270 (defconstant +tg30+ (tan (/ pi 6)))
271 (defconstant +tg60-tg30+ (- (tan (/ pi 3))(tan (/ pi 6))))
273 (defun %fractal (x y dx dy level)
275 (let ((dx/2 (* dx 0.5))
277 (pdf:move-to (- x dx/2) (- y dy/2))
278 (pdf:line-to x (+ y dy/2))
279 (pdf:line-to (+ x dx/2) (- y dy/2))
280 (pdf:close-fill-and-stroke))
281 (let* ((delta (- 1 *fractal-ratio*))
282 (delta05 (* 0.5 delta))
283 (ratio2 (- 1 delta05))
284 (deltax (* dx 0.25 (+ 1 (* 0.5 +sin60+ (- 1 ratio2)))))
285 (deltay (* dy 0.25 (+ 1 delta05)))
286 (dyf2 (* dy 0.5 (+ 1 delta05 )))
287 (dxf2 (* dx 0.5 (+ 1 delta05 ))))
291 (%down-fractal x (- y (* 1 dy)(* dx +tg30+ -1)(* 0.125 +tg60-tg30+ dxf2)) dxf2 dyf2 level)
292 (%fractal x (+ y (* dy 0.5)) (* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
293 (%fractal (+ x deltax)(- y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
294 (%fractal (- x deltax)(- y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
297 (defun %down-fractal (x y dx dy level)
300 (let ((dx/2 (* dx 0.5))
302 (pdf:move-to (- x dx/2) (+ y dy/2))
303 (pdf:line-to x (- y dy/2))
304 (pdf:line-to (+ x dx/2)(+ y dy/2))
305 (pdf:close-fill-and-stroke))
306 (let* ((delta (- 1 *fractal-ratio*))
307 (delta05 (* 0.5 delta))
308 (ratio2 (- 1 delta05))
309 (deltax (* dx 0.25 (+ 1 (* 0.5 +sin60+ (- 1 ratio2)))))
310 (deltay (* dy 0.25 (+ 1 delta05)))
311 (dyf2 (* dy 0.5 (+ 1 delta05 )))
312 (dxf2 (* dx 0.5 (+ 1 delta05 ))))
316 (%fractal x (+ y (* 1 dy)(* dx +tg30+ -1)(* 0.125 +tg60-tg30+ dxf2)) dxf2 dyf2 level)
317 (%down-fractal x (- y (* dy 0.5)) (* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
318 (%down-fractal (+ x deltax)(+ y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
319 (%down-fractal (- x deltax)(+ y deltay)(* dx *fractal-ratio*) (* dy *fractal-ratio*) level)
323 (defun fractal (x y l level)
326 (%fractal x y dx dy level)))
329 (defun example4 (&optional (file #P"/tmp/ex4.pdf"))
330 (pdf:with-document ()
331 (loop for i from 1 to 7
333 (pdf:with-outline-level ((format nil "Page ~d" i)(pdf:register-page-reference))
334 (let* ((helvetica (pdf:get-font "Helvetica")))
336 (pdf:set-font helvetica 36.0)
337 (pdf:move-text 100 800)
338 (pdf:draw-text (format nil "cl-pdf: Example 4 page ~d" i)))
339 (pdf:set-rgb-stroke 1.0 1.0 1.0)
340 (pdf:set-rgb-fill 0.4 0.4 0.9)
341 (pdf:set-line-width 0.2)
342 (fractal 298 530 600 i)))))
343 (pdf:write-document file)))
345 (defvar *dx* #(1 0 -1 0))
346 (defvar *dy* #(0 1 0 -1))
349 (defun example5 (nx ny &key (size 10) (file #P"/tmp/ex5.pdf"))
352 (visited (make-array (list nx ny) :initial-element nil))
353 (v-walls (make-array (list nx ny) :initial-element t))
354 (h-walls (make-array (list nx ny) :initial-element t))
359 (let ((tested (vector nil nil nil nil))
361 (loop while (< nb-tested 4)
362 for test = (random 4)
363 unless (svref tested test)
365 (setf (svref tested test) t)
366 (setf next-x (+ x (svref *dx* test)))
367 (setf next-y (+ y (svref *dy* test)))
368 (when (and (>= next-x 0)(< next-x nx)(>= next-y 0)(< next-y ny)
369 (not (aref visited next-x next-y)))
370 (return-from find-cell t)))
372 (setf (aref visited x y) t)
373 (loop with nb-visited = 1 and total-cells = (* nx ny)
374 while (< nb-visited total-cells)
376 (progn (push x x-stack)(push y y-stack)
378 (setf (aref h-walls (min x next-x) y) nil)
379 (setf (aref v-walls x (min y next-y)) nil))
380 (setf x next-x y next-y)
381 (setf (aref visited x y) t)
383 (progn (setf x (pop x-stack) y (pop y-stack))))))
384 (pdf:with-document ()
386 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
387 (pdf:translate (* 0.5 (- 595 (* nx size)))(* 0.5 (- 841 (* ny size))))
388 (setf (aref h-walls (1- nx) (random ny)) nil)
390 (pdf:line-to (* nx size) 0)
392 (pdf:line-to 0 (* ny size))
393 (loop for x from 0 below nx
395 for x1 from size by size
396 do (loop for y from 0 below ny
398 for y1 from size by size
400 (when (aref h-walls x y)
403 (when (aref v-walls x y)
405 (pdf:line-to x1 y1)))
407 (pdf:write-document file))))
410 (defun example6 (&optional (file #P"/tmp/ex6.pdf"))
411 (pdf:with-document ()
413 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
414 (let ((helvetica (pdf:get-font "Helvetica")))
416 (pdf:set-font helvetica 36.0)
417 (pdf:move-text 100 800)
418 (pdf:draw-text "cl-pdf: Example 6"))
419 (pdf:set-rgb-stroke 0.1 0.1 0.1)
420 (pdf:set-rgb-fill 0.8 0.8 0.8)
421 (let ((x 50) (y 600))
423 (pdf:rectangle x y 500 140 :radius 10)
424 (pdf:close-fill-and-stroke)
426 (pdf:translate 50 670)
429 for i = 8 then (* i 1.05)
431 (pdf:set-rgb-fill (* 0.1 i) (* 0.01 i) (* 0.02 i))
432 (pdf:circle x y (* 4 i))
433 (pdf:close-fill-and-stroke)
434 (pdf:ellipse (+ x 250) y (* 5 i) (* 4 i))
435 (pdf:close-fill-and-stroke)
437 (pdf:translate 0 -180)
438 (pdf:regular-polygon 150 0 50 7 :fillet-radius 8)
439 (pdf:close-fill-and-stroke)
440 (pdf:star 350 0 50 30 6 :fillet-radius 5)
441 (pdf:close-fill-and-stroke)
443 (pdf:set-rgb-fill 0.8 0.6 0.2)
444 (pdf:regular-polygon 150 0 30 5 :fillet-radius 4)
445 (pdf:close-fill-and-stroke)
446 (pdf:star 350 0 40 20 4 :fillet-radius 6)
447 (pdf:close-fill-and-stroke)
449 (pdf:set-rgb-fill 0.4 0.8 0.7)
450 (pdf:regular-polygon 150 0 15 3 :fillet-radius 3)
451 (pdf:close-fill-and-stroke)
452 (pdf:star 350 0 35 10 12 :fillet-radius 1)
453 (pdf:close-fill-and-stroke)
454 (pdf:set-line-width 0.5)
455 (loop for r from 2 to 100 by 2
456 for start = (* pi 0.001 (random 2000))
457 for length = (* pi 0.001 (random 2000))
458 do (pdf:set-rgb-stroke (* 0.01 (random 100))(* 0.01 (random 100))(* 0.01 (random 100)))
459 (pdf:arc 250 -230 r start length)
461 (pdf:write-document file)))
463 (defvar *test-jpeg-file-path* (when *load-pathname*
464 (merge-pathnames #P"banner.jpg" *load-pathname*)))
466 (unless *test-jpeg-file-path*
467 (error "please set the *test-jpeg-file-path* variable to the banner.jpg file location"))
469 (defvar *test-jpeg* *test-jpeg-file-path*)
471 (defun example7 (&optional (file #P"/tmp/ex7.pdf"))
472 (pdf:with-document ()
473 (let ((jpg-image (pdf:make-jpeg-image *test-jpeg*))
474 (helvetica (pdf:get-font "Helvetica")))
475 (pdf:with-outline-level ("Contents" "page 1")
477 (pdf:register-page-reference "page 1")
478 (pdf:with-outline-level ("Page 1" "page 1")
480 (pdf:set-font helvetica 36.0)
481 (pdf:move-text 100 800)
482 (pdf:draw-text "cl-pdf: Example 7"))
483 (pdf:set-rgb-stroke 0.1 0.1 0.1)
484 (pdf:set-rgb-fill 0.6 0.6 0.8)
486 (pdf:set-font helvetica 13.0)
487 (pdf:move-text 10 700)
488 (pdf:draw-text "Test for bookmarks, JPEG support, internal links, URI links and basic charts"))
489 (pdf:add-images-to-page jpg-image)
490 (pdf:draw-image jpg-image 10 10 239 50 0 t)
491 (pdf:add-URI-link 10 10 239 50 "http://www.fractalconcept.com/asp/html/cl-pdf.html" :border #(1 1 1))
493 (pdf:set-font helvetica 10.0)
494 (pdf:move-text 500 10)
495 (pdf:draw-text "goto page 2"))
496 (pdf:add-link 495 8 80 14 "page 2")
497 (pdf:draw-object (make-instance 'pdf:histogram :x 200 :y 450 :width 300 :height 200
498 :label-names '("Winter" "Spring" "Summer" "Autumn")
499 :labels&colors '(("Serie 1" (1.0 0.0 0.0))
500 ("Serie 2" (0.0 1.0 0.0)))
501 :series '((42 46 48 42)(40 38 51 46))
502 :background-color '(0.9 0.9 0.9)
503 :stacked-series nil ;;; try also with t
507 (pdf:draw-object (make-instance 'pdf:pie-chart :x 200 :y 100 :width 200 :height 200
508 :serie '(12 23 65 33)
509 :labels&colors '(("Winter" (1.0 0.0 0.0))
510 ("Spring" (0.0 1.0 0.0))
511 ("Summer" (0.0 0.0 1.0))
512 ("Autumn" (0.0 1.0 1.0)))))))
514 (pdf:register-page-reference "page 2")
515 (pdf:with-outline-level ("Page 2" "page 2")
517 (pdf:set-font helvetica 36.0)
518 (pdf:move-text 100 800)
519 (pdf:draw-text "Page 2"))
520 (pdf:add-images-to-page jpg-image)
521 (pdf:draw-image jpg-image 10 10 239 50 0 t)
522 (pdf:add-URI-link 10 10 239 50
523 "http://www.fractalconcept.com/asp/html/cl-pdf.html"
526 (pdf:set-font helvetica 10.0)
527 (pdf:move-text 500 10)
528 (pdf:draw-text "goto page 1"))
529 (pdf:add-link 495 8 80 14 "page 1")
531 (make-instance 'pdf:plot-xy :x 100 :y 400 :width 400 :height 200
532 :labels&colors '(("Data 1" (1.0 0.0 0.0))
533 ("Data 2" (0.0 1.0 0.0))
534 ("Data 3" (0.0 0.0 1.0)))
535 :series '(((1 40) (3 38) (5 31) (7 36))
536 ((2 53) (2.5 42) (3.7 46) (6 48))
537 ((1.3 12) (1.6 18) (2 16) (3 27)))
538 :background-color '(0.9 0.9 0.9)
540 :y-axis-options '(:min-value 0)
541 :legend-options ()))))))
542 (pdf:write-document file)))
544 ; Von Koch fractal (brute force ;-))
546 (defun vk-fractal (l level)
547 (pdf:with-saved-state
553 (loop with l3 = (/ l 3.0) and l-1 = (1- level)
554 for angle in '(nil 60 -120 60)
555 do (when angle (pdf:rotate angle))
556 (vk-fractal l3 l-1))))
560 (defun example8 (&optional (file #P"/tmp/ex8.pdf"))
561 (pdf:with-document ()
562 (loop for i from 0 to 6
564 (pdf:with-outline-level ((format nil "Page ~d" i)(pdf:register-page-reference))
565 (let* ((helvetica (pdf:get-font "Helvetica" :win-ansi-encoding)))
566 (pdf:draw-centered-text
568 (format nil "Flocon de Koch (niveau ~d, ~d segments, périmètre ~,1f mm)"
569 i (* 3 (expt 4 i))(/ (* 180 (* 3 (expt 4 i)))(expt 3 i)))
571 (pdf:translate 42 530)
572 (pdf:set-line-width 0.1)
577 (vk-fractal 510 i)))))
578 (pdf:write-document file)))
580 ;;; A Mandelbrot set from Yannick Gingras
582 (defun hsv->rgb (h s v)
583 "channels are in range [0..1]"
586 (let* ((i (floor (* h 6)))
589 (q (* v (- 1 (* s f))))
590 (t_ (* v (- 1 (* s (- 1 f)))))
600 (defun make-color-map (nb-col
608 ;; borowed from Poly-pen --YGingras
609 (let* ((stop-rad (if stop-rad stop-rad start-rad))
610 (angle-inc (* (if clockwise 1.0 -1.0) nb-loop))
611 (val-inc (- stop-rad start-rad)))
612 (coerce (loop for k from 0 to (1- nb-col) collect
613 (let ((i (/ k (1- nb-col))))
614 (mapcar #'(lambda (x) (round x 1/255))
615 (hsv->rgb (mod (+ tilt-angle (* i angle-inc)) 1)
617 (+ start-rad (* i val-inc))))))
621 (defun gen-mandelbrot-bits (w h)
622 ;; Inside a Moth by Lahvak, for other interesting regions see
623 ;; http://fract.ygingras.net/top
626 (declare (optimize speed (debug 0) (safety 0) (space 0))
628 (let* ((nb-cols 30000)
629 (nb-iter (expt 2 11)) ;; crank this if you have a fast box
630 (center #c(-0.7714390420105d0 0.1264514778485d0))
631 (zoom (* (expt (/ h 320) 2) 268436766))
632 (inc (/ h (* 150000d0 zoom)))
633 (cols (make-color-map nb-cols 1 0.2 0.9 0.24 0.21 t))
637 (declare (type double-float inc))
640 (setf c (complex (+ (realpart center)
641 (* inc (+ (the fixnum j) (/ w -2d0))))
643 (* inc (+ (the fixnum i) (/ h -2d0)))))
645 ;; standard Mandelbrot Set formula
646 (push (dotimes (n nb-iter 0)
647 (setf z (+ (* z z) c))
648 (let ((real (realpart z))
652 ;; sub-iter smoothing
653 (- n (log (log (abs (+ (* z z) c)) 10) 2)))))))
655 (with-output-to-string (s)
656 (let ((max (reduce #'max region)))
657 (dolist (x (nreverse region))
658 (destructuring-bind (r g b)
661 ;; pallette stretching
662 (elt cols (floor (expt (/ x max) (/ nb-iter 256))
663 (/ 1 (1- nb-cols)))))
664 (format s "~2,'0x~2,'0x~2,'0x" r g b)))))))
666 ;;; Example 9 is a Mandelbrot set from Yannick Gingras
667 ;;; Takes a long time...
669 (defun example9 (&optional (file #P"/tmp/ex9.pdf"))
670 "draw a nice region of the Mandelbrot Set"
671 (pdf:with-document ()
673 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
676 (helvetica (pdf:get-font "Helvetica"))
677 (image (make-instance 'pdf:image
678 :bits (gen-mandelbrot-bits w h)
679 :width w :height h)))
680 (pdf:add-images-to-page image)
682 (pdf:set-font helvetica 36.0)
683 (pdf:move-text 100 800)
684 (pdf:draw-text "cl-pdf: Example 9"))
685 (pdf:with-saved-state
687 (pdf:scale (/ w 2) (/ h 2))
688 (pdf:paint-image image)))))
689 (pdf:write-document file)))
692 ;; CL-TYPESETTING example
694 ;;; cl-typesetting copyright 2003-2004 Marc Battyani see license.txt for the details
695 ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
696 ;;; The homepage of cl-typesetting is here: http://www.fractalconcept.com/asp/html/cl-typesetting.html
702 (defparameter *boxes* nil) ;for debugging...
704 ; a very simple hello world
705 (defun hello (&optional (file #P"/tmp/hello.pdf"))
706 (with-open-file (outstream file
709 :element-type :default)
711 (pdf:with-document ()
713 (pdf:with-outline-level ("Example" (pdf:register-page-reference))
714 (pdf:set-line-width 0.1)
718 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
719 "cl-typesetting" :eol
722 (with-style (:font "Times-Italic" :font-size 26)
723 "The cool Common Lisp typesetting system")
725 (with-style (:font "Helvetica-Oblique" :font-size 100)
728 (with-style (:font "Helvetica" :font-size 12)
729 "hello" (dotted-hfill) "4.2" :eol
730 "hello world" (dotted-hfill) "4.2.4.2"))
731 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
732 "hello" (dotted-hfill) "4.2" :eol
733 "hello world" (dotted-hfill) "4.2.4.2")
735 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
736 "hello" (dotted-hfill :pattern-spacing 0.6) "4.2" :eol
737 "hello world" (dotted-hfill :pattern-spacing 0.6) "4.2.4.2")
739 (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
740 "hello" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2" :eol
741 "hello world" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2.4.2"))))
742 (draw-block content 20 800 545 700))))
743 (pdf:write-document outstream))))
747 ; a multipage simple hello world
748 (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
749 (with-open-file (outstream file
752 :element-type :default)
753 (pdf:with-document ()
757 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
758 "cl-typesetting" :eol
761 (with-style (:font "Times-Italic" :font-size 26)
762 "The cool Common Lisp typesetting system")
764 (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0 0.8))
766 (put-string "Hello World!")(new-line)))))))
767 (loop while (boxes content) do
769 (pdf:set-line-width 0.1)
770 (draw-block content 20 800 545 700))))
771 (pdf:write-document outstream))))
773 ;; (multi-page-hello)
775 ;; The Fancy Example!
778 "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.")
781 "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.")
783 ;; example of extension
785 (defclass rotated-char-box (soft-box h-mode-mixin)
786 ((boxed-char :accessor boxed-char :initarg :boxed-char)
787 (rotation :accessor rotation :initarg :rotation)))
789 (defun put-rotated-char-string (string)
790 (loop for char across string
791 do (add-box (make-instance 'rotated-char-box :dx *font-size*
792 :dy *font-size* :boxed-char char :baseline (* *font-size* 0.8)
793 :rotation (- (random 120) 60)))))
795 (defmethod stroke ((box rotated-char-box) x y)
796 (let ((dx (dx box))(dy (dy box))
797 (width (pdf:get-char-width (boxed-char box) *font* *font-size*)))
798 (pdf:with-saved-state
799 (pdf:translate (+ x (* dx 0.5)) (+ y (* dy 0.3)))
800 (pdf:set-line-width 0.5)
801 (pdf:set-gray-fill 0.8)
802 (pdf:circle 0 0 (* dx 0.45))
803 (pdf:fill-and-stroke)
804 (pdf:set-gray-fill 0)
805 (pdf:rotate (rotation box))
807 (pdf:move-text (* -0.5 width)(* -0.18 *font-size*))
808 (pdf:set-font *font* (* *font-size* 0.8))
809 (pdf:show-text (make-string 1 :initial-element (boxed-char box)))))))
811 ;; a draw function for the functional rule...
813 (defun draw-wavelet-rule (box x0 y0)
814 (let ((dx/2 (* (dx box) 0.5))
815 (dy/2 (* (dy box) 0.5)))
816 (pdf:with-saved-state
817 (pdf:translate (+ x0 dx/2) (- y0 dy/2))
818 (pdf:set-line-width 1)
819 (pdf:set-color-stroke (color box))
820 (pdf:move-to (- dx/2) 0)
821 (loop with a = (/ -0.2 (dx box)) and w = (/ 200.0 (dx box))
822 for x from (- dx/2) by 0.2
823 for y = (* dy/2 (cos (* x w)) (exp (* x x a)))
825 do (pdf:line-to x y))
830 (defun user-drawn-demo (box x y)
833 (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10
834 :font "Times-Italic" :font-size 6.5)
836 x (- y (dy box)) (- (dy box) 10) (dx box) :rotation 90 :border 0.1))
838 ;; a chart (I will have to change this in cl-pdf: it's a real mess!)
840 (defun draw-pie (box x y)
841 (pdf:draw-object (make-instance
842 'pdf:pie-chart :x (+ x 30) :y (- y 100) :width 90 :height 90
843 :serie '(12 23 65 33)
845 '(("Winter" (1.0 0.0 0.0))
846 ("Spring" (0.0 1.0 0.0))
847 ("Summer" (0.0 0.0 1.0))
848 ("Autumn" (0.0 1.0 1.0))))))
852 (defun link-all-a (box x y)
853 (pdf:with-saved-state
857 (when (and (char-box-p box) (char= (boxed-char box)#\a))
858 (push (list (+ x (* 0.5 (dx box)))
859 (+ y (* 0.2 (dy box)))
862 (pdf:set-line-width 1)
863 (pdf:set-rgb-stroke 1.0 0.8 0.4)
864 (loop for (x y box) in all-a
865 for sorted-a = (sort (copy-seq all-a)
866 #'(lambda (item1 item2)
867 (let ((dx1 (- (first item1) x))
868 (dy1 (- (second item1) y))
869 (dx2 (- (first item2) x))
870 (dy2 (- (second item2) y)))
871 (<= (sqrt (+ (* dx1 dx1)
876 for (x2 y2 box) in sorted-a
878 (pdf:set-gray-fill 0.8)
882 (pdf:set-gray-fill 0.8)
883 (pdf:set-rgb-stroke 0.5 0.7 1.0)
884 (loop for (x y box) in all-a
886 (pdf:circle x y (* (dx box) 0.7))
887 (pdf:fill-and-stroke)))))
889 ;;; rivers detection (still brute force not to be used in real life...)
890 (defun link-all-spaces (box x y)
891 (pdf:with-saved-state
892 (let ((all-spaces ()))
895 (when (white-char-box-p box)
896 (push (list (+ x (* 0.5 (dx box)))
897 (+ y (* 0.5 (dx box)))
900 (pdf:set-line-width 1)
901 (pdf:set-rgb-stroke 1.0 0.4 0)
902 (loop for (x y box) in all-spaces
903 for sorted-spaces = (sort (copy-seq all-spaces)
904 #'(lambda (item1 item2)
905 (let ((dx1 (- (first item1) x))
906 (dx2 (- (first item2) x)))
907 (<= (abs dx1)(abs dx2)))))
909 for (x2 y2 box) in sorted-spaces
910 for dy = (abs (- y2 y))
911 for dx = (abs (- x2 x))
913 (when (and (< dx (* 1.5 (+ (dx box)
915 (< 0 dy (* 5 (dx box))))
916 (pdf:set-gray-fill 0.8)
920 (pdf:circle x y (* (dx box) 0.7))
921 (pdf:circle x2 y2 (* (dx box) 0.7))
922 (pdf:fill-and-stroke)))))))
924 (defun link-all-a-and-spaces (box x y)
926 (link-all-spaces box x y))
928 (defun gen-box-graph ()
929 (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
930 :max-dx 500 :max-dy 300 :border-width nil))
931 (n1 (make-instance 'graph-node :data "box" :graph g1))
932 (n2 (make-instance 'graph-node :data "h-mode-mixin" :graph g1))
933 (n3 (make-instance 'graph-node :data "v-mode-mixin" :graph g1))
934 (n5 (make-instance 'graph-node :data "soft-box" :graph g1))
935 (n6 (make-instance 'graph-node :data "container-box" :graph g1))
936 (n7 (make-instance 'graph-node :data "vbox" :graph g1))
937 (n8 (make-instance 'graph-node :data "hbox" :graph g1))
938 (n9 (make-instance 'graph-node :data "glue" :graph g1))
939 (n10 (make-instance 'graph-node :data "hglue" :graph g1))
940 (n11 (make-instance 'graph-node :data "vglue" :graph g1))
941 (n12 (make-instance 'graph-node :data "spacing" :graph g1))
942 (n13 (make-instance 'graph-node :data "h-spacing" :graph g1))
943 (n14 (make-instance 'graph-node :data "v-spacing" :graph g1))
944 (n15 (make-instance 'graph-node :data "char-box" :graph g1))
945 (n16 (make-instance 'graph-node :data "white-char-box" :graph g1)))
946 (add-rank-constraint g1 "same" (list n1 n5 n15))
947 (make-instance 'graph-edge :head n1 :tail n5 :label "subclass" :graph g1)
948 (make-instance 'graph-edge :head n5 :tail n6 :graph g1)
949 (make-instance 'graph-edge :head n6 :tail n7 :graph g1)
950 (make-instance 'graph-edge :head n2 :tail n7 :graph g1)
951 (make-instance 'graph-edge :head n6 :tail n8 :graph g1)
952 (make-instance 'graph-edge :head n3 :tail n8 :graph g1)
953 (make-instance 'graph-edge :head n5 :tail n9 :graph g1)
954 (make-instance 'graph-edge :head n9 :tail n10 :graph g1)
955 (make-instance 'graph-edge :head n2 :tail n10 :graph g1)
956 (make-instance 'graph-edge :head n9 :tail n11 :graph g1)
957 (make-instance 'graph-edge :head n3 :tail n11 :graph g1)
958 (make-instance 'graph-edge :head n5 :tail n12 :graph g1)
959 (make-instance 'graph-edge :head n12 :tail n13 :graph g1)
960 (make-instance 'graph-edge :head n2 :tail n13 :graph g1)
961 (make-instance 'graph-edge :head n12 :tail n14 :graph g1)
962 (make-instance 'graph-edge :head n3 :tail n14 :graph g1)
963 (make-instance 'graph-edge :head n1 :tail n15 :graph g1)
964 (make-instance 'graph-edge :head n2 :tail n15 :graph g1)
965 (make-instance 'graph-edge :head n10 :tail n16 :graph g1)
966 (compute-graph-layout g1)
969 (defun make-color-node-box (graph name color description)
970 (make-instance 'graph-node :graph graph :dx 70 :data
973 (paragraph (:h-align :center :font "Helvetica-Oblique"
974 :font-size 14 :color '(0 0 0))
975 (put-string name) " "
976 (colored-box :dx 9.0 :dy 9.0 :color color :border-width 0.5)
978 (with-style (:font "Times-Italic" :font-size 10)
979 (put-string description))))
982 (defun gen-color-graph ()
983 (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
984 :max-dx 500 :max-dy 300 :border-width nil))
985 (red (make-color-node-box g1 "red" '(1.0 0 0) "(primary color)"))
986 (green (make-color-node-box g1 "green" '(0 1.0 0) "(primary color)"))
987 (blue (make-color-node-box g1 "blue" '(0 0 1.0) "(primary color)"))
988 (cyan (make-color-node-box g1 "cyan" '(0 1.0 1.0) "(green + blue)"))
989 (magenta (make-color-node-box g1 "magenta" '(1.0 0 1.0) "(red + blue)"))
990 (yellow (make-color-node-box g1 "yellow" '(1.0 1.0 0) "(red + green)")))
991 (make-instance 'graph-edge :head blue :tail cyan :graph g1)
992 (make-instance 'graph-edge :head green :tail cyan :graph g1)
993 (make-instance 'graph-edge :head red :tail magenta :graph g1)
994 (make-instance 'graph-edge :head blue :tail magenta :graph g1)
995 (make-instance 'graph-edge :head red :tail yellow :graph g1)
996 (make-instance 'graph-edge :head green :tail yellow :graph g1)
997 (compute-graph-layout g1)
1000 ;;; Example document
1001 ; Copy the files from the directory "files-for-example/" (included in
1002 ; the cl-typesetting distribution) to the /tmp directory (or somewhere else).
1004 ; Then you need to load the fonts with something like this:
1005 ; (pdf:load-t1-font "/tmp/cmex10.afm" "/tmp/cmex10.pfb")
1006 ; (pdf:load-t1-font "/tmp/cmti10.afm" "/tmp/cmti10.pfb")
1008 (defun full-example (&key (file #P"/tmp/ex.pdf")
1009 (banner-jpg #P"/tmp/banner.jpg")
1010 (fractal-jpg #P"/tmp/fractal.jpg")
1014 (pdf:with-outline-level ("Table of Content" (pdf:register-page-reference))
1018 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 60 :color '(0.0 0 0.8))
1019 "cl-typesetting" :eol
1022 (with-style (:font "Times-Italic" :font-size 28)
1023 "The Cool Common Lisp Typesetting System"))
1025 (hrule :dy 30 :stroke-fn 'draw-wavelet-rule :color '(0.0 0 0.6))
1027 (hrule :dy 0.5 :color '(0.0 0 0.6))
1028 (paragraph (:h-align :center :font "Helvetica" :font-size 16 :color '(0.0 0 0.6))
1030 (hrule :dy 0.1 :color '(0.0 0 0.6))
1031 (paragraph (:h-align :fill :font "Helvetica" :font-size 14 :color '(0.0 0 0.4)
1032 :left-margin 25 :right-margin 25)
1033 "Hello world" (dotted-hfill)
1034 (format-string "~d" (find-ref-point-page-number "hello")) :eol
1035 "Full demo" (dotted-hfill)
1036 (format-string "~d" (find-ref-point-page-number "demo")) :eol
1037 "cl-typegraph" (dotted-hfill)
1038 (format-string "~d" (find-ref-point-page-number "graph")) :eol)
1040 (hrule :dy 0.5 :color '(0.0 0 0.6)))))
1041 (draw-block content 20 800 545 700))))
1043 (pdf:with-outline-level ("Hello world" (pdf:register-page-reference))
1046 (mark-ref-point "hello")
1047 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1048 "cl-typesetting" :eol
1051 (with-style (:font "Times-Italic" :font-size 24)
1052 "The Cool Common Lisp Typesetting System")
1054 ; (with-style (:font "Helvetica-Oblique" :font-size 100)
1057 (with-style (:font "Times-Italic" :font-size 100)
1060 (draw-block content 20 800 545 700))))
1062 (pdf:with-outline-level ("Full demo" (pdf:register-page-reference))
1065 (mark-ref-point "demo")
1066 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1067 "cl-typesetting" :eol
1070 (with-style (:font "Times-Italic" :font-size 13)
1071 "The Cool Common Lisp Typesetting System"))
1072 (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
1073 :font "Times-Italic" :font-size 9)
1074 "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...")
1075 (paragraph (:h-align :center :font "Helvetica-BoldOblique" :font-size 20 :color '(1.0 0 0))
1077 (colored-box :dx 15.0 :dy 15.0 :color "#FFC0C0" :border-width 0.5) " "
1078 (colored-box :dx 15.0 :dy 15.0 :color "#C0FFC0" :border-width 0.5) " "
1079 (colored-box :dx 15.0 :dy 15.0 :color "#C0C0FF" :border-width 0.5))
1080 (paragraph (:h-align :center :font "Times-Italic" :font-size 12 :color '(0.0 0.6 0.3))
1081 "With user defined "
1082 (put-rotated-char-string "extensions") :eol
1083 (with-style (:font "Times-Italic" :font-size 11)
1084 "Support for images and functional rules" :eol
1085 (image :file banner-jpg :dx 100 :dy 20)))
1086 (hrule :dy 15 :stroke-fn 'draw-wavelet-rule)
1088 (table (:col-widths '(60 80 80) :border 0.5 :background-color '(1 1 0.8)
1089 :cell-padding 1 :padding 2)
1091 (cell (:background-color '(0.8 1 0.8) :col-span 3)
1092 (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
1093 "Title with a col-span of 3")))
1095 (cell (:background-color '(0.8 0.8 0.8))
1096 (paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1098 (cell (:background-color '(0.8 0.8 0.8))
1099 (paragraph (:h-align :center :font "Times-Roman" :font-size 9)
1100 "Centered cell content"))
1101 (cell (:background-color '(0.8 0.8 0.8))
1102 (paragraph (:h-align :right :font "Times-Bold" :font-size 9)
1103 "Right cell content")))
1105 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1106 "This cell content should take three lines."))
1107 (cell (:background-color '(1 1 1))
1108 (paragraph (:h-align :center :font "Times-Italic" :font-size 9)
1110 (image :file fractal-jpg :dx 15 :dy 15 :inline t
1113 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 11)
1114 (put-rotated-char-string "common lisp is cool"))))
1116 (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1117 "An example of table inside a cell"))
1118 (cell (:background-color '(1 1 1))
1119 (table (:col-widths '(14 14 21) :border 0.2
1120 :background-color '(0.4 0.4 0.8))
1121 (row () (cell () "12")(cell () "34")(cell () "567"))
1122 (row () (cell () "ab")(cell () "cd")(cell () "efg"))))
1123 (cell (:v-align :bottom)(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
1124 "You can nest as many tables as you want, like you do in HTML."))))
1125 (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10 :color '(0 0 0)
1126 :left-margin 5 :right-margin 5
1127 :font "Times-Roman" :font-size 10 :text-x-scale 0.7)
1128 (with-style (:color '(0 0.6 0.4))
1129 "This paragraph has been horizontally strechted by a 0.7 ratio. ")
1132 (user-drawn-box :dx 210 :dy 100 :stroke-fn 'user-drawn-demo) :eol
1133 (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 5)
1134 "An example of using cl-typesetting in an user-drawn box.")
1135 (paragraph (:h-align :left :top-margin 15
1136 :left-margin 5 :right-margin 5 :font "courier" :font-size 8)
1138 "(defmethod stroke ((box char-box) x y)
1140 (pdf:move-text x (+ y (offset box)))
1141 (pdf:set-font *font* *font-size*)
1142 (pdf:set-text-x-scale (* *text-x-scale* 100))
1143 (pdf:show-char (boxed-char box))))"))
1144 (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 3)
1145 "An example of verbatim code.")
1146 (paragraph (:h-align :justified :top-margin 9 :font "Helvetica-Oblique"
1147 :left-margin 5 :right-margin 5
1148 :font-size 9 :first-line-indent 20)
1150 (user-drawn-box :dx 240 :dy 100 :stroke-fn 'draw-pie) :eol
1151 (paragraph (:h-align :center :font "Times-Italic" :font-size 8)
1152 "An example of cl-pdf pie chart inserted.")
1153 (paragraph (:h-align :justified :top-margin 9 :font "helvetica" :font-size 9
1154 :left-margin 40 :right-margin 40)
1157 (paragraph (:h-align :center :top-margin 20 :font "Times-Bold" :font-size 20)
1159 (with-style (:font "Helvetica" :font-size 40 :left-margin 20 :right-margin 20)
1161 (paragraph (:h-align :center :top-margin 10 :font "CMTI10"
1162 :font-size 16 :color '(0 0 0))
1163 (with-style (:font "Times-Bold" :font-size 20)
1164 "Basic Math Mode Test" :eol)
1167 (with-style (:font (pdf:get-font "CMEX10" nil) :font-size 30)
1168 (with-offset (23) "H"))
1169 "E"(math-super-and-sub-script () ("n+1") ("k,m"))"="
1171 ("x"(with-superscript () "2")"+x-1")
1173 "-e"(with-superscript () "-x"(with-superscript () "2")))
1175 (with-style (:font "Times-Roman" :font-size 10)
1176 "This test now uses a TeX font (cmti10). Note the italic" :eol "correction for the super/subscript of the E."))
1177 (paragraph (:h-align :center :top-margin 20 :font "Times-Italic" :font-size 18 :color '(0.8 0 0))
1178 "This test pdf file has been typeset " :eol "with cl-typesetting 0.80" :eol
1180 (with-style (:font "Times-Italic" :font-size 14)
1183 (hrule :dy 20 :stroke-fn 'draw-wavelet-rule :color '(0.8 0 0))
1185 (paragraph (:h-align :center :font "Helvetica-Oblique" :font-size 8)
1186 "This project needs contributors. So if you are interested contact "
1187 (with-style (:font "Times-Italic" :font-size 9)
1188 "marc.battyani@fractalconcept.com") "."
1190 (pdf::draw-bar-code128 "CODE128BARCODE" 10 35 :height 25 :width 150 :start-stop-factor 0.25
1191 :font-size 7 :show-string t)
1192 (draw-block content 40 800 250 380 :rotation 5 :border 0.1)
1193 (draw-block content 50 425 250 380 :rotation -5 :border 0.1)
1194 (draw-block content 330 800 250 380 :rotation -2 :border 0.1 :special-fn 'link-all-a-and-spaces)
1195 (draw-block content 310 400 250 380 :v-align :justified :border 0.1))))
1197 (pdf:with-outline-level ("cl-typegraph" (pdf:register-page-reference))
1200 (mark-ref-point "graph")
1201 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
1205 (with-style (:font "Times-Italic" :font-size 13)
1206 "The Cool Common Lisp Graph Typesetting System"))
1208 (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
1209 :font "helvetica" :font-size 12)
1210 "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
1212 "In the first graph example below, the nodes contain only strings.")
1214 :hfill (if display-graphs
1215 (graph-box (gen-box-graph))
1216 (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
1217 (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
1218 "The class hierarchy for the boxes in cl-typesetting.")
1220 (paragraph (:h-align :justified :font "helvetica" :font-size 12)
1221 "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.")
1223 :hfill (if display-graphs
1224 (graph-box (gen-color-graph))
1225 (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
1226 (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
1227 "The primary colors"))))
1228 (draw-block content 30 810 530 780))))
1229 (pdf:write-document file)))
1231 ;;; A higher level example
1233 (defun multi-page-example (&optional (file #P"/tmp/multi-page.pdf")
1234 &aux content table (margins '(72 72 72 50)))
1236 ;(pdf:set-line-width 0.1)
1237 (let* ((print-stamp (multiple-value-bind (second minute hour date month year)
1239 (format nil "Printed on ~4D-~2,'0D-~2,'0D ~2,'0D:~2,'0D"
1240 year month date hour minute)))
1241 (header (compile-text ()
1242 (paragraph (:h-align :center
1243 :font "Helvetica-BoldOblique" :font-size 12)
1244 "Multi-page example document")
1247 (footer (lambda (pdf:*page*)
1248 (compile-text (:font "Helvetica" :font-size 10)
1250 (hbox (:align :center :adjustable-p t)
1251 (verbatim print-stamp)
1255 (format nil "Page ~d" pdf:*page-number*)))
1259 (paragraph (:font "Helvetica-Bold" :font-size 16 :top-margin 20)
1260 "1. First paragraph group")
1263 (paragraph (:font "Helvetica" :font-size (+ 6 (random 10)))
1264 (verbatim (format nil "1.~d. " (1+ i)))
1265 (dotimes (j (1+ (random 5)))
1266 (put-string "The quick brown fox jumps over the lazy dog. "))))
1268 (draw-pages content :margins margins :header header :footer footer)
1273 (paragraph (:font "Times-Bold" :font-size 16 :top-margin 20)
1274 "2. Second paragraph group"
1277 (paragraph (:font "Times-Roman" :font-size (+ 6 (random 10)))
1278 (verbatim (format nil "2.~d. " (1+ i)))
1279 (dotimes (j (1+ (random 5)))
1280 (put-string "The quick brown fox jumps over the lazy dog. "))))
1282 (table (:col-widths '(20 100 100 100) :splittable-p nil
1283 :border 0 :background-color '(1 1 0.8))
1286 (paragraph (:h-align :center
1287 :font "Times-Italic" :font-size 12)
1288 "Non-splittable table - row with a col-span of 4")))
1289 (loop for (name tel age) = (list "Dmitri Dmitriev" "555-1234" (+ 25 (random 25)))
1290 and row-number from 1 upto 4
1292 (row (:height (when (evenp row-number) 20)
1293 :background-color (if (zerop (mod row-number 3))
1296 (format nil "~d" row-number)))
1297 (cell (:background-color (when (>= age 40) :blue))
1300 (cell () (paragraph (:h-align :right) age)))))
1302 (draw-pages content :margins margins :header header :footer footer)
1306 (table (:col-widths '(20 100 100 100) :splittable-p t
1307 :border 1/2 :background-color '(1 1 0.8))
1308 (header-row (:background-color :gray)
1309 (cell (:row-span 2) (verbatim "Row #"))
1310 (cell (:row-span 2) "Name")
1311 (cell () "Telephone")
1312 ;(table (:col-widths '(100) :splittable-p nil
1313 ; :padding 0 :border 1/2); :background-color '(1 1 0.8))
1314 ; (row () (cell () "Telephone"))
1315 ; (row () (cell () "Fax"))))
1316 (cell (:row-span 2) (paragraph (:h-align :right) "Age")))
1317 (header-row (:background-color :gray)
1319 (footer-row (:background-color :gray)
1321 (paragraph (:h-align :center
1322 :font "Times-Italic" :font-size 12)
1323 "Table footer with a col-span of 4")))
1324 (loop for (name tel age) = (list "Ivan Ivanov" "555-1234" (+ 25 (random 25)))
1325 and row-number from 1 upto 40
1327 (row (:height (when (evenp row-number) 20)
1328 :background-color (if (zerop (mod row-number 3))
1331 (format nil "~d" row-number)))
1332 (cell (:background-color (when (>= age 40) :blue))
1335 (cell () (paragraph (:h-align :right) age)))))))
1336 (setq table content)
1337 (draw-pages content :margins margins :header header :footer footer) ;:break :after
1339 (setq content ;; Various spans
1341 (table (:col-widths '(20 40 60 80 120)
1342 :background-color :yellow :border 1
1346 (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
1347 "Table with cells spanning more then one row")))
1348 (row (:background-color :green)
1349 (cell (:row-span 2 :background-color :blue)
1350 "1,1 2,1 row-span 2")
1352 (cell (:col-span 2 :row-span 3 :background-color :red)
1353 "1,3 1,4 - 3,3 3,4 col-span 2 row-span 3")
1357 (cell (:row-span 2 :background-color :blue) "2,5 3,5 row-span 2"))
1358 (row (:background-color :green)
1359 (cell (:col-span 2) "3,1 3,2 col-span 2"))
1366 (draw-pages content :margins margins :header header :footer footer) ;:break :after
1368 (draw-block (compile-text () "Test block - line1" :eol "Line 2")
1369 300 300 150 150 :border 1))
1370 (when pdf:*page* (finalize-page pdf:*page*))
1372 ;(draw-page content :margins 72))
1373 (pdf:write-document file))
1377 (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
1378 (pdf:with-document ()
1382 (table (:col-widths '(100 200) :splittable-p t) ;;; start Erik changes
1385 (paragraph () "Header")))
1388 (paragraph () "Footer")))
1392 (paragraph () (put-string (format nil "test ~d" time))))))) ;;; end Erik changes
1395 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50
1396 :color '(0.0 0 0.8))
1397 "cl-typesetting" :eol
1400 (with-style (:font "Times-Italic" :font-size 26)
1401 "The cool Common Lisp typesetting system")
1403 (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0
1406 (put-string "Hello World!")(new-line)))))))
1407 (loop while (boxes content) do
1409 (pdf:set-line-width 0.1)
1410 (draw-block content 20 800 545 700))))
1411 (pdf:write-document file)))
1415 (defparameter *unicode-test-string*
1416 (map unicode-string-type 'code-char
1417 '(8252 8319 8359 8592 8593 8594 8595 8596 8597 8616 915 920 934 945 948 949 963 964 966 32
1418 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 32 65
1419 9568 9650 9658 9660 9668 9675 9688 9689 8364 1027 8218 402 8222 8230 8224 8225 32 66
1420 372 373 374 375 383 506 507 508 509 510 511 903 913 914 916 917 918 919 921 922 923 32
1421 946 947 950 951 952 953 954 955 956 957 958 1101 1102 1103 1105 1106 1107 1108 32
1422 1475 1488 1489 1490 1491 1492 1493 64304 64305 64306 64307 64308 64309 32
1423 7911 7912 7913 7914 7915 7916 7917 7918 1179 1180 1181 1186 1187 1198 1199 1200 32)))
1425 ;; Look at the unicode-readme.txt in cl-pdf to see how to load unicode fonts
1426 (defun unicode-hello (&optional (file #P"/tmp/hello-u.pdf"))
1427 (pdf:with-document ()
1429 (pdf:with-outline-level ("Unicode Example" (pdf:register-page-reference))
1430 (pdf:set-line-width 0.1)
1434 (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
1435 "cl-typesetting" :eol
1438 (with-style (:font "Times-Italic" :font-size 26)
1439 "The cool Common Lisp typesetting system")
1441 (with-style (:font "TimesNewRomanPSMT" :font-size 36)
1442 (put-string *unicode-test-string*))))))
1443 (draw-block content 20 800 545 700))))
1444 (pdf:write-document file)))