Есть пример для Java (но с использованием java.awt sth) и C#; кажется, что получение имени из файла TTF не должно быть больно. Но, к сожалению, это не так. Пожалуйста помоги.Как получить имя шрифта из файла TTF для Android
1
A
ответ
0
Пожалуйста, используя класс:
class TTFAnalyzer
{
// This function parses the TTF file and returns the font name specified in the file
public String getTtfFontName(String fontFilename)
{
try
{
// Parses the TTF file format.
// See http://developer.apple.com/fonts/ttrefman/rm06/Chap6.html
m_file = new RandomAccessFile(fontFilename, "r");
// Read the version first
int version = readDword();
// The version must be either 'true' (0x74727565) or 0x00010000
if (version != 0x74727565 && version != 0x00010000)
return null;
// The TTF file consist of several sections called "tables", and we need to know how many of them are there.
int numTables = readWord();
// Skip the rest in the header
readWord(); // skip searchRange
readWord(); // skip entrySelector
readWord(); // skip rangeShift
// Now we can read the tables
for (int i = 0; i < numTables; i++)
{
// Read the table entry
int tag = readDword();
readDword(); // skip checksum
int offset = readDword();
int length = readDword();
// Now here' the trick. 'name' field actually contains the textual string name.
// So the 'name' string in characters equals to 0x6E616D65
if (tag == 0x6E616D65)
{
// Here's the name section. Read it completely into the allocated buffer
byte[] table = new byte[ length ];
m_file.seek(offset);
read(table);
// This is also a table. See http://developer.apple.com/fonts/ttrefman/rm06/Chap6name.html
// According to Table 36, the total number of table records is stored in the second word, at the offset 2.
// Getting the count and string offset - remembering it's big endian.
int count = getWord(table, 2);
int string_offset = getWord(table, 4);
// Record starts from offset 6
for (int record = 0; record < count; record++)
{
// Table 37 tells us that each record is 6 words -> 12 bytes, and that the nameID is 4th word so its offset is 6.
// We also need to account for the first 6 bytes of the header above (Table 36), so...
int nameid_offset = record * 12 + 6;
int platformID = getWord(table, nameid_offset);
int nameid_value = getWord(table, nameid_offset + 6);
// Table 42 lists the valid name Identifiers. We're interested in 4 but not in Unicode encoding (for simplicity).
// The encoding is stored as PlatformID and we're interested in Mac encoding
if (nameid_value == 4 && platformID == 1)
{
// We need the string offset and length, which are the word 6 and 5 respectively
int name_length = getWord(table, nameid_offset + 8);
int name_offset = getWord(table, nameid_offset + 10);
// The real name string offset is calculated by adding the string_offset
name_offset = name_offset + string_offset;
// Make sure it is inside the array
if (name_offset >= 0 && name_offset + name_length < table.length)
return new String(table, name_offset, name_length);
}
}
}
}
return null;
}
catch (FileNotFoundException e)
{
// Permissions?
return null;
}
catch (IOException e)
{
// Most likely a corrupted font file
return null;
}
}
// Font file; must be seekable
private RandomAccessFile m_file = null;
// Helper I/O functions
private int readByte() throws IOException
{
return m_file.read() & 0xFF;
}
private int readWord() throws IOException
{
int b1 = readByte();
int b2 = readByte();
return b1 << 8 | b2;
}
private int readDword() throws IOException
{
int b1 = readByte();
int b2 = readByte();
int b3 = readByte();
int b4 = readByte();
return b1 << 24 | b2 << 16 | b3 << 8 | b4;
}
private void read(byte [] array) throws IOException
{
if (m_file.read(array) != array.length)
throw new IOException();
}
// Helper
private int getWord(byte [] array, int offset)
{
int b1 = array[ offset ] & 0xFF;
int b2 = array[ offset + 1 ] & 0xFF;
return b1 << 8 | b2;
}
}
коды от http://www.ulduzsoft.com/2012/01/enumerating-the-fonts-on-android-platform/
отмечают, что это не работает для всех файлов TTF.
+1
Как вы читаете другие свойства файла TTF (фамилия, версия и т. Д.)? – gonzobrains
Смежные вопросы
- 1. Как изменить имя шрифта .ttf
- 2. Java - читать символы шрифта из файла ttf
- 3. получить имя шрифта из файла шрифта в C#
- 4. Добавление поддержки для обработки файла шрифта ttf
- 5. Определение имени файла шрифта TTF
- 6. Имя семейства шрифтов из файла шрифта
- 7. C# получить стиль шрифта от .ttf
- 8. Как получить данные шрифта ttf из системных шрифтов в java
- 9. Как найти версию шрифта из файла OTF, TTF
- 10. Как получить имя семейства шрифтов с учетом файла шрифта?
- 11. Как получить имя шрифта текста из PDF?
- 12. Восстановить имя файла шрифта
- 13. Использование шрифта из файла ttf в windows/direct3d
- 14. Растерирование шрифта TTF
- 15. Как получить порядок символов китайского символа из файла ttf?
- 16. Как получить имя файла из ImageView (ANDROID)
- 17. Редактирование файла шрифта .ttf для добавления отсутствующего символа
- 18. извлечь символ символа из файла ttf
- 19. Получить ширину одного символа из ttf-шрифта в php?
- 20. Как получить свойства шрифта из имени файла шрифта?
- 21. . Приоритет шрифта .ttf в Qt
- 22. Загрузка некоторых TrueType шрифтов из TTF-файла в Java вызывает FontFormatException: имя шрифта не найдено
- 23. Добавление шрифта ttf по умолчанию для всех проектов android
- 24. Получить имя файла шрифта на основе дескриптора шрифта (HFONT)
- 25. Как использовать значки из ttf-файла
- 26. Как включить файл шрифта (.ttf) в проект Android NDK?
- 27. UIWebView для использования шрифта клиента TTF
- 28. Как читать таблицу пар для кернинга из файла TTF в Android
- 29. Delphi - Получить имя шрифта .FON из указанного файла
- 30. Добавление шрифта TTF в phar для использования GD imagettftext
см. Этот ответ о том, как получить имя шрифта https://stackoverflow.com/a/32132243/908821 –