From 25d089f8582ede5e59a446208eb442af1e1df8e5 Mon Sep 17 00:00:00 2001 From: raveit65 Date: Sun, 8 Jan 2017 18:45:58 +0100 Subject: Fontviewer: font-model, add API to get an iter from a FT_Face We'll need this to update the Install button state when a new font is installed. taken from: https://git.gnome.org/browse/gnome-font-viewer/commit/?id=1a21320 --- font-viewer/Makefile.am | 2 ++ font-viewer/font-model.c | 72 ++++++++++++++++++++++++------------------------ font-viewer/font-model.h | 3 ++ font-viewer/font-utils.c | 65 +++++++++++++++++++++++++++++++++++++++++++ font-viewer/font-utils.h | 33 ++++++++++++++++++++++ 5 files changed, 139 insertions(+), 36 deletions(-) create mode 100644 font-viewer/font-utils.c create mode 100644 font-viewer/font-utils.h diff --git a/font-viewer/Makefile.am b/font-viewer/Makefile.am index ef123991..1ef5affe 100644 --- a/font-viewer/Makefile.am +++ b/font-viewer/Makefile.am @@ -20,6 +20,8 @@ mate_font_viewer_SOURCES = \ $(font_loader_SOURCES) \ font-model.h \ font-model.c \ + font-utils.h \ + font-utils.c \ gd-main-toolbar.h \ gd-main-toolbar.c \ sushi-font-widget.h \ diff --git a/font-viewer/font-model.c b/font-viewer/font-model.c index 887de59f..5013d451 100644 --- a/font-viewer/font-model.c +++ b/font-viewer/font-model.c @@ -39,6 +39,7 @@ #include #include "font-model.h" +#include "font-utils.h" #include "sushi-font-loader.h" struct _FontViewModelPrivate { @@ -150,6 +151,7 @@ gd_queue_thumbnail_job_for_file_finish (GAsyncResult *res) typedef struct { const gchar *file; + FT_Face face; GtkTreeIter iter; gboolean found; } IterForFileData; @@ -161,15 +163,24 @@ iter_for_file_foreach (GtkTreeModel *model, gpointer user_data) { IterForFileData *data = user_data; - gchar *font_path; + gchar *font_path, *font_name, *match_name; gboolean retval; gtk_tree_model_get (GTK_TREE_MODEL (model), iter, + COLUMN_NAME, &font_name, COLUMN_PATH, &font_path, -1); - retval = (g_strcmp0 (font_path, data->file) == 0); + if (data->file) { + retval = (g_strcmp0 (font_path, data->file) == 0); + } else if (data->face) { + match_name = font_utils_get_font_name (data->face); + retval = (g_strcmp0 (font_name, match_name) == 0); + g_free (match_name); + } + g_free (font_path); + g_free (font_name); if (retval) { data->iter = *iter; @@ -179,15 +190,17 @@ iter_for_file_foreach (GtkTreeModel *model, return retval; } -gboolean -font_view_model_get_iter_for_file (FontViewModel *self, +static gboolean +font_view_model_get_iter_internal (FontViewModel *self, const gchar *file, + FT_Face face, GtkTreeIter *iter) { IterForFileData *data = g_slice_new0 (IterForFileData); gboolean found; data->file = file; + data->face = face; data->found = FALSE; gtk_tree_model_foreach (GTK_TREE_MODEL (self), @@ -203,6 +216,24 @@ font_view_model_get_iter_for_file (FontViewModel *self, return found; } +gboolean +font_view_model_get_iter_for_file (FontViewModel *self, + const gchar *file, + GtkTreeIter *iter) +{ + return font_view_model_get_iter_internal + (self, file, NULL, iter); +} + +gboolean +font_view_model_get_iter_for_face (FontViewModel *self, + FT_Face face, + GtkTreeIter *iter) +{ + return font_view_model_get_iter_internal + (self, NULL, face, iter); +} + typedef struct { FontViewModel *self; gchar *font_path; @@ -348,37 +379,6 @@ ensure_thumbnail (FontViewModel *self, g_clear_object (&info); } -static gchar * -get_font_name (FontViewModel *self, - const gchar *path) -{ - GFile *file; - gchar *uri, *contents = NULL, *name = NULL; - GError *error = NULL; - FT_Face face; - - file = g_file_new_for_path (path); - uri = g_file_get_uri (file); - - face = sushi_new_ft_face_from_uri (self->priv->library, uri, &contents, &error); - if (face != NULL) { - if (g_strcmp0 (face->style_name, "Regular") == 0) - name = g_strdup (face->family_name); - else - name = g_strconcat (face->family_name, ", ", face->style_name, NULL); - FT_Done_Face (face); - } else if (error != NULL) { - g_warning ("Can't get font name: %s\n", error->message); - g_error_free (error); - } - - g_free (uri); - g_object_unref (file); - g_free (contents); - - return name; -} - /* make sure the font list is valid */ static void ensure_font_list (FontViewModel *self) @@ -414,7 +414,7 @@ ensure_font_list (FontViewModel *self) for (i = 0; i < self->priv->font_list->nfont; i++) { FcPatternGetString (self->priv->font_list->fonts[i], FC_FILE, 0, &file); - font_name = get_font_name (self, (const gchar *) file); + font_name = font_utils_get_font_name_for_file (self->priv->library, (const gchar *) file); gtk_list_store_append (GTK_LIST_STORE (self), &iter); gtk_list_store_set (GTK_LIST_STORE (self), &iter, diff --git a/font-viewer/font-model.h b/font-viewer/font-model.h index 869e54d3..10d7f8b0 100644 --- a/font-viewer/font-model.h +++ b/font-viewer/font-model.h @@ -60,6 +60,9 @@ GtkTreeModel * font_view_model_new (void); gboolean font_view_model_get_iter_for_file (FontViewModel *self, const gchar *file, GtkTreeIter *iter); +gboolean font_view_model_get_iter_for_face (FontViewModel *self, + FT_Face face, + GtkTreeIter *iter); G_END_DECLS diff --git a/font-viewer/font-utils.c b/font-viewer/font-utils.c new file mode 100644 index 00000000..7ec5f605 --- /dev/null +++ b/font-viewer/font-utils.c @@ -0,0 +1,65 @@ +/* -*- mode: C; c-basic-offset: 4 -*- + * mate-font-viewer: + * + * Copyright (C) 2012 Cosimo Cecchi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "font-utils.h" + +#include "sushi-font-loader.h" + +gchar * +font_utils_get_font_name (FT_Face face) +{ + gchar *name; + + if (g_strcmp0 (face->style_name, "Regular") == 0) + name = g_strdup (face->family_name); + else + name = g_strconcat (face->family_name, ", ", face->style_name, NULL); + + return name; +} + +gchar * +font_utils_get_font_name_for_file (FT_Library library, + const gchar *path) +{ + GFile *file; + gchar *uri, *contents = NULL, *name = NULL; + GError *error = NULL; + FT_Face face; + + file = g_file_new_for_path (path); + uri = g_file_get_uri (file); + + face = sushi_new_ft_face_from_uri (library, uri, &contents, &error); + if (face != NULL) { + name = font_utils_get_font_name (face); + FT_Done_Face (face); + } else if (error != NULL) { + g_warning ("Can't get font name: %s\n", error->message); + g_error_free (error); + } + + g_free (uri); + g_object_unref (file); + g_free (contents); + + return name; +} + diff --git a/font-viewer/font-utils.h b/font-viewer/font-utils.h new file mode 100644 index 00000000..51bf47d5 --- /dev/null +++ b/font-viewer/font-utils.h @@ -0,0 +1,33 @@ +/* -*- mode: C; c-basic-offset: 4 -*- + * mate-font-viewer: + * + * Copyright (C) 2012 Cosimo Cecchi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __FONT_UTILS_H__ +#define __FONT_UTILS_H__ + +#include +#include FT_FREETYPE_H +#include + +gchar * font_utils_get_font_name (FT_Face face); +gchar * font_utils_get_font_name_for_file (FT_Library library, + const gchar *path); + +#endif /* __FONT_UTILS_H__ */ + -- cgit v1.2.1