2014-02-24 72 views
0

根據rtf規範,我們可以使用\ fontemb和\ fontfile控制字將字體嵌入到rtf文件中。有人能給我一個這樣的工作例子嗎?我希望rtf文件使用位於單獨文件(即.ttf文件)中的字體。RTF中的嵌入字體

回答

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函數你的設備上下文。