galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

Arabic text issue or am I missing something #326

Closed MarcoMartins86 closed 5 years ago

MarcoMartins86 commented 5 years ago

So I have used your example on how to write UTF8 strings using glyphs to a PDF page and it seems to work for most of the cases I have tested (chinese, korean, tailandese, japanese, russian, etc) until I tested with arabic strings, not sure if I am missing something but I feed the code with "مرحبا بالعالم"(Hello World) and get "ملاعلاب ابحرم" in the pdf, which, have some bytes switched. Any clue? This is the code I have: char text[]={'\xd9','\x85','\xd8','\xb1','\xd8','\xad','\xd8','\xa8','\xd8','\xa7','\x20','\xd8','\xa8','\xd8','\xa7','\xd9','\x84','\xd8','\xb9','\xd8','\xa7','\xd9','\x84','\xd9','\x85','\x0'}; PDFWriter pdfWriter; pdfWriter.StartPDF("C:\\SimpleTextUsage.PDF", ePDFVersion13); PDFPage* page = new PDFPage(); page->SetMediaBox(PDFRectangle(0, 0, 595, 842)); PageContentContext* contentContext = pdfWriter.StartPageContentContext(page); PDFUsedFont* font = pdfWriter.GetFontForFile("C:\\windows\\fonts\\unifont-11.0.02.ttf"); contentContext->BT(); contentContext->k(0, 0, 0, 1); contentContext->Tf(font, 14); contentContext->Tm(1, 0, 0, 1, 10, 100); GlyphUnicodeMappingList glyphsList; font->TranslateStringToGlyphs(text, glyphsList); contentContext->Tj(glyphsList); contentContext->ET(); pdfWriter.EndPageContentContext(contentContext); pdfWriter.WritePageAndRelease(page); pdfWriter.EndPDF();

galkahana commented 5 years ago

If i'm reading the text correctly, then it's reversed. This happens because in PDF texts are written always from left to right. so you can either reverse the text, or place the characters individually from right to left.

MarcoMartins86 commented 5 years ago

That was it, don't know why I didn't think of that first. Just called glyphsList.reverse(); before writing them. Thank you for your exceptional work on this library.