根据 rtf 规范,我们可以使用 \fontemb 和 \fontfile 控制字在 rtf 文件中嵌入字体。有人可以给我一个可行的例子吗?我希望 rtf 文件使用位于单独文件(即 .ttf 文件)中的字体
问问题
1025 次
1 回答
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 函数为您的设备上下文设置为当前字体。
于 2014-06-26T12:08:12.190 回答