]> O.S.I.I.S - jp/vkvg.git/commitdiff
implement vkvg_load_font_from_memory
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 23 Feb 2022 15:16:31 +0000 (16:16 +0100)
committerj-p <jp_bruyere@hotmail.com>
Sat, 26 Feb 2022 10:05:24 +0000 (11:05 +0100)
include/vkvg.h
src/vkvg_context.c
src/vkvg_fonts.c
src/vkvg_fonts.h

index de3dcd9229d0933cdaa0801369cfd9f2006fcae7..a06897666967d9be65036ec200497efa0b47e90f 100644 (file)
@@ -1497,6 +1497,16 @@ void vkvg_select_font_face (VkvgContext ctx, const char* name);
  */
 vkvg_public
 void vkvg_load_font_from_path (VkvgContext ctx, const char* path, const char *name);
+/**
+ * @brief Select a new font by providing a pointer on the font file loaded in memory and its size in byte.
+ *
+ * @param ctx a valid vkvg @ref context
+ * @param fontBuffer a pointer to a raw font file loaded in memory.
+ * @param fontBufferByteSize the size of the font buffer in bytes.
+ * @param name A short name to select this font afteward
+ */
+vkvg_public
+void vkvg_load_font_from_memory (VkvgContext ctx, unsigned char* fontBuffer, long fontBufferByteSize, const char* name);
 /**
  * @brief
  *
index 868cc6f08b63320a92a86f771fdd64119130c984..a1e5f0839c6c113db2e1137574d8cbea1cbb5864 100644 (file)
@@ -1109,7 +1109,18 @@ void vkvg_load_font_from_path (VkvgContext ctx, const char* path, const char* na
        if (ctx->status)
                return;
        RECORD(ctx, VKVG_CMD_SET_FONT_PATH, name);
-       _font_cache_add_font_identity(ctx, path, name);
+       _vkvg_font_identity_t* fid = _font_cache_add_font_identity(ctx, path, name);
+       _font_cache_load_font_file_in_memory (fid);
+       _select_font_face (ctx, name);
+}
+void vkvg_load_font_from_memory (VkvgContext ctx, unsigned char* fontBuffer, long fontBufferByteSize, const char* name) {
+       if (ctx->status)
+               return;
+       //RECORD(ctx, VKVG_CMD_SET_FONT_PATH, name);
+       _vkvg_font_identity_t* fid = _font_cache_add_font_identity (ctx, NULL, name);
+       fid->fontBuffer = fontBuffer;
+       fid->fontBufSize = fontBufferByteSize;
+
        _select_font_face (ctx, name);
 }
 void vkvg_set_font_size (VkvgContext ctx, uint32_t size){
index 1847d992d558266914742857427841e54ad0e9fd..ea2c4898f4a5bd7902afb844bb36e556477492df 100644 (file)
@@ -378,7 +378,16 @@ void _font_add_name (_vkvg_font_identity_t* font, const char* name, int nameLeng
        font->names[font->namesCount-1] = (char*)calloc(nameLength, sizeof (char));
        strcpy (font->names[font->namesCount-1], name);
 }
-void _font_cache_add_font_identity (VkvgContext ctx, const char* fontFilePath, const char* name){
+void _font_cache_load_font_file_in_memory (_vkvg_font_identity_t* fontId) {
+       FILE* fontFile = fopen(fontId->fontFile, "rb");
+       fseek(fontFile, 0, SEEK_END);
+       fontId->fontBufSize = ftell(fontFile); /* how long is the file ? */
+       fseek(fontFile, 0, SEEK_SET); /* reset */
+       fontId->fontBuffer = malloc(fontId->fontBufSize);
+       fread(fontId->fontBuffer, fontId->fontBufSize, 1, fontFile);
+       fclose(fontFile);
+}
+_vkvg_font_identity_t* _font_cache_add_font_identity (VkvgContext ctx, const char* fontFilePath, const char* name){
        _font_cache_t*  cache = (_font_cache_t*)ctx->dev->fontCache;
        if (++cache->fontsCount == 1)
                cache->fonts = (_vkvg_font_identity_t*) malloc (cache->fontsCount * sizeof(_vkvg_font_identity_t));
@@ -386,20 +395,16 @@ void _font_cache_add_font_identity (VkvgContext ctx, const char* fontFilePath, c
                cache->fonts = (_vkvg_font_identity_t*) realloc (cache->fonts, cache->fontsCount * sizeof(_vkvg_font_identity_t));
        _vkvg_font_identity_t nf = {0};
 
-       int fflength = strlen (fontFilePath) + 1;
-       nf.fontFile = (char*)malloc (fflength * sizeof(char));
-       strcpy (nf.fontFile, fontFilePath);
-       _font_add_name (&nf, name, fflength);
+       if (fontFilePath) {
+               int fflength = strlen (fontFilePath) + 1;
+               nf.fontFile = (char*)malloc (fflength * sizeof(char));
+               strcpy (nf.fontFile, fontFilePath);
+       }
 
-       FILE* fontFile = fopen(nf.fontFile, "rb");
-       fseek(fontFile, 0, SEEK_END);
-       nf.fontBufSize = ftell(fontFile); /* how long is the file ? */
-       fseek(fontFile, 0, SEEK_SET); /* reset */
-       nf.fontBuffer = malloc(nf.fontBufSize);
-       fread(nf.fontBuffer, nf.fontBufSize, 1, fontFile);
-       fclose(fontFile);
+       _font_add_name (&nf, name, strlen (name));
 
        cache->fonts[cache->fontsCount-1] = nf;
+       return &cache->fonts[cache->fontsCount-1];
 }
 //select current font for context
 _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx) {
@@ -480,7 +485,7 @@ bool _tryResolveFontNameWithFontConfig (VkvgContext ctx, _vkvg_font_identity_t**
        if (fontFile) {
                //try find font in cache by path
                for (int i = 0; i < cache->fontsCount; ++i) {
-                       if (strcmp (cache->fonts[i].fontFile, fontFile) == 0) {
+                       if (cache->fonts[i].fontFile && strcmp (cache->fonts[i].fontFile, fontFile) == 0) {
                                int fflength = strlen(fontFile) + 1;
                                _font_add_name (&cache->fonts[i], ctx->selectedFontName, fflength);
                                *resolvedFont = &cache->fonts[i];
@@ -489,7 +494,8 @@ bool _tryResolveFontNameWithFontConfig (VkvgContext ctx, _vkvg_font_identity_t**
                }
                if (!*resolvedFont) {
                        //if not found, create a new vkvg_font
-                       _font_cache_add_font_identity(ctx, fontFile, ctx->selectedFontName);
+                       _vkvg_font_identity_t* fid = _font_cache_add_font_identity(ctx, fontFile, ctx->selectedFontName);
+                       _font_cache_load_font_file_in_memory (fid);
                        *resolvedFont = &cache->fonts[cache->fontsCount-1];
                }
        }
index 8b45950a197671f0d506d15b6509584059490c67..2b6284879a538f99abbfa16caae7a735cd76f27a 100644 (file)
@@ -189,7 +189,8 @@ typedef struct _vkvg_text_run_t {
 void _fonts_cache_create               (VkvgDevice dev);
 //Release all ressources of font cache.
 void _font_cache_destroy       (VkvgDevice dev);
-void _font_cache_add_font_identity     (VkvgContext ctx, const char* fontFile, const char *name);
+_vkvg_font_identity_t *_font_cache_add_font_identity (VkvgContext ctx, const char* fontFile, const char *name);
+void _font_cache_load_font_file_in_memory (_vkvg_font_identity_t* fontId);
 //Draw text
 void _font_cache_show_text                             (VkvgContext ctx, const char* text);
 //Get text dimmensions