[sushi] font-widget: always init/deinit FreeType from SushiFontWidget



commit 3b03528f271f6a08dedc53070a72f6c55e194479
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Apr 27 18:40:25 2012 -0400

    font-widget: always init/deinit FreeType from SushiFontWidget
    
    Instead of from the loader. This way we ensure the FT_Face we load will
    stay valid as long as SushiFontWidget is displayed.

 src/libsushi/sushi-font-loader.c |   34 ++++++----------------------------
 src/libsushi/sushi-font-loader.h |    3 ++-
 src/libsushi/sushi-font-widget.c |   15 ++++++++++++++-
 3 files changed, 22 insertions(+), 30 deletions(-)
---
diff --git a/src/libsushi/sushi-font-loader.c b/src/libsushi/sushi-font-loader.c
index fdf91e7..6bb6ac3 100644
--- a/src/libsushi/sushi-font-loader.c
+++ b/src/libsushi/sushi-font-loader.c
@@ -44,28 +44,12 @@ typedef struct {
 } FontLoadJob;
 
 static FontLoadJob *
-font_load_job_new (const gchar *uri,
+font_load_job_new (FT_Library library,
+                   const gchar *uri,
                    GAsyncReadyCallback callback,
                    gpointer user_data)
 {
-  FontLoadJob *job = NULL;
-  FT_Library library;
-  FT_Error res;
-  GError *error = NULL;
-
-  res = FT_Init_FreeType (&library);
-
-  if (res != 0) {
-    g_set_error_literal (&error,
-                         G_IO_ERROR, 0,
-                         "Can't initialize FreeType");
-    g_simple_async_report_take_gerror_in_idle (NULL,
-                                               callback, user_data,
-                                               error);
-    goto out;
-  }
-
-  job = g_slice_new0 (FontLoadJob);
+  FontLoadJob *job = g_slice_new0 (FontLoadJob);
 
   job->library = library;
   job->face_index = 0;
@@ -76,7 +60,6 @@ font_load_job_new (const gchar *uri,
 
   g_simple_async_result_set_op_res_gpointer (job->result, job, NULL);
 
- out:
   return job;
 }
 
@@ -158,17 +141,12 @@ font_load_job (GIOSchedulerJob *sched_job,
  *
  */
 void
-sushi_new_ft_face_from_uri_async (const gchar *uri,
+sushi_new_ft_face_from_uri_async (FT_Library library,
+                                  const gchar *uri,
                                   GAsyncReadyCallback callback,
                                   gpointer user_data)
 {
-  FontLoadJob *job = NULL;
-
-  job = font_load_job_new (uri, callback, user_data);
-
-  if (!job)
-    return;
-
+  FontLoadJob *job = font_load_job_new (library, uri, callback, user_data);
   g_io_scheduler_push_job (font_load_job,
                            job, NULL,
                            G_PRIORITY_DEFAULT,
diff --git a/src/libsushi/sushi-font-loader.h b/src/libsushi/sushi-font-loader.h
index 795fe50..98cb967 100644
--- a/src/libsushi/sushi-font-loader.h
+++ b/src/libsushi/sushi-font-loader.h
@@ -32,7 +32,8 @@
 #include FT_FREETYPE_H
 #include <gio/gio.h>
 
-void sushi_new_ft_face_from_uri_async (const gchar *uri,
+void sushi_new_ft_face_from_uri_async (FT_Library library,
+                                       const gchar *uri,
                                        GAsyncReadyCallback callback,
                                        gpointer user_data);
 
diff --git a/src/libsushi/sushi-font-widget.c b/src/libsushi/sushi-font-widget.c
index d66ccb2..088184e 100644
--- a/src/libsushi/sushi-font-widget.c
+++ b/src/libsushi/sushi-font-widget.c
@@ -43,6 +43,7 @@ enum {
 struct _SushiFontWidgetPrivate {
   gchar *uri;
 
+  FT_Library library;
   FT_Face face;
   gchar *face_contents;
 
@@ -464,7 +465,8 @@ font_face_async_ready_cb (GObject *object,
 static void
 load_font_face (SushiFontWidget *self)
 {
-  sushi_new_ft_face_from_uri_async (self->priv->uri,
+  sushi_new_ft_face_from_uri_async (self->priv->library,
+                                    self->priv->uri,
                                     font_face_async_ready_cb,
                                     self);
 }
@@ -482,10 +484,16 @@ sushi_font_widget_set_uri (SushiFontWidget *self,
 static void
 sushi_font_widget_init (SushiFontWidget *self)
 {
+  FT_Error err;
+
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, SUSHI_TYPE_FONT_WIDGET,
                                             SushiFontWidgetPrivate);
 
   self->priv->face = NULL;
+  err = FT_Init_FreeType (&self->priv->library);
+
+  if (err != FT_Err_Ok)
+    g_error ("Unable to initialize FreeType");
 }
 
 static void
@@ -540,6 +548,11 @@ sushi_font_widget_finalize (GObject *object)
   g_free (self->priv->sample_string);
   g_free (self->priv->face_contents);
 
+  if (self->priv->library != NULL) {
+    FT_Done_FreeType (self->priv->library);
+    self->priv->library = NULL;
+  }
+
   G_OBJECT_CLASS (sushi_font_widget_parent_class)->finalize (object);
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]