diff options
author | Grigori Goronzy <greg@blackbox> | 2011-07-15 00:49:50 +0200 |
---|---|---|
committer | Grigori Goronzy <greg@blackbox> | 2011-07-15 15:00:14 +0200 |
commit | dd340a43024966d284f73cce0db9aa20e78cdbdd (patch) | |
tree | c42f55785e7efa7c665caf3a8f9da736e5b4a960 /libass/ass_shaper.c | |
parent | fa291a3269ee0fa76a17c976439a900b29084314 (diff) | |
download | libass-dd340a43024966d284f73cce0db9aa20e78cdbdd.tar.bz2 libass-dd340a43024966d284f73cce0db9aa20e78cdbdd.tar.xz |
Cache HarfBuzz fonts
Add a new opaque data structure to store shaper-specific font data in
an ASS_Font. At the moment, this is used for caching HarfBuzz fonts,
to reduce allocation and font initialization overhead.
Diffstat (limited to 'libass/ass_shaper.c')
-rw-r--r-- | libass/ass_shaper.c | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/libass/ass_shaper.c b/libass/ass_shaper.c index 9a4e8d54..55bcaae7 100644 --- a/libass/ass_shaper.c +++ b/libass/ass_shaper.c @@ -46,6 +46,10 @@ struct ass_shaper { hb_feature_t *features; }; +struct ass_shaper_font_data { + hb_font_t *fonts[ASS_FONT_MAX_FACES]; +}; + /** * \brief Print version information */ @@ -114,6 +118,15 @@ void ass_shaper_free(ASS_Shaper *shaper) free(shaper); } +void ass_shaper_font_data_free(ASS_ShaperFontData *priv) +{ + int i; + for (i = 0; i < ASS_FONT_MAX_FACES; i++) + if (priv->fonts[i]) + hb_font_destroy(priv->fonts[i]); + free(priv); +} + /** * \brief Set features depending on properties of the run */ @@ -127,6 +140,28 @@ static void set_run_features(ASS_Shaper *shaper, GlyphInfo *info) } /** + * \brief Retrieve HarfBuzz font from cache. + * Create it from FreeType font, if needed. + * \param info glyph cluster + * \return HarfBuzz font + */ +static hb_font_t *get_hb_font(GlyphInfo *info) +{ + ASS_Font *font = info->font; + hb_font_t **hb_fonts; + + if (!font->shaper_priv) + font->shaper_priv = calloc(sizeof(ASS_ShaperFontData), 1); + + hb_fonts = font->shaper_priv->fonts; + if (!hb_fonts[info->face_index]) + hb_fonts[info->face_index] = + hb_ft_font_create(font->faces[info->face_index], NULL); + + return hb_fonts[info->face_index]; +} + +/** * \brief Shape event text with HarfBuzz. Full OpenType shaping. * \param glyphs glyph clusters * \param len number of clusters @@ -151,11 +186,10 @@ static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len) while (i < (len - 1) && level == glyphs[i+1].shape_run_id) i++; //printf("run %d from %d to %d with level %d\n", run, k, i, level); - FT_Face run_font = glyphs[k].font->faces[glyphs[k].face_index]; runs[run].offset = k; runs[run].end = i; runs[run].buf = hb_buffer_create(i - k + 1); - runs[run].font = hb_ft_font_create(run_font, NULL); + runs[run].font = get_hb_font(glyphs + k); set_run_features(shaper, glyphs + k); hb_buffer_set_direction(runs[run].buf, direction ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); @@ -219,7 +253,6 @@ static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len) // Free runs and associated data for (i = 0; i < run; i++) { hb_buffer_destroy(runs[i].buf); - hb_font_destroy(runs[i].font); } } |