DynaPDFMBS - cannot get Chinese into tables

I have been trying to get Chinese characters into a table in DynaPDF, but fail every time.

The text is like this on screen:

I’ve tried endless variations on this code:

call table.SetFont(rownum, cx, theapp.pdffontname, pdf.kfsNormal, true, pdf.kcpUnicode)


At the point that I use .SetCellText, the variable contain:

And the result in the PDF:

Well, did you load character maps and set a font with Chinese characters?

e.g.

  Var pdf as DynaPDFMBS // your dynapdf instance
  call pdf.SetCMapDir SpecialFolder.Desktop.Child("CMAP"), pdf.klcmRecursive

For example a font like Helvetica or Arial Unicode MS should contain Chinese characters.

Chinese works in the Xojo app, and I dont need to do that with (for example) French.

Is there some documentation about this you can point to? Totally news to me..

Well, CMAPs are only a thing for CJK encodings (Chinese, Japanese and Korean).
You don’t need them for other languages like French.

The dynapdf_help.pdf file has plenty of documentation about them.

Basically whenever you do something with CJK languages, you need the character maps to map characters in font files to the unicode character.

sadly, no improvement. I have 169 CMAP files but it seems none of them do the trick.

So when I do this:

// set font
call pdf.SetFont "Helvetica", 0, 12.0, true, pdf.kcpUnicode
dim y as Double = 50

text = "你好,世界"
call pdf.WriteFTextEx(50, y, 200, -1, pdf.ktaLeft, text)

I get an error that a few glyphs are not in the Helvetica font.

Then I switch to Arial Unicode and it works:

// set font
call pdf.SetFont "Arial Unicode MS", 0, 12.0, true, pdf.kcpUnicode
dim y as Double = 50

text = "你好,世界"
call pdf.WriteFTextEx(50, y, 200, -1, pdf.ktaLeft, text)

So what font name do you try?

1 Like

So far, Arial, Times, Times New Roman, Helvetica, Courier, and Courier New

I’ll give Unicode MS a try.

Have you enabled complex text layout in DynaPDF?

No. Another term that is new to me.

Then I switch to Arial Unicode and it works:

I dont seem to have that available.

You need to use a font that has support for those glyphs. Your average font won’t - Times New Roman, for example, is explicitly a font for Latin scripts and won’t have any support for Asian scripts. The fonts you should be looking at will often say Unicode in their names: Arial Unicode is probably the most widely distributed one although you’ll also find Lucida Grande Unicode on the Mac, as it was the universal system font a few OS versions back and needed to support a very wide character set.

If you are looking for candidate fonts that have very wide glyph coverage for many languages, sort the list on this page by Character Count in descending order:

1 Like

Could be tricky. I have until now been setting ‘select font using famiiy name’ as that did solve a previous issue I was having.

(turning it off didn’t make a difference, but now I am tweaking several things at once, which is always a bad way to debug)

What I obviously don’t want to do is to break things for 99% of my customer base, for the sake of (probably) 4 people in Asia.

Testing continues.

When you draw text in Xojo, the operating system text APIs make sure that the font you have chosen contains the glyphs required to draw the text. If any of the glyphs are missing the operating system automatically switches to the required font(s). This is called font fallback.

Unfortunately, PDF doesn’t support font fallback which means you are responsible for making sure that the font you have chosen contains the glyphs.

It should be possible to write your own basic font fallback function by testing each Unicode code point in your text using the DynaPDF GetGlyphIndex function and if a glyph doesn’t exist, you then start checking other fonts for it. The obvious question is which fonts you prioritise rather than testing each font. One solution is to analyse your fonts to see which Unicode script ranges they support (tables are available on the Unicode site), determine which script your offending character is in using the same tables and then prioritise the fonts with matching scripts.

Luckily, DynaPDF has built their own font fallback solution. If you check the Complex Text / Font Substitution sections of their documentation it should tell you everything you need.

It is worth pointing out that the fonts chosen will more than likely be different to what the operating system chooses so if you are trying to 100% match the styling / layout of text on-screen then you will probably be out of luck.

1 Like

Addendum: Arial Unicode MS is no longer shipped with Windows, (and seems to costs money if you want to use it.)

There is a free alternative - Noto Sans TC Thin - available from Google fonts, which works well for me, on Mac and Windows.

1 Like