Skip to content

Instantly share code, notes, and snippets.

@pederkl
Last active September 14, 2015 05:10
Show Gist options
  • Save pederkl/8ecd27034967f7a88de1 to your computer and use it in GitHub Desktop.
Save pederkl/8ecd27034967f7a88de1 to your computer and use it in GitHub Desktop.
Example code showing weird word spacing.
(in-package :cl-user)
(asdf:load-system "cl-pdf")
(asdf:load-system "cl-typesetting")
(defun test-linebreaks (&optional (data '("Here is a line with some descriptive text, enough that it needs to linewrap. Which means lots of text if we let it use the entire page width."
"Line with less text, but still seeing weird spacing.")))
(pdf:with-document ()
(pdf:with-page ()
(let* ((lines (tt:compile-text (:font-size 8)
(loop for line in data
do
(tt:format-string line)
(tt:new-line))))
(lines-position '(36 557)))
; (break)
(tt::draw-block lines
(first lines-position)
(second lines-position)
(- (aref (pdf::bounds pdf::*page*) 2)
(first lines-position))
(- (aref (pdf::bounds pdf::*page*) 3)
(second lines-position)))))
(pdf:write-document "/tmp/test-linebreaks.pdf")))
@mbattyani
Copy link

I think it's because there is no paragraph style specified. Wrapping the lines in a paragraph fixes the issue as text settings like justified, centered, etc. are paragraph settings.
So the following works for me:

(defun test-linebreaks (&optional (data '("Here is a line with some descriptive text, enough that it needs to linewrap.  Which means lots of text if we let it use the entire page width."
                      "Line with less text, but still seeing weird spacing.")))
  (pdf:with-document ()
    (pdf:with-page ()
      (let* ((lines (tt:compile-text ()
                      (tt:paragraph (:h-align :center :font-size 16)
                       (loop for line in data
                do
                (tt:format-string line)
                (tt:new-line)))))
         (lines-position '(36 557)))
;   (break)
    (tt::draw-block lines
            (first lines-position)
            (second lines-position)
            (- (aref (pdf::bounds pdf::*page*) 2)
               (first lines-position))
            (- (aref (pdf::bounds pdf::*page*) 3)
               (second lines-position)))))
    (pdf:write-document "/tmp/test-linebreaks.pdf")))

I had to move the :font-size to the paragraph but it should have worked as a default setting as you wrote initially.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment