wstring get_ttf_name(const char *fn)
{
TT_OFFSET_TABLE hdr;
TT_TABLE_DIRECTORY tbl_dir;
TT_NAME_TABLE_HEADER tbl_hdr;
TT_NAME_RECORD name_rec;
ifstream is(fn, ios::binary);
is.read((char*)&hdr, sizeof(hdr));
if(SWAPWORD(hdr.uMajorVersion) != 1 || SWAPWORD(hdr.uMinorVersion) != 0)return L"";
hdr.uNumOfTables = SWAPWORD(hdr.uNumOfTables);
while(hdr.uNumOfTables-- && is.read((char*)&tbl_dir, sizeof(tbl_dir)))
{
if(memcmp("name", tbl_dir.szTag, 4))continue;
is.seekg(SWAPLONG(tbl_dir.uOffset), ios::beg);
is.read((char*)&tbl_hdr, sizeof(tbl_hdr));
tbl_hdr.uNRCount = SWAPWORD(tbl_hdr.uNRCount);
while (tbl_hdr.uNRCount-- && is.read((char*)&name_rec, sizeof(name_rec)))
{
if(SWAPWORD(name_rec.uNameID) != 1) continue;
size_t old_pos = is.tellg();
size_t str_offset = SWAPLONG(tbl_dir.uOffset);
str_offset += SWAPWORD(tbl_hdr.uStorageOffset);
str_offset += SWAPWORD(name_rec.uStringOffset);
is.seekg(str_offset, ios::beg);
name_rec.uStringLength = SWAPWORD(name_rec.uStringLength);
if(SWAPWORD(name_rec.uPlatformID) == 0)
{
wchar_t c;
wstring str;
while(name_rec.uStringLength && is.read((char*)&c, 2))
{
str.push_back(SWAPWORD(c));
name_rec.uStringLength -= 2;
}
return str;
}
/*
tę część dałem dla zasady, bo nie wiem, czy zawsze
występuje uPlatformID równe 0.
*/
if(SWAPWORD(name_rec.uPlatformID) == 1)
{
char c;
wstring str;
locale loc(""); //<--- trochę fuszerka, ale...
const ctype<wchar_t> &ct = use_facet< ctype<wchar_t> >(loc);
while(name_rec.uStringLength-- && is.read(&c, 1))
{
str.push_back(ct.widen(c));
}
return str;
}
is.seekg(old_pos, ios::beg);
}
break;
}
return L"";
}