diff options
author | Christopher Davis <brainblasted@disroot.org> | 2021-02-13 08:16:23 +0000 |
---|---|---|
committer | Christopher Davis <brainblasted@disroot.org> | 2021-02-13 08:16:23 +0000 |
commit | b41cfe36efb3c0e2304e5c5bcf8fcd1426d3b6bc (patch) | |
tree | 21b751801b8b6d4647acd489e3d4e701e6e374ee | |
parent | c3a45f86cc531286c560606f92b348d152298f98 (diff) | |
parent | 1e5b6a2715e1104132424e605d9a61feba4c075d (diff) | |
download | gnome-font-viewer-b41cfe36efb3c0e2304e5c5bcf8fcd1426d3b6bc.tar.gz |
Merge branch 'ewlsh/remove-ft-file-loading' into 'master'
Stop using FreeType and files for font names.
See merge request GNOME/gnome-font-viewer!20
-rw-r--r-- | src/font-model.c | 86 | ||||
-rw-r--r-- | src/font-utils.c | 47 | ||||
-rw-r--r-- | src/font-utils.h | 32 | ||||
-rw-r--r-- | src/meson.build | 2 |
4 files changed, 80 insertions, 87 deletions
diff --git a/src/font-model.c b/src/font-model.c index e66c401..b951732 100644 --- a/src/font-model.c +++ b/src/font-model.c @@ -32,7 +32,6 @@ #include <fontconfig/fontconfig.h> #include "font-model.h" -#include "font-utils.h" #include "sushi-font-loader.h" struct _FontViewModel @@ -161,6 +160,62 @@ font_infos_loaded (GObject *source_object, g_list_store_splice (self->model, 0, 0, items->pdata, items->len); } +static const gchar* weight_to_name(int weight) { + switch (weight) { + case FC_WEIGHT_THIN: return "Thin"; + case FC_WEIGHT_EXTRALIGHT: return "Extralight"; + case FC_WEIGHT_LIGHT: return "Light"; + case FC_WEIGHT_SEMILIGHT: return "Semilight"; + case FC_WEIGHT_BOOK: return "Book"; + case FC_WEIGHT_REGULAR: return "Regular"; + case FC_WEIGHT_MEDIUM: return "Medium"; + case FC_WEIGHT_SEMIBOLD: return "Semibold"; + case FC_WEIGHT_BOLD: return "Bold"; + case FC_WEIGHT_EXTRABOLD: return "Extrabold"; + case FC_WEIGHT_HEAVY: return "Heavy"; + case FC_WEIGHT_EXTRABLACK: return "Extrablack"; + } + + return NULL; +} + +static const gchar* slant_to_name(int slant) { + switch (slant) { + case FC_SLANT_ROMAN: return NULL; + case FC_SLANT_ITALIC: return "Italic"; + case FC_SLANT_OBLIQUE: return "Oblique"; + } + + return NULL; +} + +static gchar * +build_font_name (const char *style_name, const char *family_name, int slant, int weight, gboolean short_form) +{ + const char* style_name_x = style_name; + const gchar* slant_name = slant_to_name(slant); + const gchar* weight_name = weight_to_name(weight); + + if (style_name_x == NULL) { + if (slant_name != NULL && weight_name == NULL) + style_name_x = g_strdup_printf("%s", slant_name); + else if (slant_name == NULL && weight_name != NULL) + style_name_x = g_strdup_printf("%s", weight_name); + else if (slant_name != NULL && weight_name != NULL) + style_name_x = g_strdup_printf("%s %s", weight_name, slant_name); + } + + if (family_name == NULL) { + /* Use an empty string as the last fallback */ + return g_strdup (""); + } + + if (style_name_x == NULL || (short_form && g_strcmp0 (style_name_x, "Regular") == 0)) + return g_strdup (family_name); + + return g_strconcat (family_name, ", ", style_name_x, NULL); +} + static void load_font_infos (GTask *task, gpointer source_object, @@ -176,8 +231,8 @@ load_font_infos (GTask *task, for (i = 0; i < n_fonts; i++) { FontViewModelItem *item; - FcChar8 *path; - int index; + FcChar8 *path, *family, *style; + int index, slant, weight; g_autofree gchar *font_name = NULL; g_autoptr(GFile) file = NULL; @@ -185,12 +240,31 @@ load_font_infos (GTask *task, return; g_mutex_lock (&self->font_list_mutex); - FcPatternGetString (self->font_list->fonts[i], FC_FILE, 0, &path); - FcPatternGetInteger (self->font_list->fonts[i], FC_INDEX, 0, &index); + FcPattern* font = self->font_list->fonts[i]; + FcPatternGetString (font, FC_FILE, 0, &path); + FcPatternGetInteger (font, FC_INDEX, 0, &index); + g_autofree gchar *style_name = NULL; + g_autofree gchar *family_name = NULL; + FcResult result = FcPatternGetString(font, FC_FAMILY, 0, &family); + if (result == FcResultMatch) { + family_name = g_strdup((const gchar*) family); + } + result = FcPatternGetString(font, FC_STYLE, 0, &style); + if (result == FcResultMatch) { + style_name = g_strdup((const gchar*) style); + } + result = FcPatternGetInteger(font, FC_SLANT, 0, &slant); + if (result != FcResultMatch) { + slant = -1; + } + result = FcPatternGetInteger(font, FC_WEIGHT, 0, &weight); + if (result != FcResultMatch) { + weight = -1; + } g_mutex_unlock (&self->font_list_mutex); file = g_file_new_for_path ((const gchar *) path); - font_name = font_utils_get_font_name_for_file (self->library, file, index); + font_name = build_font_name(style_name, family_name, slant, weight, TRUE); if (!font_name) continue; diff --git a/src/font-utils.c b/src/font-utils.c deleted file mode 100644 index 6a41d26..0000000 --- a/src/font-utils.c +++ /dev/null @@ -1,47 +0,0 @@ -/* -*- 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_for_file (FT_Library library, - GFile *file, - gint face_index) -{ - g_autoptr(GError) error = NULL; - g_autofree gchar *uri = NULL, *contents = NULL; - gchar *name = NULL; - FT_Face face; - - uri = g_file_get_uri (file); - face = sushi_new_ft_face_from_uri (library, uri, face_index, &contents, - &error); - if (error != NULL) { - g_warning ("Can't get font name: %s", error->message); - return NULL; - } - - name = sushi_get_font_name (face, TRUE); - FT_Done_Face (face); - - return name; -} diff --git a/src/font-utils.h b/src/font-utils.h deleted file mode 100644 index 1787bbb..0000000 --- a/src/font-utils.h +++ /dev/null @@ -1,32 +0,0 @@ -/* -*- 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 <gio/gio.h> - -gchar * font_utils_get_font_name_for_file (FT_Library library, - GFile *file, - gint face_index); - -#endif /* __FONT_UTILS_H__ */ diff --git a/src/meson.build b/src/meson.build index 1f54bef..f863d45 100644 --- a/src/meson.build +++ b/src/meson.build @@ -14,8 +14,6 @@ viewer_sources = [ loader_sources, 'font-model.h', 'font-model.c', - 'font-utils.h', - 'font-utils.c', 'sushi-font-widget.h', 'sushi-font-widget.c', 'font-view.c', |