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

Marc Battyani marc.battyani at fractalconcept.com
Wed Jun 1 08:09:34 UTC 2005


"Peter Seibel" <peter at gigamonkeys.com> writes:

> 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?

You are doing something much to complex for such a simple layout.
cl-typesetting is more high level than that... ;-)

Here is how I would do it:

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

(in-package :business-card)

(defconstant +points-per-inch+ 72)

The first card example is for the all centered model:

(defun business-card1 (&optional (file #P"/tmp/card1.pdf"))
  (let ((width (* 3.5d0 +points-per-inch+))
        (height (* 2d0 +points-per-inch+)))
    (pdf:with-document ()
      (pdf:with-page ()
        (let ((center-block
               (compile-text ()
                 (paragraph (:h-align :center :font "Times-Italic"
:font-size 8)
                   "You can hire a billion monkeys or you can hire me." :eol
     :vfill
     (with-style (:font "Helvetica" :font-size 12)
       "Peter Seibel" :eol)
     (with-style (:font "Helvetica" :font-size 11)
       "Gigamonkeys Consulting" :eol)
     :vfill
     (with-style (:font "Helvetica" :font-size 8)
       (format-string "peter at gigamonkeys.com ~c www.gigamonkeys.com"
(code-char 127))) :eol
       (format-string "6205 Mathieu Avenue, Oakland, California, 94618 ~c
415.203.3716"
        (code-char 127))))))
   (pdf:translate 50 650)
          (typeset::draw-block center-block 0 height width height :v-align
:fill :border 1)))
      (pdf:write-document file))))

Notes: it's better to use format-string instead of format nil, draw-block
can draw a border, vfill and hfill are cool.

Second example is with left and right aligned texts:

(defun business-card2 (&optional (file #P"/tmp/card2.pdf"))
  (let ((width (* 3.5d0 +points-per-inch+))
        (height (* 2d0 +points-per-inch+)))
    (pdf:with-document ()
      (pdf:with-page ()
        (let ((center-block
               (compile-text ()
                 (paragraph (:h-align :center :font "Times-Italic"
:font-size 8)
                   "You can hire a billion monkeys or you can hire me." :eol
     :vfill
     (with-style (:font "Helvetica" :font-size 12)
       "Peter Seibel" :eol)
     (with-style (:font "Helvetica" :font-size 11)
       "Gigamonkeys Consulting" :vfill :vfill))))
              (left-block
        (compile-text ()
       (paragraph (:h-align :left :font "Helvetica" :font-size 8)
    "6205 Mathieu Avenue" :eol
    "Okaland, California" :eol
    "94618")))
              (right-block
        (compile-text ()
                  (paragraph (:h-align :right :font "Helvetica" :font-size
8)
        "peter at gigamonkeys.com" :eol
        "www.gigamonkeys.com" :eol
        "415.203.3716"))))
   (pdf:translate 50 650)
          (typeset::draw-block left-block 0 height width height :v-align
:bottom :border 1)
          (typeset::draw-block center-block 0 height width height :v-align
:fill)
          (typeset::draw-block right-block 0 height width height :v-align
:bottom)))
      (pdf:write-document file))))

In this example, the layout could have been done completely with
cl-typesetting in one block like this:

(defun business-card3 (&optional (file #P"/tmp/card3.pdf"))
  (let ((width (* 3.5d0 +points-per-inch+))
        (height (* 2d0 +points-per-inch+)))
    (pdf:with-document ()
      (pdf:with-page ()
        (let ((center-block
               (compile-text ()
                 (paragraph (:h-align :center :font "Times-Italic"
:font-size 8)
                   "You can hire a billion monkeys or you can hire me." :eol
     :vfill
     (with-style (:font "Helvetica" :font-size 12)
       "Peter Seibel" :eol)
     (with-style (:font "Helvetica" :font-size 11)
       "Gigamonkeys Consulting" :vfill))
   (paragraph (:h-align :fill :font "Helvetica" :font-size 8)
       "6205 Mathieu Avenue" :hfill "peter at gigamonkeys.com" :eol
       "Okaland, California" :hfill "www.gigamonkeys.com" :eol
       "94618" :hfill "415.203.3716"))))
   (pdf:translate 50 650)
          (typeset::draw-block center-block 0 height width height :v-align
:fill :border 1)))
      (pdf:write-document file))))

I've attached the generated pdfs. As you can see the glyphs layout is much
nicer. Just zoom it and compare with the raw cl-pdf layout. (look at the
"You" for instance)

Marc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: card3.pdf
Type: application/pdf
Size: 1566 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/cl-typesetting-devel/attachments/20050601/7fddd8e5/attachment.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: card1.pdf
Type: application/pdf
Size: 1571 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/cl-typesetting-devel/attachments/20050601/7fddd8e5/attachment-0001.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: card2.pdf
Type: application/pdf
Size: 1601 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/cl-typesetting-devel/attachments/20050601/7fddd8e5/attachment-0002.pdf>


More information about the cl-typesetting-devel mailing list