| 1 |
/* font-manager-family.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2024 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-family.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-family |
| 25 |
* @short_description: Font family |
| 26 |
* @title: Family |
| 27 |
* @include: font-manager-family.h |
| 28 |
* @see_also: #FontManagerJsonProxy #FontManagerFont |
| 29 |
* |
| 30 |
* #FontManagerFamily holds information about a font family along with an array |
| 31 |
* of #JsonObject representing the fonts belonging to this font family. |
| 32 |
* |
| 33 |
* The #JsonObject backing this class should have the following structure: |
| 34 |
* |
| 35 |
* |[ |
| 36 |
* { |
| 37 |
* "family" : string, |
| 38 |
* "description" : string, |
| 39 |
* "n-variations" : int, |
| 40 |
* "active" : bool, |
| 41 |
* "variations" : [ ] |
| 42 |
* } |
| 43 |
*]| |
| 44 |
* |
| 45 |
* variations is a #JsonArray of #JsonObjects representing individual fonts. |
| 46 |
* See #FontManagerFont for object description. |
| 47 |
*/ |
| 48 |
|
| 49 |
struct _FontManagerFamily |
| 50 |
{ |
| 51 |
FontManagerJsonProxy parent; |
| 52 |
}; |
| 53 |
|
| 54 |
G_DEFINE_TYPE(FontManagerFamily, font_manager_family, FONT_MANAGER_TYPE_JSON_PROXY) |
| 55 |
|
| 56 |
static void |
| 57 |
font_manager_family_class_init (FontManagerFamilyClass *klass) |
| 58 |
{ |
| 59 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 60 |
GObjectClass *parent_class = G_OBJECT_CLASS(font_manager_family_parent_class); |
| 61 |
FontManagerJsonProxyClass *proxy_class = FONT_MANAGER_JSON_PROXY_CLASS(klass); |
| 62 |
object_class->get_property = parent_class->get_property; |
| 63 |
object_class->set_property = parent_class->set_property; |
| 64 |
proxy_class->properties = FamilyProperties; |
| 65 |
proxy_class->n_properties = G_N_ELEMENTS(FamilyProperties); |
| 66 |
font_manager_json_proxy_install_properties(proxy_class); |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
font_manager_family_init (FontManagerFamily *self) |
| 72 |
{ |
| 73 |
g_return_if_fail(self != NULL); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* font_manager_family_get_default_index: |
| 78 |
* @self: #FontManagerFamily |
| 79 |
* |
| 80 |
* Returns: index of default variant or 0 |
| 81 |
*/ |
| 82 |
gint |
| 83 |
font_manager_family_get_default_index (FontManagerFamily *self) |
| 84 |
{ |
| 85 |
g_return_val_if_fail(self != NULL, 0); |
| 86 |
g_autoptr(JsonObject) source = NULL; |
| 87 |
g_object_get(self, "source-object", &source, NULL); |
| 88 |
const gchar *family_desc = json_object_get_string_member(source, "description"); |
| 89 |
JsonArray *arr = json_object_get_array_member(source, "variations"); |
| 90 |
guint i, arr_length = json_array_get_length(arr); |
| 91 |
for (i = 0; i < arr_length; i++) { |
| 92 |
JsonObject *font = json_array_get_object_element(arr, i); |
| 93 |
const gchar *font_desc = json_object_get_string_member(font, "description"); |
| 94 |
if (g_strcmp0(family_desc, font_desc) == 0) |
| 95 |
return i; |
| 96 |
} |
| 97 |
g_return_val_if_reached(0); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* font_manager_family_get_default_variant: |
| 103 |
* @self: #FontManagerFamily |
| 104 |
* |
| 105 |
* Returns: (transfer none): #JsonObject which should not be freed. |
| 106 |
*/ |
| 107 |
JsonObject * |
| 108 |
font_manager_family_get_default_variant (FontManagerFamily *self) |
| 109 |
{ |
| 110 |
g_return_val_if_fail(self != NULL, NULL); |
| 111 |
g_autoptr(JsonObject) source = NULL; |
| 112 |
g_object_get(self, "source-object", &source, NULL); |
| 113 |
JsonArray *arr = json_object_get_array_member(source, "variations"); |
| 114 |
return json_array_get_object_element(arr, font_manager_family_get_default_index(self)); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* font_manager_family_new: |
| 119 |
* |
| 120 |
* Returns: (transfer full): A newly created #FontManagerFamily. |
| 121 |
* Free the returned object using #g_object_unref(). |
| 122 |
*/ |
| 123 |
FontManagerFamily * |
| 124 |
font_manager_family_new (void) |
| 125 |
{ |
| 126 |
return g_object_new(FONT_MANAGER_TYPE_FAMILY, NULL); |
| 127 |
} |
| 128 |
|