Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for UTF and for loading of TTF fonts #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pdf.mod/common.bmx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Extern
Function HPDF_GetCurrentEncoder:Byte ptr(handler:Byte Ptr, encodingName:Byte Ptr)
Function HPDF_SetCurrentEncoder:ULongInt(handler:Byte Ptr, encodingName:Byte Ptr)
Function HPDF_LoadRawImageFromMem:Byte Ptr(handler:Byte Ptr, buf:Byte Ptr, width:UInt, height:UInt, colorSpace:EPDFColorSpace, bitsPerComponent:UInt)
Function HPDF_UseUTFEncodings(handler:Byte Ptr)
Function HPDF_LoadTTFontFromFile:Byte Ptr(handler:Byte Ptr, path:Byte Ptr, embedding:Int)

Function HPDF_Page_BeginText:ULongInt(handle:Byte Ptr)
Function HPDF_Page_EndText:ULongInt(handle:Byte Ptr)
Expand Down
Binary file added pdf.mod/examples/FallingSky.ttf
Binary file not shown.
22 changes: 22 additions & 0 deletions pdf.mod/examples/load_font.bmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Framework Text.PDF

Local pdf:TPDFDoc = New TPDFDoc()

Local font:TPDFFont = pdf.GetFont(pdf.LoadTTFontFromFile("FallingSky.ttf"))

Local page:TPDFPage = pdf.AddPage()
page.SetWidth(200)
page.SetHeight(200)
page.SetFontAndSize(font, 24)

Local text:String = "Testing"

Local tw:Float = page.TextWidth(text)

page.BeginText()
page.TextOut((page.GetWidth() - tw) / 2, (page.GetHeight() - 26) / 2 , text)
page.EndText()

pdf.Save("load_font.pdf")

pdf.Free()
23 changes: 23 additions & 0 deletions pdf.mod/examples/utf.bmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Framework Text.PDF

Local pdf:TPDFDoc = New TPDFDoc()
pdf.UseUTFEncodings()

Local font:TPDFFont = pdf.GetFont(pdf.LoadTTFontFromFile("FallingSky.ttf", true), "UTF-8")

Local page:TPDFPage = pdf.AddPage()
page.SetWidth(200)
page.SetHeight(200)
page.SetFontAndSize(font, 24)

Local text:String = "Testing åäö ÅÄÖ"

Local tw:Float = page.TextWidth(text)

page.BeginText()
page.TextOut((page.GetWidth() - tw) / 2, (page.GetHeight() - 26) / 2 , text)
page.EndText()

pdf.Save("utf.pdf")

pdf.Free()
26 changes: 25 additions & 1 deletion pdf.mod/pdf.bmx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,22 @@ Type TPDFDoc
Function GetLastError:TPDFException()
Return lastError
End Function


Rem
bbdoc: Load TrueType font from external .ttf file and register it in the document object.
about:
<a href="../examples/load_font.bmx">Example source</a>
EndRem
Method LoadTTFontFromFile:String(path:String, embedding:Int=True)
Local pb:Byte Ptr = path.ToCString()
Local fb:Byte Ptr = HPDF_LoadTTFontFromFile(docPtr, pb, embedding)
MemFree(pb)

Local f:String = String.FromCString(fb)

Return f
EndMethod

Rem
bbdoc: Gets the requested font object.
returns: Returns the handle of a font object. Otherwise, it returns #Null and error-handler is called.
Expand Down Expand Up @@ -387,6 +402,15 @@ An application can change the setting of a pages tree by invoking #SetPagesConfi
Method UseCNTFonts:Int()
Return HPDF_UseCNTFonts(docPtr)
End Method

Rem
bbdoc: Enables UTF support for better text rendering when using non-ascii characters.
about:
<a href="../examples/utf.bmx">Example source</a>
EndRem
Method UseUTFEncodings()
HPDF_UseUTFEncodings(docPtr)
EndMethod

Rem
bbdoc: Sets the text of an info dictionary attribute, using current encoding of the document.
Expand Down
1 change: 1 addition & 0 deletions pdf.mod/source.bmx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Import "libharu/src/hpdf_encoder_cns.c"
Import "libharu/src/hpdf_encoder_cnt.c"
Import "libharu/src/hpdf_encoder_jp.c"
Import "libharu/src/hpdf_encoder_kr.c"
Import "libharu/src/hpdf_encoder_utf.c"
Import "libharu/src/hpdf_encoder.c"
Import "libharu/src/hpdf_encrypt.c"
Import "libharu/src/hpdf_encryptdict.c"
Expand Down