TrueType fonts are used in Windows platform, but there are still few Chinese materials involved in the specific operational level, and many problems have plagued them for a while.
1, font selection through CFontDialog, but normally the resulting font column represents all the supported fonts in the current system, including, of course, some other non-TrueType fonts, to remove non-TrueType fonts in the CFontDialog list is simple, The following settings are required when configuring CFontDialog:
CFontDialog dlg;
dlg.m_cf.Flags |= CF_TTONLY; //only enum TrueType
2. To traverse all the characters contained in the selected font, we can't help thinking that we can do it through the parsing of the. ttf file, but how to get the corresponding. ttf file for the selected font. With the actual operation, it is learned that the name of the font displayed in CFontDialog and the corresponding. ttf file is not a one by one correspondence, for example: The selected font is Arial then there is no Arial.ttf file under system/fonts/, On the internet to find a lot of examples can not be a full realization of the codeproject, there is an example of sample through the registry method to find, but also exist above the problem. And even if you can find the corresponding. ttf file, it is difficult to parse the file because of the TTC problem (i.e., one is a TTF collection, a file might define several TrueType fonts), This also depends on the font selected and then in the process of reading the TTF file, find the part that describes this font, and later discover that the selected font may be inconsistent with the name of the TrueType font defined in the document because there is a Chinese font, which I have not tested. Because in the subsequent resolution process found another way to solve.
DWORD CDC::GetFontData( DWORD dwTable, DWORD dwOffset, LPVOID lpData, DWORD cbData ) const;
Remarks
Retrieves font-metric information from a scalable font file. The information to retrieve is identified
by specifying an offset into the font file and the length of the information to return. An application
can sometimes use the GetFontData member function to save a TrueType font with a document. To do this,
the application determines whether the font can be embedded and then retrieves the entire font file,
specifying 0 for the dwTable, dwOffset, and cbData parameters.
The above function makes it easy to get the data from the TTF file of the TrueType fonts selected in the DC, but if you want to get the entire font file there is a problem with TTC, because the data in this function is the type of font that you choose. But the definition of offset for each data segment is for the entire document. If there is a problem with the direct reference, and no other better solution, but can accurately get the data about any one of the table, just good for characters contained in character set in the "CMap" Table, the problem is resolved as follows:
// Macro to pack a TrueType table name into a DWORD.
#define MAKETABLENAME(ch1, ch2, ch3, ch4) (\
(((DWORD)(ch4)) << 24) | \
(((DWORD)(ch3)) << 16) | \
(((DWORD)(ch2)) << 8) | \
((DWORD)(ch1))
)
DWORD tag = MAKETABLENAME( ''c'',''m'',''a'',''p'' );
DWORD size = m_pFontDC->GetFontData(tag,0, NULL, 0);
unsigned char *pBuffer = new unsigned char[size];
m_pFontDC->GetFontData(tag,0,pBuffer,size);
//do something with pBuffer
delete[] pBuffer;