问题
According to the rtf specs, we can embed a font in an rtf file using the \fontemb and \fontfile control words. Can someone give me a working example of that? I'd like the rtf file to use the font that's located in a separate file (i.e. .ttf file)
回答1:
You should use TTEmbedFont function to create embedded font data. http://msdn.microsoft.com/en-us/library/windows/desktop/dd145145(v=vs.85).aspx
Like this.
//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;
}
Font you want to embed should be set as current for you device context by SelectObject function.
来源:https://stackoverflow.com/questions/21980432/embedded-fonts-in-rtf