[cl-typesetting-devel] Laying out sub-boxes?

Peter Seibel peter at gigamonkeys.com
Wed Jun 1 04:45:41 UTC 2005


Peter Seibel <peter at gigamonkeys.com> writes:

> Peter Seibel <peter at gigamonkeys.com> writes:
>
>> So to layout, say, a business card using cl-typesetting (as opposed to
>> raw cl-pdf as I did before) it seems like it'd make some sense to
>> generate a few different chunks of content and then place them, as a
>> unit, the right places on the page. Are there any examples of this in
>> test.lisp or can someone explain how I might do it.
>
> As usual, right after I asked, I went back and figured it out, more or
> less. Anyway, if anyone has any advice or examples, I'm still
> interested but I did figure out you can compile different bits of text
> with compile-text and then draw them with draw-block.

Okay, so now I've got a real question. In order to layout my boxes
correctly I need to know what their natural sizes are. But the values
returned by compute-boxes-natural-size don't make much sense to me.
For instance, the following code shows the natural heights of three
lines of text to be around 6 inches. Or do I have some units problem?

-Peter

  (defpackage :business-card (:use :cl :typeset))

  (in-package :business-card)

  (defconstant +points-per-inch+ 72)

  (defun natural-height (text)
    (typeset::compute-boxes-natural-size 
     (typeset::boxes text)
     'typeset::dy))

  (defun natural-width (text)
    (typeset::compute-boxes-natural-size 
     (typeset::boxes text)
     'typeset::dx))

  (defmacro with-natural-sizes ((width height) text &body body)
    `(let ((,width (natural-width ,text))
           (,height (natural-height ,text)))
       (format t "text: ~a;~,25twidth: ~,2f;~,25theight: ~,2f~%" ',text ,width ,height)
       , at body))

  (defun business-card (&optional (file #P"/tmp/hello.pdf"))
    (let ((width (* 3.5d0 +points-per-inch+))
          (height (* 2d0 +points-per-inch+)))
      (pdf:with-document ()
        (pdf:with-page ()
          (let ((tagline
                 (compile-text ()
                   (paragraph (:h-align :center :font "Times-Italic" :font-size 8)
                     "You can hire a billion monkeys or you can hire me." :eol)))
                (name
                 (compile-text ()
                   (paragraph (:h-align :center :font "Helvetica" :font-size 12)
                     "Peter Seibel" :eol)))
                (company
                 (compile-text ()
                   (paragraph (:h-align :center :font "Helvetica" :font-size 11)
                     "Gigamonkeys Consulting" :eol)))
                (network-one-line
                 (compile-text ()
                   (paragraph (:h-align :center :font "Helvetica" :font-size 8)
                     (put-string (format nil "peter at gigamonkeys.com ~c www.gigamonkeys.com" (code-char 127))) :eol)))
                (address-one-line 
                 (compile-text ()
                   (paragraph (:h-align :center :font "Helvetica" :font-size 8)
                     (put-string (format nil "6205 Mathieu Avenue, Oakland, California, 94618 ~c 415.203.3716" (code-char 127))) :eol)))
                (address-chunk
                  (compile-text ()
                    (paragraph (:h-align :left :font "Helvetica" :font-size 8) "6205 Mathieu Avenue" :eol)
                    (paragraph (:h-align :left :font "Helvetica" :font-size 8) "Okaland, California" :eol)
                    (paragraph (:h-align :left :font "Helvetica" :font-size 8) "94618" :eol)))
                (network-chunk
                  (compile-text ()
                    (paragraph (:h-align :right :font "Helvetica" :font-size 8)
                      "peter at gigamonkeys.com" :eol
                      "www.gigamonkeys.com" :eol
                      "415.203.3716" :eol))))
            (declare (ignorable address-chunk network-chunk address-one-line network-one-line))

            (pdf:set-rgb-fill 1.0 1.0 1.0)
            (pdf:set-line-width .1)
            (pdf:translate 100 100)
            (pdf:set-rgb-stroke 0.0 0.0 0.0)
            (pdf:rectangle 0 0 width height)
            (pdf:close-and-stroke)
            (pdf:set-rgb-fill 0.0 0.0 0.0)
            (with-natural-sizes (w h) tagline
              (typeset::draw-block tagline 0 (- height 10) width h))
            (with-natural-sizes (w h) name
              (typeset::draw-block name 0 (- height 30) width h))
            (with-natural-sizes (w h) company
              (typeset::draw-block company 0 (- height 50) width h))
            (with-natural-sizes (w h) network-one-line)
            (with-natural-sizes (w h) address-one-line)
            (with-natural-sizes (w h) address-chunk
              (typeset::draw-block address-chunk 0 h (/ width 2) h))
            (with-natural-sizes (w h) network-chunk
              (typeset::draw-block network-chunk (/ width 2) h (/ width 2) h))))
        (pdf:write-document file))))



-- 
Peter Seibel                                     peter at gigamonkeys.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp



More information about the cl-typesetting-devel mailing list