[cl-typesetting-devel] Anyway to measure the size of some text

Marc Battyani marc.battyani at fractalconcept.com
Tue Oct 5 19:08:40 UTC 2004


Peter Seibel wrote:


> Is there any way to determine the "natural" size of some generated
> text? For instance I have this function that generates a header:
>
> (defun compile-header (content-width)
>   (let ((cols 2))
>     (typeset:table
>       (:col-widths (loop repeat cols collect (/ content-width cols))
>                    :border 0
>                    :padding 0
>                    :cell-padding 0)
>       (typeset::row ()
>         (typeset:cell ()
>           (typeset::paragraph
>               (:h-align :left :font "Times-Italic" :font-size 10)
>             (typeset::put-string
>              (typeset::get-contextual-variable 'chapter-name))))
>         (typeset:cell ()
>           (typeset::paragraph
>               (:h-align :right :font "Times-Italic" :font-size 10)
>             (typeset::put-string (format nil "Page ~d of ~d"
>                                          pdf:*page-number*
>
(typeset:find-ref-point-page-number :the-end)))))))))
>
> But I want to layout the table based on the natural widths of the
> contents of the cells. In particular in this case I'd like to know the
> minimum width of the second cell in order to avoid wrapping the
> contents so I can then set :col-widths of the table to make the second
> column that wide and give the remainder to the first cell.

Automatic layout of tables is a very tough problem. It's a pity that I can't
find some time to play with it. :(

To compute the size of some content you just have to do something like this:

META-WEB 19 >   (defparameter *content*
                  (typeset::compile-text ()
                    (typeset::paragraph
                        (:h-align :right :font "Times-Italic" :font-size 10)
                        (typeset::put-string (format nil "Page ~d of ~d" 12
15)))))
*content*

META-WEB 20 > (typeset::compute-boxes-natural-size (typeset::boxes
*content*) 'typeset::dx)
54.83

META-WEB 21 > (typeset::compute-parallel-size (typeset::boxes *content*)
'typeset::dy)
12.0
10

This will only work on a single line content.

If you add the following functions to typo.lisp you can then directly put
the text-content in the cell.
(warning: not tested, it it works I will add this to typo.lisp)

(defun add-boxes-to-content (content boxes)
  (when content
    (setf (boxes-tail content) (setf (cdr (boxes-tail content)) boxes)))
  boxes)

(defun add-text-content (content)
  (when content
    (add-boxes-to-content *content* (cdr (boxes content)))
    (clear-content content))
  content)

Marc





More information about the cl-typesetting-devel mailing list