const char *font)
{
Elm_Font_Properties *efp = NULL;
- char *s1;
+ char *token = strchr(font, ':');
- s1 = strchr(font, ':');
- if (s1)
+ if (token &&
+ !strncmp(token, ELM_FONT_TOKEN_STYLE, strlen(ELM_FONT_TOKEN_STYLE)))
{
- char *s2, *name, *style;
+ char *name, *subname, *style, *substyle;
int len;
- len = s1 - font;
+ /* get font name */
+ len = token - font;
name = calloc(sizeof(char), len + 1);
+ if (!name) return NULL;
strncpy(name, font, len);
- /* get subname (should be english) */
- s2 = strchr(name, ',');
- if (s2)
+ /* remove subnames from the font name (should be english) */
+ subname = strchr(name, ',');
+ if (subname)
{
- len = s2 - name;
+ len = subname - name;
name = realloc(name, sizeof(char) * len + 1);
- memset(name, 0, sizeof(char) * len + 1);
- strncpy(name, font, len);
+ if (name)
+ {
+ memset(name, 0, sizeof(char) * len + 1);
+ strncpy(name, font, len);
+ }
}
- if (!strncmp(s1, ELM_FONT_TOKEN_STYLE, strlen(ELM_FONT_TOKEN_STYLE)))
+ /* add a font name */
+ if (font_hash)
+ efp = eina_hash_find(*font_hash, name);
+ if (!efp)
{
- style = s1 + strlen(ELM_FONT_TOKEN_STYLE);
-
- if (font_hash) efp = eina_hash_find(*font_hash, name);
- if (!efp)
+ efp = calloc(1, sizeof(Elm_Font_Properties));
+ if (!efp) return NULL;
+
+ efp->name = eina_stringshare_add(name);
+ if (font_hash)
{
- efp = calloc(1, sizeof(Elm_Font_Properties));
- efp->name = eina_stringshare_add(name);
- if (font_hash)
- {
- if (!*font_hash)
- *font_hash = eina_hash_string_superfast_new(NULL);
- eina_hash_add(*font_hash, name, efp);
- }
+ if (!*font_hash)
+ *font_hash = eina_hash_string_superfast_new(NULL);
+ eina_hash_add(*font_hash, name, efp);
}
- s2 = strchr(style, ',');
- if (s2)
- {
- char *style_old;
+ }
+
+ free(name);
+
+ style = token + strlen(ELM_FONT_TOKEN_STYLE);
+ substyle = strchr(style, ',');
+
+ //TODO: Seems to need to add all styles. not only one.
+ if (substyle)
+ {
+ char *style_old = style;
- len = s2 - style;
- style_old = style;
- style = calloc(sizeof(char), len + 1);
+ len = substyle - style;
+ style = calloc(sizeof(char), len + 1);
+ if (style)
+ {
strncpy(style, style_old, len);
efp->styles = eina_list_append(efp->styles,
eina_stringshare_add(style));
free(style);
}
- else
- efp->styles = eina_list_append(efp->styles,
- eina_stringshare_add(style));
}
- free(name);
+ else
+ efp->styles = eina_list_append(efp->styles,
+ eina_stringshare_add(style));
}
- else
+ else if ((font_hash) && (!eina_hash_find(*font_hash, font)))
{
- if (font_hash) efp = eina_hash_find(*font_hash, font);
- if (!efp)
- {
- efp = calloc(1, sizeof(Elm_Font_Properties));
- efp->name = eina_stringshare_add(font);
- if (font_hash)
- {
- if (!*font_hash)
- *font_hash = eina_hash_string_superfast_new(NULL);
- eina_hash_add(*font_hash, font, efp);
- }
- }
+ efp = calloc(1, sizeof(Elm_Font_Properties));
+ if (!efp) return NULL;
+
+ efp->name = eina_stringshare_add(font);
+ if (!*font_hash) *font_hash = eina_hash_string_superfast_new(NULL);
+ eina_hash_add(*font_hash, font, efp);
}
+
return efp;
}
-/* FIXME: do we really need it? */
Eina_Hash *
_elm_font_available_hash_add(Eina_Hash *font_hash,
const char *full_name)
void
_elm_font_available_hash_del(Eina_Hash *hash)
{
- if (!hash) return ;
+ if (!hash) return;
eina_hash_foreach(hash, _font_hash_free_cb, NULL);
+ eina_hash_free(hash);
+}
+
+EAPI Elm_Font_Properties *
+elm_font_properties_get(const char *font)
+{
+ EINA_SAFETY_ON_NULL_RETURN_VAL(font, NULL);
+ return _elm_font_properties_get(NULL, font);
+}
+
+EAPI void
+elm_font_properties_free(Elm_Font_Properties *efp)
+{
+ const char *str;
+
+ EINA_SAFETY_ON_NULL_RETURN(efp);
+ EINA_LIST_FREE(efp->styles, str)
+ if (str) eina_stringshare_del(str);
+ if (efp->name) eina_stringshare_del(efp->name);
+ free(efp);
+}
+
+EAPI char *
+elm_font_fontconfig_name_get(const char *name,
+ const char *style)
+{
+ char buf[256];
+
+ EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
+ if (!style || style[0] == 0) return (char *) eina_stringshare_add(name);
+ snprintf(buf, 256, "%s" ELM_FONT_TOKEN_STYLE "%s", name, style);
+ return (char *) eina_stringshare_add(buf);
+}
+
+EAPI void
+elm_font_fontconfig_name_free(char *name)
+{
+ eina_stringshare_del(name);
+}
+
+EAPI Eina_Hash *
+elm_font_available_hash_add(Eina_List *list)
+{
+ Eina_Hash *font_hash;
+ Eina_List *l;
+ void *key;
+
+ font_hash = NULL;
+
+ /* populate with default font families */
+ //FIXME: Need to check whether fonts are being added multiple times.
+ font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Regular");
+ font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Bold");
+ font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Oblique");
+ font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Bold Oblique");
+ font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Regular");
+ font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Bold");
+ font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Oblique");
+ font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Bold Oblique");
+ font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Regular");
+ font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Bold");
+ font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Oblique");
+ font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Bold Oblique");
+
+ EINA_LIST_FOREACH(list, l, key)
+ if (key) _elm_font_available_hash_add(font_hash, key);
+
+ return font_hash;
+}
+
+EAPI void
+elm_font_available_hash_del(Eina_Hash *hash)
+{
+ _elm_font_available_hash_del(hash);
}