Skip to content

Commit

Permalink
Merge pull request #247 from kellemNegasi/US-395-create-example-code-…
Browse files Browse the repository at this point in the history
…for-usage-of-multi-font-encoder

[US-395] Add example code for multifont encoder
  • Loading branch information
anovik authored May 3, 2024
2 parents af296ca + 1d3bf41 commit 59c11f7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/adrg/sysfont v0.1.2 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
Binary file added multi-font-encoder/fonts/Batang.ttf
Binary file not shown.
Binary file not shown.
Binary file added multi-font-encoder/fonts/OpenSans-Regular.ttf
Binary file not shown.
Binary file added multi-font-encoder/fonts/PCSB Hebrew Regular.ttf
Binary file not shown.
Binary file not shown.
61 changes: 61 additions & 0 deletions multi-font-encoder/use_composite_TTFPdfFonts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* renders a multi language text using a multi font encoder.
*
* Run as: go run render_multi_language_text.go
*/
package main

import (
"os"

"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/creator"
"github.com/unidoc/unipdf/v3/model"
)

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}

func main() {
inputText := "Sample text 示例文本 טקסט לדוגמה ጽሑፍ ኣብነት 샘플 텍스트"
fonts := getFonts([]string{
"./fonts/PCSB Hebrew Regular.ttf",
"./fonts/NotoSerifEthiopic_Condensed-Black.ttf",
"./fonts/OpenSans-Regular.ttf",
"./fonts/Batang.ttf",
})

c := creator.New()
c.SetPageSize(creator.PageSizeA5)
p := c.NewStyledParagraph()
style := &p.Append(inputText).Style
style.MultiFont = model.NewMultipleFontEncoder(fonts)

err := c.Draw(p)
if err != nil {
panic(err)
}
if err := c.WriteToFile("multiple-language-text-multi-font.pdf"); err != nil {
panic(err)
}
}

// getFonts returns list of *model.PdfFont from list of paths to fonts files.
func getFonts(fontPaths []string) []*model.PdfFont {
fonts := []*model.PdfFont{}
for _, path := range fontPaths {
font, err := model.NewCompositePdfFontFromTTFFile(path)
if err != nil {
panic(err)
}
fonts = append(fonts, font)
}

return fonts
}

0 comments on commit 59c11f7

Please sign in to comment.