[gnome-font-viewer] font-model: add API to get an iter from a FT_Face
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-font-viewer] font-model: add API to get an iter from a FT_Face
- Date: Thu, 3 May 2012 17:36:49 +0000 (UTC)
commit 1a213203fb0b9591e252e90e303924f274c0e7e4
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Thu May 3 13:09:57 2012 -0400
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.
src/Makefile.am | 2 +
src/font-model.c | 72 +++++++++++++++++++++++++++---------------------------
src/font-model.h | 3 ++
src/font-utils.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++
src/font-utils.h | 32 ++++++++++++++++++++++++
5 files changed, 137 insertions(+), 36 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 3cab3c4..d62e549 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -29,6 +29,8 @@ gnome_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/src/font-model.c b/src/font-model.c
index c7a3308..ba6b7b6 100644
--- a/src/font-model.c
+++ b/src/font-model.c
@@ -39,6 +39,7 @@
#include <libgnome-desktop/gnome-desktop-thumbnail.h>
#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/src/font-model.h b/src/font-model.h
index 49cd548..eeb7e57 100644
--- a/src/font-model.h
+++ b/src/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/src/font-utils.c b/src/font-utils.c
new file mode 100644
index 0000000..5268ac5
--- /dev/null
+++ b/src/font-utils.c
@@ -0,0 +1,64 @@
+/* -*- mode: C; c-basic-offset: 4 -*-
+ * gnome-font-viewer:
+ *
+ * Copyright (C) 2012 Cosimo Cecchi <cosimoc gnome org>
+ *
+ * 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/src/font-utils.h b/src/font-utils.h
new file mode 100644
index 0000000..992624e
--- /dev/null
+++ b/src/font-utils.h
@@ -0,0 +1,32 @@
+/* -*- mode: C; c-basic-offset: 4 -*-
+ * gnome-font-viewer:
+ *
+ * Copyright (C) 2012 Cosimo Cecchi <cosimoc gnome org>
+ *
+ * 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 <ft2build.h>
+#include FT_FREETYPE_H
+#include <glib.h>
+
+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__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]