In Android, I need to query a font installed on the system by providing either the family name or the postscript name, and retrieve the font's file path or the font buffer.
I’m looking for a solution in C, but if you know a solution in Java/Kotlin, I can find a way to translate it to C.
Initially, I tried using AFontMatcher_match, but it requires the family name to be one of the generic font families from the W3C specification, rather than using the family name or postscript name.
From what I understand, with a Typeface, I can use Typeface.create(family_name, style) to create a font. However, I'm unsure how to retrieve the font's file path or its buffer after creation.
Lastly, I came across the Font class, but it doesn’t seem to support creating a font from a family and/or postscript name.
In Android, I need to query a font installed on the system by providing either the family name or the postscript name, and retrieve the font's file path or the font buffer.
I’m looking for a solution in C, but if you know a solution in Java/Kotlin, I can find a way to translate it to C.
Initially, I tried using AFontMatcher_match, but it requires the family name to be one of the generic font families from the W3C specification, rather than using the family name or postscript name.
From what I understand, with a Typeface, I can use Typeface.create(family_name, style) to create a font. However, I'm unsure how to retrieve the font's file path or its buffer after creation.
Lastly, I came across the Font class, but it doesn’t seem to support creating a font from a family and/or postscript name.
Share Improve this question edited Feb 18 at 4:58 Steve Friedl 4,2471 gold badge29 silver badges33 bronze badges asked Feb 18 at 0:43 jeremie bergeronjeremie bergeron 5277 silver badges13 bronze badges 5 |1 Answer
Reset to default 0#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FONT_CONFIG_PATH "/system/etc/fonts.xml"
// Function to search for a font file in fonts.xml
void find_font_path(const char *font_name) {
FILE *file = fopen(FONT_CONFIG_PATH, "r");
if (!file) {
printf("Cannot open font config file\n");
return;
}
char line[1024];
while (fgets(line, sizeof(line), file)) {
if (strstr(line, font_name)) {
// Extract file path (Assuming XML format: <file>path/to/font.ttf</file>)
char *start = strstr(line, "<file>");
if (start) {
start += 6; // Move past <file>
char *end = strstr(start, "</file>");
if (end) {
*end = '\0';
printf("Font path: %s\n", start);
fclose(file);
return;
}
}
}
}
fclose(file);
printf("Font not found\n");
}
int main() {
find_font_path("Roboto-Regular.ttf");
return 0;
}
Once you have the file path, you can map the font into memory for direct access:
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
void *load_font_buffer(const char *font_path, size_t *size) {
int fd = open(font_path, O_RDONLY);
if (fd < 0) return NULL;
struct stat st;
if (fstat(fd, &st) < 0) {
close(fd);
return NULL;
}
*size = st.st_size;
void *buffer = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
return buffer;
}
int main() {
size_t size;
void *buffer = load_font_buffer("/system/fonts/Roboto-Regular.ttf", &size);
if (buffer) {
printf("Font loaded, size: %zu bytes\n", size);
munmap(buffer, size);
} else {
printf("Failed to load font\n");
}
return 0;
}
font installed on the system
- Android only allows for fonts inside of apps and those provided by system, see my answer to: Why has Android made it impossible to install system fonts?. Unclear if you've seen How to retrieve a list of available/installed fonts in android? – Morrison Chang Commented Feb 18 at 1:27