[reportlab-users] [PATCH] Add TTF Embedding Option

Robin Becker robin at reportlab.com
Thu Oct 16 09:16:46 EDT 2008

Yoann Roman wrote:

> Per the earlier discussion (see "Disable TTF Embedding"), I was looking

> for a way to replicate Acrobat's "never embed" font option in RL.


> The only patch needed for TrueType fonts was to ttfonts.py. The default

> behavior does not change; this adds an optional "embed" argument to

> TTFont that, if explicitly set to 0, will prevent the font embedding.


> I wasn't sure if the encoding was necessary. Acrobat specifies it, the

> spec says its optional, and RL leaves it out for TTFs. I followed RL

> and left it out.


> I'm certainly no PDF spec expert and don't know if this has unintended

> side effects. I also realize most people won't need this, but, since it

> doesn't change the default behavior and is a minimal patch, I don't see

> any harm in adding it.


my earlier suspicions are proved correct. Using more than the first subset
causes an error

ValueError: redefining named object: 'fontDescriptor:ArialMT'

that is one of our errors related to subsetting.

We are trying to write out text using bytes; that means to use arial
non-embedded we either need to subset it and make the subsets meaningful in some
way or we need to regard arial as a T1 font with eg one of the standard adobe

There may be some way to use unicode as the output format, but I don't know
enough about how adobe reader works with these non-embedded system fonts to
answer for sure. I don't think we should let this patch go till we can get at
least this primitive test to work.

> def testTTFNoEmbed(self):

> if sys.platform!='win32': return

> for x in 'windows', 'winnt':

> fn = os.path.join('\\'+x,'fonts','arial.ttf')

> if os.path.isfile(fn): break

> fn = None

> if not fn: return

> def drawUsingArial(embed=1):

> font = TTFont("Arial",fn)

> pdfmetrics.registerFont(TTFont("Arial",fn,embed=embed))

> c = Canvas(outputfile('test_pdfbase_ttfonts-arial-%s.pdf' % (embed and 'embedded' or 'non-embedded')))

> c.drawString(100, 700, 'Testing Arial non-embedded')


> # Draw a table of Unicode characters

> y = 700-14

> c.setFont('Arial', 10)

> for i in xrange(0x20,0x132,8):

> x = 100

> lab='%2.2x: '%i

> c.drawString(x,y,lab)

> x += font.stringWidth(lab,10)

> for j in xrange(i,i+8):

> c.drawString(x,y,unichr(j))

> x += 15

> y -= 14

> c.save()

> from reportlab.pdfbase.pdfmetrics import _reset

> _reset()

> drawUsingArial(1)

> drawUsingArial(0)

Robin Becker

More information about the reportlab-users mailing list