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")))
@pederkl
Copy link
Author

pederkl commented Sep 13, 2015

Inspecting LINES at the commented out breakpoint gives a series of boxes, which look like this for the word "descriptive":

46: #<TYPESET::CHAR-BOX #\d>
47: #<TYPESET::HGLUE 421024C8DB>
48: #<TYPESET::CHAR-BOX #\e>
49: #<TYPESET::HGLUE 421024C90B>
50: #<TYPESET::CHAR-BOX #\s>
51: #<TYPESET::HGLUE 421024C93B>
52: #<TYPESET::CHAR-BOX #\c>
53: #<TYPESET::HGLUE 421024C96B>
54: #<TYPESET::CHAR-BOX #\r>
55: #<TYPESET::HGLUE 421024C99B>
56: #<TYPESET::HGLUE 421024C9B3>
57: #<TYPESET::CHAR-BOX #\i>
58: #<TYPESET::HGLUE 421024C9E3>
59: #<TYPESET::CHAR-BOX #\p>
60: #<TYPESET::HGLUE 421024CA13>
61: #<TYPESET::HYPHEN-BOX 421024CA2B>
62: #<TYPESET::CHAR-BOX #\t>
63: #<TYPESET::HGLUE 421024CA5B>
64: #<TYPESET::CHAR-BOX #\i>
65: #<TYPESET::HGLUE 421024CA8B>
66: #<TYPESET::CHAR-BOX #\v>
67: #<TYPESET::HGLUE 421024CABB>
68: #<TYPESET::HGLUE 421024CAD3>
69: #<TYPESET::CHAR-BOX #\e>

There's a hyphen-box where the extra spacing turns up, but it's a correct place for a hyphen, should one be needed, so I guess that's all right?

@pederkl
Copy link
Author

pederkl commented Sep 13, 2015

In the PDF file, it looks like

BT
0.000000   -9.516 Td
/CLF101 8.00 Tf
   100.0 Tz
[ (Here) -417 (is) -417 (a) -417 (line) -417 (with) -417 (some) -417 (descr) -22 (ip) ] TJ
ET
BT
  169.56   -9.516 Td
/CLF101 8.00 Tf
   100.0 Tz
[ (tiv) 38 (e) -417 (te) 45 (xt,) -500 (enough) -417 (that) -417 (it) -417 (needs) -417 (to) -417 (line) 30 (wr) ] TJ
ET

This, I have noe clue wether is correct or not, and I don't know cl-pdf or the PDF spec well enough to find out how this is generated and what it should have generated instead.

@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