Edje_edit font_add with alias
authortiago <tiago>
Mon, 26 Apr 2010 18:19:46 +0000 (18:19 +0000)
committertiago <tiago@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Mon, 26 Apr 2010 18:19:46 +0000 (18:19 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/edje@48338 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/lib/Edje_Edit.h
src/lib/edje_edit.c

index 63d67f6..f7ae0f8 100644 (file)
@@ -731,6 +731,36 @@ EAPI Eina_Bool edje_edit_part_restack_above(Evas_Object *obj, const char *part);
  */
 EAPI Eina_Bool edje_edit_part_name_set(Evas_Object *obj, const char *part, const char *new_name);
 
+/**Add a new ttf font to the edje file.
+ * The newly created font will be available to all the groups in the edje, not only the current one.
+ * If font can't be load EINA_FALSE is returned.
+ */
+EAPI Eina_Bool          ///@return EINA_TRUE on success or EINA_FALSE on failure
+edje_edit_font_add(
+   Evas_Object *obj,       ///< The edje object
+   const char* path,       ///< The file path to load the ttf font from
+   const char* alias       ///< The file alias, or NULL to use filename
+);
+
+/**Get font name for a given part state. Remember to free the returned string using edje_edit_string_free().*/
+EAPI const char *          ///@return The name of the font used in the given part state
+edje_edit_state_font_get(
+   Evas_Object *obj,       ///< The edje object
+   const char *part,       ///< The name of the part
+   const char *state,      ///< The name of the 'part state' (ex. "default")
+   double value
+);
+
+/**Set font name for a given part state. */
+EAPI void
+edje_edit_state_font_set(
+   Evas_Object *obj,       ///< The edje object
+   const char *part,       ///< The name of the part
+   const char *state,      ///< The name of the 'part state' (ex. "default")
+   double value,
+   const char *font        ///< The name of the font to use in the given part state
+);
+
 /** Get the type of a part.
  *
  * @param obj Object being edited.
@@ -2187,10 +2217,11 @@ EAPI Eina_List * edje_edit_fonts_list_get(Evas_Object *obj);
  *
  * @param obj Object being edited.
  * @param path The file path to load the font from.
+ * @param alias The alias for file, or NULL to use filename
  *
  * @return EINA_TRUE if font cat be loaded, EINA_FALSE otherwise.
  */
-EAPI Eina_Bool edje_edit_font_add(Evas_Object *obj, const char *path);
+EAPI Eina_Bool edje_edit_font_add(Evas_Object *obj, const char *path, const char* alias);
 
 /** Get font name for a given part state.
  *
index a317391..fde18ff 100644 (file)
@@ -304,6 +304,75 @@ _edje_edit_update_edje_file(Edje *ed, Eet_File *eetf)
   return EINA_TRUE;
 }
 
+static Eina_Bool
+_edje_import_font_file(Edje *ed, const char *path, const char *entry)
+{
+   void *fdata = NULL;
+   long fsize = 0;
+
+   /* Read font data from file */
+   {
+      FILE *f = fopen(path, "rb");
+      if (!f)
+       {
+          ERR("Unable to open font file \"%s\"", path);
+          return EINA_FALSE;
+       }
+
+      fseek(f, 0, SEEK_END);
+      fsize = ftell(f);
+      rewind(f);
+      fdata = malloc(fsize);
+      if (!fdata)
+         {
+           ERR("Unable to alloc font file \"%s\"", path);
+           fclose(f);
+           return EINA_FALSE;
+         }
+      if (fread(fdata, fsize, 1, f) != 1)
+        {
+           ERR("Unable to read all of font file \"%s\"", path);
+           return EINA_FALSE;
+        }
+      fclose(f);
+   }
+
+   /* Write font to edje file */
+   {
+      /* open the eet file */
+      Eet_File *eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE);
+      if (!eetf)
+       {
+          ERR("Unable to open \"%s\" for writing output", ed->path);
+          free(fdata);
+          return EINA_FALSE;
+       }
+
+      if (eet_write(eetf, entry, fdata, fsize, 1) <= 0)
+        {
+           ERR("Unable to write font part \"%s\" as \"%s\" part entry",
+              path, entry);
+           eet_close(eetf);
+           free(fdata);
+           return EINA_FALSE;
+        }
+
+      free(fdata);
+
+      /* write the edje_file */
+      if (!_edje_edit_update_edje_file(ed, eetf))
+       {
+          eet_delete(eetf, entry);
+          eet_close(eetf);
+          return EINA_FALSE;
+       }
+
+      eet_close(eetf);
+   }
+
+   return EINA_TRUE;
+}
+
 
 static Eina_Bool
 _edje_import_image_file(Edje *ed, const char *path, int id)
@@ -4156,101 +4225,76 @@ edje_edit_fonts_list_get(Evas_Object *obj)
 }
 
 EAPI Eina_Bool
-edje_edit_font_add(Evas_Object *obj, const char* path)
+edje_edit_font_add(Evas_Object *obj, const char* path, const char* alias)
 {
-   char buf[PATH_MAX];
-   Edje_Font_Directory_Entry *fnt;
-   Eet_File *eetf;
+   char entry[PATH_MAX];
    struct stat st;
-   char *name;
-   FILE *f;
-   void *fdata = NULL;
-   int fsize = 0;
-
-   GET_ED_OR_RETURN(0);
+   Edje_Font_Directory_Entry *fnt;
 
-   //printf("ADD FONT: %s\n", path);
+   GET_ED_OR_RETURN(EINA_FALSE);
 
-   if (!path) return 0;
-   if (stat(path, &st) || !S_ISREG(st.st_mode)) return 0;
-   if (!ed->file) return 0;
-   if (!ed->path) return 0;
+   INF("ADD FONT: %s\n", path);
 
+   if (!path) return EINA_FALSE;
+   if (stat(path, &st) || !S_ISREG(st.st_mode)) return EINA_FALSE;
+   if (!ed->file) return EINA_FALSE;
+   if (!ed->path) return EINA_FALSE;
 
    /* Create Font_Directory if not exist */
    if (!ed->file->font_dir)
-   {
-     ed->file->font_dir = _alloc(sizeof(Edje_Font_Directory));
-     if (!ed->file->font_dir) return 0;
-   }
-
-   if ((name = strrchr(path, '/'))) name ++;
-   else name = (char *)path;
-
-   /* Read font data from file */
-   f = fopen(path, "rb");
-   if (f)
      {
-       long pos;
-
-       fseek(f, 0, SEEK_END);
-       pos = ftell(f);
-       rewind(f);
-       fdata = malloc(pos);
-       if (fdata)
-         {
-            if (fread(fdata, pos, 1, f) != 1)
-              {
-                 ERR("Edje_Edit: Error. unable to read all of font file \"%s\"",
-                     path);
-                 return 0;
-              }
-            fsize = pos;
-         }
-       fclose(f);
+       ed->file->font_dir = _alloc(sizeof(Edje_Font_Directory));
+       if (!ed->file->font_dir) return EINA_FALSE;
+     }
+   if (!ed->file->font_hash)
+     {
+       ed->file->font_hash = eina_hash_string_superfast_new(NULL);
+       if (!ed->file->font_hash) return EINA_FALSE;
      }
-   /* Write font to edje file */
-   snprintf(buf, sizeof(buf), "fonts/%s", name);
 
-   if (fdata)
+   /* Alias */
+   if (!alias)
      {
-       /* open the eet file */
-       eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE);
-       if (!eetf)
-         {
-           ERR("Edje_Edit: Error. unable to open \"%s\" for writing output",
-               ed->path);
-            return 0;
-         }
+       if ((alias = strrchr(path, '/'))) alias ++;
+       else alias = (char *)path;
+     }
+   snprintf(entry, sizeof(entry), "fonts/%s", alias);
 
-       if (eet_write(eetf, buf, fdata, fsize, 1) <= 0)
-         {
-            ERR("Edje_Edit: Error. unable to write font part \"%s\" as \"%s\" part entry",
-                path, buf);
-            eet_close(eetf);
-            free(fdata);
-            return 0;
-         }
+   /* Check if exists */
+   fnt = eina_hash_find(ed->file->font_hash, alias);
+   if (fnt)
+     return EINA_FALSE;
 
-       eet_close(eetf);
-       free(fdata);
+   /* Create Edje_Font_Directory_Entry */
+   fnt = _alloc(sizeof(Edje_Font_Directory_Entry));
+   if (!fnt)
+     {
+       ERR("Unable to alloc font entry part \"%s\"", alias);
+       return EINA_FALSE;
      }
+   fnt->entry = strdup(alias);
+   fnt->path = strdup(entry);
 
-   /* Create Edje_Font_Directory_Entry */
-   if (ed->file->font_dir)
+   ed->file->font_dir->entries = eina_list_append(
+                                      ed->file->font_dir->entries,
+                                      fnt);
+   eina_hash_direct_add(ed->file->font_hash, fnt->entry, fnt);
+
+   /* Import font */
+   if (!_edje_import_font_file(ed, path, entry))
      {
-       fnt = _alloc(sizeof(Edje_Font_Directory_Entry));
-       if (!fnt) return 0;
-       fnt->entry = strdup(name);
-       fnt->path = strdup(buf);
+       ed->file->font_dir->entries = eina_list_remove(
+                                            ed->file->font_dir->entries,
+                                            fnt);
+       eina_hash_del_by_key(ed->file->font_hash, alias);
 
-       ed->file->font_dir->entries = eina_list_append(ed->file->font_dir->entries, fnt);
-       if (!ed->file->font_hash)
-         ed->file->font_hash = eina_hash_string_superfast_new(NULL);
-       eina_hash_direct_add(ed->file->font_hash, fnt->entry, fnt);
+       free((char *)fnt->entry);
+       free((char *)fnt->path);
+       free(fnt);
+       return EINA_FALSE;
      }
 
-   return 1;
+   return EINA_TRUE;
 }
 
 EAPI const char *