diff options
author | Cosimo Cecchi <cosimoc@gnome.org> | 2019-07-06 09:40:53 -0700 |
---|---|---|
committer | Cosimo Cecchi <cosimoc@gnome.org> | 2019-07-06 09:40:53 -0700 |
commit | 919dfbe684b75904563b8c6723c9778a4e00aad7 (patch) | |
tree | 8047dc3cb62782f768c9b2bbc46f306c7d1ae68d /src | |
parent | f08938213273283b02477299868c42711681c495 (diff) | |
download | gnome-font-viewer-919dfbe684b75904563b8c6723c9778a4e00aad7.tar.gz |
font-widget: sync from Sushi
Diffstat (limited to 'src')
-rw-r--r-- | src/sushi-font-loader.c | 52 | ||||
-rw-r--r-- | src/sushi-font-widget.c | 54 |
2 files changed, 37 insertions, 69 deletions
diff --git a/src/sushi-font-loader.c b/src/sushi-font-loader.c index c612b41..f7cf1de 100644 --- a/src/sushi-font-loader.c +++ b/src/sushi-font-loader.c @@ -60,10 +60,13 @@ static void font_load_job_free (FontLoadJob *job) { g_clear_object (&job->file); + g_free (job->face_contents); g_slice_free (FontLoadJob, job); } +G_DEFINE_AUTOPTR_CLEANUP_FUNC (FontLoadJob, font_load_job_free) + static FT_Face create_face_from_contents (FontLoadJob *job, gchar **contents, @@ -79,34 +82,23 @@ create_face_from_contents (FontLoadJob *job, &retval); if (ft_error != 0) { - gchar *uri; - uri = g_file_get_uri (job->file); + g_autofree gchar *uri = g_file_get_uri (job->file); g_set_error (error, G_IO_ERROR, 0, "Unable to read the font face file '%s'", uri); - retval = NULL; - g_free (job->face_contents); - g_free (uri); - } else { - *contents = job->face_contents; + return NULL; } + *contents = g_steal_pointer (&job->face_contents); return retval; } -static void +static gboolean font_load_job_do_load (FontLoadJob *job, GError **error) { - gchar *contents; - gsize length; - - g_file_load_contents (job->file, NULL, - &contents, &length, NULL, error); - - if ((error != NULL) && (*error == NULL)) { - job->face_contents = contents; - job->face_length = length; - } + return g_file_load_contents (job->file, NULL, + &job->face_contents, &job->face_length, + NULL, error); } static void @@ -116,12 +108,12 @@ font_load_job (GTask *task, GCancellable *cancellable) { FontLoadJob *job = user_data; - GError *error = NULL; + g_autoptr(GError) error = NULL; font_load_job_do_load (job, &error); if (error != NULL) - g_task_return_error (task, error); + g_task_return_error (task, g_steal_pointer (&error)); else g_task_return_boolean (task, TRUE); } @@ -137,21 +129,11 @@ sushi_new_ft_face_from_uri (FT_Library library, gchar **contents, GError **error) { - FontLoadJob *job = NULL; - FT_Face face; - - job = font_load_job_new (library, uri, face_index, NULL, NULL); - font_load_job_do_load (job, error); - - if ((error != NULL) && (*error != NULL)) { - font_load_job_free (job); + g_autoptr(FontLoadJob) job = font_load_job_new (library, uri, face_index, NULL, NULL); + if (!font_load_job_do_load (job, error)) return NULL; - } - face = create_face_from_contents (job, contents, error); - font_load_job_free (job); - - return face; + return create_face_from_contents (job, contents, error); } /** @@ -166,12 +148,10 @@ sushi_new_ft_face_from_uri_async (FT_Library library, gpointer user_data) { FontLoadJob *job = font_load_job_new (library, uri, face_index, callback, user_data); - GTask *task; + g_autoptr(GTask) task = g_task_new (NULL, NULL, callback, user_data); - task = g_task_new (NULL, NULL, callback, user_data); g_task_set_task_data (task, job, (GDestroyNotify) font_load_job_free); g_task_run_in_thread (task, font_load_job); - g_object_unref (task); } /** diff --git a/src/sushi-font-widget.c b/src/sushi-font-widget.c index 43b5a0c..144a3d6 100644 --- a/src/sushi-font-widget.c +++ b/src/sushi-font-widget.c @@ -173,11 +173,11 @@ text_extents (cairo_t *cr, const char *text, cairo_text_extents_t *extents) { - cairo_glyph_t *glyphs; + g_autofree cairo_glyph_t *glyphs = NULL; gint num_glyphs; + text_to_glyphs (cr, text, &glyphs, &num_glyphs); cairo_glyph_extents (cr, glyphs, num_glyphs, extents); - g_free (glyphs); } /* adapted from gnome-utils:font-viewer/font-view.c @@ -194,9 +194,9 @@ draw_string (SushiFontWidget *self, const gchar *text, gint *pos_y) { + g_autofree cairo_glyph_t *glyphs = NULL; cairo_font_extents_t font_extents; cairo_text_extents_t extents; - cairo_glyph_t *glyphs; GtkTextDirection text_dir; gint pos_x; gint num_glyphs; @@ -227,8 +227,6 @@ draw_string (SushiFontWidget *self, cairo_move_to (cr, pos_x, *pos_y); cairo_show_glyphs (cr, glyphs, num_glyphs); - g_free (glyphs); - *pos_y += LINE_SPACING / 2; } @@ -236,30 +234,25 @@ static gboolean check_font_contain_text (FT_Face face, const gchar *text) { - gunichar *string; + g_autofree gunichar *string = NULL; glong len, idx; - gboolean retval = TRUE; string = g_utf8_to_ucs4_fast (text, -1, &len); for (idx = 0; idx < len; idx++) { gunichar c = string[idx]; - if (!FT_Get_Char_Index (face, c)) { - retval = FALSE; - break; - } + if (!FT_Get_Char_Index (face, c)) + return FALSE; } - g_free (string); - - return retval; + return TRUE; } static gchar * build_charlist_for_face (FT_Face face, gint *length) { - GString *string; + g_autoptr(GString) string = NULL; gulong c; guint glyph; gint total_chars = 0; @@ -277,16 +270,16 @@ build_charlist_for_face (FT_Face face, if (length) *length = total_chars; - return g_string_free (string, FALSE); + return g_strdup (string->str); } static gchar * random_string_from_available_chars (FT_Face face, gint n_chars) { - gchar *chars; + g_autofree gchar *chars = NULL; + g_autoptr(GString) retval = NULL; gint idx, rand, total_chars; - GString *retval; gchar *ptr, *end; idx = 0; @@ -296,7 +289,7 @@ random_string_from_available_chars (FT_Face face, return NULL; if (total_chars <= n_chars) - return chars; + return g_steal_pointer (&chars); retval = g_string_new (NULL); @@ -310,7 +303,7 @@ random_string_from_available_chars (FT_Face face, idx++; } - return g_string_free (retval, FALSE); + return g_strdup (retval->str); } static gboolean @@ -456,7 +449,8 @@ sushi_font_widget_size_request (GtkWidget *drawing_area, cairo_text_extents_t extents; cairo_font_extents_t font_extents; cairo_font_face_t *font; - gint *sizes = NULL, n_sizes, alpha_size, title_size; + g_autofree gint *sizes = NULL; + gint n_sizes, alpha_size, title_size; cairo_t *cr; cairo_surface_t *surface; FT_Face face = self->face; @@ -562,7 +556,6 @@ sushi_font_widget_size_request (GtkWidget *drawing_area, cairo_destroy (cr); cairo_font_face_destroy (font); cairo_surface_destroy (surface); - g_free (sizes); } static void @@ -596,7 +589,8 @@ sushi_font_widget_draw (GtkWidget *drawing_area, cairo_t *cr) { SushiFontWidget *self = SUSHI_FONT_WIDGET (drawing_area); - gint *sizes = NULL, n_sizes, alpha_size, title_size, pos_y = 0, i; + g_autofree gint *sizes = NULL; + gint n_sizes, alpha_size, title_size, pos_y = 0, i; cairo_font_face_t *font = NULL; FT_Face face = self->face; GtkStyleContext *context; @@ -606,7 +600,7 @@ sushi_font_widget_draw (GtkWidget *drawing_area, gint allocated_width, allocated_height; if (face == NULL) - goto end; + return FALSE; context = gtk_widget_get_style_context (drawing_area); state = gtk_style_context_get_state (context); @@ -668,9 +662,7 @@ sushi_font_widget_draw (GtkWidget *drawing_area, } end: - if (font != NULL) - cairo_font_face_destroy (font); - g_free (sizes); + cairo_font_face_destroy (font); return FALSE; } @@ -681,7 +673,7 @@ font_face_async_ready_cb (GObject *object, gpointer user_data) { SushiFontWidget *self = user_data; - GError *error = NULL; + g_autoptr(GError) error = NULL; self->face = sushi_new_ft_face_from_uri_finish (result, @@ -691,7 +683,6 @@ font_face_async_ready_cb (GObject *object, if (error != NULL) { g_signal_emit (self, signals[ERROR], 0, error); g_print ("Can't load the font face: %s\n", error->message); - g_error_free (error); return; } @@ -715,10 +706,7 @@ sushi_font_widget_load (SushiFontWidget *self) static void sushi_font_widget_init (SushiFontWidget *self) { - FT_Error err; - - self->face = NULL; - err = FT_Init_FreeType (&self->library); + FT_Error err = FT_Init_FreeType (&self->library); if (err != FT_Err_Ok) g_error ("Unable to initialize FreeType"); |