Writing international characters with Polybios

Discuss PDF file handling with the Polybios plugin here
Post Reply
User avatar
jPV
Posts: 600
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Writing international characters with Polybios

Post by jPV »

I'm having hard time trying to write PDF files with international charactes (like ö, ä, etc) with Polybios.

I'm trying to load a separate TTF font, and it displays fine under Hollywood, but when I write a PDF with it, I only get some wrong characters or just empty space where the international characters should be.

I've tried to change between UTF-8 and ISO encodings, and the only time I've seen characters correctly in a PDF file, is when I use ISO encoding and disable the Embedding option in the LoadTTFont function. Then it renders the "ä" character in my example on MorphOS PDF viewers, but still the same file doesn't render the character correcly under Windows! And disabling Embedding isn't nice, because then some viewers (like VPDF) don't render character spacings correctly.

So, am I doing something wrong or is there something that should be fixed in Polybios?

Here's the test code I've been playing with now:

Code: Select all

@OPTIONS {Encoding = #ENCODING_ISO8859_1}
@REQUIRE "polybios"
@DISPLAY {Width=595, Height=842, Color=#WHITE}

font$ = "System:MorphOS/Fonts/_ttf/DejaVuSans.ttf"
size = 24
text$ = "This is ä text line!!1"

; Enable this line if your editor uses UTF-8 and disable otherwise
; text$ = ConvertStr(text$, #ENCODING_UTF8, #ENCODING_ISO8859_1)

SetFont(font$, size, {Engine = #FONTENGINE_INBUILT, UsePoints=True})
SetFontColor(#BLACK)
SetFontStyle(#ANTIALIAS)
TextOut(0, 0, text$)

doc=pdf.CreateDocument()
; This doesn't seem to make any difference:
;doc:UseUTFEncodings()
page = doc:AddPage()
page:SetSize(#HPDF_PAGE_SIZE_A4, #HPDF_PAGE_PORTRAIT)

; Setting False here will make characters to work in some degree under MorphOS,
; but generates a spacing issue on some readers + doesn't still work under Windows
f$ = doc:LoadTTFont(font$, True)

font = doc:GetFont(f$)
page:SetFontAndSize(font, size)

page:BeginText()
page:TextOut(0, 842 - size, text$)
page:EndText()
doc:SaveToFile("ram:test.pdf")

WaitLeftMouse()
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Writing international characters with Polybios

Post by airsoftsoftwair »

You need to set the current encoder to UTF-8 and also pass UTF-8 to GetFont() in order to get this right. Of course, this also means that your script should use UTF-8 and not ISO-8859-1. Here's a corrected version of your script:

Code: Select all

@REQUIRE "polybios"
@DISPLAY {Width=595, Height=842, Color=#WHITE}

font$ = "DejaVuSansCondensed.ttf"
size = 24
text$ = "This is ä text line!!1"

; Enable this line if your editor uses UTF-8 and disable otherwise
; text$ = ConvertStr(text$, #ENCODING_UTF8, #ENCODING_ISO8859_1)

SetFont(font$, size, {Engine = #FONTENGINE_INBUILT, UsePoints=True})
SetFontColor(#BLACK)
SetFontStyle(#ANTIALIAS)
TextOut(0, 0, text$)

doc=pdf.CreateDocument()
; This doesn't seem to make any difference:
doc:UseUTFEncodings()
doc:SetCurrentEncoder("UTF-8")
page = doc:AddPage()
page:SetSize(#HPDF_PAGE_SIZE_A4, #HPDF_PAGE_PORTRAIT)

; Setting False here will make characters to work in some degree under MorphOS,
; but generates a spacing issue on some readers + doesn't still work under Windows
f$ = doc:LoadTTFont(font$, True)

font = doc:GetFont(f$, "UTF-8")
page:SetFontAndSize(font, size)

page:BeginText()
page:TextOut(0, 842 - size, text$)
page:EndText()
doc:SaveToFile("test.pdf")

WaitLeftMouse()
User avatar
jPV
Posts: 600
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Writing international characters with Polybios

Post by jPV »

Ok, thanks.

BTW. is it that Base 14 fonts don't support ä, ö, etc characters? I get an error if I try to use them in UTF-8... maybe they don't have all characters from ISO-8859-1 set either, so no work-arounds for them?

Code: Select all

@REQUIRE "polybios"
@DISPLAY {Width=595, Height=842, Color=#WHITE}

font$ = "Courier"
size = 24
text$ = "This is ä text line!!1"

doc=pdf.CreateDocument()
doc:UseUTFEncodings()
doc:SetCurrentEncoder("UTF-8")
page = doc:AddPage()
page:SetSize(#HPDF_PAGE_SIZE_A4, #HPDF_PAGE_PORTRAIT)

font = doc:GetFont(font$, "UTF-8")
page:SetFontAndSize(font, size)

page:BeginText()
page:TextOut(0, 842 - size, text$)
page:EndText()
doc:SaveToFile("test.pdf")
The previous code gives: Combination between font and encoder is wrong. (detail 0)
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Writing international characters with Polybios

Post by airsoftsoftwair »

For some reason, the UTF-8 encoder doesn't seem to work with Base14 fonts, so just use one of the other encodings. "WinAnsiEncoding" should be a pretty safe bet because it's mostly the same as ISO 8859-1. The following code works for me:

Code: Select all

@REQUIRE "polybios"
@DISPLAY {Width=595, Height=842, Color=#WHITE}

font$ = "Courier"
size = 24
text$ = "This is ä text line!!1"
enc$ = "WinAnsiEncoding"

doc=pdf.CreateDocument()
doc:SetCurrentEncoder(enc$)
page = doc:AddPage()
page:SetSize(#HPDF_PAGE_SIZE_A4, #HPDF_PAGE_PORTRAIT)

font = doc:GetFont(font$, enc$)
page:SetFontAndSize(font, size)

page:BeginText()
page:TextOut(0, 842 - size, ConvertStr(text$, #ENCODING_UTF8, #ENCODING_ISO8859_1))
page:EndText()
doc:SaveToFile("test.pdf")
Post Reply