[reportlab-users] Embedded font problems

Robin Becker robin at reportlab.com
Mon Mar 13 12:19:43 EDT 2017


I found the set of unicodes that the font uses and wrote the test program below.

Inspection of the output reveals that the fonts is using a special 'private' 
encoding.

There are three ranges

u'\uf020' ----> u'\uf07e'
space     -----> tilde    Ascii printable chars + f000

u'\uf0c8' -----> u'\uf0d4' ????

u'\uffff'   the unknown glyph

If you want to do anything sensible with the font you will need to figure out 
what the drawn bits of barcode actually mean and apply an encoding.

Good luck or get the barcode class to do what you need.


########################################################
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
registerFont(TTFont('128', 'code128.ttf'))

canv = Canvas('tcode128.pdf',A4)
y = ytop = A4[1] - 72
for u in '''61472 3
61473 4
61474 5
61475 6
61476 7
61477 8
61478 9
61479 10
61480 11
61481 12
61482 13
61483 14
61484 15
61485 16
61486 17
61487 18
61488 19
61489 20
61490 21
61491 22
61492 23
61493 24
61494 25
61495 26
61496 27
61497 28
61498 29
61499 30
61500 31
61501 32
61502 33
61503 34
61504 35
61505 36
61506 37
61507 38
61508 39
61509 40
61510 41
61511 42
61512 43
61513 44
61514 45
61515 46
61516 47
61517 48
61518 49
61519 50
61520 51
61521 52
61522 53
61523 54
61524 55
61525 56
61526 57
61527 58
61528 59
61529 60
61530 61
61531 62
61532 63
61533 64
61534 65
61535 66
61536 67
61537 68
61538 69
61539 70
61540 71
61541 72
61542 73
61543 74
61544 75
61545 76
61546 77
61547 78
61548 79
61549 80
61550 81
61551 82
61552 83
61553 84
61554 85
61555 86
61556 87
61557 88
61558 89
61559 90
61560 91
61561 92
61562 93
61563 94
61564 95
61565 96
61566 97
61640 98
61641 99
61642 100
61643 101
61644 102
61645 103
61646 104
61647 105
61648 106
61649 107
61650 108
61651 109
61652 3
65535 0'''.split('\n'):
	u, g = map(int,u.split())
	u = unichr(u)
	if y<72:
		y = ytop
		canv.showPage()
	canv.saveState()
	canv.setFont('Helvetica',16)
	label = 'u=%r g=%3d ' % (u,g)
	canv.drawString(72,y,label)
	x = 72+canv.stringWidth(label)
	canv.setFillColorRGB(1,0,0)
	canv.setFont('128',16)
	canv.drawString(x,y,u)
	canv.restoreState()
	y -= 20
canv.showPage()
canv.save()
########################################################




On 13/03/2017 12:09, jsmith4332--- via reportlab-users wrote:
> Hello,
> Does anyone have troubleshooting tips for figuring out why an embedded TTF font does not work in reportlab (just shows rectangles)? I'm trying to use https://grandzebu.net/informatique/codbar/code128.ttf (yes, I would like to use a barcode font instead of the built in barcode generator due to quality issues when printing on a low DPI printer)
> The relevant code is:
> stylesheet = StyleSheet1()
>
> stylesheet.add(
>     ParagraphStyle(
>         name='Normal',
>         fontName='Helvetica',
>         fontSize=12,
>         leading=16,
>         spaceBefore=8,
>         spaceAfter=8,
>     )
> )
>
> stylesheet.add(
>     ParagraphStyle(
>         name='Barcode',
>         fontName='128',
>         fontSize=12,
>     )
> )
> pdfmetrics.registerFont(TTFont('128', os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'code128.ttf')))# story definition omitted...story.append(Paragraph('helloworld'), stylesheet['Barcode']))
> Unfortunately, the resulting PDF has a bunch of squares instead of using the barcode font. When I switch to a different ttf, things seem to work. Furthermore, the code128 font referenced above seems to work fine in applications like Microsoft Word/Excel.
>
> Any ideas?
> Thanks!
>
>
>
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>


-- 
Robin Becker


More information about the reportlab-users mailing list