Skip to content

Commit

Permalink
Rework font loading slightly.
Browse files Browse the repository at this point in the history
Now we have a default font and an additional set of system available fallback fonts
  • Loading branch information
andoma authored and Andreas Öman committed Jul 26, 2012
1 parent 5e4bc96 commit b36971e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 66 deletions.
1 change: 0 additions & 1 deletion configure.linux
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ enable httpserver
enable timegm
enable inotify
enable realpath
enable font_liberation
#enable libxrandr -- code does not really work yet

for opt do
Expand Down
1 change: 0 additions & 1 deletion configure.osx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ enable timegm
enable realpath
enable polarssl
enable librtmp
enable font_liberation
enable dvd
enable spotify
enable vda
Expand Down
4 changes: 4 additions & 0 deletions src/arch/arch_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ arch_get_seed(void)
void
arch_preload_fonts(void)
{
#ifdef __APPLE__
freetype_load_font("file:https:///Library/Fonts/Arial Unicode.ttf",
FONT_DOMAIN_FALLBACK, NULL);
#endif
}


Expand Down
20 changes: 10 additions & 10 deletions src/arch/arch_ps3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1103,16 +1103,16 @@ arch_get_seed(void)
void
arch_preload_fonts(void)
{
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-VR-R-LATIN2.TTF", 0,
NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-NR-R-JPN.TTF", 0,
NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-YG-R-KOR.TTF", 0,
NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-DH-R-CGB.TTF", 0,
NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-CP-R-KANA.TTF", 0,
NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-VR-R-LATIN2.TTF",
FONT_DOMAIN_FALLBACK, NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-NR-R-JPN.TTF",
FONT_DOMAIN_FALLBACK, NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-YG-R-KOR.TTF",
FONT_DOMAIN_FALLBACK, NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-DH-R-CGB.TTF",
FONT_DOMAIN_FALLBACK, NULL);
freetype_load_font("file:https:///dev_flash/data/font/SCE-PS3-CP-R-KANA.TTF",
FONT_DOMAIN_FALLBACK, NULL);
}

const char *
Expand Down
107 changes: 57 additions & 50 deletions src/text/freetype.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
static FT_Library text_library;
static FT_Stroker text_stroker;
static hts_mutex_t text_mutex;
static int context_tally;
static int font_domain_tally = 10;

#define GLYPH_HASH_SIZE 128
#define GLYPH_HASH_MASK (GLYPH_HASH_SIZE-1)
Expand Down Expand Up @@ -84,6 +84,7 @@ typedef struct face {
} face_t;

static struct face_queue faces;
static face_t *default_font;


//------------------------- Glyph cache -----------------------
Expand Down Expand Up @@ -380,30 +381,6 @@ face_create_from_memory(const void *ptr, size_t len, int context)
}



/**
*
*/
static int
face_resolve(int uc, uint8_t style, int family_id,
char *urlbuf, size_t urllen)
{
#if ENABLE_LIBFONTCONFIG
if(!fontconfig_resolve(uc, style, family_get_by_id(family_id),
urlbuf, urllen))
return 0;
#endif

#if ENABLE_FONT_LIBERATION
snprintf(urlbuf, urllen,
"%s/resources/fonts/liberation/LiberationSans-Regular.ttf",
showtime_dataroot());
return 0;
#endif
return -1;
}


/**
*
*/
Expand All @@ -429,7 +406,10 @@ face_set_family(face_t *f, int family_id)

if(f->family_id_vec != NULL)
while(f->family_id_vec[len] != 0)
len++;
if(f->family_id_vec[len] == family_id)
return;
else
len++;
#if 0
printf("Font %s alias to %s\n",
f->family_id_vec ? family_get_by_id(f->family_id_vec[0]) : "<yet unnamed>",
Expand Down Expand Up @@ -464,34 +444,30 @@ face_find2(int uc, uint8_t style, int family_id)
FT_Get_Char_Index(f->face, uc))
return f;

if(!face_resolve(uc, style, family_id, url, sizeof(url))) {
// Try default font
if(default_font != NULL)
if(FT_Get_Char_Index(default_font->face, uc))
f = default_font;

if(f == NULL)
TAILQ_FOREACH(f, &faces, link)
if(f->url != NULL && !strcmp(f->url, url))
if(f->font_domain == FONT_DOMAIN_FALLBACK &&
FT_Get_Char_Index(f->face, uc))
break;

#if ENABLE_LIBFONTCONFIG
if(f == NULL)
if(!fontconfig_resolve(uc, style, family_get_by_id(family_id),
url, sizeof(url)))
f = face_create_from_uri(url, FONT_DOMAIN_FALLBACK, NULL);
#endif

if(f != NULL) {
// Same family, just return
if(face_is_family(f, family_id))
return f;

face_set_family(f, family_id);
return f;
}

f = face_create_from_uri(url, 0, NULL);
}

if(f == NULL) {
TAILQ_FOREACH(f, &faces, link)
if(f->style == style && FT_Get_Char_Index(f->face, uc))
break;
}
if(f == NULL) {

// Last resort, anything that has the glyph
if(f == NULL)
TAILQ_FOREACH(f, &faces, link)
if(f->style == 0 && FT_Get_Char_Index(f->face, uc))
if(FT_Get_Char_Index(f->face, uc))
break;
}

if(f != NULL)
face_set_family(f, family_id);
Expand Down Expand Up @@ -867,7 +843,7 @@ text_render0(const uint32_t *uc, const int len,
int global_alignment, int max_width, int max_lines,
const char *family, int context)
{
const int default_family_id = family_get(family ?: "Arial", context);
const int default_family_id = family_get(family ?: "Sans", context);
int family_id = default_family_id;
pixmap_t *pm;
FT_UInt prev = 0;
Expand Down Expand Up @@ -1349,13 +1325,37 @@ text_render(const uint32_t *uc, const int len, int flags, int default_size,
}


/**
*
*/
static void
freetype_set_default_font(const char *url)
{
hts_mutex_lock(&text_mutex);

if(default_font) {
if(--default_font->persistent == 0)
face_destroy(default_font);
default_font = NULL;
}

if(url) {
default_font = face_create_from_uri(url, FONT_DOMAIN_DEFAULT, NULL);
if(default_font != NULL)
default_font->persistent++;
}
hts_mutex_unlock(&text_mutex);
}


/**
*
*/
int
freetype_init(void)
{
int error;
char url[512];

error = FT_Init_FreeType(&text_library);
if(error) {
Expand All @@ -1367,6 +1367,13 @@ freetype_init(void)
TAILQ_INIT(&allglyphs);
hts_mutex_init(&text_mutex);
arch_preload_fonts();

snprintf(url, sizeof(url),
"%s/resources/fonts/liberation/LiberationSans-Regular.ttf",
showtime_dataroot());

freetype_set_default_font(url);

return 0;
}

Expand Down Expand Up @@ -1458,7 +1465,7 @@ freetype_get_context(void)
{
int id;
hts_mutex_lock(&text_mutex);
id = ++context_tally;
id = ++font_domain_tally;
hts_mutex_unlock(&text_mutex);
return id;
}
3 changes: 3 additions & 0 deletions src/text/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
struct pixmap;
struct rstr;

#define FONT_DOMAIN_FALLBACK 0
#define FONT_DOMAIN_DEFAULT 1

#define TR_STYLE_BOLD 0x1
#define TR_STYLE_ITALIC 0x2

Expand Down
5 changes: 1 addition & 4 deletions support/configure.inc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ CONFIG_LIST="
trex
emu_thread_specifics
ps3_vdec
font_liberation
libfontconfig
binreplace
readahead_cache
Expand Down Expand Up @@ -353,9 +352,7 @@ finalize() {
fi

# Fonts
if enabled font_liberation; then
echo >>${CONFIG_MAK} "BUNDLES+=resources/fonts/liberation"
fi
echo >>${CONFIG_MAK} "BUNDLES+=resources/fonts"

# Languages
echo >>${CONFIG_MAK} "BUNDLES+=lang"
Expand Down

0 comments on commit b36971e

Please sign in to comment.