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

writing unicode characters in pdf #40

Open
shoaib-mi opened this issue May 14, 2022 · 3 comments
Open

writing unicode characters in pdf #40

shoaib-mi opened this issue May 14, 2022 · 3 comments

Comments

@shoaib-mi
Copy link

Hi, I am new to nim, and I am still learning. I was wondering if there was any way to write utf-8 unicode characters and specially right to left characters inside a pdf using nimpdf. for example here is the code I run:

      import nimPDF/nimPDF
      var doc = newPDF()
      #doc.setFont("Aims.ttf", {FS_REGULAR}, 5.0, ENC_UTF8)
      doc.addPage(getSizeFromName("A4"), PGO_PORTRAIT)
      doc.drawText(15, 15, "hello β")
      if not doc.writePDF("hello.pdf"):
        echo "cannot open: hello.pdf"

what I see in pdf file is <hello ˛†>. and when I try to define a font that I know have the glyph for unicode characters, the code comiles without any error, but when I run the compiled file under linux, this error occures:

      /home/shoaib/Documents/ex3.nim(4) ex3
      /home/shoaib/.nimble/pkgs/nimPDF-0.4.3/nimPDF/nimPDF.nim(164) setFont
      /usr/lib/nim/system/assertions.nim(38) failedAssertImpl
      /usr/lib/nim/system/assertions.nim(28) raiseAssert
      /usr/lib/nim/system/fatal.nim(53) sysFatal
      Error: unhandled exception: /home/shoaib/.nimble/pkgs/nimPDF-0.4.3/nimPDF/nimPDF.nim(164, 9) `doc.curPage != nil`  [AssertionDefect]

I think it's because it can not find correct path to the font (that is installed in .local/share/fonts/). and when I use the full path to the file, the same error still occurs.

@jangko
Copy link
Owner

jangko commented May 15, 2022

The way fonts are handled in nimPDF is similar to a web browser where you only need to tell which font family to use and not the filename itself. A font family can be organized into one or more file, or one file can contains multiple font style in the same family.

You only need to tell nimPDF where to find those font file using PDFOptions, and when you use setFont, you only need to specify the font name/family and not the file name itself. nimPDF then will scan the folder to find correct font.

var opts = newPDFOptions()
opts.addFontsPath("fonts")
opts.addImagesPath("resources")
opts.addImagesPath("pngsuite")
opts.addResourcesPath("resources")

var doc = newPDF(opts)
doc.setFont("Times", {FS_REGULAR}, 4)

@shoaib-mi
Copy link
Author

Thank you @jangko. I used your suggestion, and I could change the font in both Linux and windows environment. In windows the pdf shows Greek letters correctly and in the right order, but for Arabic letters, they were in reverse order. But in Linux neither Greek nor Arabic leters were not shown at all, just some strange symbols appeared. In python there are some modules (python-bidi and arabic_rtlize) that could fix the reverse order and separation of letters for Arabic.

I think for Nim we must have such modules or at least some wrappers for those python modules. Right now I have no idea what happens in Linux that ends to this problem.

I think opening an issue was not a good decision, and I should have asked my question in forums like stackoverflow instead. But I for one appreciate your job and thank you for that.

@shoaib-mi
Copy link
Author

I just wrote a code in pure Nim similar to python module arabic_rtlize. Here is the link to the code, just in case someone encountered the same problem RTL. It works in both linux and windows environments. It's not installable through nimble yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants