PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/clbuild/source/cl-typesetting/test.lisp

https://bitbucket.org/nunb/weblocks-boxset-nunb
Lisp | 724 lines | 650 code | 37 blank | 37 comment | 24 complexity | e4afdcb16f69d020bf834983662a2855 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause
  1. ;;; cl-typesetting copyright 2003-2004 Marc Battyani see license.txt for the details
  2. ;;; You can reach me at marc.battyani@fractalconcept.com or marc@battyani.net
  3. ;;; The homepage of cl-typesetting is here: http://www.fractalconcept.com/asp/html/cl-typesetting.html
  4. (in-package typeset)
  5. ;;; Low level tests
  6. (defparameter *boxes* nil) ;for debugging...
  7. ; a very simple hello world
  8. (defun hello (&optional (file #P"/tmp/hello.pdf"))
  9. (pdf:with-document ()
  10. (pdf:with-page ()
  11. (pdf:with-outline-level ("Example" (pdf:register-page-reference))
  12. (pdf:set-line-width 0.1)
  13. (let ((content
  14. (compile-text ()
  15. (vspace 100)
  16. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
  17. "cl-typesetting" :eol
  18. (vspace 2)
  19. (hrule :dy 1)
  20. (with-style (:font "Times-Italic" :font-size 26)
  21. "The cool Common Lisp typesetting system")
  22. (vspace 50)
  23. (with-style (:font "Helvetica-Oblique" :font-size 100)
  24. "Hello World!")
  25. (vspace 50)
  26. (with-style (:font "Helvetica" :font-size 12)
  27. "hello" (dotted-hfill) "4.2" :eol
  28. "hello world" (dotted-hfill) "4.2.4.2"))
  29. (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
  30. "hello" (dotted-hfill) "4.2" :eol
  31. "hello world" (dotted-hfill) "4.2.4.2")
  32. (vspace 50)
  33. (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
  34. "hello" (dotted-hfill :pattern-spacing 0.6) "4.2" :eol
  35. "hello world" (dotted-hfill :pattern-spacing 0.6) "4.2.4.2")
  36. (vspace 50)
  37. (paragraph (:h-align :justified :font "Helvetica" :font-size 12 :color '(0.0 0 0.0))
  38. "hello" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2" :eol
  39. "hello world" (dotted-hfill :char-pattern ".+" :pattern-spacing 0) "4.2.4.2"))))
  40. (draw-block content 20 800 545 700))))
  41. (pdf:write-document file)))
  42. ; a multipage simple hello world
  43. (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
  44. (pdf:with-document ()
  45. (let ((content
  46. (compile-text ()
  47. (vspace 100)
  48. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
  49. "cl-typesetting" :eol
  50. (vspace 2)
  51. (hrule :dy 1)
  52. (with-style (:font "Times-Italic" :font-size 26)
  53. "The cool Common Lisp typesetting system")
  54. (vspace 50)
  55. (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0 0.8))
  56. (dotimes (i 100)
  57. (put-string "Hello World!")(new-line)))))))
  58. (loop while (boxes content) do
  59. (pdf:with-page ()
  60. (pdf:set-line-width 0.1)
  61. (draw-block content 20 800 545 700))))
  62. (pdf:write-document file)))
  63. ;; The Fancy Example!
  64. (defparameter *par1*
  65. "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.")
  66. (defparameter *par2*
  67. "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.")
  68. ;; example of extension
  69. (defclass rotated-char-box (soft-box h-mode-mixin)
  70. ((boxed-char :accessor boxed-char :initarg :boxed-char)
  71. (rotation :accessor rotation :initarg :rotation)))
  72. (defun put-rotated-char-string (string)
  73. (loop for char across string
  74. do (add-box (make-instance 'rotated-char-box :dx *font-size*
  75. :dy *font-size* :boxed-char char :baseline (* *font-size* 0.8)
  76. :rotation (- (random 120) 60)))))
  77. (defmethod stroke ((box rotated-char-box) x y)
  78. (let ((dx (dx box))(dy (dy box))
  79. (width (pdf:get-char-width (boxed-char box) *font* *font-size*)))
  80. (pdf:with-saved-state
  81. (pdf:translate (+ x (* dx 0.5)) (+ y (* dy 0.3)))
  82. (pdf:set-line-width 0.5)
  83. (pdf:set-gray-fill 0.8)
  84. (pdf:circle 0 0 (* dx 0.45))
  85. (pdf:fill-and-stroke)
  86. (pdf:set-gray-fill 0)
  87. (pdf:rotate (rotation box))
  88. (pdf:in-text-mode
  89. (pdf:move-text (* -0.5 width)(* -0.18 *font-size*))
  90. (pdf:set-font *font* (* *font-size* 0.8))
  91. (pdf:show-text (make-string 1 :initial-element (boxed-char box)))))))
  92. ;; a draw function for the functional rule...
  93. (defun draw-wavelet-rule (box x0 y0)
  94. (let ((dx/2 (* (dx box) 0.5))
  95. (dy/2 (* (dy box) 0.5)))
  96. (pdf:with-saved-state
  97. (pdf:translate (+ x0 dx/2) (- y0 dy/2))
  98. (pdf:set-line-width 1)
  99. (pdf:set-color-stroke (color box))
  100. (pdf:move-to (- dx/2) 0)
  101. (loop with a = (/ -0.2 (dx box)) and w = (/ 200.0 (dx box))
  102. for x from (- dx/2) by 0.2
  103. for y = (* dy/2 (cos (* x w)) (exp (* x x a)))
  104. while (< x dx/2)
  105. do (pdf:line-to x y))
  106. (pdf:stroke))))
  107. ;; user-drawn box
  108. (defun user-drawn-demo (box x y)
  109. (draw-block (compile-text ()
  110. (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10
  111. :font "Times-Italic" :font-size 6.5)
  112. *par1*))
  113. x (- y (dy box)) (- (dy box) 10) (dx box) :rotation 90 :border 0.1))
  114. ;; a chart (I will have to change this in cl-pdf: it's a real mess!)
  115. (defun draw-pie (box x y)
  116. (pdf:draw-object (make-instance
  117. 'pdf:pie-chart :x (+ x 30) :y (- y 100) :width 90 :height 90
  118. :serie '(12 23 65 33)
  119. :labels&colors
  120. '(("Winter" (1.0 0.0 0.0))
  121. ("Spring" (0.0 1.0 0.0))
  122. ("Summer" (0.0 0.0 1.0))
  123. ("Autumn" (0.0 1.0 1.0))))))
  124. ;; a stupid trick
  125. ;; brute force!!!
  126. (defun link-all-a (box x y)
  127. (pdf:with-saved-state
  128. (let ((all-a ()))
  129. (map-boxes box 0 0 #'(lambda (box x y)
  130. (when (and (char-box-p box) (char= (boxed-char box)#\a))
  131. (push (list (+ x (* 0.5 (dx box)))(+ y (* 0.2 (dy box))) box) all-a))))
  132. (pdf:set-line-width 1)
  133. (pdf:set-rgb-stroke 1.0 0.8 0.4)
  134. (loop for (x y box) in all-a
  135. for sorted-a = (sort (copy-seq all-a)
  136. #'(lambda (item1 item2)
  137. (let ((dx1 (- (first item1) x))
  138. (dy1 (- (second item1) y))
  139. (dx2 (- (first item2) x))
  140. (dy2 (- (second item2) y)))
  141. (<= (sqrt (+ (* dx1 dx1)(* dy1 dy1)))(sqrt (+ (* dx2 dx2)(* dy2 dy2)))))))
  142. do (loop repeat 4
  143. for (x2 y2 box) in sorted-a
  144. do
  145. (pdf:set-gray-fill 0.8)
  146. (pdf::move-to x y)
  147. (pdf::line-to x2 y2)
  148. (pdf::stroke)))
  149. (pdf:set-gray-fill 0.8)
  150. (pdf:set-rgb-stroke 0.5 0.7 1.0)
  151. (loop for (x y box) in all-a
  152. do
  153. (pdf:circle x y (* (dx box) 0.7))
  154. (pdf:fill-and-stroke)))))
  155. ;;; rivers detection (still brute force not to be used in real life...)
  156. (defun link-all-spaces (box x y)
  157. (pdf:with-saved-state
  158. (let ((all-spaces ()))
  159. (map-boxes box 0 0 #'(lambda (box x y)
  160. (when (white-char-box-p box)
  161. (push (list (+ x (* 0.5 (dx box)))(+ y (* 0.5 (dx box))) box) all-spaces))))
  162. (pdf:set-line-width 1)
  163. (pdf:set-rgb-stroke 1.0 0.4 0)
  164. (loop for (x y box) in all-spaces
  165. for sorted-spaces = (sort (copy-seq all-spaces)
  166. #'(lambda (item1 item2)
  167. (let ((dx1 (- (first item1) x))
  168. (dx2 (- (first item2) x)))
  169. (<= (abs dx1)(abs dx2)))))
  170. do (loop repeat 5
  171. for (x2 y2 box) in sorted-spaces
  172. for dy = (abs (- y2 y))
  173. for dx = (abs (- x2 x))
  174. do
  175. (when (and (< dx (* 1.5 (+ (dx box)(delta-size box)))) (< 0 dy (* 5 (dx box))))
  176. (pdf:set-gray-fill 0.8)
  177. (pdf::move-to x y)
  178. (pdf::line-to x2 y2)
  179. (pdf::stroke)
  180. (pdf:circle x y (* (dx box) 0.7))
  181. (pdf:circle x2 y2 (* (dx box) 0.7))
  182. (pdf:fill-and-stroke)))))))
  183. (defun link-all-a-and-spaces (box x y)
  184. (link-all-a box x y)
  185. (link-all-spaces box x y))
  186. (defun gen-box-graph ()
  187. (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
  188. :max-dx 500 :max-dy 300 :border-width nil))
  189. (n1 (make-instance 'graph-node :data "box" :graph g1))
  190. (n2 (make-instance 'graph-node :data "h-mode-mixin" :graph g1))
  191. (n3 (make-instance 'graph-node :data "v-mode-mixin" :graph g1))
  192. (n5 (make-instance 'graph-node :data "soft-box" :graph g1))
  193. (n6 (make-instance 'graph-node :data "container-box" :graph g1))
  194. (n7 (make-instance 'graph-node :data "vbox" :graph g1))
  195. (n8 (make-instance 'graph-node :data "hbox" :graph g1))
  196. (n9 (make-instance 'graph-node :data "glue" :graph g1))
  197. (n10 (make-instance 'graph-node :data "hglue" :graph g1))
  198. (n11 (make-instance 'graph-node :data "vglue" :graph g1))
  199. (n12 (make-instance 'graph-node :data "spacing" :graph g1))
  200. (n13 (make-instance 'graph-node :data "h-spacing" :graph g1))
  201. (n14 (make-instance 'graph-node :data "v-spacing" :graph g1))
  202. (n15 (make-instance 'graph-node :data "char-box" :graph g1))
  203. (n16 (make-instance 'graph-node :data "white-char-box" :graph g1)))
  204. (add-rank-constraint g1 "same" (list n1 n5 n15))
  205. (make-instance 'graph-edge :head n1 :tail n5 :label "subclass" :graph g1)
  206. (make-instance 'graph-edge :head n5 :tail n6 :graph g1)
  207. (make-instance 'graph-edge :head n6 :tail n7 :graph g1)
  208. (make-instance 'graph-edge :head n2 :tail n7 :graph g1)
  209. (make-instance 'graph-edge :head n6 :tail n8 :graph g1)
  210. (make-instance 'graph-edge :head n3 :tail n8 :graph g1)
  211. (make-instance 'graph-edge :head n5 :tail n9 :graph g1)
  212. (make-instance 'graph-edge :head n9 :tail n10 :graph g1)
  213. (make-instance 'graph-edge :head n2 :tail n10 :graph g1)
  214. (make-instance 'graph-edge :head n9 :tail n11 :graph g1)
  215. (make-instance 'graph-edge :head n3 :tail n11 :graph g1)
  216. (make-instance 'graph-edge :head n5 :tail n12 :graph g1)
  217. (make-instance 'graph-edge :head n12 :tail n13 :graph g1)
  218. (make-instance 'graph-edge :head n2 :tail n13 :graph g1)
  219. (make-instance 'graph-edge :head n12 :tail n14 :graph g1)
  220. (make-instance 'graph-edge :head n3 :tail n14 :graph g1)
  221. (make-instance 'graph-edge :head n1 :tail n15 :graph g1)
  222. (make-instance 'graph-edge :head n2 :tail n15 :graph g1)
  223. (make-instance 'graph-edge :head n10 :tail n16 :graph g1)
  224. (compute-graph-layout g1)
  225. g1))
  226. (defun make-color-node-box (graph name color description)
  227. (make-instance 'graph-node :graph graph :dx 70 :data
  228. (make-filled-vbox
  229. (compile-text ()
  230. (paragraph (:h-align :center :font "Helvetica-Oblique"
  231. :font-size 14 :color '(0 0 0))
  232. (put-string name) " "
  233. (colored-box :dx 9.0 :dy 9.0 :color color :border-width 0.5)
  234. :eol
  235. (with-style (:font "Times-Italic" :font-size 10)
  236. (put-string description))))
  237. 70 +huge-number+)))
  238. (defun gen-color-graph ()
  239. (let* ((g1 (make-instance 'graph :dot-attributes '(#+nil("rankdir" "LR")("nodesep" "0.3")("ranksep" "0.8"))
  240. :max-dx 500 :max-dy 300 :border-width nil))
  241. (red (make-color-node-box g1 "red" '(1.0 0 0) "(primary color)"))
  242. (green (make-color-node-box g1 "green" '(0 1.0 0) "(primary color)"))
  243. (blue (make-color-node-box g1 "blue" '(0 0 1.0) "(primary color)"))
  244. (cyan (make-color-node-box g1 "cyan" '(0 1.0 1.0) "(green + blue)"))
  245. (magenta (make-color-node-box g1 "magenta" '(1.0 0 1.0) "(red + blue)"))
  246. (yellow (make-color-node-box g1 "yellow" '(1.0 1.0 0) "(red + green)")))
  247. (make-instance 'graph-edge :head blue :tail cyan :graph g1)
  248. (make-instance 'graph-edge :head green :tail cyan :graph g1)
  249. (make-instance 'graph-edge :head red :tail magenta :graph g1)
  250. (make-instance 'graph-edge :head blue :tail magenta :graph g1)
  251. (make-instance 'graph-edge :head red :tail yellow :graph g1)
  252. (make-instance 'graph-edge :head green :tail yellow :graph g1)
  253. (compute-graph-layout g1)
  254. g1))
  255. ;;; Example document
  256. ; Copy the files from the directory "files-for-example/" (included in
  257. ; the cl-typesetting distribution) to the /tmp directory (or somewhere else).
  258. ;
  259. ; Then you need to load the fonts with something like this:
  260. ; (pdf:load-t1-font "/tmp/cmex10.afm" "/tmp/cmex10.pfb")
  261. ; (pdf:load-t1-font "/tmp/cmti10.afm" "/tmp/cmti10.pfb")
  262. (defun full-example (&key (file #P"/tmp/ex.pdf")
  263. (banner-jpg #P"/tmp/banner.jpg")
  264. (fractal-jpg #P"/tmp/fractal.jpg")
  265. display-graphs)
  266. (with-document ()
  267. (pdf:with-page ()
  268. (pdf:with-outline-level ("Table of Content" (pdf:register-page-reference))
  269. (let ((content
  270. (compile-text ()
  271. (vspace 100)
  272. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 60 :color '(0.0 0 0.8))
  273. "cl-typesetting" :eol
  274. (vspace 2)
  275. (hrule :dy 1)
  276. (with-style (:font "Times-Italic" :font-size 28)
  277. "The Cool Common Lisp Typesetting System"))
  278. (vspace 100)
  279. (hrule :dy 30 :stroke-fn 'draw-wavelet-rule :color '(0.0 0 0.6))
  280. (vspace 250)
  281. (hrule :dy 0.5 :color '(0.0 0 0.6))
  282. (paragraph (:h-align :center :font "Helvetica" :font-size 16 :color '(0.0 0 0.6))
  283. "Table of content")
  284. (hrule :dy 0.1 :color '(0.0 0 0.6))
  285. (paragraph (:h-align :fill :font "Helvetica" :font-size 14 :color '(0.0 0 0.4)
  286. :left-margin 25 :right-margin 25)
  287. "Hello world" (dotted-hfill)
  288. (format-string "~d" (find-ref-point-page-number "hello")) :eol
  289. "Full demo" (dotted-hfill)
  290. (format-string "~d" (find-ref-point-page-number "demo")) :eol
  291. "cl-typegraph" (dotted-hfill)
  292. (format-string "~d" (find-ref-point-page-number "graph")) :eol)
  293. (vspace 2)
  294. (hrule :dy 0.5 :color '(0.0 0 0.6)))))
  295. (draw-block content 20 800 545 700))))
  296. (pdf:with-page ()
  297. (pdf:with-outline-level ("Hello world" (pdf:register-page-reference))
  298. (let ((content
  299. (compile-text ()
  300. (mark-ref-point "hello")
  301. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
  302. "cl-typesetting" :eol
  303. (vspace 2)
  304. (hrule :dy 1)
  305. (with-style (:font "Times-Italic" :font-size 24)
  306. "The Cool Common Lisp Typesetting System")
  307. ; (vspace 50)
  308. ; (with-style (:font "Helvetica-Oblique" :font-size 100)
  309. ; "Hello World!")
  310. :vfill
  311. (with-style (:font "Times-Italic" :font-size 100)
  312. "Hello World!")
  313. :vfill))))
  314. (draw-block content 20 800 545 700))))
  315. (pdf:with-page ()
  316. (pdf:with-outline-level ("Full demo" (pdf:register-page-reference))
  317. (let ((content
  318. (compile-text ()
  319. (mark-ref-point "demo")
  320. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
  321. "cl-typesetting" :eol
  322. (vspace 2)
  323. (hrule :dy 1)
  324. (with-style (:font "Times-Italic" :font-size 13)
  325. "The Cool Common Lisp Typesetting System"))
  326. (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
  327. :font "Times-Italic" :font-size 9)
  328. "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...")
  329. (paragraph (:h-align :center :font "Helvetica-BoldOblique" :font-size 20 :color '(1.0 0 0))
  330. "Now in Color! "
  331. (colored-box :dx 15.0 :dy 15.0 :color "#FFC0C0" :border-width 0.5) " "
  332. (colored-box :dx 15.0 :dy 15.0 :color "#C0FFC0" :border-width 0.5) " "
  333. (colored-box :dx 15.0 :dy 15.0 :color "#C0C0FF" :border-width 0.5))
  334. (paragraph (:h-align :center :font "Times-Italic" :font-size 12 :color '(0.0 0.6 0.3))
  335. "With user defined "
  336. (put-rotated-char-string "extensions") :eol
  337. (with-style (:font "Times-Italic" :font-size 11)
  338. "Support for images and functional rules" :eol
  339. (image :file banner-jpg :dx 100 :dy 20)))
  340. (hrule :dy 15 :stroke-fn 'draw-wavelet-rule)
  341. (vspace 3)
  342. (table (:col-widths '(60 80 80) :border 0.5 :background-color '(1 1 0.8)
  343. :cell-padding 1 :padding 2)
  344. (row ()
  345. (cell (:background-color '(0.8 1 0.8) :col-span 3)
  346. (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
  347. "Title with a col-span of 3")))
  348. (row ()
  349. (cell (:background-color '(0.8 0.8 0.8))
  350. (paragraph (:h-align :left :font "Times-Italic" :font-size 9)
  351. "Left aligned"))
  352. (cell (:background-color '(0.8 0.8 0.8))
  353. (paragraph (:h-align :center :font "Times-Roman" :font-size 9)
  354. "Centered cell content"))
  355. (cell (:background-color '(0.8 0.8 0.8))
  356. (paragraph (:h-align :right :font "Times-Bold" :font-size 9)
  357. "Right cell content")))
  358. (row ()
  359. (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
  360. "This cell content should take three lines."))
  361. (cell (:background-color '(1 1 1))
  362. (paragraph (:h-align :center :font "Times-Italic" :font-size 9)
  363. "A jpeg "
  364. (image :file fractal-jpg :dx 15 :dy 15 :inline t
  365. :offset 9)
  366. " in the text"))
  367. (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 11)
  368. (put-rotated-char-string "common lisp is cool"))))
  369. (row ()
  370. (cell ()(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
  371. "An example of table inside a cell"))
  372. (cell (:background-color '(1 1 1))
  373. (table (:col-widths '(14 14 21) :border 0.2
  374. :background-color '(0.4 0.4 0.8))
  375. (row () (cell () "12")(cell () "34")(cell () "567"))
  376. (row () (cell () "ab")(cell () "cd")(cell () "efg"))))
  377. (cell (:v-align :bottom)(paragraph (:h-align :left :font "Times-Italic" :font-size 9)
  378. "You can nest as many tables as you want, like you do in HTML."))))
  379. (paragraph (:h-align :justified :top-margin 5 :first-line-indent 10 :color '(0 0 0)
  380. :left-margin 5 :right-margin 5
  381. :font "Times-Roman" :font-size 10 :text-x-scale 0.7)
  382. (with-style (:color '(0 0.6 0.4))
  383. "This paragraph has been horizontally strechted by a 0.7 ratio. ")
  384. *par1*)
  385. (vspace 10)
  386. (user-drawn-box :dx 210 :dy 100 :stroke-fn 'user-drawn-demo) :eol
  387. (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 5)
  388. "An example of using cl-typesetting in an user-drawn box.")
  389. (paragraph (:h-align :left :top-margin 15
  390. :left-margin 5 :right-margin 5 :font "courier" :font-size 8)
  391. (verbatim
  392. "(defmethod stroke ((box char-box) x y)
  393. (pdf:in-text-mode
  394. (pdf:move-text x (+ y (offset box)))
  395. (pdf:set-font *font* *font-size*)
  396. (pdf:set-text-x-scale (* *text-x-scale* 100))
  397. (pdf:show-char (boxed-char box))))"))
  398. (paragraph (:h-align :center :font "Times-Italic" :font-size 8 :top-margin 3)
  399. "An example of verbatim code.")
  400. (paragraph (:h-align :justified :top-margin 9 :font "Helvetica-Oblique"
  401. :left-margin 5 :right-margin 5
  402. :font-size 9 :first-line-indent 20)
  403. *par1*)
  404. (user-drawn-box :dx 240 :dy 100 :stroke-fn 'draw-pie) :eol
  405. (paragraph (:h-align :center :font "Times-Italic" :font-size 8)
  406. "An example of cl-pdf pie chart inserted.")
  407. (paragraph (:h-align :justified :top-margin 9 :font "helvetica" :font-size 9
  408. :left-margin 40 :right-margin 40)
  409. *par2*)
  410. (vspace 10)
  411. (paragraph (:h-align :center :top-margin 20 :font "Times-Bold" :font-size 20)
  412. "Kerning test" :eol
  413. (with-style (:font "Helvetica" :font-size 40 :left-margin 20 :right-margin 20)
  414. "Yes, AWAY"))
  415. (paragraph (:h-align :center :top-margin 10 :font "CMTI10"
  416. :font-size 16 :color '(0 0 0))
  417. (with-style (:font "Times-Bold" :font-size 20)
  418. "Basic Math Mode Test" :eol)
  419. (vspace 5)
  420. (display-formula ()
  421. (with-style (:font (pdf:get-font "CMEX10" nil) :font-size 30)
  422. (with-offset (23) "H"))
  423. "E"(math-super-and-sub-script () ("n+1") ("k,m"))"="
  424. (fraction ()
  425. ("x"(with-superscript () "2")"+x-1")
  426. ("F(x)+b-3"))
  427. "-e"(with-superscript () "-x"(with-superscript () "2")))
  428. (vspace 5)
  429. (with-style (:font "Times-Roman" :font-size 10)
  430. "This test now uses a TeX font (cmti10). Note the italic" :eol "correction for the super/subscript of the E."))
  431. (paragraph (:h-align :center :top-margin 20 :font "Times-Italic" :font-size 18 :color '(0.8 0 0))
  432. "This test pdf file has been typeset " :eol "with cl-typesetting 0.80" :eol
  433. (vspace 10)
  434. (with-style (:font "Times-Italic" :font-size 14)
  435. "Marc Battyani"))
  436. :vfill
  437. (hrule :dy 20 :stroke-fn 'draw-wavelet-rule :color '(0.8 0 0))
  438. :vfill
  439. (paragraph (:h-align :center :font "Helvetica-Oblique" :font-size 8)
  440. "This project needs contributors. So if you are interested contact "
  441. (with-style (:font "Times-Italic" :font-size 9)
  442. "marc.battyani@fractalconcept.com") "."
  443. ))))
  444. (pdf::draw-bar-code128 "CODE128BARCODE" 10 35 :height 25 :width 150 :start-stop-factor 0.25
  445. :font-size 7 :show-string t)
  446. (draw-block content 40 800 250 380 :rotation 5 :border 0.1)
  447. (draw-block content 50 425 250 380 :rotation -5 :border 0.1)
  448. (draw-block content 330 800 250 380 :rotation -2 :border 0.1 :special-fn 'link-all-a-and-spaces)
  449. (draw-block content 310 400 250 380 :v-align :justified :border 0.1))))
  450. (pdf:with-page ()
  451. (pdf:with-outline-level ("cl-typegraph" (pdf:register-page-reference))
  452. (let ((content
  453. (compile-text ()
  454. (mark-ref-point "graph")
  455. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 30 :color '(0.0 0 0.8))
  456. "cl-typegraph" :eol
  457. (vspace 2)
  458. (hrule :dy 1)
  459. (with-style (:font "Times-Italic" :font-size 13)
  460. "The Cool Common Lisp Graph Typesetting System"))
  461. (vspace 20)
  462. (paragraph (:h-align :justified :top-margin 10 :first-line-indent 10
  463. :font "helvetica" :font-size 12)
  464. "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
  465. (vspace 20)
  466. "In the first graph example below, the nodes contain only strings.")
  467. (vspace 20)
  468. :hfill (if display-graphs
  469. (graph-box (gen-box-graph))
  470. (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
  471. (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
  472. "The class hierarchy for the boxes in cl-typesetting.")
  473. (vspace 20)
  474. (paragraph (:h-align :justified :font "helvetica" :font-size 12)
  475. "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.")
  476. (vspace 20)
  477. :hfill (if display-graphs
  478. (graph-box (gen-color-graph))
  479. (with-style (:color '(0.0 0 0.8)) "display-graphs is nil")) :hfill
  480. (paragraph (:h-align :center :font "Times-Italic" :font-size 11)
  481. "The primary colors"))))
  482. (draw-block content 30 810 530 780))))
  483. (pdf:write-document file)))
  484. ;;; A higher level example
  485. (defun multi-page-example (&optional (file #P"/tmp/multi-page.pdf")
  486. &aux content table (margins '(72 72 72 50)))
  487. (with-document ()
  488. ;(pdf:set-line-width 0.1)
  489. (let* ((print-stamp (multiple-value-bind (second minute hour date month year)
  490. (get-decoded-time)
  491. (format nil "Printed on ~4D-~2,'0D-~2,'0D ~2,'0D:~2,'0D"
  492. year month date hour minute)))
  493. (header (compile-text ()
  494. (paragraph (:h-align :center
  495. :font "Helvetica-BoldOblique" :font-size 12)
  496. "Multi-page example document")
  497. ;(vspace 1) ;:vfill
  498. (hrule :dy 1/2)))
  499. (footer (lambda (pdf:*page*)
  500. (compile-text (:font "Helvetica" :font-size 10)
  501. (hrule :dy 1/2)
  502. (hbox (:align :center :adjustable-p t)
  503. (verbatim print-stamp)
  504. ;(hspace 100)
  505. :hfill
  506. (verbatim
  507. (format nil "Page ~d" pdf:*page-number*)))
  508. ))))
  509. (setq content
  510. (compile-text ()
  511. (paragraph (:font "Helvetica-Bold" :font-size 16 :top-margin 20)
  512. "1. First paragraph group")
  513. (hrule :dy 2)
  514. (dotimes (i 40)
  515. (paragraph (:font "Helvetica" :font-size (+ 6 (random 10)))
  516. (verbatim (format nil "1.~d. " (1+ i)))
  517. (dotimes (j (1+ (random 5)))
  518. (put-string "The quick brown fox jumps over the lazy dog. "))))
  519. :eop))
  520. (draw-pages content :margins margins :header header :footer footer)
  521. (setq content
  522. (compile-text ()
  523. ;(vbox ()
  524. (paragraph (:font "Times-Bold" :font-size 16 :top-margin 20)
  525. "2. Second paragraph group"
  526. (hrule :dy 2))
  527. (dotimes (i 40)
  528. (paragraph (:font "Times-Roman" :font-size (+ 6 (random 10)))
  529. (verbatim (format nil "2.~d. " (1+ i)))
  530. (dotimes (j (1+ (random 5)))
  531. (put-string "The quick brown fox jumps over the lazy dog. "))))
  532. (table (:col-widths '(20 100 100 100) :splittable-p nil
  533. :border 0 :background-color '(1 1 0.8))
  534. (row ()
  535. (cell (:col-span 4)
  536. (paragraph (:h-align :center
  537. :font "Times-Italic" :font-size 12)
  538. "Non-splittable table - row with a col-span of 4")))
  539. (loop for (name tel age) = (list "Dmitri Dmitriev" "555-1234" (+ 25 (random 25)))
  540. and row-number from 1 upto 4
  541. do
  542. (row (:height (when (evenp row-number) 20)
  543. :background-color (if (zerop (mod row-number 3))
  544. :red nil))
  545. (cell () (verbatim
  546. (format nil "~d" row-number)))
  547. (cell (:background-color (when (>= age 40) :blue))
  548. name)
  549. (cell () tel)
  550. (cell () (paragraph (:h-align :right) age)))))
  551. ))
  552. (draw-pages content :margins margins :header header :footer footer)
  553. (setq content
  554. (compile-text ()
  555. (table (:col-widths '(20 100 100 100) :splittable-p t
  556. :border 1/2 :background-color '(1 1 0.8))
  557. (header-row (:background-color :gray)
  558. (cell (:row-span 2) (verbatim "Row #"))
  559. (cell (:row-span 2) "Name")
  560. (cell () "Telephone")
  561. ;(table (:col-widths '(100) :splittable-p nil
  562. ; :padding 0 :border 1/2); :background-color '(1 1 0.8))
  563. ; (row () (cell () "Telephone"))
  564. ; (row () (cell () "Fax"))))
  565. (cell (:row-span 2) (paragraph (:h-align :right) "Age")))
  566. (header-row (:background-color :gray)
  567. (cell () "Fax"))
  568. (footer-row (:background-color :gray)
  569. (cell (:col-span 4)
  570. (paragraph (:h-align :center
  571. :font "Times-Italic" :font-size 12)
  572. "Table footer with a col-span of 4")))
  573. (loop for (name tel age) = (list "Ivan Ivanov" "555-1234" (+ 25 (random 25)))
  574. and row-number from 1 upto 40
  575. do
  576. (row (:height (when (evenp row-number) 20)
  577. :background-color (if (zerop (mod row-number 3))
  578. :red nil))
  579. (cell () (verbatim
  580. (format nil "~d" row-number)))
  581. (cell (:background-color (when (>= age 40) :blue))
  582. name)
  583. (cell () tel)
  584. (cell () (paragraph (:h-align :right) age)))))))
  585. (setq table content)
  586. (draw-pages content :margins margins :header header :footer footer) ;:break :after
  587. (setq content ;; Various spans
  588. (compile-text ()
  589. (table (:col-widths '(20 40 60 80 120)
  590. :background-color :yellow :border 1
  591. :splittable-p t)
  592. (header-row ()
  593. (cell (:col-span 5)
  594. (paragraph (:h-align :center :font "Times-Italic" :font-size 12)
  595. "Table with cells spanning more then one row")))
  596. (row (:background-color :green)
  597. (cell (:row-span 2 :background-color :blue)
  598. "1,1 2,1 row-span 2")
  599. (cell () "1,2")
  600. (cell (:col-span 2 :row-span 3 :background-color :red)
  601. "1,3 1,4 - 3,3 3,4 col-span 2 row-span 3")
  602. (cell () "1,5"))
  603. (row ()
  604. (cell () "2,2")
  605. (cell (:row-span 2 :background-color :blue) "2,5 3,5 row-span 2"))
  606. (row (:background-color :green)
  607. (cell (:col-span 2) "3,1 3,2 col-span 2"))
  608. (row ()
  609. (cell () "4,1")
  610. (cell () "4,2")
  611. (cell () "4,3")
  612. (cell () "4,4")
  613. (cell () "4,5")))))
  614. (draw-pages content :margins margins :header header :footer footer) ;:break :after
  615. (draw-block (compile-text () "Test block - line1" :eol "Line 2")
  616. 300 300 150 150 :border 1))
  617. (when pdf:*page* (finalize-page pdf:*page*))
  618. ;(pdf:with-page ()
  619. ;(draw-page content :margins 72))
  620. (pdf:write-document file))
  621. table)
  622. #+nil
  623. (defun multi-page-hello (&optional (file #P"/tmp/hello.pdf"))
  624. (pdf:with-document ()
  625. (let ((content
  626. (compile-text ()
  627. (vspace 100)
  628. (table (:col-widths '(100 200) :splittable-p t) ;;; start Erik changes
  629. (header-row ()
  630. (cell ()
  631. (paragraph () "Header")))
  632. (footer-row ()
  633. (cell ()
  634. (paragraph () "Footer")))
  635. (dotimes (time 50)
  636. (row ()
  637. (cell ()
  638. (paragraph () (put-string (format nil "test ~d" time))))))) ;;; end Erik changes
  639. (vspace 10)
  640. :eol
  641. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50
  642. :color '(0.0 0 0.8))
  643. "cl-typesetting" :eol
  644. (vspace 2)
  645. (hrule :dy 1)
  646. (with-style (:font "Times-Italic" :font-size 26)
  647. "The cool Common Lisp typesetting system")
  648. (vspace 50)
  649. (with-style (:font "Times-Italic" :font-size 36 :color '(0.0 0
  650. 0.8))
  651. (dotimes (i 100)
  652. (put-string "Hello World!")(new-line)))))))
  653. (loop while (boxes content) do
  654. (pdf:with-page ()
  655. (pdf:set-line-width 0.1)
  656. (draw-block content 20 800 545 700))))
  657. (pdf:write-document file)))
  658. ;;; Unicode test
  659. (defparameter *unicode-test-string*
  660. (map unicode-string-type 'code-char
  661. '(8252 8319 8359 8592 8593 8594 8595 8596 8597 8616 915 920 934 945 948 949 963 964 966 32
  662. 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 32 65
  663. 9568 9650 9658 9660 9668 9675 9688 9689 8364 1027 8218 402 8222 8230 8224 8225 32 66
  664. 372 373 374 375 383 506 507 508 509 510 511 903 913 914 916 917 918 919 921 922 923 32
  665. 946 947 950 951 952 953 954 955 956 957 958 1101 1102 1103 1105 1106 1107 1108 32
  666. 1475 1488 1489 1490 1491 1492 1493 64304 64305 64306 64307 64308 64309 32
  667. 7911 7912 7913 7914 7915 7916 7917 7918 1179 1180 1181 1186 1187 1198 1199 1200 32)))
  668. ;; Look at the unicode-readme.txt in cl-pdf to see how to load unicode fonts
  669. (defun unicode-hello (&optional (file #P"/tmp/hello-u.pdf"))
  670. (pdf:with-document ()
  671. (pdf:with-page ()
  672. (pdf:with-outline-level ("Unicode Example" (pdf:register-page-reference))
  673. (pdf:set-line-width 0.1)
  674. (let ((content
  675. (compile-text ()
  676. (vspace 100)
  677. (paragraph (:h-align :center :font "Helvetica-Bold" :font-size 50 :color '(0.0 0 0.8))
  678. "cl-typesetting" :eol
  679. (vspace 2)
  680. (hrule :dy 1)
  681. (with-style (:font "Times-Italic" :font-size 26)
  682. "The cool Common Lisp typesetting system")
  683. (vspace 50)
  684. (with-style (:font "TimesNewRomanPSMT" :font-size 36)
  685. (put-string *unicode-test-string*))))))
  686. (draw-block content 20 800 545 700))))
  687. (pdf:write-document file)))