رفتن به محتوا
بازگشت به وبلاگ
بهترین روش‌ها
14 آبان 1405
GuruPDF Team
زمان مطالعه: 8 دقیقه

What it takes to put Hindi into a PDF correctly

Shaping is only half the job. How to draw Devanagari in a PDF so it renders right, and still copies and searches.

Getting Hindi to look right in a PDF is hard. Getting it to look right and also copy, search and select correctly in every reader is much harder, because those are different problems that pull against each other.

Here is what we learned building this into a browser-based PDF editor.

Why you can't just draw the letters

Devanagari is a complex script. The stored text and the shapes on the page differ in order and in number.

  • The vowel sign in कि is typed after क but drawn before it.
  • क्ष is three code points that become one glyph.
  • Marks shift position depending on what sits beside them.

A library that maps each character to a glyph and advances the pen produces text that any Hindi reader sees as wrong. You need a shaper, which applies the font's GSUB rules (which glyphs) and GPOS rules (where they go).

Common JavaScript PDF libraries don't do this. pdf-lib keeps the glyph ids that its layout engine gives it, but it drops GPOS positions and writes ToUnicode in visual order. So marks land in the wrong place, and a copied word comes out scrambled.

Test the shaper against a reference

We used fontkit for shaping and found its Indic shaper produced wrong results on thousands of our test words. So we compare against HarfBuzz, the de facto reference: shape a corpus with both and require no differences. A reference file built from different font bytes is refused, so the test can't quietly compare two different things.

If you shape text yourself, keep a check like this. Your eyes will not catch a mark that is off by a few units, and a native reader will.

Keeping the text layer honest

Once glyphs are in place, the PDF has to say what text they represent. This is where it gets interesting, because glyphs and characters no longer match one to one, and readers rebuild text differently.

We tested four: xpdf, MuPDF, PDFium and pdf.js. Each of these rules exists because one of them failed without it:

  • Every glyph-and-text pair gets its own CID. The same glyph can stand for different text in different contexts, so a shared code can't be right in both.
  • Spacing glyphs carry the word in drawing order, one character per visible glyph, and leftover characters ride on invisible zero-width glyphs.
  • Marks are drawn as outlines in form XObjects, so they don't add noise to the text layer.
  • Each glyph's width runs to the next text glyph. MuPDF breaks words at gaps of about 0.15 em.
  • The word and its trailing space sit in one ActualText span. Without it, MuPDF doubled word endings and PDFium dropped a lone space after a mark.

ActualText is only safe here because these scripts run left to right. Don't use it on right-to-left text, as some viewers would copy the reversed letters.

The line-start trap

A line's text must begin on its first glyph, which for कि is the vowel sign. That keeps selection and cover boxes aligned to the ink. But PDFium merges a text object into the line above when the baselines are closer than half the object's first character width. A wide first character caused two stacked lines of text to mix. Keeping the first character under 1 em solved it.

This is the kind of bug that no unit test finds by accident. You only see it when you build a page like a real user would.

Performance is part of correctness

Our first fixes copied the glyph buffer once per lookup. That was fine on a sentence and terrible on 20,000 unspaced Thai characters. Shaping has to stay linear, and any cache has to be bounded, or typing in a long box gets slower with each keystroke.

What we would tell anyone doing this

  1. Compare against HarfBuzz, and don't trust a shaper by reading its output.
  2. Treat "looks right" and "copies right" as separate requirements.
  3. Test more than one reader, and write down which reader forced which rule.
  4. Keep the fixes scoped: text with no complex script should produce byte-identical output to before.

You can try the result in our PDF editor, or read the Hindi guide we wrote for readers.