c++pdflibharu

German text not proper on PDF created by Libharu C++


I have to write German text on a pdf created by Libharu. I assign German Text to a string variable (i.e. std::string TestString = "VariableGesamtlänge";) and then put that text to a pdf. My simple code is following:

        //-----UTF8 Encoding
        HPDF_UseUTFEncodings(pdf);
        HPDF_SetCurrentEncoder(pdf, "UTF-8"); 
        const char *fontname = HPDF_LoadTTFontFromFile(pdf, "FreeSans.ttf", HPDF_TRUE);
        HPDF_Font font = HPDF_GetFont(pdf, fontname, "UTF-8");
        HPDF_Page_SetFontAndSize(page, font, 24);

        std::string TestString = "VariableGesamtlänge";
        DrawText(page, font, TestString.c_str(), y);

Problem: I get two square boxes instead of ä. I am using VS2010.


Solution

  • 'ä' is not an ASCII character. It may be stored as a single character (in which case, which one?), or it may be stored as multiple characters (in which case, which ones?).

    You have told the HPDF functions that you are going to pass text around as UTF-8 (which is an entirely sensible choice). This means 'ä' is represented by 0xC3 0xA4.

    The source file is almost certainly encoded in 8-bit text, using (probably) code-page 1252. So 'ä' will be the single character 0xE4. You either need to tell the compiler to store strings as UTF-8, or it may be possible to re-encode the source files in UTF-8.

    Your final option is to store the text in a (UTF-8) file, and read it from there.