]> O.S.I.I.S - jp/vkvg.git/commitdiff
add vkvg_text_run_create2, should find a better name
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 22 Feb 2022 21:10:56 +0000 (22:10 +0100)
committerj-p <jp_bruyere@hotmail.com>
Sat, 26 Feb 2022 10:04:31 +0000 (11:04 +0100)
src/vkvg_context.c
src/vkvg_fonts.c
src/vkvg_fonts.h

index bab639a7896f85a23ca0f94fbe3a2595a4d74681..f000bf15e970f0702733fc7ec22f029265d2bbc1 100644 (file)
@@ -1146,7 +1146,14 @@ VkvgText vkvg_text_run_create (VkvgContext ctx, const char* text) {
        if (ctx->status)
                return NULL;
        VkvgText tr = (vkvg_text_run_t*)calloc(1, sizeof(vkvg_text_run_t));
-       _font_cache_create_text_run(ctx, text, tr);
+       _font_cache_create_text_run(ctx, text, -1, tr);
+       return tr;
+}
+VkvgText vkvg_text_run_create2 (VkvgContext ctx, const char* text, uint32_t length) {
+       if (ctx->status)
+               return NULL;
+       VkvgText tr = (vkvg_text_run_t*)calloc(1, sizeof(vkvg_text_run_t));
+       _font_cache_create_text_run(ctx, text, length, tr);
        return tr;
 }
 void vkvg_text_run_destroy (VkvgText textRun) {
@@ -1165,7 +1172,7 @@ void vkvg_text_run_get_extents (VkvgText textRun, vkvg_text_extents_t* extents)
 void vkvg_text_extents (VkvgContext ctx, const char* text, vkvg_text_extents_t* extents) {
        if (ctx->status)
                return;
-       _font_cache_text_extents(ctx, text, extents);
+       _font_cache_text_extents(ctx, text, -1, extents);
 }
 void vkvg_font_extents (VkvgContext ctx, vkvg_font_extents_t* extents) {
        if (ctx->status)
index e9353a54e3250708f1ba164ecab996832b40b278..736e5e9e4b3d9da28ad5cf78416f89a4ded12d14 100644 (file)
@@ -532,19 +532,19 @@ void _update_current_font (VkvgContext ctx) {
 
 #ifdef VKVG_USE_HARFBUZZ
 //Get harfBuzz buffer for provided text.
-hb_buffer_t * _get_hb_buffer (_vkvg_font_t* font, const char* text) {
+hb_buffer_t * _get_hb_buffer (_vkvg_font_t* font, const char* text, int length) {
        hb_buffer_t *buf = hb_buffer_create();
 
        const char *lng  = "fr";
        hb_script_t script = HB_SCRIPT_LATIN;
-       script = hb_script_from_string (text, (int)strlen (text));
+       script = hb_script_from_string (text, length);
 
        hb_direction_t dir = hb_script_get_horizontal_direction(script);
        //dir = HB_DIRECTION_TTB;
        hb_buffer_set_direction (buf, dir);
        hb_buffer_set_script    (buf, script);
        hb_buffer_set_language  (buf, hb_language_from_string (lng, (int)strlen(lng)));
-       hb_buffer_add_utf8              (buf, text, (int)strlen(text), 0, (int)strlen(text));
+       hb_buffer_add_utf8              (buf, text, length, 0, length);
 
        hb_shape (font->hb_font, buf, NULL, 0);
        return buf;
@@ -578,14 +578,14 @@ void _font_cache_font_extents (VkvgContext ctx, vkvg_font_extents_t *extents) {
 #endif
 }
 //compute text extends for provided string.
-void _font_cache_text_extents (VkvgContext ctx, const char* text, vkvg_text_extents_t *extents) {
+void _font_cache_text_extents (VkvgContext ctx, const char* text, int length, vkvg_text_extents_t *extents) {
        if (text == NULL) {
                memset(extents, 0, sizeof(vkvg_text_extents_t));
                return;
        }
 
        vkvg_text_run_t tr = {0};
-       _font_cache_create_text_run (ctx, text, &tr);
+       _font_cache_create_text_run (ctx, text, length, &tr);
 
        if (ctx->status)
                return;
@@ -594,7 +594,7 @@ void _font_cache_text_extents (VkvgContext ctx, const char* text, vkvg_text_exte
 
        _font_cache_destroy_text_run (&tr);
 }
-void _font_cache_create_text_run (VkvgContext ctx, const char* text, VkvgText textRun) {
+void _font_cache_create_text_run (VkvgContext ctx, const char* text, int length, VkvgText textRun) {
 
        _update_current_font (ctx);
 
@@ -608,13 +608,17 @@ void _font_cache_create_text_run (VkvgContext ctx, const char* text, VkvgText te
        LOCK_FONTCACHE (ctx->dev)
 
 #ifdef VKVG_USE_HARFBUZZ
-       textRun->hbBuf = _get_hb_buffer (ctx->currentFontSize, text);
+       textRun->hbBuf = _get_hb_buffer (ctx->currentFontSize, text,  length);
        textRun->glyphs = hb_buffer_get_glyph_positions  (textRun->hbBuf, &textRun->glyph_count);
 #else
        int textByteLength = strlen (text);
        if (textByteLength > 0) {
                setlocale(LC_ALL, "");//TODO:move this elsewhere
-               size_t wsize = mbstowcs(NULL, text, 0);
+               size_t wsize;
+               if (length < 0)
+                       wsize = mbstowcs(NULL, text, 0);
+               else
+                       wsize = (size_t)length;
                wchar_t *tmp = (wchar_t*)malloc((wsize+1) * sizeof (wchar_t));
                textRun->glyph_count = mbstowcs (tmp, text, wsize);
                textRun->glyphs = (vkvg_glyph_info_t*)malloc(textRun->glyph_count * sizeof (vkvg_glyph_info_t));
@@ -758,7 +762,7 @@ void _font_cache_show_text_run (VkvgContext ctx, VkvgText tr) {
 void _font_cache_show_text (VkvgContext ctx, const char* text){
 
        vkvg_text_run_t tr = {0};
-       _font_cache_create_text_run (ctx, text, &tr);
+       _font_cache_create_text_run (ctx, text, -1, &tr);
 
        if (ctx->status)
                return;
index 33cdccd1a7d87f07ef803bfabd3e98d8a2c5da7c..d0a6dacab93d1928f12f8e6898eaca5fcb449ec9 100644 (file)
@@ -192,11 +192,11 @@ void _font_cache_add_font_identity        (VkvgContext ctx, const char* fontFile, const
 //Draw text
 void _font_cache_show_text                             (VkvgContext ctx, const char* text);
 //Get text dimmensions
-void _font_cache_text_extents                  (VkvgContext ctx, const char* text, vkvg_text_extents_t *extents);
+void _font_cache_text_extents                  (VkvgContext ctx, const char* text, int length, vkvg_text_extents_t *extents);
 //Get font global dimmensions
 void _font_cache_font_extents                  (VkvgContext ctx, vkvg_font_extents_t* extents);
 //Create text object that could be drawn multiple times minimizing harfbuzz and compute processing.
-void _font_cache_create_text_run               (VkvgContext ctx, const char* text, VkvgText textRun);
+void _font_cache_create_text_run               (VkvgContext ctx, const char* text, int length, VkvgText textRun);
 //Release ressources held by a text run.
 void _font_cache_destroy_text_run              (VkvgText textRun);
 //Draw text run