[reportlab-users] Character placement off - bug or am I using it wrong?

52 views
Skip to first unread message

ellie

unread,
Aug 20, 2022, 8:03:09 AM8/20/22
to reportl...@lists2.reportlab.com
Hi everyone,

I have an issue with .drawString() called on reportlab.pdfgen.Canvas, it
seems characters are placed wrong. For example, for the word "Two"
(created with reportlab's placement), the "T" and "w" are oddly apart.
Commas are spaced wrong as well, when I use a hack to use a placement of
another TTF lib it works.

I use reportlab open-source 3.6.10, Python 3.10.6, and the font "Tex
Gyre Pagella" from the LaTeX community in TTF format.


It looks like reportlab is placing letters with naive bounding boxes
rather than to properly overlap special pairs in whatever smart way the
font would provide. (That might be wrong, just my uninformed guess.)
Basically, like the sort of "placement hinting" of the font is off (my
bad if that's not how it's called) and it looks horrible as a result.

Is this a reportlab bug? Is it a problem with the reportlab install,
like a missing library? Or do I need to pass some option for proper
placement? If it's a bug, is there hope for a fix? If it's an install
issue, can I detect this in Python to avoid silently generating broken
PDFs? The result is ad enough to be useless for serious print.

Regards, ellie

PS: I would attach font & demonstration image but I was told not to, so
I guess you'll have to find them yourself if you want to check it out
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Tim Roberts

unread,
Aug 20, 2022, 2:56:22 PM8/20/22
to reportlab-users
On 8/20/22 05:02, ellie wrote:

> Hi everyone,
>
> I have an issue with .drawString() called on reportlab.pdfgen.Canvas,
> it seems characters are placed wrong. For example, for the word "Two"
> (created with reportlab's placement), the "T" and "w" are oddly apart.
> Commas are spaced wrong as well, when I use a hack to use a placement
> of another TTF lib it works.
>
> I use reportlab open-source 3.6.10, Python 3.10.6, and the font "Tex
> Gyre Pagella" from the LaTeX community in TTF format.

How did you convert it to a TTF?  The font in the TeX library is an OTF
with Postscript outlines, which Reportlab doesn't support.


> It looks like reportlab is placing letters with naive bounding boxes
> rather than to properly overlap special pairs in whatever smart way
> the font would provide. (That might be wrong, just my uninformed
> guess.) Basically, like the sort of "placement hinting" of the font is
> off (my bad if that's not how it's called) and it looks horrible as a
> result.

"Kerning" is the word you're looking for.  Reportlab knows about
kerning, so if they're in the font, it should work.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

ellie

unread,
Aug 21, 2022, 1:42:37 AM8/21/22
to reportl...@lists2.reportlab.com, ti...@probo.com
Thanks for your answer!
Sorry for responding to the wrong message here, but your e-mail sadly
didn't arrive for me while some others did, I'm not sure why.

Tim Roberts ti...@probo.com wrote:
> How did you convert it to a TTF? The font in the TeX library is an
> OTF

There is also a TTF version which I used, sadly I can't attach it.
However, I noticed a screenshot alone works, so I attached that. Look at
the "Tw" in "Two" or all the commas, upper render is reportlab
placement, lower is with what should be with font kerning working
(although due to my hacky approach to override it, maybe some hinting or
other details isn't, so it's not 100% correct, just less wrong).

Given the kerning works with a different TTF library with the same font
file, I would assume it's kind of out of the question the font contains
it. That is why I'm wondering why it doesn't work with reportlab.

Is this a bug in reportlab then? Where can I report it with all the
files like the font attached? I assume people would kind of hope for
something like basic glyph placement in a PDF library to work, so maybe
a fix might be worth a consideration.

On 8/16/22 8:21 PM, ellie wrote:
> Hi everyone,
>
> I have an issue with .drawString() called on reportlab.pdfgen.Canvas
> shown in the attached image, it seems characters are placed wrong. For
> example, look at the word "Two" in the upper paragraph (created with
> reportlab's placement), see how the "T" and "w" are oddly apart.
>
> Also compare to the lower image (created via a hack overriding reportlab
> placement), especially also all the commas which seem to be spaced too
> far to the right by reportlab as well.
>
> I use reportlab open-source 3.6.10, Python 3.10.6, and the attached font
> "Tex Gyre Pagella" from the LaTeX community in TTF format.
>
>
> It looks like reportlab is placing letters with naive bounding boxes
> rather than to properly overlap special pairs in whatever smart way the
> font would provide. (That might be wrong, just my uninformed guess.)
>
> Is this a reportlab bug? Is it a problem with the reportlab install,
> like a missing library? Or do I need to pass some option for better
> placement? If it's a bug, is there hope for a fix? If it's an install
> issue, can I detect this in Python to avoid silently generating broken
> PDFs?
>
> Regards, ellie
reportlabhmweird.jpg

Tim Roberts

unread,
Aug 21, 2022, 1:55:43 AM8/21/22
to reportl...@lists2.reportlab.com
On 8/20/22 22:42, ellie wrote:

>
> There is also a TTF version which I used, sadly I can't attach it.

Where did you get it?  My working theory is that the conversion to TTF
was done incorrectly.


> However, I noticed a screenshot alone works, so I attached that. Look
> at the "Tw" in "Two" or all the commas, upper render is reportlab
> placement, lower is with what should be with font kerning working
> (although due to my hacky approach to override it, maybe some hinting
> or other details isn't, so it's not 100% correct, just less wrong).

Were both of those generated from the TTF?  Or was one from the OTF? 
Most of the kerning looks just fine.  There are only a couple of
placements that are bad.  The comma is a bit off, and the "Tw" pair
could be closer.


> Given the kerning works with a different TTF library with the same
> font file, I would assume it's kind of out of the question the font
> contains it. That is why I'm wondering why it doesn't work with
> reportlab.

What do you mean by "different TTF library"?  How did you generate the
other sample?  What application?  Same TTF, or was it the OTF?  For that
matter, what does your Reportlab code look like?  Are you using Platypus
to do automated layout?


> Is this a bug in reportlab then? Where can I report it with all the
> files like the font attached? I assume people would kind of hope for
> something like basic glyph placement in a PDF library to work, so
> maybe a fix might be worth a consideration.

Now, stand back a minute.  "Basic glyph placement" is just fine. You
seem to have uncovered a potential kerning issue in a couple of kerning
pairs in a single font, but you haven't given them nearly enough to work
with.  At this point, you haven't even convinced me that the problem is
in Reportlab.  If you can't give them the actual font that fails, how
can they possibly do any debugging?

ellie

unread,
Aug 21, 2022, 9:55:39 AM8/21/22
to reportl...@lists2.reportlab.com
Update: reportlab support just confirmed to me this is a reportlab
limitation/bug, and TTF fonts are placed without kerning.

I'll try to sum up the main points I sent them (I CC'ed it here but
messed up the CC):

- I think whether this is visually acceptable for every font is
debatable and I hoped this would be an option at least. For the example
I linked with image I don't find no kerning visually acceptable, at
least not for professional print.

- I find it surprising simply because any other TTF using lib under the
sun that isn't advertised as simple/stripped down seems to do kerning.
(Maybe I'm wrong? But I haven't seen one that does yet.)

- It seems to me this will cause interoperability problems for people.
E.g. I layout with the help of a different TTF lib, then hammer that
layout into reportlab with per word placement. But since reportlab
doesn't use kerning (and the lib I layouted with does) the words are now
too wide, so a lot of word pair spacing is unreadably wrong. I could use
drawString() per character instead, but wouldn't I break ligatures or
such things?

- Summed up, I'm hoping TTF kerning will be added one day and I find
this limitation somewhat notable. Also, I'm hoping for alternate python
PDF library suggestions where TTF kerning is done out of the box.

I hope that was an adequate summary!

On 8/16/22 8:21 PM, ellie wrote:
> Hi everyone,
>
> I have an issue with .drawString() called on reportlab.pdfgen.Canvas
> shown in the attached image, it seems characters are placed wrong. For
> example, look at the word "Two" in the upper paragraph (created with
> reportlab's placement), see how the "T" and "w" are oddly apart.
>
> Also compare to the lower image (created via a hack overriding reportlab
> placement), especially also all the commas which seem to be spaced too
> far to the right by reportlab as well.
>
> I use reportlab open-source 3.6.10, Python 3.10.6, and the attached font
> "Tex Gyre Pagella" from the LaTeX community in TTF format.
>
>
> It looks like reportlab is placing letters with naive bounding boxes
> rather than to properly overlap special pairs in whatever smart way the
> font would provide. (That might be wrong, just my uninformed guess.)
>
> Is this a reportlab bug? Is it a problem with the reportlab install,
> like a missing library? Or do I need to pass some option for better
> placement? If it's a bug, is there hope for a fix? If it's an install
> issue, can I detect this in Python to avoid silently generating broken
> PDFs?
>
> Regards, ellie

Robin Becker

unread,
Aug 22, 2022, 6:46:13 AM8/22/22
to reportlab-users, ellie
...........
>
> - Summed up, I'm hoping TTF kerning will be added one day and I find this limitation somewhat notable. Also, I'm hoping
> for alternate python PDF library suggestions where TTF kerning is done out of the box.
............

Hi Ellie, I'm not sure I disagree with anything you say regarding the desirability of making the text look nice.

The problem for ReportLab is that it was aimed at a different usage and speed was important. As you rightly note use of
kerning, ligatures and similar have requirements in the whole render so functions like stringWidth and subsetting need
to be aware of the many possibilities.

For some languages even simple kerning is not enough see eg
https://www.cairn.info/load_pdf.php?ID_ARTICLE=DN_093_0069&download=1

If we have to do it the RL toolkit will almost surely end up using a complete rendering scheme as used by cairo. That
will make things quite slow so I assume it would be reserved for specific fonts.

We are moving towards using pyCairo to render graphics, but currently we don't use cairo's text handling. Perhaps pango
will get used eventually. I will probably not be around when that gets done.

--
Robin Becker
Reply all
Reply to author
Forward
0 new messages