[ot/ft] Port font_funcs statis to lazy-loader

This commit is contained in:
Behdad Esfahbod 2018-08-12 13:46:53 -07:00
parent cb3fc3685c
commit c7ca30a533
3 changed files with 63 additions and 58 deletions

View File

@ -32,6 +32,7 @@
#include "hb-ft.h" #include "hb-ft.h"
#include "hb-font-private.hh" #include "hb-font-private.hh"
#include "hb-machinery-private.hh"
#include FT_ADVANCES_H #include FT_ADVANCES_H
#include FT_MULTIPLE_MASTERS_H #include FT_MULTIPLE_MASTERS_H
@ -416,30 +417,13 @@ hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
return true; return true;
} }
static hb_atomic_ptr_t<hb_font_funcs_t> static_ft_funcs; static void free_static_ft_funcs (void);
#ifdef HB_USE_ATEXIT static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
static
void free_static_ft_funcs (void)
{ {
retry: static inline hb_font_funcs_t *create (void)
hb_font_funcs_t *ft_funcs = static_ft_funcs.get ();
if (unlikely (!static_ft_funcs.cmpexch (ft_funcs, nullptr)))
goto retry;
hb_font_funcs_destroy (ft_funcs);
}
#endif
static void
_hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
{
retry:
hb_font_funcs_t *funcs = static_ft_funcs.get ();
if (unlikely (!funcs))
{ {
funcs = hb_font_funcs_create (); hb_font_funcs_t *funcs = hb_font_funcs_create ();
hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr); hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr);
//hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr); //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr);
@ -458,21 +442,35 @@ retry:
hb_font_funcs_make_immutable (funcs); hb_font_funcs_make_immutable (funcs);
if (unlikely (!static_ft_funcs. cmpexch (nullptr, funcs))) #ifdef HB_USE_ATEXIT
{ atexit (free_static_ft_funcs);
hb_font_funcs_destroy (funcs); #endif
goto retry;
} return funcs;
}
} static_ft_funcs;
#ifdef HB_USE_ATEXIT #ifdef HB_USE_ATEXIT
atexit (free_static_ft_funcs); /* First person registers atexit() callback. */ static
void free_static_ft_funcs (void)
{
static_ft_funcs.fini ();
}
#endif #endif
};
static hb_font_funcs_t *
_hb_ft_get_font_funcs (void)
{
return const_cast<hb_font_funcs_t *> (static_ft_funcs.get ());
}
static void
_hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
{
bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL; bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
hb_font_set_funcs (font, hb_font_set_funcs (font,
funcs, _hb_ft_get_font_funcs (),
_hb_ft_font_create (ft_face, symbol, unref), _hb_ft_font_create (ft_face, symbol, unref),
_hb_ft_font_destroy); _hb_ft_font_destroy);
} }

View File

@ -749,6 +749,20 @@ struct hb_table_lazy_loader_t : hb_lazy_loader_t<hb_table_lazy_loader_t<T, Where
} }
}; };
template <typename Subclass>
struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<Subclass,
void, 0,
hb_font_funcs_t>
{
static inline void destroy (hb_font_funcs_t *p)
{
hb_font_funcs_destroy (p);
}
static inline const hb_font_funcs_t *get_null (void)
{
return hb_font_funcs_get_empty ();
}
};
template <typename Subclass> template <typename Subclass>
struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<Subclass, struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<Subclass,
void, 0, void, 0,

View File

@ -29,6 +29,7 @@
#include "hb-ot.h" #include "hb-ot.h"
#include "hb-font-private.hh" #include "hb-font-private.hh"
#include "hb-machinery-private.hh"
#include "hb-ot-cmap-table.hh" #include "hb-ot-cmap-table.hh"
#include "hb-ot-glyf-table.hh" #include "hb-ot-glyf-table.hh"
@ -225,30 +226,14 @@ hb_ot_get_font_v_extents (hb_font_t *font,
return ot_font->v_metrics.has_font_extents; return ot_font->v_metrics.has_font_extents;
} }
static hb_atomic_ptr_t <hb_font_funcs_t> static_ot_funcs;
#ifdef HB_USE_ATEXIT static void free_static_ot_funcs (void);
static
void free_static_ot_funcs (void) static struct hb_ot_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ot_font_funcs_lazy_loader_t>
{ {
retry: static inline hb_font_funcs_t *create (void)
hb_font_funcs_t *ot_funcs = static_ot_funcs.get ();
if (unlikely (!static_ot_funcs.cmpexch (ot_funcs, nullptr)))
goto retry;
hb_font_funcs_destroy (ot_funcs);
}
#endif
static hb_font_funcs_t *
_hb_ot_get_font_funcs (void)
{
retry:
hb_font_funcs_t *funcs = static_ot_funcs.get ();
if (unlikely (!funcs))
{ {
funcs = hb_font_funcs_create (); hb_font_funcs_t *funcs = hb_font_funcs_create ();
hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, nullptr, nullptr); hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, nullptr, nullptr);
hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, nullptr, nullptr); hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, nullptr, nullptr);
@ -267,18 +252,26 @@ retry:
hb_font_funcs_make_immutable (funcs); hb_font_funcs_make_immutable (funcs);
if (unlikely (!static_ot_funcs.cmpexch (nullptr, funcs))) #ifdef HB_USE_ATEXIT
{ atexit (free_static_ot_funcs);
hb_font_funcs_destroy (funcs); #endif
goto retry;
} return funcs;
}
} static_ot_funcs;
#ifdef HB_USE_ATEXIT #ifdef HB_USE_ATEXIT
atexit (free_static_ot_funcs); /* First person registers atexit() callback. */ static
void free_static_ot_funcs (void)
{
static_ot_funcs.fini ();
}
#endif #endif
};
return funcs; static hb_font_funcs_t *
_hb_ot_get_font_funcs (void)
{
return const_cast<hb_font_funcs_t *> (static_ot_funcs.get ());
} }