Debian Codegraph @

lib/fontconfig/font-manager-alias.c

raw
1 /* font-manager-alias.c
2 *
3 * Copyright (C) 2009-2023 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-alias.h"
22
23 /**
24 * SECTION: font-manager-alias
25 * @short_description: Font substitution elements
26 * @title: Alias Elements
27 * @include: font-manager-alias.h
28 *
29 * #FontManagerAliasElement represents an &lt;alias&gt; element in a fontconfig
30 * configuration file.
31 *
32 * Alias elements provide a shorthand notation for
33 * the set of common match operations needed to substitute one font
34 * family for another.
35 *
36 * Fonts matching @family are edited to prepend the list of &lt;@prefer&gt;ed
37 * families before the matching @family, append the &lt;@accept&gt;able
38 * families after the matching @family and append the &lt;@default&gt;
39 * families to the end of the family list.
40 */
41
42 struct _FontManagerAliasElement
43 {
44 GObject parent;
45 };
46
47 typedef struct
48 {
49 gchar *family;
50 FontManagerStringSet *prefer;
51 FontManagerStringSet *accept;
52 FontManagerStringSet *_default;
53 }
54 FontManagerAliasElementPrivate;
55
56 G_DEFINE_TYPE_WITH_PRIVATE(FontManagerAliasElement, font_manager_alias_element, G_TYPE_OBJECT)
57
58 enum
59 {
60 PROP_RESERVED,
61 PROP_FAMILY,
62 PROP_PREFER,
63 PROP_ACCEPT,
64 PROP_DEFAULT,
65 N_PROPERTIES
66 };
67
68 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
69
70 #define DEFAULT_PARAM_FLAGS (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
71
72 static void
73 font_manager_alias_element_dispose (GObject *gobject)
74 {
75 g_return_if_fail(gobject != NULL);
76 FontManagerAliasElement *self = FONT_MANAGER_ALIAS_ELEMENT(gobject);
77 FontManagerAliasElementPrivate *priv = font_manager_alias_element_get_instance_private(self);
78 g_clear_pointer(&priv->family, g_free);
79 g_clear_object(&priv->prefer);
80 g_clear_object(&priv->accept);
81 g_clear_object(&priv->_default);
82 G_OBJECT_CLASS(font_manager_alias_element_parent_class)->dispose(gobject);
83 return;
84 }
85
86 static void
87 font_manager_alias_element_get_property (GObject *gobject,
88 guint property_id,
89 GValue *value,
90 GParamSpec *pspec)
91 {
92 g_return_if_fail(gobject != NULL);
93 FontManagerAliasElement *self = FONT_MANAGER_ALIAS_ELEMENT(gobject);
94 FontManagerAliasElementPrivate *priv = font_manager_alias_element_get_instance_private(self);
95 switch (property_id) {
96 case PROP_FAMILY:
97 g_value_set_string(value, priv->family);
98 break;
99 case PROP_PREFER:
100 g_value_set_object(value, priv->prefer);
101 break;
102 case PROP_ACCEPT:
103 g_value_set_object(value, priv->accept);
104 break;
105 case PROP_DEFAULT:
106 g_value_set_object(value, priv->_default);
107 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
110 break;
111 }
112 return;
113 }
114
115 static void
116 font_manager_alias_element_set_property (GObject *gobject,
117 guint property_id,
118 const GValue *value,
119 GParamSpec *pspec)
120 {
121 g_return_if_fail(gobject != NULL);
122 FontManagerAliasElement *self = FONT_MANAGER_ALIAS_ELEMENT(gobject);
123 FontManagerAliasElementPrivate *priv = font_manager_alias_element_get_instance_private(self);
124 switch (property_id) {
125 case PROP_FAMILY:
126 g_free(priv->family);
127 priv->family = g_value_dup_string(value);
128 break;
129 case PROP_PREFER:
130 g_set_object(&priv->prefer, g_value_get_object(value));
131 break;
132 case PROP_ACCEPT:
133 g_set_object(&priv->accept, g_value_get_object(value));
134 break;
135 case PROP_DEFAULT:
136 g_set_object(&priv->_default, g_value_get_object(value));
137 break;
138 default:
139 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
140 break;
141 }
142 return;
143 }
144
145 static void
146 font_manager_alias_element_class_init (FontManagerAliasElementClass *klass)
147 {
148 GObjectClass *object_class = G_OBJECT_CLASS(klass);
149 object_class->dispose = font_manager_alias_element_dispose;
150 object_class->get_property = font_manager_alias_element_get_property;
151 object_class->set_property = font_manager_alias_element_set_property;
152
153 /**
154 * FontManagerAliasElement:family:
155 *
156 * Family targeted for substitution.
157 */
158 obj_properties[PROP_FAMILY] = g_param_spec_string("family",
159 NULL,
160 "Target font family",
161 NULL,
162 DEFAULT_PARAM_FLAGS);
163 /**
164 * FontManagerAliasElement:prefer:
165 *
166 * Set of font families which should be preferred over @family.
167 */
168 obj_properties[PROP_PREFER] = g_param_spec_object("prefer",
169 NULL,
170 "List of preferred font families",
171 FONT_MANAGER_TYPE_STRING_SET,
172 DEFAULT_PARAM_FLAGS);
173
174 /**
175 * FontManagerAliasElement:accept:
176 *
177 * Set of font families which are acceptable substitutes for @family.
178 */
179 obj_properties[PROP_ACCEPT] = g_param_spec_object("accept",
180 NULL,
181 "List of acceptable font families",
182 FONT_MANAGER_TYPE_STRING_SET,
183 DEFAULT_PARAM_FLAGS);
184
185 /**
186 * FontManagerAliasElement:default:
187 *
188 * Set of font families to be used as a fallback.
189 */
190 obj_properties[PROP_DEFAULT] = g_param_spec_object("default",
191 NULL,
192 "List of fallback fonts",
193 FONT_MANAGER_TYPE_STRING_SET,
194 DEFAULT_PARAM_FLAGS);
195
196 g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
197 return;
198 }
199
200 static void
201 font_manager_alias_element_init (FontManagerAliasElement *self)
202 {
203 g_return_if_fail(self != NULL);
204 FontManagerAliasElementPrivate *priv = font_manager_alias_element_get_instance_private(self);
205 priv->prefer = font_manager_string_set_new();
206 priv->accept = font_manager_string_set_new();
207 priv->_default = font_manager_string_set_new();
208 return;
209 }
210
211 /**
212 * font_manager_alias_element_get: (skip)
213 * @self: #FontManagerAliasElement
214 * @priority: "prefer", "accept" or "default"
215 *
216 * Returns: (transfer none) (nullable): A #FontManagerStringSet or %NULL on error
217 */
218 FontManagerStringSet *
219 font_manager_alias_element_get (FontManagerAliasElement *self, const gchar *priority) {
220 g_return_val_if_fail(self != NULL, NULL);
221 FontManagerAliasElementPrivate *priv = font_manager_alias_element_get_instance_private(self);
222 if (g_strcmp0(priority, "prefer") == 0)
223 return priv->prefer;
224 else if (g_strcmp0(priority, "accept") == 0)
225 return priv->accept;
226 else if (g_strcmp0(priority, "default") == 0)
227 return priv->_default;
228 else
229 g_warning("Requested invalid member : %s", priority);
230 g_return_val_if_reached(NULL);
231 }
232
233 /**
234 * font_manager_alias_element_new:
235 * @family: (nullable): family name
236 *
237 * Returns: (transfer full): A newly created #FontManagerAliasElement.
238 * Free the returned object using #g_object_unref().
239 */
240 FontManagerAliasElement *
241 font_manager_alias_element_new (const gchar *family)
242 {
243 return g_object_new(FONT_MANAGER_TYPE_ALIAS_ELEMENT, "family", family, NULL);
244 }