RTF中的嵌入字体

时间:2014-02-24 06:29:31

标签: ms-word rtf font-embedding wordperfect

根据rtf规范,我们可以使用\ fontemb和\ fontfile控制字在rtf文件中嵌入字体。有人可以给我一个有效的例子吗?我希望rtf文件使用位于单独文件中的字体(即.ttf文件)

1 个答案:

答案 0 :(得分:1)

您应该使用TTEmbedFont函数来创建嵌入字体数据。 http://msdn.microsoft.com/en-us/library/windows/desktop/dd145145(v=vs.85).aspx

喜欢这个。

    //WRITEEMBEDPROC
    unsigned long WriteEmbedProc(void *lpvWriteStream, const void *lpvBuffer, const unsigned long cbBuffer)
    {
        BYTE *rgByte = new BYTE[cbBuffer];
        memcpy(rgByte, lpvBuffer, cbBuffer);

        //stream to store your font information
        std::ofstream *ofs = static_cast<std::ofstream*>(lpvWriteStream);

        //convert binary data to hexadeciaml, that rtf uses
        std::string byte_string = BinToHex(rgByte, cbBuffer);

        //Write formated data to your file (stream)
        for (int i = 0; i < byte_string.size(); ++i)
        {   
            *ofs << byte_string[i];
            if((i + 1) % 128 == 0)
            {
                *ofs << "\n";
            }
        }

        delete rgByte;
        return cbBuffer;
    }

   void EmbedFontWrap(HDC hdc)
   {
    ULONG ulPrivStatus = 0;
    ULONG ulStatus = 0;
    std::ofstream *lpvWriteStream = new std::ofstream("D:\\out.txt", std::ios::binary);
    USHORT *pusCharCodeSet;
    USHORT usCharCodeCount;
    USHORT usLanguage;
    LONG ret = TTEmbedFont(
        hdc,
        TTEMBED_RAW | TTEMBED_EMBEDEUDC,
        CHARSET_UNICODE,
        &ulPrivStatus,
        &ulStatus,
        WriteEmbedProc,
        lpvWriteStream,
        nullptr,
        0,
        0,
        nullptr);
    lpvWriteStream->close();
    delete lpvWriteStream;
}

要嵌入的字体应通过SelectObject函数设置为当前设备上下文。