最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to query a font by its familypostscript name and get its filepathbuffer? - Stack Overflow

programmeradmin3浏览0评论

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
  • 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
  • I am NOT looking to install a font. The second link talk about how to get all the font installed on the system. The object in the list returned are Font. I explained in my message why I think i cannot use Font. It is because it doesn't allow me to query a font by it's family/postscript name. – jeremie bergeron Commented Feb 18 at 2:06
  • Thanks for the clarification. Have you seen Get the font name from the file from the asset folder As the fonts on Android are normally pre-installed system (so don't change) or the app developer (you know what meta data they have) I believe you'll have to provide your own code/library for parsing font files. – Morrison Chang Commented Feb 18 at 3:21
  • I don't want to parse the font in the asset folder. I want to query fonts installed on the system. font asset are not system font. Yes, I could use ASystemFontIterator to get all the font and parse them by myself, but this is highly inefficient. If I really need to do that, I will use fontconfig. – jeremie bergeron Commented 2 days ago
  • This question is similar to: How to retrieve a list of available/installed fonts in android?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Style-7 Commented 2 days ago
Add a comment  | 

1 Answer 1

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;
}
发布评论

评论列表(0)

  1. 暂无评论