rust-fontconfig icon indicating copy to clipboard operation
rust-fontconfig copied to clipboard

C interface

Open farid-cs opened this issue 1 year ago • 1 comments

Does this library have C bindings?

farid-cs avatar Aug 10 '24 09:08 farid-cs

No, but it shouldn't be hard to add since the API is just a couple of functions.

I do wonder why people are interested in this library, I just wrote it as a one-off function in a weekend or so and it's less than 500 lines long.

fschutt avatar Aug 10 '24 14:08 fschutt

There's a C API now:

#include <stdio.h>
#include "rust_fontconfig.h"

int main() {
    // Build the font cache
    FcFontCache cache = fc_cache_build();
    if (!cache) {
        fprintf(stderr, "Failed to build font cache\n");
        return 1;
    }
    
    // Create a pattern to search for Arial
    FcPattern* pattern = fc_pattern_new();
    fc_pattern_set_name(pattern, "Arial");
    
    // Search for the font
    FcTraceMsg* trace = NULL;
    size_t trace_count = 0;
    FcFontMatch* match = fc_cache_query(cache, pattern, &trace, &trace_count);
    
    if (match) {
        char id_str[40];
        fc_font_id_to_string(&match->id, id_str, sizeof(id_str));
        printf("Found font! ID: %s\n", id_str);
        
        // Get the font path
        FcFontPath* font_path = fc_cache_get_font_path(cache, &match->id);
        if (font_path) {
            printf("Font path: %s (index: %zu)\n", font_path->path, font_path->font_index);
            fc_font_path_free(font_path);
        }
        
        fc_font_match_free(match);
    } else {
        printf("Font not found\n");
    }
    
    // Clean up
    fc_pattern_free(pattern);
    if (trace) fc_trace_free(trace, trace_count);
    fc_cache_free(cache);
    
    return 0;
}

fschutt avatar Mar 13 '25 19:03 fschutt