-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from kellemNegasi/US-395-create-example-code-…
…for-usage-of-multi-font-encoder [US-395] Add example code for multifont encoder
- Loading branch information
Showing
7 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |